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#gst-launch -v rtpbin name=rtpbin audiotestsrc ! audioconvert ! alawenc ! rtppcmapay ! rtpbin.send_rtp_sink_0 \ 10# rtpbin.send_rtp_src_0 ! udpsink port=10000 host=xxx.xxx.xxx.xxx \ 11# rtpbin.send_rtcp_src_0 ! udpsink port=10001 host=xxx.xxx.xxx.xxx sync=false async=false \ 12# udpsrc port=10002 ! rtpbin.recv_rtcp_sink_0 13 14DEST_HOST = '127.0.0.1' 15 16AUDIO_SRC = 'audiotestsrc' 17AUDIO_ENC = 'alawenc' 18AUDIO_PAY = 'rtppcmapay' 19 20RTP_SEND_PORT = 5002 21RTCP_SEND_PORT = 5003 22RTCP_RECV_PORT = 5007 23 24GObject.threads_init() 25Gst.init(sys.argv) 26 27# the pipeline to hold everything 28pipeline = Gst.Pipeline('rtp_server') 29 30# the pipeline to hold everything 31audiosrc = Gst.ElementFactory.make(AUDIO_SRC, 'audiosrc') 32audioconv = Gst.ElementFactory.make('audioconvert', 'audioconv') 33audiores = Gst.ElementFactory.make('audioresample', 'audiores') 34 35# the pipeline to hold everything 36audioenc = Gst.ElementFactory.make(AUDIO_ENC, 'audioenc') 37audiopay = Gst.ElementFactory.make(AUDIO_PAY, 'audiopay') 38 39# add capture and payloading to the pipeline and link 40pipeline.add(audiosrc, audioconv, audiores, audioenc, audiopay) 41 42audiosrc.link(audioconv) 43audioconv.link(audiores) 44audiores.link(audioenc) 45audioenc.link(audiopay) 46 47# the rtpbin element 48rtpbin = Gst.ElementFactory.make('rtpbin', 'rtpbin') 49 50pipeline.add(rtpbin) 51 52# the udp sinks and source we will use for RTP and RTCP 53rtpsink = Gst.ElementFactory.make('udpsink', 'rtpsink') 54rtpsink.set_property('port', RTP_SEND_PORT) 55rtpsink.set_property('host', DEST_HOST) 56 57rtcpsink = Gst.ElementFactory.make('udpsink', 'rtcpsink') 58rtcpsink.set_property('port', RTCP_SEND_PORT) 59rtcpsink.set_property('host', DEST_HOST) 60# no need for synchronisation or preroll on the RTCP sink 61rtcpsink.set_property('async', False) 62rtcpsink.set_property('sync', False) 63 64rtcpsrc = Gst.ElementFactory.make('udpsrc', 'rtcpsrc') 65rtcpsrc.set_property('port', RTCP_RECV_PORT) 66 67pipeline.add(rtpsink, rtcpsink, rtcpsrc) 68 69# now link all to the rtpbin, start by getting an RTP sinkpad for session 0 70sinkpad = Gst.Element.get_request_pad(rtpbin, 'send_rtp_sink_0') 71srcpad = Gst.Element.get_static_pad(audiopay, 'src') 72lres = Gst.Pad.link(srcpad, sinkpad) 73 74# get the RTP srcpad that was created when we requested the sinkpad above and 75# link it to the rtpsink sinkpad 76srcpad = Gst.Element.get_static_pad(rtpbin, 'send_rtp_src_0') 77sinkpad = Gst.Element.get_static_pad(rtpsink, 'sink') 78lres = Gst.Pad.link(srcpad, sinkpad) 79 80# get an RTCP srcpad for sending RTCP to the receiver 81srcpad = Gst.Element.get_request_pad(rtpbin, 'send_rtcp_src_0') 82sinkpad = Gst.Element.get_static_pad(rtcpsink, 'sink') 83lres = Gst.Pad.link(srcpad, sinkpad) 84 85# we also want to receive RTCP, request an RTCP sinkpad for session 0 and 86# link it to the srcpad of the udpsrc for RTCP 87srcpad = Gst.Element.get_static_pad(rtcpsrc, 'src') 88sinkpad = Gst.Element.get_request_pad(rtpbin, 'recv_rtcp_sink_0') 89lres = Gst.Pad.link(srcpad, sinkpad) 90 91# set the pipeline to playing 92Gst.Element.set_state(pipeline, Gst.State.PLAYING) 93 94# we need to run a GLib main loop to get the messages 95mainloop = GObject.MainLoop() 96mainloop.run() 97 98Gst.Element.set_state(pipeline, Gst.State.NULL) 99