2014-11-10 21:34:39 +00:00
|
|
|
#! /usr/bin/env python
|
2010-10-04 15:52:22 +00:00
|
|
|
|
2014-11-10 21:34:39 +00:00
|
|
|
import gi
|
|
|
|
import sys
|
|
|
|
gi.require_version('Gst', '1.0')
|
|
|
|
from gi.repository import GObject, Gst
|
2010-10-04 15:52:22 +00:00
|
|
|
|
|
|
|
#
|
2014-11-10 21:34:39 +00:00
|
|
|
# A simple RTP receiver
|
2010-10-04 15:52:22 +00:00
|
|
|
#
|
|
|
|
# receives alaw encoded RTP audio on port 5002, RTCP is received on port 5003.
|
|
|
|
# the receiver RTCP reports are sent to port 5007
|
|
|
|
#
|
|
|
|
# .-------. .----------. .---------. .-------. .--------.
|
|
|
|
# RTP |udpsrc | | rtpbin | |pcmadepay| |alawdec| |alsasink|
|
|
|
|
# port=5002 | src->recv_rtp recv_rtp->sink src->sink src->sink |
|
|
|
|
# '-------' | | '---------' '-------' '--------'
|
2014-11-10 21:34:39 +00:00
|
|
|
# | |
|
2010-10-04 15:52:22 +00:00
|
|
|
# | | .-------.
|
|
|
|
# | | |udpsink| RTCP
|
|
|
|
# | send_rtcp->sink | port=5007
|
|
|
|
# .-------. | | '-------' sync=false
|
|
|
|
# RTCP |udpsrc | | | async=false
|
2014-11-10 21:34:39 +00:00
|
|
|
# port=5003 | src->recv_rtcp |
|
|
|
|
# '-------' '----------'
|
2010-10-04 15:52:22 +00:00
|
|
|
|
|
|
|
AUDIO_CAPS = 'application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)PCMA'
|
|
|
|
AUDIO_DEPAY = 'rtppcmadepay'
|
|
|
|
AUDIO_DEC = 'alawdec'
|
|
|
|
AUDIO_SINK = 'autoaudiosink'
|
|
|
|
|
|
|
|
DEST = '127.0.0.1'
|
|
|
|
|
|
|
|
RTP_RECV_PORT = 5002
|
|
|
|
RTCP_RECV_PORT = 5003
|
2014-11-10 21:34:39 +00:00
|
|
|
RTCP_SEND_PORT = 5007
|
|
|
|
|
|
|
|
GObject.threads_init()
|
|
|
|
Gst.init(sys.argv)
|
2010-10-04 15:52:22 +00:00
|
|
|
|
2011-11-24 00:52:40 +00:00
|
|
|
#gst-launch -v rtpbin name=rtpbin \
|
2010-10-04 15:52:22 +00:00
|
|
|
# udpsrc caps=$AUDIO_CAPS port=$RTP_RECV_PORT ! rtpbin.recv_rtp_sink_0 \
|
|
|
|
# rtpbin. ! rtppcmadepay ! alawdec ! audioconvert ! audioresample ! autoaudiosink \
|
|
|
|
# udpsrc port=$RTCP_RECV_PORT ! rtpbin.recv_rtcp_sink_0 \
|
|
|
|
# rtpbin.send_rtcp_src_0 ! udpsink port=$RTCP_SEND_PORT host=$DEST sync=false async=false
|
|
|
|
|
|
|
|
def pad_added_cb(rtpbin, new_pad, depay):
|
2014-11-10 21:34:39 +00:00
|
|
|
sinkpad = Gst.Element.get_static_pad(depay, 'sink')
|
|
|
|
lres = Gst.Pad.link(new_pad, sinkpad)
|
2010-10-04 15:52:22 +00:00
|
|
|
|
2014-11-10 21:34:39 +00:00
|
|
|
# the pipeline to hold eveything
|
|
|
|
pipeline = Gst.Pipeline('rtp_client')
|
2010-10-04 15:52:22 +00:00
|
|
|
|
|
|
|
# the udp src and source we will use for RTP and RTCP
|
2014-11-10 21:34:39 +00:00
|
|
|
rtpsrc = Gst.ElementFactory.make('udpsrc', 'rtpsrc')
|
2010-10-04 15:52:22 +00:00
|
|
|
rtpsrc.set_property('port', RTP_RECV_PORT)
|
|
|
|
|
|
|
|
# we need to set caps on the udpsrc for the RTP data
|
2014-11-10 21:34:39 +00:00
|
|
|
caps = Gst.caps_from_string(AUDIO_CAPS)
|
2010-10-04 15:52:22 +00:00
|
|
|
rtpsrc.set_property('caps', caps)
|
|
|
|
|
2014-11-10 21:34:39 +00:00
|
|
|
rtcpsrc = Gst.ElementFactory.make('udpsrc', 'rtcpsrc')
|
2010-10-04 15:52:22 +00:00
|
|
|
rtcpsrc.set_property('port', RTCP_RECV_PORT)
|
|
|
|
|
2014-11-10 21:34:39 +00:00
|
|
|
rtcpsink = Gst.ElementFactory.make('udpsink', 'rtcpsink')
|
2010-10-04 15:52:22 +00:00
|
|
|
rtcpsink.set_property('port', RTCP_SEND_PORT)
|
|
|
|
rtcpsink.set_property('host', DEST)
|
|
|
|
|
|
|
|
# no need for synchronisation or preroll on the RTCP sink
|
|
|
|
rtcpsink.set_property('async', False)
|
2014-11-10 21:34:39 +00:00
|
|
|
rtcpsink.set_property('sync', False)
|
2010-10-04 15:52:22 +00:00
|
|
|
|
|
|
|
pipeline.add(rtpsrc, rtcpsrc, rtcpsink)
|
|
|
|
|
|
|
|
# the depayloading and decoding
|
2014-11-10 21:34:39 +00:00
|
|
|
audiodepay = Gst.ElementFactory.make(AUDIO_DEPAY, 'audiodepay')
|
|
|
|
audiodec = Gst.ElementFactory.make(AUDIO_DEC, 'audiodec')
|
2010-10-04 15:52:22 +00:00
|
|
|
|
|
|
|
# the audio playback and format conversion
|
2014-11-10 21:34:39 +00:00
|
|
|
audioconv = Gst.ElementFactory.make('audioconvert', 'audioconv')
|
|
|
|
audiores = Gst.ElementFactory.make('audioresample', 'audiores')
|
|
|
|
audiosink = Gst.ElementFactory.make(AUDIO_SINK, 'audiosink')
|
2010-10-04 15:52:22 +00:00
|
|
|
|
|
|
|
# add depayloading and playback to the pipeline and link
|
|
|
|
pipeline.add(audiodepay, audiodec, audioconv, audiores, audiosink)
|
|
|
|
|
2014-11-10 21:34:39 +00:00
|
|
|
audiodepay.link(audiodec)
|
|
|
|
audiodec.link(audioconv)
|
|
|
|
audioconv.link(audiores)
|
|
|
|
audiores.link(audiosink)
|
2010-10-04 15:52:22 +00:00
|
|
|
|
|
|
|
# the rtpbin element
|
2014-11-10 21:34:39 +00:00
|
|
|
rtpbin = Gst.ElementFactory.make('rtpbin', 'rtpbin')
|
2010-10-04 15:52:22 +00:00
|
|
|
|
|
|
|
pipeline.add(rtpbin)
|
|
|
|
|
|
|
|
# now link all to the rtpbin, start by getting an RTP sinkpad for session 0
|
2014-11-10 21:34:39 +00:00
|
|
|
srcpad = Gst.Element.get_static_pad(rtpsrc, 'src')
|
|
|
|
sinkpad = Gst.Element.get_request_pad(rtpbin, 'recv_rtp_sink_0')
|
|
|
|
lres = Gst.Pad.link(srcpad, sinkpad)
|
2010-10-04 15:52:22 +00:00
|
|
|
|
|
|
|
# get an RTCP sinkpad in session 0
|
2014-11-10 21:34:39 +00:00
|
|
|
srcpad = Gst.Element.get_static_pad(rtcpsrc, 'src')
|
|
|
|
sinkpad = Gst.Element.get_request_pad(rtpbin, 'recv_rtcp_sink_0')
|
|
|
|
lres = Gst.Pad.link(srcpad, sinkpad)
|
2010-10-04 15:52:22 +00:00
|
|
|
|
|
|
|
# get an RTCP srcpad for sending RTCP back to the sender
|
2014-11-10 21:34:39 +00:00
|
|
|
srcpad = Gst.Element.get_request_pad(rtpbin, 'send_rtcp_src_0')
|
|
|
|
sinkpad = Gst.Element.get_static_pad(rtcpsink, 'sink')
|
|
|
|
lres = Gst.Pad.link(srcpad, sinkpad)
|
2010-10-04 15:52:22 +00:00
|
|
|
|
2014-11-10 21:34:39 +00:00
|
|
|
rtpbin.connect('pad-added', pad_added_cb, audiodepay)
|
2010-10-04 15:52:22 +00:00
|
|
|
|
2014-11-10 21:34:39 +00:00
|
|
|
Gst.Element.set_state(pipeline, Gst.State.PLAYING)
|
2010-10-04 15:52:22 +00:00
|
|
|
|
2014-11-10 21:34:39 +00:00
|
|
|
mainloop = GObject.MainLoop()
|
|
|
|
mainloop.run()
|
2010-10-04 15:52:22 +00:00
|
|
|
|
2014-11-10 21:34:39 +00:00
|
|
|
Gst.Element.set_state(pipeline, Gst.State.NULL)
|
2010-10-04 15:52:22 +00:00
|
|
|
|