Sending Audio Chunks as a Contiguous Stream to RTMP Server using GStreamer

I’m working on a project where I need to send audio data in chunks to an RTMP server using GStreamer. Multiple audio chunks are generated periodically, and I struggle to send them as a contiguous stream. I’ve put together a basic example in Python using GStreamer, but there is a delay between each audio chunk. Is there a way to ensure they are sent as a stream without gaps between them?

Here is the code I’ve come up with:

import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject

GObject.threads_init()
Gst.init(None)

pipeline_description = "appsrc name=mysource is-live=true format=time block=true blocksize=16384 ! audioconvert ! audioresample ! rtmpsink location='rtmp://your.rtmp.server/live/stream' sync=false"

pipeline = Gst.parse_launch(pipeline_description)
source = pipeline.get_by_name("mysource")

def push_audio_chunk(data):
    buffer = Gst.Buffer.new_wrapped(bytes(data))
    source.emit("push-buffer", buffer)

audio_chunks = [b"chunk1", b"chunk2", b"chunk3"]

for chunk in audio_chunks:
    push_audio_chunk(chunk)

pipeline.set_state(Gst.State.PLAYING)

mainloop = GObject.MainLoop()
try:
    mainloop.run()
except KeyboardInterrupt:
    pass
finally:
    pipeline.set_state(Gst.State.NULL)

Leave a Comment