RTSP stream doesn't work in remote mode with port forwarding on router

Hello, i am using OpenCV 2.4.1 C++ to render the RTSP Stream from IP camera. When i connect to the stream in local home network everything works perfect, but if i try to connect to the RTSP Stream from outside (make a port forwarding in my router) it doesn’t work and capture is empty, while the VLC media player render the RTSP Stream from outside (port forwarded) with no problem.

My IP Camera: Cdiscount

Format of RTSP connection string: rtsp://admin:8888@xxx.xxx.x.xxx:1987/rtsp_live1

Code example:

   try {  
		videoCapture.open("rtsp://admin:8888@xxx.xxx.x.xxx:1987/rtsp_live1");
	}
	catch (cv::Exception& e) {
		AfxMessageBox(e.msg.c_str());// Here is ok, there is no errors
	}

        while (true)
	{
		try
		{
				cv::Mat processfrm;			
				//Get frame from webcam
				videoCapture >> processfrm;// Here is empty frame in remote mode
                }
        }

Help me please to find a solution how to render the RTSP stream from outside of home network. Let me know please if you need any additional information.

OpenCV uses ffmpeg and gstreamer.

reproduce this with those first, standalone. when that works, it’s an OpenCV problem. if that doesn’t work, it’s not an opencv problem but an ffmpeg/gstreamer problem.

that will receive no support at all in any case. why? if there was an issue, and it was fixed since then, you’re causing everyone pointless detective work.

1 Like

Thank you for a quick reply, unfortunately it doesn’t work standalone, but i am curios why does it work with VLC URL Stream…

i used the following command: ffplay.exe rtsp://admin:8888@xxx.xx.xx.xxx:1987/rtsp_live1

I also tried OpenCV v4.6.0, all the same problem it doesn’t work when port triggered((

Do i have any other options how to solve the problem? Maybe i could try some ffmpeg settings from OpenCV???

VLC and ffmpeg are different programs. they use different libraries. they very likely do their networking differently.

differing behavior is to be expected.

now you’ll have to investigate what is going on. you might need WireShark. and hope the traffic isn’t encrypted, so you can look at what’s going on.

or you could go and ask the ffmpeg community for support. in any case, it’s no longer an OpenCV problem. OpenCV can do nothing about this. it’s all ffmpeg. you confirmed that using ffplay fails on that URL.

1 Like

Ok, thank you!!! I wil ask ffmpeg community

Finally i was able to run port forwarded RTSP stream standalone with ffplay, using the following connection string:

ffplay -rtsp_transport tcp -i rtsp://admin:8888@xxx.xx.xx.xxx:1987/rtsp_live1

It seems like an OpenCV problem, some times very rare (one time from 30 -50 tries) openCV runs the stream successfull with the next connection string:

cv::VideoCapture source("rtsp://admin:8888@xxx.xx.xx.xxx:1987/rtsp_live1", cv::CAP_FFMPEG);

Is there a way to delay the captures or drop the captures if they are empty or maybe i should use any buffering technics? It seems like it need long time to read a capture…

Here is a OpenCV command prompt output:

Here is a ffplay command prompt output (but but despite the errors, videostream is successfull):


Here is OpenCV configuration:

General configuration for OpenCV 5.0.0-pre =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            D:/build/opencv_next/extramodules/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2021-08-18T15:14:29Z
    Host:                        Windows 10.0.19043 AMD64
    CMake:                       3.21.1
    CMake generator:             Visual Studio 16 2019
    CMake build tool:            C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/MSBuild/Current/Bin/MSBuild.exe
    MSVC:                        1929
    Configuration:               Debug Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (15 files):         + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (4 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (29 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (4 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

  C/C++:
    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe  (ver 19.29.30133.0)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MT /O2 /Ob2 /DNDEBUG
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MTd /Zi /Ob0 /Od /RTC1
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP   /MT /O2 /Ob2 /DNDEBUG
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP /MTd /Zi /Ob0 /Od /RTC1
    Linker flags (Release):      /machine:x64  /NODEFAULTLIB:atlthunk.lib /INCREMENTAL:NO  /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:libcpmtd.lib /NODEFAULTLIB:msvcrtd.lib
    Linker flags (Debug):        /machine:x64  /NODEFAULTLIB:atlthunk.lib /debug /INCREMENTAL  /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:libcpmt.lib /NODEFAULTLIB:msvcrt.lib
    ccache:                      NO
    Precompiled headers:         NO
    Extra dependencies:          wsock32 comctl32 gdi32 ole32 setupapi ws2_32 freetype harfbuzz
    3rdparty dependencies:       libprotobuf libclapack ade ittnotify libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf zlib quirc

  OpenCV modules:
    To be built:                 3d aruco barcode bgsegm bioinspired calib ccalib core datasets dnn dnn_objdetect dnn_superres dpm face features2d flann freetype fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor ml objdetect optflow phase_unwrapping photo plot quality rapid reg saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab wechat_qrcode xfeatures2d ximgproc xobjdetect xphoto xstereo
    Disabled:                    java_bindings_generator python_bindings_generator python_tests world
    Disabled by dependency:      mcc python3 rgbd
    Unavailable:                 alphamat cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv hdf java julia matlab ovis python2 sfm viz
    Applications:                perf_tests
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI:
    Win32 UI:                    YES
    VTK support:                 NO
    Built-in Unicode font:       YES

  Media I/O:
    ZLib:                        build (ver 1.2.11)
    JPEG:                        build-libjpeg-turbo (ver 2.1.0-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.2.0)
    JPEG 2000:                   build (ver 2.4.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.134.100)
      avformat:                  YES (58.76.100)
      avutil:                    YES (56.70.100)
      swscale:                   YES (5.9.100)
      avresample:                YES (4.0.0)
    GStreamer:                   NO
    Media Foundation:            YES
      DXVA:                      YES

  Parallel framework:            Concurrency

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Lapack:                      YES (libclapack 3.9.0)
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.5.1)

  OpenCL:                        YES (NVD3D11)
    Include path:                D:/build/opencv_next/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 C:/Users/User/AppData/Local/Programs/Python/Python39/python.exe (ver 3.9.6)
    Libraries:                   NO
    numpy:                       C:/Users/User/AppData/Local/Programs/Python/Python39/lib/site-packages/numpy/core/include (ver 1.21.2)
    install path:                -

  Python (for build):            C:/Users/User/AppData/Local/Programs/Python/Python39/python.exe

  Install to:                    D:/build/opencv_next/build/install
-----------------------------------------------------------------

Hi dear community! Any help would be really appreciated, i still can’t find solution. I try to use standalone gstreamer with the following command:
gst-launch-1.0 -v playbin uri=rtsp://admin:8888@xxx.xx.xx.xxx:1987/rtsp_live1 uridecodebin0::source::latency=300
and it works good with port forwarded video stream, but if i try the same command in opencv it doesn’t work:
cv::VideoCapture source("rtsp://admin:8888@xxx.xx.xx.xxx:1987/rtsp_live1 uridecodebin0::source::latency=300", cv::CAP_GSTREAMER);

also tried this way:

cv::VideoCapture source("rtsp://admin:8888@xxx.xx.xx.xxx:1987/rtsp_live1", cv::CAP_GSTREAMER);

do not pay attention on the previous screenshot with opencv configuration, i rebuild the opencv using cmake with gstreamer support and my current version of openCV support gstreamer.

Thank you very much