Giter Site home page Giter Site logo

Comments (7)

Myzhar avatar Myzhar commented on May 31, 2024 1

Yes, it takes advantage of the double buffering already available with UVC

from zed-open-capture.

Myzhar avatar Myzhar commented on May 31, 2024 1

Yes, a copy is always the best solution in similar cases... I will test it further

from zed-open-capture.

Myzhar avatar Myzhar commented on May 31, 2024

Hi @peds
thank you for reporting this issue.
I think that there are two possible solutions to fix it:

  1. double buffering (or triple...)
  2. returning a copied buffer

The first solution introduces a possible frame latency. The UVC driver already implements a multi-buffer mechanism (we use double buffering), but it can be not enough:

memcpy(mLastFrame.data, (unsigned char*) mBuffers[mCurrentIndex].start, mBuffers[mCurrentIndex].length);

The second solution requires more memory and can be slow for hosts with slow memory.

Any thoughts?

from zed-open-capture.

Myzhar avatar Myzhar commented on May 31, 2024

@peds can you test if this branch solves the problem:
https://github.com/stereolabs/zed-open-capture/tree/fix_race_cond

from zed-open-capture.

peds avatar peds commented on May 31, 2024

Hi @peds thank you for reporting this issue. I think that there are two possible solutions to fix it:

1. double buffering (or triple...)

2. returning a copied buffer

The first solution introduces a possible frame latency. The UVC driver already implements a multi-buffer mechanism (we use double buffering), but it can be not enough:

memcpy(mLastFrame.data, (unsigned char*) mBuffers[mCurrentIndex].start, mBuffers[mCurrentIndex].length);

The second solution requires more memory and can be slow for hosts with slow memory.

Any thoughts?

Hi @Myzhar,

Thank you for getting back to me so fast!
I did implement a solution that does work by making a copy of the last frame that is only updated when getLastFrame is called. I think this is similar to your second proposed solution?

Did you implement double buffering in https://github.com/stereolabs/zed-open-capture/tree/fix_race_cond?
I will test this and get back to you.

from zed-open-capture.

peds avatar peds commented on May 31, 2024

The issue persists even with double buffering, but it seems to be more subtle.
zed_oc_glitch_example_left_frame_double_buffer

I think the most reliable solution would be to return a copy of mLastFrame. The following worked for me.

static void copyFrame(Frame& dstFrame, Frame& srcFrame){
    if (srcFrame.data != nullptr) {
        int bufSize = srcFrame.width * srcFrame.height * srcFrame.channels;
        if(dstFrame.data == nullptr ){
            dstFrame.data = new unsigned char[bufSize];
        }
        // memcpy( dstFrame.data, srcFrame.data, bufSize);
        std::copy( srcFrame.data, srcFrame.data + bufSize, dstFrame.data);
    }
    dstFrame.frame_id = srcFrame.frame_id;
    dstFrame.timestamp = srcFrame.timestamp;
    dstFrame.width = srcFrame.width;
    dstFrame.height = srcFrame.height;
    dstFrame.channels = srcFrame.channels;
}

const Frame& VideoCapture::getLastFrame( uint64_t timeout_msec )

{
    // ----> Wait for a new frame
    uint64_t time_count = timeout_msec*10;
    while( !mNewFrame )
    {
        if(time_count==0)
        {
            // const std::lock_guard<std::mutex> lock(mBufMutex);
            // copyFrame(mLastFrameCopy, mLastFrame);
            return mLastFrameCopy;
        }
        time_count--;
        usleep(100);
    }
    // <---- Wait for a new frame

    // Get the frame mutex
    const std::lock_guard<std::mutex> lock(mBufMutex);
    mNewFrame = false;
    copyFrame(mLastFrameCopy, mLastFrame);
    return mLastFrameCopy;
}

Declaration of Frame mLastFrameCopy is also added to

Frame mLastFrame; //!< Last grabbed frame

And

    if(mLastFrameCopy.data)
    {
        delete [] mLastFrameCopy.data;
        mLastFrameCopy.data = nullptr;
    }

is added to //deinint in

if(mLastFrame.data)

Edit:
I commented out

const std::lock_guard<std::mutex> lock(mBufMutex);
copyFrame(mLastFrameCopy, mLastFrame);

in getLastFrame before the timeout return, because the last frame has not changed.

from zed-open-capture.

github-actions avatar github-actions commented on May 31, 2024

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment otherwise it will be automatically closed in 5 days

from zed-open-capture.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.