Erix A. 8 Posted September 15, 2020 (edited) 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 September 15, 2020 by Erix A. Typo Share this post Link to post
Remy Lebeau 1392 Posted September 15, 2020 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. 1 Share this post Link to post
Erix A. 8 Posted September 20, 2020 (edited) 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 September 20, 2020 by Erix A. Share this post Link to post