dynamic nvarguscamerasrc + nvcompositor == Failed to create CaptureSession

Can anyone help me figure this out?

I am trying to dynamically (in 3 sec after pipeline start) connect nvarguscamerasrc to nvcompositor but i get

Failed to create CaptureSession

and

Error AlreadyAllocated: Device 0 (of 1) is in use

in nvargus-daemon logs and of course no camera image is shown.

At the same time when I’m trying to dynamically connect a usb camera (via v4l2src) it works fine

Also when I am connecting camera to compositor statically it works on both v4l2src and/or nvarguscamerasrc

JetPack 4.3
GStreamer 1.14.5
Jetson Nano
Raspberry Pi Camera V2 IMX219

The minimal script to reproduce the problem is here
https://gist.github.com/zhuker/dfaddd22f6acd6aa410f500cbc72fecc

run script to reproduce:

GST_DEBUG=2 python3 nvargusbug.py

to change to usb camera to see it works set

camera_desc = usb_camera

nvarguscamerasrc doesnt work:

nvarguscamerasrc name=front do-timestamp=true silent=false
    ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720, format=(string)NV12, framerate=30/1 
    ! nvvidconv
    ! queue

v4l2src works:

v4l2src device=/dev/video1 do-timestamp=true name=front 
    ! image/jpeg,width=1280,height=720,framerate=30/1 
    ! jpegdec 
    ! videoconvert 
    ! nvvidconv
    ! queue

nvcompositor pipeline

nvcompositor name=comp 
    sink_0::xpos=0 sink_0::ypos=0 sink_0::width=1280 sink_0::height=1080 
    sink_1::xpos=0 sink_1::ypos=0 sink_1::width=1280 sink_1::height=720 

    videotestsrc is-live=true do-timestamp=true name=background ! 
        video/x-raw,width=320,height=180,framerate=30/1 ! 
        nvvidconv !
        comp.sink_0 
    comp. ! nvegltransform ! nveglglessink

reproduce script output

Using winsys: x11
11:23:52.219097 MainThread connecting front camera
11:23:52.224443 MainThread _pad_added comp sink_1
GST_ARGUS: Creating output stream
CONSUMER: Waiting until producer is connected...
GST_ARGUS: Available Sensor modes :
GST_ARGUS: 3264 x 2464 FR = 21.000000 fps Duration = 47619048 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 3264 x 1848 FR = 28.000001 fps Duration = 35714284 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1920 x 1080 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1280 x 720 FR = 59.999999 fps Duration = 16666667 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1280 x 720 FR = 120.000005 fps Duration = 8333333 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: Running with following settings:
   Camera index = 0
   Camera mode  = 4
   Output Stream W = 1280 H = 720
   seconds to Run    = 0
   Frame Rate = 120.000005
GST_ARGUS: PowerService: requested_clock_Hz=6048000
GST_ARGUS: Setup Complete, Starting captures for 0 seconds
GST_ARGUS: Starting repeat capture requests.
CONSUMER: Producer has connected; continuing.
CONSUMER: Acquired Frame. 1828717638
CONSUMER: Acquired Frame: 1, time 489755328562000
Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, execute:543 Failed to create CaptureSession
CONSUMER: Acquired Frame: 2, time 489755361599000
CONSUMER: Acquired Frame: 3, time 489755394972000
CONSUMER: Acquired Frame: 4, time 489755428255000

nvargus-daemon log

=== python3[20924]: CameraProvider initialized (0x7f74a97050)LSC: LSC surface is not based on full res!
(Argus) Error AlreadyAllocated: Device 0 (of 1) is in use (in src/api/CameraProviderImpl.cpp, function createCaptureSession(), line 232)

reproduce script

import datetime
import threading
import traceback

import gi

gi.require_version('Gst', '1.0')

from gi.repository import Gst, GObject, GLib

def debug(*args) -> None:
    print(datetime.datetime.now().time().isoformat(), threading.current_thread().name, *args)

# doesnt work
mipi_camera = '''nvarguscamerasrc name=front do-timestamp=true silent=false
    ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720, format=(string)NV12, framerate=30/1 
    ! nvvidconv
    ! queue
'''

# works
usb_camera = '''
v4l2src device=/dev/video1 do-timestamp=true name=front 
    ! image/jpeg,width=1280,height=720,framerate=30/1 
    ! jpegdec 
    ! videoconvert 
    ! nvvidconv
    ! queue
'''

pipeline_desc = f'''
nvcompositor name=comp 
    sink_0::xpos=0 sink_0::ypos=0 sink_0::width=1280 sink_0::height=1080 
    sink_1::xpos=0 sink_1::ypos=0 sink_1::width=1280 sink_1::height=720 
    videotestsrc is-live=true do-timestamp=true name=background ! 
        video/x-raw,width=320,height=180,framerate=30/1 ! 
        nvvidconv !
        comp.sink_0 
    comp. ! nvegltransform ! nveglglessink
'''

# works
camera_desc = usb_camera

# doesnt work
camera_desc = mipi_camera

Gst.init(None)

pipe = Gst.parse_launch(pipeline_desc)
bus = pipe.get_bus()

comp = pipe.get_by_name('comp')

def _pad_added(element, pad):
    debug('_pad_added', element.name, pad.name)

    srcbin = Gst.parse_bin_from_description(camera_desc, True)
    static_pad = srcbin.get_static_pad('src')
    pipe.add(srcbin)
    pipe.sync_children_states()
    static_pad.link(pad)

comp.connect('pad-added', _pad_added)
pipe.set_state(Gst.State.PLAYING)

def connect_front():
    debug("connecting front camera")
    comp.get_request_pad('sink_1')

gloop = GObject.MainLoop()
GLib.timeout_add(3000, connect_front)
try:
    gloop.run()
except:
    traceback.print_exc()
    pipe.send_event(Gst.Event.new_eos())
    keep_polling = True
    while keep_polling:
        message = bus.poll(Gst.MessageType.ANY, 1005000000)
        debug(message)
        keep_polling = message is not None

    pipe.set_state(Gst.State.NULL)

Hi,
Could you check if it works in gst-launch-1.0 command? Would like to know if it is specific to running python code. Not sure but it looks like you have to specifiy format=I420(or NV12) in sinkpad of nvcompositor.

Thats the point

static gst-launch pipeline works with exactly the caps as specified

but when i want to add camera dynamically (via python) it errors out

Pretty sure when caps get reconfigured camera is opened the second time

Have a reference to below link that was verified without problem.

https://gist.github.com/jkjung-avt/86b60a7723b97da19f7bfa3cb7d2690e