HTTP realtime audio streaming server HTTP realtime audio streaming server flask flask

HTTP realtime audio streaming server


Chunked transfer encoding is recommended, since the resource has an indefinite length. Without it, you would need to specify a Content-Length header. Older clients used to not be able to handled chunked transfer encoding well, so the old hack was to either leave out the Content-Length header entirely (HTTP/1.0 behavior), or to specify a very large (effectively infinite) length.

As for Content-Type, you can use audio/vnd.wav;codec=1 for regular PCM.

Be sure to set preload="none" on your <audio> element so that the browser doesn't try to buffer things ahead of time.


Actually I've made a kind of workaround with the following code (without any index.html) and it works fine without any interruptions:

from flask import Flask, Response,render_templateimport pyaudioimport audio_processing as audioRecapp = Flask(__name__)def genHeader(sampleRate, bitsPerSample, channels, samples):    datasize = 10240000 # Some veeery big number here instead of: #samples * channels * bitsPerSample // 8    o = bytes("RIFF",'ascii')                                               # (4byte) Marks file as RIFF    o += (datasize + 36).to_bytes(4,'little')                               # (4byte) File size in bytes excluding this and RIFF marker    o += bytes("WAVE",'ascii')                                              # (4byte) File type    o += bytes("fmt ",'ascii')                                              # (4byte) Format Chunk Marker    o += (16).to_bytes(4,'little')                                          # (4byte) Length of above format data    o += (1).to_bytes(2,'little')                                           # (2byte) Format type (1 - PCM)    o += (channels).to_bytes(2,'little')                                    # (2byte)    o += (sampleRate).to_bytes(4,'little')                                  # (4byte)    o += (sampleRate * channels * bitsPerSample // 8).to_bytes(4,'little')  # (4byte)    o += (channels * bitsPerSample // 8).to_bytes(2,'little')               # (2byte)    o += (bitsPerSample).to_bytes(2,'little')                               # (2byte)    o += bytes("data",'ascii')                                              # (4byte) Data Chunk Marker    o += (datasize).to_bytes(4,'little')                                    # (4byte) Data size in bytes    return oFORMAT = pyaudio.paInt16CHUNK = 1024 #1024RATE = 44100bitsPerSample = 16 #16CHANNELS = 1wav_header = genHeader(RATE, bitsPerSample, CHANNELS, CHUNK)audio = pyaudio.PyAudio()# start Recordingstream = audio.open(format=FORMAT, channels=CHANNELS,    rate=RATE, input=True, input_device_index=10,    frames_per_buffer=CHUNK)# print "recording..."@app.route('/audio_unlim')def audio_unlim():    # start Recording    def sound():        data = wav_header        data += stream.read(CHUNK)        yield(data)        while True:            data = stream.read(CHUNK)            yield(data)    return Response(sound(), mimetype="audio/x-wav")if __name__ == "__main__":    app.run(host='0.0.0.0', debug=True, threaded=True,port=5000)

I've just started with sending a WAV header, but the size written there is veeery big number telling the player to wait veeery big buffer of data. Until the "end" player plays the coming chunks of data without any problem (no WAV headers anymore just chunks of audio data!). This is without any "Transfer-encoding: chunked" or anything else! Just set the mimetype to "audio/x-wav". And the HTTP response is extremely simple as follows:

enter image description here


Server-side Streaming Technologies

In order to stream live audio, you will need to run specific streaming software on your server.



  • Icecast

    The Icecast server is an open source technology for streaming media. Maintained by the Xiph.org Foundation, it streams Ogg Vorbis/Theora as well as MP3 and AAC format via the SHOUTcast protocol.

    Note: SHOUTcast and Icecast are among the most established and popular technologies, but there are many more streaming media systems available.


Edit

I'm a Django guy and I've been testing things, and, seems, it works fine, only needs some proper file management and stuff. I've been working with mp3, but you can work with anything browsers have support for.

from django.http import StreamingHttpResponsedef stream(request):    return StreamingHttpResponse(streamer(200000) ,content_type='audio/mp3')def streamer(pointer):    with open('media/Indila - Parle A Ta Tete.mp3', 'rb') as file:        file.seek(pointer)        for chunk in iter(lambda: file.read(4096), b''):            yield chunk#the connection is open until this iterator hasn't finished