cudacodec::createVideoReader lose many frames when i read multi rtsp streams by using multi thread

i found a problem that cudacodec::createVideoReader drop several frames every few seconds. i use multi thread to read 4 rtsp streams at same time .
the key code is blow

#include <thread>

int readFrameGpu1(string address) {
    cout << address << endl;
    cuda::GpuMat g_frame, g_frame_c3;
    cuda::GpuMat frame;
    Ptr<cudacodec::VideoReader> videoReader = cudacodec::createVideoReader(address);
    while (videoReader->nextFrame(g_frame)) {
        g_frame.copyTo(frame);
        q1.push(frame);
    }
    return 0;
}
thread cap1(readFrameGpu1, camera1);
thread cap2(readFrameGpu2, camera2);
thread cap3(readFrameGpu3, camera3);
thread cap4(readFrameGpu4, camera4);

thanks very much ,it really confuse me

Do you have the same problem if you read in a single thread or if you read from a video file?

How do you know you are loosing frames, is the picture distorted?

It is common to lose frames because you are not requesting them quickly enough, what is the resolution, frame rate, GPU, CPU you are using?

i save every frame that i read ,and i get 1500 frames every minute. It seems that it did not lose any frames when it read the video stream. Maybe it drop some frame when i process these frames。I will do some tests to confirm it. I want to know that videoReader->nextFrame(g_frame) ,weather this code will wait for the new frame when it doesnt get the new frame .
i save the image when something moves and i found that it moves a large step suddenly every a few seconds. i compared the original video, it moves uniformly.so i think it must lose some frames.
the resolution is 1280*720, cpu is 3.2GHz, GPU is rtx6000

It will wait until a frame is available before returning.

If you have any processing or synchronization of the thread which reads from the rtsp source then this could easily be causing you issues. Are you streaming via tcp or udp, if its tcp I would expect disconnections instead of dropped frames.

i print the time when these streams read their own new frame and i found a wired phenomenon, it read very quik ,every 4-5ms, these streams read their new frame!!!
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/770
frame size840 810 750 779 716 6363
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/774
frame size840 810 750 779 717 6364
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/778
frame size841 810 751 779 717 6365
flag 1 1 1 1 1
push frame size841 810 751 779 717 6365
push time2022/10/16/23/21/0/782
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/782
frame size841 810 751 779 717 6366
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/787
frame size841 810 751 779 717 6367
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/791
frame size841 810 751 780 717 6368
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/795
frame size841 810 751 780 717 6369
flag 1 1 1 1 1
frame= 688 fps= 25 q=17.0 size= 85187kB time=00:00:25.64 bitrate=27216.1kbits/s speed=0.932x ^M-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/800
frame size841 810 751 780 717 6370
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/804
frame size841 810 751 780 717 6371
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/809
frame size841 810 751 780 717 6372
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/813
frame size841 811 751 780 718 6373
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/817
frame size841 811 751 780 718 6374
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/821
frame size842 811 751 780 718 6375
flag 1 1 1 1 1
push frame size842 811 751 780 718 6375
push time2022/10/16/23/21/0/824
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/825
frame size842 811 751 780 718 6376
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/830
frame size842 811 751 780 718 6377
flag 1 1 1 1 1
-------resultFrame size-------[3840 x 1440]time 2022/10/16/23/21/0/834
frame size842 811 751 781 718 6378

the stream fps is 25.
i cannot believe it, why they read the frame so quickly?
does it means that if i read the streams(by using videoReader->nextFrame(g_frame)) less than 40ms, it will get the same picture to me ?

I’m not sure exactly what you are showing above. Can you share the code which produces the above output?

you can understand it as this, i use videoReader->nextFrame(g_frame) to get next frame every 4ms, and i found that i get 10 same pictures in 40ms. but i want to get the new frame, if it is the same frame as before, i do not want to read it .how can i do it ?

I can’t help if I can’t see the actual code because there is as much chance of there being and issue with the code as the logic.

Are you sure you are getting the same picture or 10 different pictures?

If I could see the code I could give more insight without it all I can do is guess.

Guess: You are starting cudacodec::VideoReader a little while before calling nextFrame therefore on your first few calls to nextFrame you getting back old frames.

first :I am sure i will get 10 same pictures, you can make a simple test if you can read a camera.
second : i have found the reason why they read frames so quickly, i use a thread to read the stream without any time delay, this code(videoReader->nextFrame(g_frame) ) takes 4ms on my device, so it will read frame every 4ms. when i add std::this_thread::sleep_for(std::chrono::milliseconds(36)); to this thread, the frequency will be right.

If you are sure then I can’t help you any more. The code for cudacodec::VideoReader::nexFrame() waits for a new frame to be available here, it can’t duplicate them if you call it more often, unless there is an logic error which I am unaware of and have not experianced myself. Therefore the only conclusion I can come to is your video source is duplicating 10 frames in a row which is unlikely.

If you need any more help then please provide the make/model of your ip camera and some example code which shows you reading from an ip camera and verifying that the frames are the same. I am happy to test this on the many many ip camera’s which I have to see where the problem lies.