# import necessary arguments
import gi
import cv2
import argparse
# import required library like Gstreamer and GstreamerRtspServer
gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject, GLib
IMAGE_WIDTH = 384
IMAGE_HEIGHT = 288
FPS = 25
INPUT_SOURCE = '/path/to/my/video/file.mp4'
# Sensor Factory class which inherits the GstRtspServer base class and add
# properties to it.
class SensorFactory(GstRtspServer.RTSPMediaFactory):
def __init__(self, args, **properties):
super(SensorFactory, self).__init__(**properties)
self.cap = cv2.VideoCapture(args.input_source)
self.number_frames = 0
self.fps = args.fps
self.duration = 1 / self.fps * Gst.SECOND # duration of a frame in nanoseconds
self.launch_string = 'appsrc name=source is-live=true block=true format=GST_FORMAT_TIME caps=video/x-raw,format=BGR,width={},height={},framerate={}/1 ' \
'! videoconvert ! video/x-raw,format=I420 ! x264enc speed-preset=ultrafast tune=zerolatency ! rtph264pay config-interval=1 name=pay0 pt=96' \
.format(args.image_width, args.image_height, self.fps)
self.args = args
self.frame_id = 0
# method to capture the video feed from the camera and push it to the
# streaming buffer.
def on_need_data(self, src, length):
if self.cap.isOpened():
ret, frame = self.cap.read()
self.frame_id += 1
if ret:
# It is better to change the resolution of the camera
# instead of changing the image shape as it affects the image quality.
frame = cv2.resize(frame, (self.args.image_width, self.args.image_height), interpolation = cv2.INTER_LINEAR)
cv2.putText(frame, str(self.frame_id), (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2)
data = frame.tobytes() # 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)
# attach the launch string to the override method
def do_create_element(self, url):
return Gst.parse_launch(self.launch_string)
# attaching the source element to the rtsp media
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)
# Rtsp server implementation where we attach the factory sensor with the stream uri
class GstServer(GstRtspServer.RTSPServer):
def __init__(self, args, **properties):
super(GstServer, self).__init__(**properties)
self.factory = SensorFactory(args)
self.factory.set_shared(True)
self.set_service(str(args.port))
self.get_mount_points().add_factory(args.stream_uri, self.factory)
self.attach(None)
def get_args():
# getting the required information from the user
parser = argparse.ArgumentParser()
parser.add_argument("--input_source", default=INPUT_SOURCE, help="device id for the \
video device or video file location")
parser.add_argument("--fps", default=FPS, help="fps of the camera", type=int)
parser.add_argument("--image_width", default=IMAGE_WIDTH, help="video frame width", type=int)
parser.add_argument("--image_height", default=IMAGE_HEIGHT, help="video frame height", type=int)
parser.add_argument("--port", default=8554, help="port to stream video", type=int)
parser.add_argument("--stream_uri", default = "/video_stream", help="rtsp video stream uri")
args = parser.parse_args()
return args
def main():
args = get_args()
if args.input_source.isdigit():
args.input_source = int(args.input_source)
print('RTSP streaming at: rtsp://127.0.0.0:8554{}'.format(args.stream_uri))
# initializing the threads and running the stream on loop.
# GObject.threads_init() -> no longer needed
Gst.init(None)
server = GstServer(args)
loop = GLib.MainLoop() # GObject.MainLoop()
loop.run()
if __name__ == '__main__':
main()
который по сути использует видеофайл в системе и создает поток RTSP с помощью appsrc. Я включил простой счетчик в создаваемые кадры, чтобы проверить, какие кадры распространяются. Есть несколько проблем с функциональностью кода:
a) Хотя это работает для одного потребителя (например, одного экземпляра VLC), оно не работает, если используется второй (например, второй экземпляр VLC). Я не мог понять, почему это так и как я могу решить эту проблему.
b) Кажется, в некоторых моментах при обмене потоковыми потоками при плавной работе возникают некоторые зависания. Например, на 38-м кадре он замер и возобновился примерно на 250-м кадре и т. д. Есть ли что-нибудь, что я мог бы использовать в конвейере для улучшения плавности распространения?
Честно говоря, я немного отстал от огромного количества параметров GStreamer. Другой пример, в котором вместо этого используется не appsrc, а multifilesrc, может быть прочитан более чем одним экземпляром VLC без проблем, но не удалось найти, какой параметр (если таковой имеется) я мог бы перенести в свой пример. Изменение параметров is-live и block, похоже, не оказало никакого влияния на проблемы, упомянутые ранее.
Я пытаюсь подключить свой модуль к потоку RTSP с помощью GStreamer. В основном я использую этот пример и немного изменил его под свои нужды. [code]# import necessary arguments import gi import cv2 import argparse
# import required library like Gstreamer and GstreamerRtspServer gi.require_version('Gst', '1.0') gi.require_version('GstRtspServer', '1.0') from gi.repository import Gst, GstRtspServer, GObject, GLib
# Sensor Factory class which inherits the GstRtspServer base class and add # properties to it. class SensorFactory(GstRtspServer.RTSPMediaFactory): def __init__(self, args, **properties): super(SensorFactory, self).__init__(**properties) self.cap = cv2.VideoCapture(args.input_source) self.number_frames = 0 self.fps = args.fps self.duration = 1 / self.fps * Gst.SECOND # duration of a frame in nanoseconds self.launch_string = 'appsrc name=source is-live=true block=true format=GST_FORMAT_TIME caps=video/x-raw,format=BGR,width={},height={},framerate={}/1 ' \ '! videoconvert ! video/x-raw,format=I420 ! x264enc speed-preset=ultrafast tune=zerolatency ! rtph264pay config-interval=1 name=pay0 pt=96' \ .format(args.image_width, args.image_height, self.fps) self.args = args self.frame_id = 0
# method to capture the video feed from the camera and push it to the # streaming buffer. def on_need_data(self, src, length): if self.cap.isOpened(): ret, frame = self.cap.read() self.frame_id += 1 if ret: # It is better to change the resolution of the camera # instead of changing the image shape as it affects the image quality. frame = cv2.resize(frame, (self.args.image_width, self.args.image_height), interpolation = cv2.INTER_LINEAR) cv2.putText(frame, str(self.frame_id), (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 0, 0), 2) data = frame.tobytes() # 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) # attach the launch string to the override method def do_create_element(self, url): return Gst.parse_launch(self.launch_string)
# attaching the source element to the rtsp media 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)
# Rtsp server implementation where we attach the factory sensor with the stream uri class GstServer(GstRtspServer.RTSPServer): def __init__(self, args, **properties): super(GstServer, self).__init__(**properties) self.factory = SensorFactory(args) self.factory.set_shared(True) self.set_service(str(args.port)) self.get_mount_points().add_factory(args.stream_uri, self.factory) self.attach(None)
def get_args(): # getting the required information from the user parser = argparse.ArgumentParser() parser.add_argument("--input_source", default=INPUT_SOURCE, help="device id for the \ video device or video file location") parser.add_argument("--fps", default=FPS, help="fps of the camera", type=int) parser.add_argument("--image_width", default=IMAGE_WIDTH, help="video frame width", type=int) parser.add_argument("--image_height", default=IMAGE_HEIGHT, help="video frame height", type=int) parser.add_argument("--port", default=8554, help="port to stream video", type=int) parser.add_argument("--stream_uri", default = "/video_stream", help="rtsp video stream uri") args = parser.parse_args() return args
def main(): args = get_args() if args.input_source.isdigit(): args.input_source = int(args.input_source)
# initializing the threads and running the stream on loop. # GObject.threads_init() -> no longer needed Gst.init(None) server = GstServer(args) loop = GLib.MainLoop() # GObject.MainLoop() loop.run()
if __name__ == '__main__': main() [/code] который по сути использует видеофайл в системе и создает поток RTSP с помощью appsrc. Я включил простой счетчик в создаваемые кадры, чтобы проверить, какие кадры распространяются. Есть несколько проблем с функциональностью кода: a) Хотя это работает для одного потребителя (например, одного экземпляра VLC), оно не работает, если используется второй (например, второй экземпляр VLC). Я не мог понять, почему это так и как я могу решить эту проблему. b) Кажется, в некоторых моментах при обмене потоковыми потоками при плавной работе возникают некоторые зависания. Например, на 38-м кадре он замер и возобновился примерно на 250-м кадре и т. д. Есть ли что-нибудь, что я мог бы использовать в конвейере для улучшения плавности распространения? Честно говоря, я немного отстал от огромного количества параметров GStreamer. Другой пример, в котором вместо этого используется не appsrc, а multifilesrc, может быть прочитан более чем одним экземпляром VLC без проблем, но не удалось найти, какой параметр (если таковой имеется) я мог бы перенести в свой пример. Изменение параметров is-live и block, похоже, не оказало никакого влияния на проблемы, упомянутые ранее.