def setup_streamer(self, width, height):
        fps = Fraction(FPS)
        fps_str = fraction_to_str(fps)
        caps = f'video/x-raw,format={VIDEO_FORMAT},width={width},height={height},framerate={fps_str}'

        # Converts list of plugins to gst-launch string
        # ['plugin_1', 'plugin_2', 'plugin_3'] => plugin_1 ! plugin_2 ! plugin_3
        default_pipeline = utils.to_gst_string([
            f'appsrc caps={caps}', 'videoscale method=1 add-borders=false',
            'video/x-raw,width=1280,height=720', 'videoconvert',
            'v4l2sink device=/dev/video0 sync=false'
        ])

        self.duration = 10**9 / (fps.numerator / fps.denominator)
        self.appsrc = self.pts = self.pipeline = None

        self.context = GstContext()
        self.context.startup()
        self.pipeline = GstPipeline(default_pipeline)

        def on_pipeline_init(other_self):
            """Setup AppSrc element"""
            self.appsrc = other_self.get_by_cls(GstApp.AppSrc)[0]  # get AppSrc

            # instructs appsrc that we will be dealing with timed buffer
            self.appsrc.set_property("format", Gst.Format.TIME)

            # instructs appsrc to block pushing buffers until ones in queue are preprocessed
            # allows to avoid huge queue internal queue size in appsrc
            self.appsrc.set_property("block", True)
            self.appsrc.set_property("is-live", True)

            # set input format (caps)
            self.appsrc.set_caps(Gst.Caps.from_string(caps))

        # override on_pipeline_init to set specific properties before launching pipeline
        self.pipeline._on_pipeline_init = on_pipeline_init.__get__(
            self.pipeline)  # noqa

        try:
            self.pipeline.startup()
            self.appsrc = self.pipeline.get_by_cls(
                GstApp.AppSrc)[0]  # GstApp.AppSrc

            self.pts = 0  # buffers presentation timestamp
        except Exception as e:
            print("Error: ", e)
示例#2
0
    print("--- Before ---")
    print("Max rank plugin:", max_rank_element.get_name(), "(",
          max_rank_element.get_rank(), ")")
    print("Rank of target plugin:", target_element.get_name(), "(",
          target_element.get_rank(), ")")

    print("--- After ---")

# Increase target's element rank
target_element.set_rank(max_rank_element.get_rank() + 1)
print("Rank of target plugin:", target_element.get_name(), "(",
      target_element.get_rank(), ")")

pipeline_str = pipeline

with GstContext(), GstPipeline(pipeline_str) as p:
    try:
        while not p.is_done:
            time.sleep(1)
    except Exception:
        pass
    finally:
        # print all elements and notify of target plugin presence
        elements = [
            el.get_factory().get_name() for el in p.pipeline.iterate_recurse()
        ]
        print("All elements: ", elements)
        print("Target element ({}) is {}".format(
            target_element_name,
            'present' if target_element_name in set(elements) else "missing"))
示例#3
0
        array = array.reshape(h, w, c).squeeze()
    return np.squeeze(array)  # remove single dimension if exists


def on_buffer(sink: GstApp.AppSink, data: typ.Any) -> Gst.FlowReturn:
    """Callback on 'new-sample' signal"""
    # Emit 'pull-sample' signal
    # https://lazka.github.io/pgi-docs/GstApp-1.0/classes/AppSink.html#GstApp.AppSink.signals.pull_sample

    sample = sink.emit("pull-sample")  # Gst.Sample

    if isinstance(sample, Gst.Sample):
        array = extract_buffer(sample)
        print(
            "Received {type} with shape {shape} of type {dtype}".format(type=type(array),
                                                                        shape=array.shape,
                                                                        dtype=array.dtype))
        return Gst.FlowReturn.OK

    return Gst.FlowReturn.ERROR


with GstContext():  # create GstContext (hides MainLoop)
    # create GstPipeline (hides Gst.parse_launch)
    with GstPipeline(command) as pipeline:
        appsink = pipeline.get_by_cls(GstApp.AppSink)[0]  # get AppSink
        # subscribe to <new-sample> signal
        appsink.connect("new-sample", on_buffer, None)
        while not pipeline.is_done:
            time.sleep(.1)
示例#4
0
import time
import argparse

from gstreamer import GstPipeline, GstContext

