1#! /usr/bin/env python 2 3import gi 4import sys 5gi.require_version('Gst', '1.0') 6from gi.repository import GObject, Gst 7 8# 9# A simple RTP receiver 10# 11# receives alaw encoded RTP audio on port 5002, RTCP is received on port 5003. 12# the receiver RTCP reports are sent to port 5007 13# 14# .-------. .----------. .---------. .-------. .--------. 15# RTP |udpsrc | | rtpbin | |pcmadepay| |alawdec| |alsasink| 16# port=5002 | src->recv_rtp recv_rtp->sink src->sink src->sink | 17# '-------' | | '---------' '-------' '--------' 18# | | 19# | | .-------. 20# | | |udpsink| RTCP 21# | send_rtcp->sink | port=5007 22# .-------. | | '-------' sync=false 23# RTCP |udpsrc | | | async=false 24# port=5003 | src->recv_rtcp | 25# '-------' '----------' 26 27AUDIO_CAPS = 'application/x-rtp,media=(string)audio,clock-rate=(int)8000,encoding-name=(string)PCMA' 28AUDIO_DEPAY = 'rtppcmadepay' 29AUDIO_DEC = 'alawdec' 30AUDIO_SINK = 'autoaudiosink' 31 32DEST = '127.0.0.1' 33 34RTP_RECV_PORT = 5002 35RTCP_RECV_PORT = 5003 36RTCP_SEND_PORT = 5007 37 38GObject.threads_init() 39Gst.init(sys.argv) 40 41#gst-launch -v rtpbin name=rtpbin \ 42# udpsrc caps=$AUDIO_CAPS port=$RTP_RECV_PORT ! rtpbin.recv_rtp_sink_0 \ 43# rtpbin. ! rtppcmadepay ! alawdec ! audioconvert ! audioresample ! autoaudiosink \ 44# udpsrc port=$RTCP_RECV_PORT ! rtpbin.recv_rtcp_sink_0 \ 45# rtpbin.send_rtcp_src_0 ! udpsink port=$RTCP_SEND_PORT host=$DEST sync=false async=false 46 47def pad_added_cb(rtpbin, new_pad, depay): 48 sinkpad = Gst.Element.get_static_pad(depay, 'sink') 49 lres = Gst.Pad.link(new_pad, sinkpad) 50 51# the pipeline to hold eveything 52pipeline = Gst.Pipeline('rtp_client') 53 54# the udp src and source we will use for RTP and RTCP 55rtpsrc = Gst.ElementFactory.make('udpsrc', 'rtpsrc') 56rtpsrc.set_property('port', RTP_RECV_PORT) 57 58# we need to set caps on the udpsrc for the RTP data 59caps = Gst.caps_from_string(AUDIO_CAPS) 60rtpsrc.set_property('caps', caps) 61 62rtcpsrc = Gst.ElementFactory.make('udpsrc', 'rtcpsrc') 63rtcpsrc.set_property('port', RTCP_RECV_PORT) 64 65rtcpsink = Gst.ElementFactory.make('udpsink', 'rtcpsink') 66rtcpsink.set_property('port', RTCP_SEND_PORT) 67rtcpsink.set_property('host', DEST) 68 69# no need for synchronisation or preroll on the RTCP sink 70rtcpsink.set_property('async', False) 71rtcpsink.set_property('sync', False) 72 73pipeline.add(rtpsrc, rtcpsrc, rtcpsink) 74 75# the depayloading and decoding 76audiodepay = Gst.ElementFactory.make(AUDIO_DEPAY, 'audiodepay') 77audiodec = Gst.ElementFactory.make(AUDIO_DEC, 'audiodec') 78 79# the audio playback and format conversion 80audioconv = Gst.ElementFactory.make('audioconvert', 'audioconv') 81audiores = Gst.ElementFactory.make('audioresample', 'audiores') 82audiosink = Gst.ElementFactory.make(AUDIO_SINK, 'audiosink') 83 84# add depayloading and playback to the pipeline and link 85pipeline.add(audiodepay, audiodec, audioconv, audiores, audiosink) 86 87audiodepay.link(audiodec) 88audiodec.link(audioconv) 89audioconv.link(audiores) 90audiores.link(audiosink) 91 92# the rtpbin element 93rtpbin = Gst.ElementFactory.make('rtpbin', 'rtpbin') 94 95pipeline.add(rtpbin) 96 97# now link all to the rtpbin, start by getting an RTP sinkpad for session 0 98srcpad = Gst.Element.get_static_pad(rtpsrc, 'src') 99sinkpad = Gst.Element.get_request_pad(rtpbin, 'recv_rtp_sink_0') 100lres = Gst.Pad.link(srcpad, sinkpad) 101 102# get an RTCP sinkpad in session 0 103srcpad = Gst.Element.get_static_pad(rtcpsrc, 'src') 104sinkpad = Gst.Element.get_request_pad(rtpbin, 'recv_rtcp_sink_0') 105lres = Gst.Pad.link(srcpad, sinkpad) 106 107# get an RTCP srcpad for sending RTCP back to the sender 108srcpad = Gst.Element.get_request_pad(rtpbin, 'send_rtcp_src_0') 109sinkpad = Gst.Element.get_static_pad(rtcpsink, 'sink') 110lres = Gst.Pad.link(srcpad, sinkpad) 111 112rtpbin.connect('pad-added', pad_added_cb, audiodepay) 113 114Gst.Element.set_state(pipeline, Gst.State.PLAYING) 115 116mainloop = GObject.MainLoop() 117mainloop.run() 118 119Gst.Element.set_state(pipeline, Gst.State.NULL) 120 121