recording concurrently multiple streams from usb cameras to files with python

It is an effort to adopt the template [credits to Honey_Patouceul] from TX2 to Xavier environment.

#!/usr/bin/env python

import sys, os
import gi
gi.require_version('Gst', '1.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gst, GObject, Gtk

class GTK_Main:
    def __init__(self):
        window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        window.set_title("CamRecorder")
        window.set_default_size(100, 100)
        window.connect("destroy", Gtk.main_quit, "WM destroy")
        vbox = Gtk.VBox()
        window.add(vbox)
        self.movie_window = Gtk.DrawingArea()
        vbox.add(self.movie_window)
        hbox = Gtk.HBox()
        vbox.pack_start(hbox, False, False, 0)
        hbox.set_border_width(10)
        hbox.pack_start(Gtk.Label(), False, False, 0)
        self.button = Gtk.Button("Start")
        self.button.connect("clicked", self.start_stop)
        hbox.pack_start(self.button, False, False, 0)
        self.button2 = Gtk.Button("Quit")
        self.button2.connect("clicked", self.exit)
        hbox.pack_start(self.button2, False, False, 0)
        hbox.add(Gtk.Label())
        window.show_all()

        # Set up the gstreamer pipeline
        self.player = Gst.parse_launch ("v4l2src device=/dev/video1 num-buffers=300 ! video/x-h264,width=1920,height=1080,framerate=25/1 ! h264parse ! qtmux ! filesink location=camera1.mov v4l2src device=/dev/video3 num-buffers=300 ! video/x-h264,width=1920,height=1080,framerate=25/1 ! h264parse ! qtmux ! filesink location=camera3.mov v4l2src device=/dev/video5 num-buffers=300 ! video/x-h264,width=1920,height=1080,framerate=25/1 ! h264parse ! qtmux ! filesink location=camera5.mov")
        bus = self.player.get_bus()
        bus.add_signal_watch()
        bus.enable_sync_message_emission()
        bus.connect("message", self.on_message)
        bus.connect("sync-message::element", self.on_sync_message)

    def start_stop(self, w):
        if self.button.get_label() == "Start":
            self.button.set_label("Stop")
            self.player.set_state(Gst.State.PLAYING)
        else:
            self.player.send_event(Gst.Event.new_eos())
            self.player.set_state(Gst.State.NULL)
            self.button.set_label("Start")

    def exit(self, widget, data=None):
        Gtk.main_quit()

    def on_message(self, bus, message):
        t = message.type
        if t == Gst.MessageType.EOS:
            self.player.set_state(Gst.State.NULL)
            self.button.set_label("Start")
        elif t == Gst.MessageType.ERROR:
            err, debug = message.parse_error()
            print "Error: %s" % err, debug
            self.player.set_state(Gst.State.NULL)
            self.button.set_label("Start")

    def on_sync_message(self, bus, message):
        struct = message.get_structure()
        if not struct:
            return
        message_name = struct.get_name()
        if message_name == "prepare-xwindow-id":
            # Assign the viewport
            imagesink = message.src
            imagesink.set_property("force-aspect-ratio", True)
            imagesink.set_xwindow_id(self.movie_window.window.xid)

Gst.init(None)
GTK_Main()
GObject.threads_init()
Gtk.main()
python 3cam.py 
Error: gst-resource-error-quark: Failed to allocate required memory. (13) gstv4l2src.c(650): gst_v4l2src_decide_allocation (): /GstPipeline:pipeline0/GstV4l2Src:v4l2src1:
Buffer pool activation failed

Hi,
Could you go to http://gstreamer-devel.966125.n4.nabble.com/ ?

There is no NVIDIA plugins in your pipeline. For 3rdpary plugins, you will get better assistance in gstreamer forum.
Not sure but it might be about ‘io-mode’ property.

Thank you for pointing out!
I have posted request for comments at their mailing list .
However, what works with two cameras is:

gst-launch-1.0 v4l2src device=/dev/video0 io-mode=2 do-timestamp=true num-buffers=1 ! image/jpeg,width=1920,height=1080,framerate=25/1 ! jpegparse ! multifilesink location=camera0_%03d.jpeg v4l2src  device=/dev/video2 io-mode=2 do-timestamp=true num-buffers=1  ! image/jpeg,width=1920,height=1080,framerate=25/1 ! jpegparse ! multifilesink location=camera2_%03d.jpeg

Moreover, it seems to work both with io-mode and without it been specified. On the other hand, when I add third device it fails with “out-of-memory”

SOLVED
It was bottlenecking with 3x25fps 1080p.
reduce fps to 15 1080p allowed to write three streams.
Thanks

#!/usr/bin/env python

import sys, os
import gi
gi.require_version('Gst', '1.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gst, GObject, Gtk

class GTK_Main:
    def __init__(self):
        window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        window.set_title("CamRecorder")
        window.set_default_size(100, 100)
        window.connect("destroy", Gtk.main_quit, "WM destroy")
        vbox = Gtk.VBox()
        window.add(vbox)
        self.movie_window = Gtk.DrawingArea()
        vbox.add(self.movie_window)
        hbox = Gtk.HBox()
        vbox.pack_start(hbox, False, False, 0)
        hbox.set_border_width(10)
        hbox.pack_start(Gtk.Label(), False, False, 0)
        self.button = Gtk.Button("Start")
        self.button.connect("clicked", self.start_stop)
        hbox.pack_start(self.button, False, False, 0)
        self.button2 = Gtk.Button("Quit")
        self.button2.connect("clicked", self.exit)
        hbox.pack_start(self.button2, False, False, 0)
        hbox.add(Gtk.Label())
        window.show_all()

        # Set up the gstreamer pipeline
        self.player = Gst.parse_launch ("v4l2src device=/dev/video1 num-buffers=1800 ! video/x-h264,width=1920,height=1080,framerate=15/1 ! h264parse ! qtmux ! filesink location=camera1.mov v4l2src device=/dev/video3 num-buffers=300 ! video/x-h264,width=1920,height=1080,framerate=15/1 ! h264parse ! qtmux ! filesink location=camera3.mov v4l2src device=/dev/video5 num-buffers=1800 ! video/x-h264,width=1920,height=1080,framerate=15/1 ! h264parse ! qtmux ! filesink location=camera5.mov")
        bus = self.player.get_bus()
        bus.add_signal_watch()
        bus.enable_sync_message_emission()
        bus.connect("message", self.on_message)
        bus.connect("sync-message::element", self.on_sync_message)

    def start_stop(self, w):
        if self.button.get_label() == "Start":
            self.button.set_label("Stop")
            self.player.set_state(Gst.State.PLAYING)
        else:
            self.player.send_event(Gst.Event.new_eos())
            self.player.set_state(Gst.State.NULL)
            self.button.set_label("Start")

    def exit(self, widget, data=None):
        Gtk.main_quit()

    def on_message(self, bus, message):
        t = message.type
        if t == Gst.MessageType.EOS:
            self.player.set_state(Gst.State.NULL)
            self.button.set_label("Start")
        elif t == Gst.MessageType.ERROR:
            err, debug = message.parse_error()
            print "Error: %s" % err, debug
            self.player.set_state(Gst.State.NULL)
            self.button.set_label("Start")

    def on_sync_message(self, bus, message):
        struct = message.get_structure()
        if not struct:
            return
        message_name = struct.get_name()
        if message_name == "prepare-xwindow-id":
            # Assign the viewport
            imagesink = message.src
            imagesink.set_property("force-aspect-ratio", True)
            imagesink.set_xwindow_id(self.movie_window.window.xid)

Gst.init(None)
GTK_Main()
GObject.threads_init()
Gtk.main()