Jump to content
Erix A.

Streaming binary data via TIdHTTPServer

Recommended Posts

Hi,
I want to create a simple http server, which would stream binary (audio) data, basically the same way as internet radio's do.
I run the curl against some available on the Internet and the way they're doing it seems pretty simple, i.e. they set the content-type:audio/mpeg and then just push the audio stream.

 

How to implement this using TIdHTTPServer? I don't need the exact code, just the explanation on how to do it 🙂

The thing I did is I set the AResponseInfo.ContentStream to the TFileStream in IdHTTPServer1CommandGet and that sent the whole file, but that is not how it should work as this method sets the content-length.

 

As the starting point, just "streaming" one big file would be nice. And then the fun part with the buffering etc.  🙂

Edited by Erix A.
Typo

Share this post


Link to post

TIdHTTPServer is not really designed for streaming media.  If you don't provide the AResponseInfo with a ContentText or ContentStream value, a default HTML page is sent instead.  And if you don't provide a ContentLength value, one is calculated automatically from the ContentText/ContentStream by default.  And if you don't send a response before the OnCommand... event handler exits, one is sent automatically.

 

So basically, the only way I see to do what you want is to either:

 

- NOT using HTTP's "chunked" encoding - have your OnCommand... event handler set the AResponseInfo.HeaderHasBeenWritten property to True to disable the server's default response handling, send your own HTTP response headers manually, and then run a loop that sends the raw media samples as needed until finished, and then exit the event handler.

 

- USING HTTP's "chunked" encoding - have your OnCommand... event handler set the AResponseInfo.TransferEncoding property to 'chunked' to disable the server's default ContentLength handling, then call AResponseInfo.WriteHeader() to send the server's default HTTP response headers normally, and then run a loop that sends the media samples (in HTTP chunked format) as needed until finished, and then exit the event handler.


There are probably other solutions, like writing a custom TStream class for use with the ContentStream, but that can get a little ugly.

  • Like 1

Share this post


Link to post

Thank you. I was able to get it working by sending the raw mpeg data into the stream as you described.

I had to implement the stream throttling as well to match the bitrate of the mp3 file, otherwise the receiver just choked.

Edited by Erix A.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×