tags:

views:

365

answers:

3

Iv'e been really interested in adding support for video podcasts to Media Browser.

I would like users to be able to navigate through the available video podcasts and stream them from the internets. That's really easy cause media player etc.. will happily play a file that lives in the cloud.

The problem is that I want cache these files locally so subsequent viewings of the same episode will not involve streaming and instead will play the local file.

So... I was thinking, why not host an HttpListener and as media player asks it for bits of the file, have the HttpListener download and store it locally. Next time a user plays the file we will already have portions of the file locally.

Does anyone know of example code that uses HttpListener for proxying?

EDIT

The idea would be only to proxy simple streamable content like MP3 or Mov. The bounty will go to an actual implementation.

Here is the API I would like:

// will proxy a uri on the local port, if cacheFile exists it will resume the 
// download from cacheFile.
// while the file is downloading it will be name cacheFile.partial, after the 
// download is complete the file will be renamed to cacheFile.
// Example usage: ProxyFile("http://media.railscasts.com/videos/176_searchlogic.mov", 8000, @"c:\downloads\railscasts\176_searchlogic.mov")
//
// Directly after this call http://localhost:8000 will be the proxy stream, it will be playable locally. 
void ProxyUri(Uri uri, int port, string cacheFile)

Edit 2

HttpListener is looking pretty unpromising I will probably need to do the work at a TCP socket level as HttpListeners seem to require the program runs as admin which is going to be really tricky.

A: 

Streaming was not designed to be saved, and also these protocols are very custom and very complex to implement, streaming sessions do lots of validation and synchronization which will be extremely difficult to imitate. Of course it is not an impossible task, but its fairly big task to do. Only other way is to read and save it as local media file, and use that as a reference. Because you can use windows media encoder to read stream and write stream data as local file, but it still may not allow you to do copy protected data.

Akash Kava
Akash, I'm not looking to proxy streams just downloadable files. eg.. http://www.somewhere.com/xyz.wmv , many formats eg. mp3 are usable before you download the entire file.
Sam Saffron
A: 

Did you consider using HTTP proxy with caching features?

Like:

See also Web Cache @ wikipedia

If you want your application to have such web cache component, I suggest you look for Web Cache implementation in .Net, and not code it from scratch.

Ron Klein
not really, I would like to deploy this with a .net app, with minimal configuration.
Sam Saffron
+2  A: 

I hadn't done anything with HttpListener before, so I thought this would be a nice little exercise to bring myself up to speed with it - and so it proved. I implemented it as a single ProxyListener class whose constructor takes the parameters of the ProxyUri function you specified. Once you obtain an instance, you start it listening (and potentially downloading) by calling its Start method. When you're done with it, call Cleanup.

There are one or two rough edges but basically it works as per your question. To test it, I built it up as a console application with a Program class which accepts input lines consisting of (uri, port, filename), space-separated, creates the ProxyListener instances and starts them. You can run this console application, type in a suitable line, and the downloader will start (printing out progress to console). Simultaneously you can e.g. fire up IE and fetch the file from the specified port, and you will be able to download it while the downloader is still working. The "uploader" progress will be printed to console, too.

I'm having a bit of trouble pasting it in here, maybe due to size (it's not that big, but bigger than the snippets you normally see here - the ProxyListener class is a tad under 200 lines). Does it sound interesting? If so, I'll post it to a pastebin and update this answer with a link.

Update: Posted as a gist.

Note that you will need Administrator privileges to run the program, since HttpListener requires this.

Update 2: Under certain circumstances, it is not necessary to have admin privileges to run HttpListener. See this link and this one. The idea is, if you can reserve an URL namespace during installation time, then the user does not have to have admin privileges if listening against that namespace.

Vinay Sajip
SO is notoriously bad for long snippets of code, try posting a gist instead http://gist.github.com/ also, do you mind if I use this code in mediabrowser (www.mediabrowser.tv)
Sam Saffron
No, I don't mind. After all, you mentioned it in your question.
Vinay Sajip
it defiantly looks like what I wanted (still resume support would be very nice), I will let others have a chance with this as well but if there are no other solutions in the next 48 hours I will award the bounty to you. ... side note, the code is licensed under the GPL and out project is under MIT so we have a bit of a licensing computability issue.
Sam Saffron
Eh? I checked the mediabrowser website and I got the impression that mediabrowser was GPL-licensed. I got that from the "About" page of mediabrowser.tv, as well as the Google Code page for videobrowser.
Vinay Sajip
See: http://code.google.com/p/videobrowser/source/browse/trunk/MediaBrowser/Library/LICENSE.txt, we have been moving portion away to MIT, we are mid way separating it into a diff assembly.
Sam Saffron
Also, its unlikely I will be able to use this cause I do not have the privilege of running as admin so I need to do the work at a TCP socket level.
Sam Saffron