gstreamer cycle encoder

Hi,

We want implement a test function for gstreamer cycle encoder,
and we try to modify the tegra_multimedia_api/argus/samples/gstVideoEncode for this job.
But based on our modify, the program will freeze when the first video recording is complete, and second video recording is starting.

Do you have any advice for this job or the implement?
Thanks.

There is our sample code:

  1. add a while loop
  2. add streaming reset and re-initial flow
static bool execute()
{
    using namespace Argus;

    // Initialize the preview window and EGL display.
    Window &window = Window::getInstance();
    PROPAGATE_ERROR(g_display.initialize(window.getEGLNativeDisplay()));

    // Create CameraProvider.
    UniqueObj<CameraProvider> cameraProvider(CameraProvider::create());
    ICameraProvider *iCameraProvider = interface_cast<ICameraProvider>(cameraProvider);
    if (!iCameraProvider)
        ORIGINATE_ERROR("Failed to open CameraProvider");

    // Get/use the first available CameraDevice.
    std::vector<CameraDevice*> cameraDevices;
    if (iCameraProvider->getCameraDevices(&cameraDevices) != STATUS_OK)
        ORIGINATE_ERROR("Failed to get CameraDevices");
    if (cameraDevices.size() == 0)
        ORIGINATE_ERROR("No CameraDevices available");
    CameraDevice *cameraDevice = cameraDevices[0];
    ICameraProperties *iCameraProperties = interface_cast<ICameraProperties>(cameraDevice);
    if (!iCameraProperties)
        ORIGINATE_ERROR("Failed to get ICameraProperties interface");

    // Create CaptureSession.
    UniqueObj<CaptureSession> captureSession(iCameraProvider->createCaptureSession(cameraDevice));
    ICaptureSession *iSession = interface_cast<ICaptureSession>(captureSession);
    if (!iSession)
        ORIGINATE_ERROR("Failed to create CaptureSession");

    // Get the sensor mode to determine the video output stream resolution.
    std::vector<Argus::SensorMode*> sensorModes;
    iCameraProperties->getSensorModes(&sensorModes);
    if (sensorModes.size() == 0)
        ORIGINATE_ERROR("Failed to get sensor modes");
    ISensorMode *iSensorMode = interface_cast<ISensorMode>(sensorModes[0]);
    if (!iSensorMode)
        ORIGINATE_ERROR("Failed to get sensor mode interface");

    // Set common output stream settings.
    UniqueObj<OutputStreamSettings> streamSettings(iSession->createOutputStreamSettings());
    IOutputStreamSettings *iStreamSettings = interface_cast<IOutputStreamSettings>(streamSettings);
    if (!iStreamSettings)
        ORIGINATE_ERROR("Failed to create OutputStreamSettings");
    iStreamSettings->setPixelFormat(PIXEL_FMT_YCbCr_420_888);
    iStreamSettings->setEGLDisplay(g_display.get());

    // Create video encoder stream.
    iStreamSettings->setResolution(iSensorMode->getResolution());
    UniqueObj<OutputStream> videoStream(iSession->createOutputStream(streamSettings.get()));
    IStream *iVideoStream = interface_cast<IStream>(videoStream);
    if (!iVideoStream)
        ORIGINATE_ERROR("Failed to create video stream");

    // Create preview stream.
    iStreamSettings->setResolution(PREVIEW_STREAM_SIZE);
    UniqueObj<OutputStream> previewStream(iSession->createOutputStream(streamSettings.get()));
    IStream *iPreviewStream = interface_cast<IStream>(previewStream);
    if (!iPreviewStream)
        ORIGINATE_ERROR("Failed to create preview stream");

    // Create capture Request and enable the streams in the Request.
    UniqueObj<Request> request(iSession->createRequest(CAPTURE_INTENT_VIDEO_RECORD));
    IRequest *iRequest = interface_cast<IRequest>(request);
    if (!iRequest)
        ORIGINATE_ERROR("Failed to create Request");
    if (iRequest->enableOutputStream(videoStream.get()) != STATUS_OK)
        ORIGINATE_ERROR("Failed to enable video stream in Request");
    if (iRequest->enableOutputStream(previewStream.get()) != STATUS_OK)
        ORIGINATE_ERROR("Failed to enable preview stream in Request");

    // Initialize the GStreamer video encoder consumer.
    GstVideoEncoder gstVideoEncoder;
    if (!gstVideoEncoder.initialize(iVideoStream->getEGLStream(), iSensorMode->getResolution(),
                                    FRAMERATE, BITRATE, ENCODER, MUXER, OUTPUT))
        ORIGINATE_ERROR("Failed to initialize GstVideoEncoder EGLStream consumer");
    if (!gstVideoEncoder.startRecording())
        ORIGINATE_ERROR("Failed to start video recording");

    // Initialize the preview consumer.
    PreviewConsumerThread previewConsumer(g_display.get(), iPreviewStream->getEGLStream());
    PROPAGATE_ERROR(previewConsumer.initialize());
    PROPAGATE_ERROR(previewConsumer.waitRunning());

    // Perform repeat capture requests for LENGTH seconds.
    if (iSession->repeat(request.get()) != STATUS_OK)
        ORIGINATE_ERROR("Failed to start repeat capture requests");

    int i = 0 ;
[b]    while (true)
    {
        PROPAGATE_ERROR(window.pollingSleep(10));

        // Stop video recording.
        if (!gstVideoEncoder.stopRecording())
            ORIGINATE_ERROR("Failed to stop video recording");
        gstVideoEncoder.shutdown();
        if (iRequest->disableOutputStream(videoStream.get()) != STATUS_OK)
            ORIGINATE_ERROR("Failed to enable video stream in Request");
        videoStream.reset();

        // create next video recording flow
        OUTPUT = "argus_gstvideoencode_out2.mp4";
        iStreamSettings->setResolution(iSensorMode->getResolution());
        videoStream = (UniqueObj<OutputStream>)iSession->createOutputStream(streamSettings.get());
        iVideoStream = interface_cast<IStream>(videoStream);
        if (!iVideoStream)
            ORIGINATE_ERROR("Failed to create video stream");

        if (iRequest->enableOutputStream(videoStream.get()) != STATUS_OK)
            ORIGINATE_ERROR("Failed to enable video stream in Request");

        if (!gstVideoEncoder.initialize(iVideoStream->getEGLStream(), iSensorMode->getResolution(),
                                    FRAMERATE, BITRATE, ENCODER, MUXER, OUTPUT))
            ORIGINATE_ERROR("Failed to initialize GstVideoEncoder EGLStream consumer");
        if (!gstVideoEncoder.startRecording())
            ORIGINATE_ERROR("Failed to start video recording");

    }[/b]
    iSession->stopRepeat();

    // Wait until all frames have completed before stopping recording.
    /// @todo: Not doing this may cause a deadlock.
    iSession->waitForIdle();

    // Stop video recording.
    if (!gstVideoEncoder.stopRecording())
        ORIGINATE_ERROR("Failed to stop video recording");
    gstVideoEncoder.shutdown();
    videoStream.reset();

    // Stop preview.
    previewStream.reset();
    PROPAGATE_ERROR(previewConsumer.shutdown());

    return true;
}

Hi WalkerHsiao,
Please refer to
https://devtalk.nvidia.com/default/topic/1028387/jetson-tx1/closed-gst-encoding-pipeline-with-frame-processing-using-cuda-and-libargus/post/5256753/#5256753

Hi DaneLLL,

Thanks for your support,

But my problem is seem different with you provide topic.
There is no problem when I stop the first recording,
but when I want to create the second recording job with same camera(in my sample code line:103),
my program will freeze.

So, could you have any idea for my issue?
Thank you very much.

Hi WalkerHsiao,
In your code, you also have to re-init Argus along with GstVideoEncoder.

Hi DaneLLL,

Ok…, but i’m a new for libargus,
could you please provide the sample code how to re-init the Argus?
Thank you for your kind assistance.

Hi WalkerHsiao,
We encourage users to refer to the samples and integrate into your own usecases. Other users may have similar usecases and share experience.