10
votes

I have an opencv application in cpp.

It captures video stream and saves it to video files with the simple constructs from opencv.

It works perfectly with my webcam.

But, it crashes maybe after about ten seconds, while I run it to capture the stream from IP Camara.

My compile command is:

g++ -O3 IP_Camera_linux.cpp -o IP_Camera `pkg-config --cflags --libs opencv`

My Stream from IP cam is accessed like this:

const string Stream = "rtsp://admin:[email protected]/";

It does run perfectly, shows video and saves it until the displayed video freezes and the application crashes. While the error message on the terminal is:

[h264 @ 0x15e6f60] error while decoding MB 59 31, bytestream (-20)
[h264 @ 0x15e8200] error while decoding MB 61 27, bytestream (-3)
[h264 @ 0x109c880] missing picture in access unit
[h264 @ 0x109c000] no frame!

To my understanding, the fist two lines in the above error message might have something to do but does not actually crash the application. The last two lines are probably the reasons or the cause?

Any help?

3
check out the following RTSP UPD vs TCPPim
well that might solve what actually is not the main issue, Good! btw, there is no accepted answer, seems a debate yet.tod
Can we see your cpp file? Or some code snippet on how you are accessing the camera's stream?Manny
@Manny I use Cap.grab(), and this is where it fails to grab.tod
that -1 is not me, but can you share your code, I suspect something is wrong in your code. from what you said, it seems it was dropping frames, and the empty frames were causing the issue.fireant

3 Answers

3
votes

Got the solution after lots of hit and trial. Just changed the stream address a bit and it worked.

From:

const string Stream = "rtsp://admin:[email protected]/";

To:

const string Stream = "rtsp://admin:[email protected]/ch1-s1?tcp";

NO idea, what change it did make?

BUT IT WORKS PERFECTLY!!!

Even the pervasive warnings of the form:

[h264 @ 0x15e6f60] error while decoding MB 59 31, bytestream (-20)
[h264 @ 0x15e8200] error while decoding MB 61 27, bytestream (-3) 

are gone.

Anyways would appreciate if some one could explain it with the logical reason behind.

CREDIT

0
votes

It is an error from ffmpeg. Probably your ffmpeg is old version and you may want to update it. It solved the problem perfectly for my case by reinstalling the latest opencv and ffmpeg as follows:

  • Install latest ffmpeg

    git clone git://source.ffmpeg.org/ffmpeg.git
    cd ffmpeg 
    ./configure --enable-shared --disable-static 
    make 
    sudo make install
    
  • Install the latest opencv

    git clone [email protected]:opencv/opencv.git
    cd opencv
    mkdir build
    cd build 
    cmake ../ -DCMAKE_BUILD_TYPE=Release
    make
    sudo make install
    
-1
votes

As a citation to the original answer, adding ?tcp to the end forces the rtsp connection to run using the tcp protocol instead of the udp protocol which is useful if you do not actively check for any connection problem and therefore you can't afford to have any packet loss.

For robust running you can check for NULL image in you loop and if you get a NULL image, you can reset the camera connection:

IplImage *img = cvQueryFrame(camera);
        if (img == NULL) {
            printf("img == null ");
            fflush(stdout);
            camera = cvCreateFileCapture("rtsp://admin:[email protected]/ch1-s1?tcp");
        }