Which protocol should I use for streaming audio (not live)? [closed]

淺唱寂寞╮ 提交于 2019-12-05 08:20:55
Brad

The SHOUTcast client protocol is effectively the same as HTTP/1.0. The only relevant difference is the response status line:

ICY 200 OK

Instead of HTTP/1.0, you get ICY. That's really it! From there, it behaves the same. Web browsers, and most HTTP clients ignore this. Android chokes on it, and some browsers, but most are fine. Icecast's client stream behavior is the same as SHOUTcast's, except that it actually returns HTTP/1.0 200 OK for its status line.

Now, you have noticed in those response headers, there are some extra headers with stream information. All but one are just extra information that don't have any impact on the data you're getting back. If you request no metadata, then the server does nothing but take every byte sent to it from the source, and relay it on to each client (with a little server-side buffer as well).

If in your request headers, you specify Icy-MetaData: 1, then the behavior changes slightly. In the response, you will get Icy-MetaInt: 8192 or similar. This means that every 8,192 bytes, there will be a chunk of metadata. Sometimes that chunk is just 0x00 which means that there is no metadata update. Other times there will be a byte like 0x01. If you multiply that value by 16, you know that the next 16 bytes will be ASCII metadata, like StreamTitle: 'My Stream';StreamUrl='';, padded by 0x00. I've described the metadata in more detail in another post, if you are curious.

All of this to say is that the most popular streaming protocol is in fact HTTP, and SHOUTcast/Icecast/many other servers have added a request header where you can get metadata interleaved into the stream. HTTP clients that don't request that metadata will just get a regular MP3 stream, which a browser will think is just some file somewhere. Afterall, the browser doesn't care how you get the data.

Now, what should you be using? Your requirement:

I'm trying to write a Python server that streams one requested mp3 file from start to end. (No live streaming)

HTTP is all you need. In fact, no need to write some server for this. Apache/Nginx/whatever will work fine. Just a simple HTTP server! If you want to serve up files by ID, that's where your Python comes in. Write something that goes and fetches the appropriate resource from disk, based on that ID. I wouldn't bother with RTSP for this... that is way too much overhead for what you need, and you will be hurting client compatibility.

I'd like to have the functionality to play that stream with any media player (like VLC) and be able to change playback position.

For that requirement, just make sure your server supports range requests. The client will take care of the rest.

Summing this all up

  • SHOUTcast/Icecast servers are for "live" radio-like streams where all the clients get the same audio stream at (roughly) the same time
  • HTTP is the most compatible protocol for delivering anything to the client, streaming or not
  • RTSP/RTMP/RTP and all related protocols are not necessary unless you are streaming a long-running or live stream where variable bitrate based on client bandwidth availability is important. (There are other features of these protocols, but this seems to be the deciding factor for choosing them. I recommend reading up on each if you want more info.)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!