Python3 : How can we stream multiple videos as RTSP stream on the same port with different extensions

satyanarayana satyarepala3 at gmail.com
Mon Jan 4 15:47:02 UTC 2021


Example : i have video1 and video2, i want to stream these 2 as
rtsp://127.0.0.1:8012/video1 and rtsp://127.0.0.1:8012/video2

i am using below code for creating single rtsp stream:

#!/usr/bin/env python3

import cv2
import gi

gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject

##Overwriting Sensor factory class in local
class SensorFactory(GstRtspServer.RTSPMediaFactory):
    def __init__(self, **properties):
        super(SensorFactory, self).__init__(**properties)
        self.cap = cv2.VideoCapture(0)
        self.number_frames = 0
        self.fps = 30
        self.duration = 1 / self.fps * Gst.SECOND  # duration of a frame in
nanoseconds
        ##pipeline for transfering each frame as rtsp stream
        self.launch_string = 'appsrc name=source is-live=true block=true
format=GST_FORMAT_TIME ' \
                            
'caps=video/x-raw,format=BGR,width=600,height=600,framerate={}/1 ' \
                             '! videoconvert ! video/x-raw,format=I420 ' \
                             '! x264enc speed-preset=ultrafast
tune=zerolatency ' \
                             '! rtph264pay config-interval=1 name=pay0
pt=96'.format(self.fps)

    #when need buffer data , read each frame and encode and add to buffer
    def on_need_data(self, src, lenght):
        if self.cap.isOpened():
            ret, frame = self.cap.read()
            if ret:
                frame=cv2.resize(frame,(600,600))
                data = frame.tostring()
                buf = Gst.Buffer.new_allocate(None, len(data), None)
                buf.fill(0, data)
                buf.duration = self.duration
                timestamp = self.number_frames * self.duration
                buf.pts = buf.dts = int(timestamp)
                buf.offset = timestamp
                self.number_frames += 1
                retval = src.emit('push-buffer', buf)
                print('pushed buffer, frame {}, duration {} ns, durations {}
s'.format(self.number_frames,
                                                                                      
self.duration,
                                                                                      
self.duration / Gst.SECOND))
                if retval != Gst.FlowReturn.OK:
                    print(retval)

    def do_create_element(self, url):
        return Gst.parse_launch(self.launch_string)

    def do_configure(self, rtsp_media):
        self.number_frames = 0
        appsrc = rtsp_media.get_element().get_child_by_name('source')
        appsrc.connect('need-data', self.on_need_data)

##overwriting gst server class,add new factory
class GstServer(GstRtspServer.RTSPServer):
    def __init__(self, **properties):
        super(GstServer, self).__init__(**properties)
        #setting port to 8012, default port is 8554
        self.set_service("8012")
        self.gst_rtsp_media_factory_set_shared()
        self.factory = SensorFactory()
        self.factory.set_shared(True)
        self.get_mount_points().add_factory("/test1", self.factory)
        self.attach(None)


GObject.threads_init()
Gst.init(None)
server = GstServer()
loop = GObject.MainLoop()
loop.run()



--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/


More information about the gstreamer-devel mailing list