DEFAULT_PIPELINE = "videotestsrc num-buffers=100 ! fakesink sync=false"

ap = argparse.ArgumentParser()
ap.add_argument("-p", "--pipeline", required=True,
                default=DEFAULT_PIPELINE, help="Gstreamer pipeline without gst-launch")

args = vars(ap.parse_args())

if __name__ == '__main__':
    with GstContext(), GstPipeline(args['pipeline']) as pipeline:
        while not pipeline.is_done:
            time.sleep(.1)
from gstreamer import GstVideoSource, GstVideoSink, GstVideo, Gst, GLib, GstContext

WIDTH, HEIGHT, CHANNELS = 640, 480, 3
NUM_BUFFERS = 1000
VIDEO_FORMAT = GstVideo.VideoFormat.RGB

video_format_str = GstVideo.VideoFormat.to_string(VIDEO_FORMAT)
caps_filter = "capsfilter caps=video/x-raw,format={video_format_str},width={WIDTH},height={HEIGHT}".format(
    **locals())
capture_cmd = "videotestsrc num-buffers={NUM_BUFFERS} ! {caps_filter} ! appsink emit-signals=True sync=false".format(
    **locals())

display_cmd = "appsrc emit-signals=True is-live=True ! videoconvert ! gtksink sync=false"


with GstContext(), GstVideoSource(capture_cmd) as capture, \
        GstVideoSink(display_cmd, width=WIDTH, height=HEIGHT, video_frmt=VIDEO_FORMAT) as display:

    # wait pipeline to initialize
    max_num_tries, num_tries = 5, 0
    while not display.is_active and num_tries <= max_num_tries:
        time.sleep(.1)
        num_tries += 1

    while not capture.is_done or capture.queue_size > 0:
        buffer = capture.pop()
        if buffer:
            display.push(buffer.data,
                         pts=buffer.pts,
                         dts=buffer.dts,
                         offset=buffer.offset)
示例#6
0
import time
from random import randint

from gstreamer import GstPipeline, GstContext

if __name__ == '__main__':
    with GstContext():
        pipelines = [
            GstPipeline("videotestsrc num-buffers={} ! gtksink".format(
                randint(50, 300))) for _ in range(5)
        ]

        for p in pipelines:
            p.startup()

        while any(p.is_active for p in pipelines):
            time.sleep(.5)

        for p in pipelines:
            p.shutdown()
示例#7
0
import numpy as np

from gstreamer import GstVideoSink, GstVideo, GstContext

WIDTH, HEIGHT, CHANNELS = 640, 480, 3
NUM_BUFFERS = 1000
VIDEO_FORMAT = GstVideo.VideoFormat.RGB
command = "appsrc emit-signals=True is-live=True ! videoconvert ! gtksink sync=false"

with GstContext(), GstVideoSink(command, width=WIDTH, height=HEIGHT, video_frmt=VIDEO_FORMAT) as pipeline:

    for _ in range(NUM_BUFFERS):
        buffer = np.random.randint(low=0, high=255, size=(
            HEIGHT, WIDTH, CHANNELS), dtype=np.uint8)
        pipeline.push(buffer)

    while pipeline.is_done:
        pass

    print("Displayed {} buffers".format(pipeline.total_buffers_count))
示例#8
0
import numpy as np
import time
import threading

from gstreamer import GstVideoSource, GstVideo, Gst, GLib, GstContext

WIDTH, HEIGHT, CHANNELS = 640, 480, 3
NUM_BUFFERS = 50
VIDEO_FORMAT = GstVideo.VideoFormat.RGB

video_format_str = GstVideo.VideoFormat.to_string(VIDEO_FORMAT)
caps_filter = "capsfilter caps=video/x-raw,format={video_format_str},width={WIDTH},height={HEIGHT}".format(
    **locals())
command = "videotestsrc num-buffers={NUM_BUFFERS} ! {caps_filter} ! appsink emit-signals=True sync=false".format(
    **locals())

last_buffer = None
with GstContext(), GstVideoSource(command) as pipeline:

    while pipeline.is_active or pipeline.queue_size > 0:
        buffer = pipeline.pop()
        if buffer:
            print("{}: shape {}".format(Gst.TIME_ARGS(buffer.pts),
                                        buffer.data.shape))
            last_buffer = buffer

print("Read {} buffers".format(last_buffer.offset))