Skip to content

Streaming from my cell phone camera to a rtmp server #321

@asokone

Description

@asokone

Hi
I am new to stream development. I ask chatGPT to provide me with some code in python for streaming from my cell phone camera to a rtmp server. The RTMP server is working fine. I have tested with OBS and VLC Media Player.

When I executed this code on Jupyter Notebook I got this error below
Can someone help figure out what is wrong?

Regards
A Sokone.

------------------------- PYTHON CODE START HERE
import cv2
import gi

# ---- I added these lines
import ctypes
import numpy as np
import cairo
# ---- End of added lines

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

# Initialize GStreamer
Gst.init(None)

# Define the RTMP server URL
rtmp_server_url = 'rtmp://192.168.0.30/live'

# Set up the GStreamer pipeline
pipeline = Gst.Pipeline.new()

# Create the elements for video capture and encoding
src = Gst.ElementFactory.make("appsrc", "source")
caps = Gst.Caps.from_string("video/x-raw,format=BGR")
filter = Gst.ElementFactory.make("capsfilter", "filter")
filter.set_property("caps", caps)
video_convert = Gst.ElementFactory.make("videoconvert", "video_convert")
x264enc = Gst.ElementFactory.make("x264enc", "x264enc")
mux = Gst.ElementFactory.make("flvmux", "mux")
sink = Gst.ElementFactory.make("rtmpsink", "sink")
sink.set_property("location", rtmp_server_url)

# Add elements to the pipeline
for element in [src, filter, video_convert, x264enc, mux, sink]:
    pipeline.add(element)

# Link elements in the pipeline
src.link(filter)
filter.link(video_convert)
video_convert.link(x264enc)
x264enc.link(mux)
mux.link(sink)

# Start the pipeline
pipeline.set_state(Gst.State.PLAYING)

# Open the video capture device
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    
    # Convert frame to Cairo surface
    height, width, _ = frame.shape

    stride = width * 1  # Assuming 3 channels (RGB)
    
    surface = cairo.ImageSurface.create_for_data(frame.data, cairo.FORMAT_RGB24, width, height)
    
    print("surface.get_data", surface.get_data())
    
    # Push frame to the GStreamer pipeline
    buf = Gst.Buffer.new_wrapped(surface.get_data())
    
    print("buf", buf)
    buf.pts = buf.dts = Gst.CLOCK_TIME_NONE
    buf.duration = 1
    src.emit("push-buffer", buf)

# Clean up
cap.release()
pipeline.set_state(Gst.State.NULL)

--------------------------- End of Python Code

Error below

TypeError Traceback (most recent call last)
Cell In[1], line 65
61 print ("height", height, "width", width, "stride", stride)
63 #surface = cairo.ImageSurface.create_for_data(frame.data, cairo.FORMAT_RGB24, width, height, stride)
---> 65 surface = cairo.ImageSurface.create_for_data(frame.data, cairo.FORMAT_RGB24, width, height)
67 print("surface.get_data", surface.get_data())
69 # Push frame to the GStreamer pipeline

TypeError: buffer is not long enough

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions