Powered by Blogger.

Video Streaming with Gstreamer - 03

Thursday, January 3, 2013

Today, I was able to complete the Python Gstreamer receiver with RTCP. But still synchronization within streams is not perfect. Have to research further. I used 4 vertically divided videos from the same original video. Here is the code of the receiver.

#!/usr/bin/python

import pygst
pygst.require("0.10")
import gst
import pygtk
import gtk

class Main:
    def __init__(self):
       
        def onPad(obj, pad, target):
            sinkpad = target.get_compatible_pad(pad, pad.get_caps())
            if sinkpad:
                pad.link(sinkpad)
            print "pad added"
            print obj
            return True
 
        self.pipeline = gst.Pipeline("mypipeline")
        self.mixer = gst.element_factory_make('videomixer')
        self.pipeline.add(self.mixer)
        self.videosink = gst.element_factory_make('autovideosink')
        self.pipeline.add(self.videosink)
        self.ffmpegcs = gst.element_factory_make("ffmpegcolorspace", "ffmpegcs")
        self.pipeline.add(self.ffmpegcs)

        # video 1
        self.udpsrc11 = gst.element_factory_make("udpsrc")
        self.udpsrc11.set_property("port", 5011)
        self.udpsrc12 = gst.element_factory_make("udpsrc")
        self.udpsrc12.set_property("port", 5012)
        caps = gst.caps_from_string("application/x-rtp, media=(string)video, \
                        clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96")
        self.udpsrc11.set_property('caps', caps)
        self.rtpbin1 = gst.element_factory_make("gstrtpbin", 'rtpbin1')
        self.rtpbin1.set_property('latency', 100)
        self.rtp_depay1 = gst.element_factory_make("rtph264depay")
        self.q1 = gst.element_factory_make('queue')
        self.decoder1 = gst.element_factory_make('ffdec_h264')
        self.box1 = gst.element_factory_make('videobox')
        self.box1.set_property('top', 0)
        self.box1.set_property('border-alpha', 0)
        self.udpsink1 = gst.element_factory_make("udpsink")
        self.udpsink1.set_property("host", "10.42.0.13")
        self.udpsink1.set_property("port", 5013)
        self.udpsink1.set_property('sync', False)
        self.udpsink1.set_property('async', False)
        self.pipeline.add_many(self.udpsrc11, self.udpsrc12, self.rtpbin1, self.rtp_depay1, \
                               self.q1, self.decoder1, self.udpsink1, self.box1)
     
        # video 1 linking
        self.udpsrc11.link_pads('src', self.rtpbin1, 'recv_rtp_sink_0')
        self.udpsrc12.link_pads('src', self.rtpbin1, 'recv_rtcp_sink_0')
        self.rtpbin1.connect('pad-added', onPad, self.rtp_depay1)
        self.rtpbin1.link_pads('send_rtcp_src_0', self.udpsink1, 'sink')
        self.rtp_depay1.link(self.q1)
        self.q1.link(self.decoder1)
        self.decoder1.link(self.box1)
        self.box1.link(self.mixer)
       
        # video 2
        self.udpsrc21 = gst.element_factory_make("udpsrc")
        self.udpsrc21.set_property("port", 5021)
        self.udpsrc22 = gst.element_factory_make("udpsrc")
        self.udpsrc22.set_property("port", 5022)
        caps = gst.caps_from_string("application/x-rtp, media=(string)video, \
                        clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96")
        self.udpsrc21.set_property('caps', caps)
        self.rtpbin2 = gst.element_factory_make("gstrtpbin", 'rtpbin2')
        self.rtpbin2.set_property('latency', 100)
        self.rtp_depay2 = gst.element_factory_make("rtph264depay")
        self.q2 = gst.element_factory_make('queue')
        self.decoder2 = gst.element_factory_make('ffdec_h264')
        self.box2 = gst.element_factory_make('videobox')
        self.box2.set_property('top', -120)
        self.box2.set_property('border-alpha', 0)
        self.udpsink2 = gst.element_factory_make("udpsink")
        self.udpsink2.set_property("host", "10.42.0.13")
        self.udpsink2.set_property("port", 5023)
        self.udpsink2.set_property('sync', False)
        self.udpsink2.set_property('async', False)
        self.pipeline.add_many(self.udpsrc21, self.udpsrc22, self.rtpbin2, self.rtp_depay2, \
                               self.q2, self.decoder2, self.udpsink2, self.box2)
     
        # video 2 linking
        self.udpsrc21.link_pads('src', self.rtpbin2, 'recv_rtp_sink_0')
        self.udpsrc22.link_pads('src', self.rtpbin2, 'recv_rtcp_sink_0')
        self.rtpbin2.connect('pad-added', onPad, self.rtp_depay2)
        self.rtpbin2.link_pads('send_rtcp_src_0', self.udpsink2, 'sink')
        self.rtp_depay2.link(self.q2)
        self.q2.link(self.decoder2)
        self.decoder2.link(self.box2)
        self.box2.link(self.mixer)
       
        # video 3
        self.udpsrc31 = gst.element_factory_make("udpsrc")
        self.udpsrc31.set_property("port", 5031)
        self.udpsrc32 = gst.element_factory_make("udpsrc")
        self.udpsrc32.set_property("port", 5032)
        caps = gst.caps_from_string("application/x-rtp, media=(string)video, \
                        clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96")
        self.udpsrc31.set_property('caps', caps)
        self.rtpbin3 = gst.element_factory_make("gstrtpbin", 'rtpbin3')
        self.rtpbin3.set_property('latency', 100)
        self.rtp_depay3 = gst.element_factory_make("rtph264depay")
        self.q3 = gst.element_factory_make('queue')
        self.decoder3 = gst.element_factory_make('ffdec_h264')
        self.box3 = gst.element_factory_make('videobox')
        self.box3.set_property('top', -240)
        self.box3.set_property('border-alpha', 0)
        self.udpsink3 = gst.element_factory_make("udpsink")
        self.udpsink3.set_property("host", "10.42.0.13")
        self.udpsink3.set_property("port", 5033)
        self.udpsink3.set_property('sync', False)
        self.udpsink3.set_property('async', False)
        self.pipeline.add_many(self.udpsrc31, self.udpsrc32, self.rtpbin3, self.rtp_depay3, \
                               self.q3, self.decoder3, self.udpsink3, self.box3)
     
        # video 3 linking
        self.udpsrc31.link_pads('src', self.rtpbin3, 'recv_rtp_sink_0')
        self.udpsrc32.link_pads('src', self.rtpbin3, 'recv_rtcp_sink_0')
        self.rtpbin3.connect('pad-added', onPad, self.rtp_depay3)
        self.rtpbin3.link_pads('send_rtcp_src_0', self.udpsink3, 'sink')
        self.rtp_depay3.link(self.q3)
        self.q3.link(self.decoder3)
        self.decoder3.link(self.box3)
        self.box3.link(self.mixer)
       
        # video 4
        self.udpsrc41 = gst.element_factory_make("udpsrc")
        self.udpsrc41.set_property("port", 5041)
        self.udpsrc42 = gst.element_factory_make("udpsrc")
        self.udpsrc42.set_property("port", 5042)
        caps = gst.caps_from_string("application/x-rtp, media=(string)video, \
                        clock-rate=(int)90000, encoding-name=(string)H264, payload=(int)96")
        self.udpsrc41.set_property('caps', caps)
        self.rtpbin4 = gst.element_factory_make("gstrtpbin", 'rtpbin4')
        self.rtpbin4.set_property('latency', 100)
        self.rtp_depay4 = gst.element_factory_make("rtph264depay")
        self.q4 = gst.element_factory_make('queue')
        self.decoder4 = gst.element_factory_make('ffdec_h264')
        self.box4 = gst.element_factory_make('videobox')
        self.box4.set_property('top', -360)
        self.box4.set_property('border-alpha', 0)
        self.udpsink4 = gst.element_factory_make("udpsink")
        self.udpsink4.set_property("host", "10.42.0.13")
        self.udpsink4.set_property("port", 5043)
        self.udpsink4.set_property('sync', False)
        self.udpsink4.set_property('async', False)
        self.pipeline.add_many(self.udpsrc41, self.udpsrc42, self.rtpbin4, self.rtp_depay4, \
                               self.q4, self.decoder4, self.udpsink4, self.box4)
     
        # video 4 linking
        self.udpsrc41.link_pads('src', self.rtpbin4, 'recv_rtp_sink_0')
        self.udpsrc42.link_pads('src', self.rtpbin4, 'recv_rtcp_sink_0')
        self.rtpbin4.connect('pad-added', onPad, self.rtp_depay4)
        self.rtpbin4.link_pads('send_rtcp_src_0', self.udpsink4, 'sink')
        self.rtp_depay4.link(self.q4)
        self.q4.link(self.decoder4)
        self.decoder4.link(self.box4)
        self.box4.link(self.mixer)
       
        self.mixer.link(self.ffmpegcs)
        self.ffmpegcs.link(self.videosink)
       
        self.pipeline.set_state(gst.STATE_PLAYING)
 
start = Main()
gtk.main()

No comments: