I’ve 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.
I hadn’t done anything with
HttpListenerbefore, 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 singleProxyListenerclass whose constructor takes the parameters of theProxyUrifunction you specified. Once you obtain an instance, you start it listening (and potentially downloading) by calling itsStartmethod. When you’re done with it, callCleanup.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
Programclass which accepts input lines consisting of (uri, port, filename), space-separated, creates theProxyListenerinstances 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
ProxyListenerclass 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
HttpListenerrequires 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.