Search code examples
c++audio-streaminggstreamer

gstreamer choose one channel and convert to mono (deinterleave)


i'm creating an audio stream from an audio server and streaming it to the receiver and I would like the receiver to choose one channel and convert it to mono.

The code below is the pipeline of my receiver. It is receiving an rtp stream.

gst-launch-0.10 -v \
udpsrc multicast-group=224.0.0.7 port=5000 \
! "application/x-rtp,media=audio, clock-rate=44100, width=16, height=16, encoding-name=L16, encoding-params=2, payload=96" \
! gstrtpjitterbuffer latency=200 ! rtpL16depay ! audioconvert ! deinterleave name=d    interleave name=i ! alsasink \
d.src_0 ! queue ! audioconvert !"audio/x-raw-int,channels=1"! i.sink1 \
d.src_0 ! queue ! audioconvert !"audio/x-raw-int,channels=1"! i.sink0 

It will run listening to nothing but when the stream comes through, it throws an error.

ERROR: from element /GstPipeline:pipeline0/GstUDPSrc:udpsrc0: Internal data flow error.

Solution

  • you can't connect d.src_0 twice.

    Instead, you can ditch the interleave part and just concentrate on playing a mono stream (I hope it's ok that I replaced your udpsrc with a uribin, but the pipeline is easier to read that way):

    gst-launch-0.10 -v uridecodebin uri=file://file.ogg ! audioconvert ! \
      deinterleave name=d  d.src0 ! queue ! audioconvert ! alsasink
    

    This will, like your pipeline, deinterleave the stereo stream and just take the left channel (d.src0, use d.src1 for the other one) which provides a mono stream and feed it to alsasink.

    ALSA doesn't mind whether you feed mono or stereo data to it, but what if you explicitly wanted a stereo stream/file? Just add audiopanorama. It takes an audio stream and places it somewhere between the left and the right speaker (the panorama parameter defaults to 0 which is the center) and produces a stereo stream:

    gst-launch-0.10 -v uridecodebin uri=file://file.mp3 ! audioconvert ! \
      deinterleave name=d d.src0 ! queue ! audioconvert ! audiopanorama ! alsasink
    

    But as I said, ALSA (and most other sound servers like pulse, osd, ...) doesn't care whether you feed mono or stereo into it, it's gonna play it on both channels.