3

I have purchased an IP camera which I am trying to connect to using RTSP. The RTSP connection URL is rtsp://admin:@192.168.0.27/channel=1&stream=0.554. I am using OpenCV to open and display the stream:

#include <opencv2/opencv.hpp>

int main() {
   cv::VideoCapture cap;
   if (!cap.open("rtsp://admin:@192.168.0.27/channel=1&stream=0.554")) {
        std::cout << "Unable to open video capture\n";
        return -1;
    }

    while(true) {
        cv::Mat frame;

        auto ret = cap.grab();
        cap >> frame;

        if (frame.empty()) {
            break; // End of video stream
        }

        cv::resize(frame, frame, cv::Size(640, 480));

        // Do other stuff here with frame

        cv::imshow("frame", frame);

        if (cv::waitKey(10) == 27) {
            break; // stop capturing by pressing ESC
        }
    }

    return 0;
}

When I run the program, it will connect successfully and display a few frames, but then will start to lag badly and usually hang and output some error like this before crashing:

[h264 @ 0x558ae8e601a0] error while decoding MB 93 40, bytestream -11

I am not sure why I am having issues displaying the stream consistently. Additionally, when it is able to display the stream, I find that it is quickly become out of sync (note I am doing some heavy processing on the frame which takes quite a lot of time). As in, it is not displaying the real time frame, but there is a growing lag.

How can I also ensure to use the "latest" frame, and discard all the other ones which may have accumulated in some buffer. Also, any ideas why it is crashing and how I can improve the streaming?

I was able to find this SO post which deals with getting the latest frame using gstreamer. When I modify my video capture string to utilize gstreamer, it works a bit better.

Here is the modified connection string: "rtspsrc location=rtsp://admin:@192.168.0.27/channel=1&stream=0.554 ! decodebin ! videoconvert ! appsink max-buffers=1 drop=true"

I have no experience with gstreamer so I am not sure what it is doing, but it seems to improve things. However, once it a while, it will all go grey and only display the pixels when there is movement, as shown in the following images. With my experince with codecs, I believe the reference frame is missing, but I am not really sure. Any ideas on how to fix this? If I am not using the correct gstreamer parameters, please make a suggestion as to what I should be using for fast streaming (always using the latest frame). As I mentioned, I have minimal experience with gstreamer. Thanks for the help!

enter image description hereenter image description here

cyrusbehr
  • 1,100
  • 1
  • 12
  • 32
  • Gray images might be due to missing I frames dropped during transmission or reasons unknown without further debugging. You can get more info on why that happens on this video -> https://youtu.be/qbGQBT2Vwvc?t=214 – Mayank Jan 24 '21 at 05:42

2 Answers2

2

This may be due to packet loss of the network transmission. You can give it a try to modify the URL to use the rtspt:// protocol. This will try to establish a TCP transmission which should prevent packet loss on your receiving side.

Florian Zwoch
  • 6,764
  • 2
  • 12
  • 21
  • This seems to help with removing the grey frames, thanks! However, after displaying some frames, I will eventually hit this error where it hangs. Any ideas? ``` [ WARN:0] global /home/cyrus/work/libs/opencv-4.1.1/modules/videoio/src/cap_gstreamer.cpp (488) isPipelinePlaying OpenCV | GStreamer warning: unable to query pipeline state [ WARN:0] global /home/cyrus/work/libs/opencv-4.1.1/modules/videoio/src/cap_gstreamer.cpp (1756) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module rtspsrc0 reported: Could not read from resource. ``` – cyrusbehr May 07 '20 at 17:57
  • Note for the above comment I am using this connection string "rtspsrc location=rtspt://admin:@192.168.0.15/channel=1&stream=0.554 ! decodebin ! videoconvert ! appsink max-buffers=1 drop=true" – cyrusbehr May 07 '20 at 17:58
  • And sometimes when I run the program won't even start and will immediately output the following: – cyrusbehr May 07 '20 at 18:00
  • [ WARN:0] global /home/cyrus/work/libs/opencv-4.1.1/modules/videoio/src/cap_gstreamer.cpp (1756) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module rtspsrc0 reported: Could not read from resource. [ WARN:0] global /home/cyrus/work/libs/opencv-4.1.1/modules/videoio/src/cap_gstreamer.cpp (886) open OpenCV | GStreamer warning: unable to start pipeline [ WARN:0] global /home/cyrus/work/libs/opencv-4.1.1/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created Unable to open video capture – cyrusbehr May 07 '20 at 18:00
  • Not 100% sure if `appsink` blocks the streaming thread if you block it with your processing. You should try to decouple the processing from the streaming thread. Alternatively perhaps using a `queue leaky=downstream` before the appsink is enough for thread decoupling. – Florian Zwoch May 07 '20 at 18:17
1

The best approach is to use threads to read frames continuously and assign them on an attribute of a class. In this way if some thread encounters the packet loss, the other thread buddies compensate for it.

check this out, I hope it helps:

from threading import Thread
import cv2

class RTSPVideoWriterObject(object):
    def __init__(self, src=0):
        # Create a VideoCapture object
        self.capture = cv2.VideoCapture(src)
        self.status, self.frame = None, None

        # Default resolutions of the frame are obtained (system dependent)
        self.frame_width = int(self.capture.get(3))
        self.frame_height = int(self.capture.get(4))

        # Set up codec and output video settings
        self.codec = cv2.VideoWriter_fourcc(*'MJPG')
        self.output_video = cv2.VideoWriter('output.avi', self.codec, 30, (self.frame_width, self.frame_height))

        # Start the thread to read frames from the video stream
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()

    def update(self):
        # Read the next frame from the stream in a different thread
        while True:
            if self.capture.isOpened():
                (self.status, self.frame) = self.capture.read()

    def show_frame(self):
        # Display frames in main program
        # if self.status:
        #     cv2.imshow('frame', self.frame)

        # Press Q on keyboard to stop recording
        key = cv2.waitKey(1)
        if key == ord('q'):
            self.capture.release()
            self.output_video.release()
            cv2.destroyAllWindows()
            exit(1)

    def save_frame(self):
        # Save obtained frame into video output file
        self.output_video.write(self.frame)


if __name__ == '__main__':
    rtsp_link = "rtsp://admin:@192.168.0.27/channel=1&stream=0.554"
    video_stream_widget = RTSPVideoWriterObject(rtsp_stream_link)
    while True:
        try:
            video_stream_widget.show_frame()
            video_stream_widget.save_frame()
        except AttributeError:
            pass
Masoud Masoumi Moghadam
  • 1,094
  • 3
  • 23
  • 45