|
|
@ -6,10 +6,30 @@ import numpy as np |
|
|
|
gi.require_version('Gst', '1.0') |
|
|
|
gi.require_version('GstRtspServer', '1.0') |
|
|
|
from gi.repository import Gst, GstRtspServer, GLib |
|
|
|
import socket |
|
|
|
|
|
|
|
def get_local_ip(): |
|
|
|
try: |
|
|
|
# Create a socket object |
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
|
|
|
|
|
|
|
# Connect to an external server (doesn't actually send any data) |
|
|
|
s.connect(("8.8.8.8", 80)) |
|
|
|
|
|
|
|
# Get the local IP address |
|
|
|
local_ip = s.getsockname()[0] |
|
|
|
|
|
|
|
# Close the socket |
|
|
|
s.close() |
|
|
|
|
|
|
|
return local_ip |
|
|
|
except Exception as e: |
|
|
|
return str(e) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VideoStream(GstRtspServer.RTSPMediaFactory): |
|
|
|
def __init__(self, fps=30, width=640, height=480): |
|
|
|
def __init__(self, fps=25, width=1920, height=1080): |
|
|
|
super(VideoStream, self).__init__() |
|
|
|
self.fps = fps |
|
|
|
self.width = width |
|
|
@ -40,10 +60,11 @@ class VideoStream(GstRtspServer.RTSPMediaFactory): |
|
|
|
"appsrc name=source is-live=true format=GST_FORMAT_TIME " |
|
|
|
"caps=video/x-raw,format=RGB,width={},height={},framerate={}/1 " |
|
|
|
"! videoconvert ! video/x-raw,format=I420 " |
|
|
|
"! x264enc tune=zerolatency bitrate=500 speed-preset=ultrafast " |
|
|
|
"! rtph264pay config-interval=1 name=pay0 pt=96" |
|
|
|
"! nvh264enc preset=low-latency rc-mode=cbr bitrate=2500 gop-size=7 zerolatency=true " |
|
|
|
"! h264parse ! rtph264pay config-interval=1 name=pay0 pt=96" |
|
|
|
).format(self.width, self.height, self.fps) |
|
|
|
|
|
|
|
|
|
|
|
pipeline = Gst.parse_launch(pipeline_str) |
|
|
|
src = pipeline.get_by_name("source") |
|
|
|
src.connect("need-data", self.on_need_data) |
|
|
@ -71,21 +92,21 @@ class RTSPServer: |
|
|
|
self.video_stream.update_frame(frame) |
|
|
|
|
|
|
|
# Global server instance |
|
|
|
rtsp_server = RTSPServer("192.168.0.196",41231) |
|
|
|
rtsp_server = RTSPServer(get_local_ip(),41231) |
|
|
|
|
|
|
|
def run_server(): |
|
|
|
rtsp_server.start() |
|
|
|
|
|
|
|
def stream_webcam(): |
|
|
|
cap = cv2.VideoCapture("/home/mht/Downloads/bcd2890d71caaf0e095b95c9b525973f61186656-360p.mp4") # Open webcam |
|
|
|
while cap.isOpened(): |
|
|
|
ret, frame = cap.read() |
|
|
|
if ret: |
|
|
|
rtsp_server.update_frame(frame) # Send frame to RTSP server |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
# Start RTSP server in a separate thread |
|
|
|
threading.Thread(target=run_server, daemon=True).start() |
|
|
|
|
|
|
|
# Stream webcam frames |
|
|
|
stream_webcam() |
|
|
|
# def stream_webcam(): |
|
|
|
# cap = cv2.VideoCapture("/home/mht/Downloads/bcd2890d71caaf0e095b95c9b525973f61186656-360p.mp4") # Open webcam |
|
|
|
# while cap.isOpened(): |
|
|
|
# ret, frame = cap.read() |
|
|
|
# if ret: |
|
|
|
# rtsp_server.update_frame(frame) # Send frame to RTSP server |
|
|
|
|
|
|
|
# if __name__ == "__main__": |
|
|
|
# # Start RTSP server in a separate thread |
|
|
|
# threading.Thread(target=run_server, daemon=True).start() |
|
|
|
# |
|
|
|
# # Stream webcam frames |
|
|
|
# stream_webcam() |