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 HttpListener
s seem to require the program runs as admin which is going to be really tricky.
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.
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.
Did you consider using HTTP proxy with caching features?
Like:
- Apache httpd with
mod_proxy
and mod_cache
- Squid
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.