Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gstreamer backend #76

Open
mariannasb opened this issue Feb 15, 2022 · 0 comments
Open

Gstreamer backend #76

mariannasb opened this issue Feb 15, 2022 · 0 comments

Comments

@mariannasb
Copy link

Hello, I have created a simple Gstreamer based backend (which in this case I used with v4l2src), in case you want to add that to the repo

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

Gst.init(None)

class Camera(BaseCamera):
	video_device = "/dev/video0"

	@staticmethod
	def set_video_device(device):
		Camera.video_device = device

	def __init__(self):
		if os.environ.get('GST_CAMERA_VIDEO_DEVICE'):
			Camera.set_video_device(int(os.environ['GST_CAMERA_VIDEO_DEVICE']))
		super(Camera, self).__init__()

	@staticmethod
	def frames():
		MJPEG_PIPELINE = f"v4l2src device={Camera.video_device} ! jpegenc ! appsink name=sink"
		pipeline = Gst.parse_launch(MJPEG_PIPELINE)
		sink = pipeline.get_by_name("sink")

		# Start playing
		ret = pipeline.set_state(Gst.State.PLAYING)
		if ret == Gst.StateChangeReturn.FAILURE:
			print("Unable to set the pipeline to the playing state.")
			exit(-1)

		try:
			while True:
				sample = sink.emit("pull-sample")
				buf = sample.get_buffer()
				#print("Timestamp: ", buf.pts)
				yield buf.extract_dup(0, buf.get_size())
		finally:
			pipeline.set_state(Gst.State.NULL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant