opencv/modules/videoio/src
Csaba Kertész b1e0c4d119
Merge pull request #24243 from kecsap:4.x
Fix gstreamer backend with manual pipelines #24243

- Fix broken seeking in audio/video playback
- Fix broken audio playback
- Fix unreliable seeking
- Estimate frame count if it is not available directly
- Return -1 for frame count and fps if it is not available. 
- Return 0 for fps if the video has variable frame rate
- Enable and fix tests


### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [-] There is a reference to the original bug report and related work => Reproducible test provided
- [-] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [X] The feature is well documented and sample code can be built with the project CMake

1. Download two test videos:

```bash
wget https://github.com/ietf-wg-cellar/matroska-test-files/raw/master/test_files/test1.mkv
wget https://test-videos.co.uk/vids/jellyfish/mkv/360/Jellyfish_360_10s_5MB.mkv
```

2. I modified a OpenCV videoio sample to demonstrate the problem, here it is the patch: http://dpaste.com//C9MAT2K6W

3. Build the sample, on Ubuntu:

```bash
g++ -g videocapture_audio_combination.cpp -I/usr/include/opencv4 `pkg-config --libs --cflags opencv4` -o videocapture_audio_combination
```

4. Play an audio stream with seeking BEFORE the fix:

```bash
$ ./videocapture_audio_combination --audio "filesrc location=test1.mkv ! queue ! matroskademux name=demux demux.audio_0 ! decodebin ! audioconvert ! appsink"[ERROR:0@0.009] global cap.cpp:164 open VIDEOIO(GSTREAMER): raised OpenCV exception:

OpenCV(4.8.0-dev) ./modules/videoio/src/cap_gstreamer.cpp:153: error: (-215:Assertion failed) ptr in function 'get'


[ WARN:0@0.009] global cap.cpp:204 open VIDEOIO(GSTREAMER): backend is generally available but can't be used to capture by name
ERROR! Can't to open file: filesrc location=test1.mkv ! queue ! matroskademux name=demux demux.audio_0 ! decodebin ! audioconvert ! appsink
```

5. Play a video stream with seeking BEFORE the fix:

```bash
$ ./videocapture_audio_combination --audio "filesrc location=Jellyfish_360_10s_5MB.mkv ! queue ! matroskademux name=demux demux.video_0 ! decodebin ! videoconvert ! video/x-raw, format=BGR ! appsink drop=1"
[ WARN:0@0.034] global cap_gstreamer.cpp:1728 open OpenCV | GStreamer warning: Cannot query video position: status=1, value=22, duration=300
CAP_PROP_AUDIO_DATA_DEPTH: CV_16S
CAP_PROP_AUDIO_SAMPLES_PER_SECOND: 44100
CAP_PROP_AUDIO_TOTAL_CHANNELS: 0
CAP_PROP_AUDIO_TOTAL_STREAMS: [ WARN:0@0.034] global cap_gstreamer.cpp:1898 getProperty OpenCV | GStreamer: CAP_PROP_AUDIO_TOTAL_STREAMS property is not supported
0
[ WARN:0@0.034] global cap_gstreamer.cpp:1817 getProperty OpenCV | GStreamer: CAP_PROP_POS_MSEC property result may be unrealiable: https://github.com/opencv/opencv/issues/19025
Timestamp: 0.6218
Timestamp: 33.1085
Timestamp: 67.1274
Timestamp: 100.1182
Timestamp: 133.1204
Timestamp: 167.1195
Timestamp: 200.1161
Timestamp: 233.1147
Timestamp: 267.1194
Timestamp: 300.1202
[ WARN:0@0.338] global cap_gstreamer.cpp:1949 setProperty OpenCV | GStreamer warning: GStreamer: unable to seek
0:00:00.338215907 3892572 0x5592899c7580 WARN                 basesrc gstbasesrc.c:3127:gst_base_src_loop:<filesrc0> error: Internal data stream error.
0:00:00.338235884 3892572 0x5592899c7580 WARN                 basesrc gstbasesrc.c:3127:gst_base_src_loop:<filesrc0> error: streaming stopped, reason not-linked (-1)
0:00:00.338264287 3892572 0x5592899c7580 WARN                   queue gstqueue.c:992:gst_queue_handle_sink_event:<queue0> error: Internal data stream error.
0:00:00.338270329 3892572 0x5592899c7580 WARN                   queue gstqueue.c:992:gst_queue_handle_sink_event:<queue0> error: streaming stopped, reason not-linked (-1)
[ WARN:0@0.339] global cap_gstreamer.cpp:2784 handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module filesrc0 reported: Internal data stream error.
[ WARN:0@0.339] global cap_gstreamer.cpp:1199 startPipeline OpenCV | GStreamer warning: unable to start pipeline
Number of audio samples: 0
Number of video frames: 10
[ WARN:0@0.339] global cap_gstreamer.cpp:1164 isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
```

6. Play an audio stream with seeking AFTER the fix:

```bash
$ ./videocapture_audio_combination --audio "filesrc location=test1.mkv ! queue ! matroskademux name=demux demux.audio_0 ! decodebin ! audioconvert ! appsink"CAP_PROP_AUDIO_DATA_DEPTH: CV_16S
CAP_PROP_AUDIO_SAMPLES_PER_SECOND: 48000
CAP_PROP_AUDIO_TOTAL_CHANNELS: 2
CAP_PROP_AUDIO_TOTAL_STREAMS: [ WARN:0@0.025] global cap_gstreamer.cpp:1903 getProperty OpenCV | GStreamer: CAP_PROP_AUDIO_TOTAL_STREAMS property is not supported
0
Timestamp: 0.0000
Timestamp: 24.0000
Timestamp: 48.0000
Timestamp: 72.0000
Timestamp: 96.0000
Timestamp: 120.0000
Timestamp: 144.0000
Timestamp: 168.0000
Timestamp: 192.0000
Timestamp: 216.0000
Timestamp: 3500.0000
Timestamp: 3504.0000
Timestamp: 3528.0000
Timestamp: 3552.0000
Timestamp: 3576.0000
Timestamp: 3600.0000
Timestamp: 3624.0000
Timestamp: 3648.0000
Timestamp: 3672.0000
Timestamp: 3696.0000
Timestamp: 3720.0000
Timestamp: 3744.0000
Timestamp: 3768.0000
Timestamp: 3792.0000
Timestamp: 3816.0000
Timestamp: 3840.0000
Timestamp: 3864.0000
Timestamp: 3888.0000
Timestamp: 3912.0000
Timestamp: 3936.0000
```

7. Play a video stream with seeking AFTER the fix:

```bash
$ ./videocapture_audio_combination --audio "filesrc location=Jellyfish_360_10s_5MB.mkv ! queue ! matroskademux name=demux demux.video_0 ! decodebin ! videoconvert ! video/x-raw, format=BGR ! appsink drop=1"
[ WARN:0@0.033] global cap_gstreamer.cpp:1746 open OpenCV | GStreamer warning: Cannot query video position: status=1, value=22, duration=300
CAP_PROP_AUDIO_DATA_DEPTH: CV_16S
CAP_PROP_AUDIO_SAMPLES_PER_SECOND: 44100
CAP_PROP_AUDIO_TOTAL_CHANNELS: 0
CAP_PROP_AUDIO_TOTAL_STREAMS: [ WARN:0@0.034] global cap_gstreamer.cpp:1903 getProperty OpenCV | GStreamer: CAP_PROP_AUDIO_TOTAL_STREAMS property is not supported
0
Timestamp: 0.0000
Timestamp: 33.0000
Timestamp: 67.0000
Timestamp: 100.0000
Timestamp: 133.0000
Timestamp: 167.0000
Timestamp: 200.0000
Timestamp: 233.0000
Timestamp: 267.0000
Timestamp: 300.0000
0:00:00.335931693 3893501 0x55bbe76ad920 WARN      matroskareadcommon matroska-read-common.c:759:gst_matroska_read_common_parse_skip:<demux:sink> Unknown CueTrackPositions subelement 0xf0 - ignoring
0:00:00.335952823 3893501 0x55bbe76ad920 WARN      matroskareadcommon matroska-read-common.c:759:gst_matroska_read_common_parse_skip:<demux:sink> Unknown CueTrackPositions subelement 0xf0 - ignoring
0:00:00.335988029 3893501 0x55bbe76ad920 WARN                 basesrc gstbasesrc.c:1742:gst_base_src_perform_seek:<filesrc0> duplicate event found 184
Timestamp: 3467.0000
Timestamp: 3500.0000
Timestamp: 3533.0000
Timestamp: 3567.0000
Timestamp: 3600.0000
Timestamp: 3633.0000
Timestamp: 3667.0000
Timestamp: 3700.0000
Timestamp: 3733.0000
Timestamp: 3767.0000
Timestamp: 3800.0000
Timestamp: 3833.0000
Timestamp: 3867.0000
Timestamp: 3900.0000
Timestamp: 3933.0000
Timestamp: 3967.0000
Timestamp: 4000.0000
Timestamp: 4033.0000
Timestamp: 4067.0000
Timestamp: 4100.0000
```
2023-11-08 13:41:50 +03:00
..
cap_obsensor Merge pull request #23237 from hzcyf:feature/orbbec_femto_mega_support 2023-03-01 07:13:22 +00:00
cap_winrt Fix build for UWP 2019-08-05 17:19:36 +03:00
backend_plugin_legacy.impl.hpp videoio: split capture/writer plugin APIs 2021-01-14 06:19:23 +00:00
backend_plugin.cpp plugins: reverse candidates order to fetch higher versions first 2021-10-08 20:34:07 +00:00
backend_static.cpp videoio(plugin): add query API for plugins 2021-04-19 14:25:45 +00:00
backend.hpp videoio(plugin): add query API for plugins 2021-04-19 14:25:45 +00:00
cap_android_camera.cpp Merge pull request #22343 from komakai:android_cam_polling 2022-09-19 08:51:13 +03:00
cap_android_mediandk.cpp fix #22490 2022-09-15 13:24:18 +02:00
cap_aravis.cpp Merge pull request #20363 from SentToDevNull:master 2022-01-13 23:22:36 +03:00
cap_avfoundation_mac.mm Backport C-API cleanup (imgproc) from 5.x 2023-01-16 23:29:50 +03:00
cap_avfoundation.mm Backport C-API cleanup (imgproc) from 5.x 2023-01-16 23:29:50 +03:00
cap_dc1394_v2.cpp Merge pull request #21107 from take1014:remove_assert_21038 2021-11-27 18:34:52 +00:00
cap_dshow.cpp Adding support for Streamlabs Desktop Virtual Webcam 2023-08-13 16:40:38 +08:00
cap_dshow.hpp Merge pull request #19370 from OlivierLDff:patch-dshow-convertrgb 2021-01-29 10:18:09 +00:00
cap_ffmpeg_hw.hpp videoio: Include missing locale header for std::wstring_convert 2022-08-19 13:15:58 +05:30
cap_ffmpeg_impl.hpp Merge pull request #24363 from cudawarped:videoio_ffmpeg_add_stream_encapsulation 2023-10-25 13:21:01 +03:00
cap_ffmpeg_legacy_api.hpp videoio/FFmpeg: added CV_16UC1 read/write support 2022-12-07 12:12:31 +03:00
cap_ffmpeg.cpp Merge pull request #24363 from cudawarped:videoio_ffmpeg_add_stream_encapsulation 2023-10-25 13:21:01 +03:00
cap_gphoto2.cpp Merge pull request #22149 from seanm:sprintf 2022-06-25 06:48:22 +03:00
cap_gstreamer.cpp Merge pull request #24243 from kecsap:4.x 2023-11-08 13:41:50 +03:00
cap_images.cpp videoio: fix CAP_IMAGES with non-numbered file 2023-07-06 22:26:53 +03:00
cap_interface.hpp videoio: fixed FFmpeg plugin build 2022-12-01 20:26:08 +03:00
cap_ios_abstract_camera.mm build: fix warnings about TARGET_OS_MACCATALYST 2020-05-21 20:41:47 +00:00
cap_ios_photo_camera.mm Merge pull request #7178 from valeriyvan:iosfixes 2016-09-14 14:48:41 +03:00
cap_ios_video_camera.mm Fix customPreviewLayer layout issue when rotateVideo is NO in iOS 2017-11-21 12:56:23 +08:00
cap_librealsense.cpp Framework for dynamic videoio backends, FFmpeg and GStreamer plugins 2019-02-26 13:50:30 +03:00
cap_librealsense.hpp Framework for dynamic videoio backends, FFmpeg and GStreamer plugins 2019-02-26 13:50:30 +03:00
cap_mfx_common.cpp Merge pull request #20614 from mshabunin:use-onevpl-load 2021-10-21 13:56:11 +00:00
cap_mfx_common.hpp Merge pull request #20614 from mshabunin:use-onevpl-load 2021-10-21 13:56:11 +00:00
cap_mfx_plugin.cpp videoio: dump exception's messages in plugins 2021-02-24 17:48:04 +00:00
cap_mfx_reader.cpp Merge pull request #20614 from mshabunin:use-onevpl-load 2021-10-21 13:56:11 +00:00
cap_mfx_reader.hpp Merge pull request #20614 from mshabunin:use-onevpl-load 2021-10-21 13:56:11 +00:00
cap_mfx_writer.cpp Merge pull request #20614 from mshabunin:use-onevpl-load 2021-10-21 13:56:11 +00:00
cap_mfx_writer.hpp Merge pull request #20614 from mshabunin:use-onevpl-load 2021-10-21 13:56:11 +00:00
cap_mjpeg_decoder.cpp Merge remote-tracking branch 'upstream/3.4' into merge-3.4 2020-12-04 18:26:58 +00:00
cap_mjpeg_encoder.cpp Fixed bug with the last 4 bytes in MJPEG encoder. 2023-09-05 10:38:19 +03:00
cap_msmf.cpp Merge pull request #24142 from beanjoy:4.x 2023-09-07 13:06:39 +03:00
cap_msmf.hpp Remove unused code 2018-04-24 12:13:19 +03:00
cap_obsensor_capture.cpp add support for Orbbec Femto Mega RGB-D camera 2023-02-11 16:22:35 +08:00
cap_obsensor_capture.hpp Merge pull request #22936 from hzcyf:orbbec_new_cam_support 2022-12-16 12:23:12 +03:00
cap_openni2.cpp Backport C-API cleanup (imgproc) from 5.x 2023-01-16 23:29:50 +03:00
cap_pvapi.cpp Framework for dynamic videoio backends, FFmpeg and GStreamer plugins 2019-02-26 13:50:30 +03:00
cap_ueye.cpp Merge pull request #20475 from nibeh:patch-1 2021-08-24 12:31:54 +03:00
cap_v4l.cpp Merge pull request #24180 from MambaWong:4.x 2023-09-07 15:47:00 +03:00
cap_winrt_bridge.cpp
cap_winrt_bridge.hpp
cap_winrt_capture.cpp Framework for dynamic videoio backends, FFmpeg and GStreamer plugins 2019-02-26 13:50:30 +03:00
cap_winrt_capture.hpp Framework for dynamic videoio backends, FFmpeg and GStreamer plugins 2019-02-26 13:50:30 +03:00
cap_winrt_video.cpp
cap_winrt_video.hpp
cap_ximea.cpp Merge pull request #22149 from seanm:sprintf 2022-06-25 06:48:22 +03:00
cap_xine.cpp Merge pull request #22149 from seanm:sprintf 2022-06-25 06:48:22 +03:00
cap.cpp Added depricated_backends list. Added new information masseges. It needs to inform user, when he tries to use depricated or not uses backend 2023-05-05 14:22:18 +03:00
container_avi.cpp Merge remote-tracking branch 'upstream/3.4' into merge-3.4 2021-07-10 13:01:03 +00:00
ffmpeg_codecs.hpp Add missing header for LIBAVCODEC_VERSION_INT 2022-08-08 23:33:28 +02:00
plugin_api.hpp core(parallel): plugins support 2021-02-15 17:07:36 +00:00
plugin_capture_api.hpp core(parallel): plugins support 2021-02-15 17:07:36 +00:00
plugin_writer_api.hpp core(parallel): plugins support 2021-02-15 17:07:36 +00:00
precomp.hpp Merge remote-tracking branch 'upstream/3.4' into merge-3.4 2021-12-03 12:32:49 +00:00
videoio_c.cpp Framework for dynamic videoio backends, FFmpeg and GStreamer plugins 2019-02-26 13:50:30 +03:00
videoio_registry.cpp Added depricated_backends list. Added new information masseges. It needs to inform user, when he tries to use depricated or not uses backend 2023-05-05 14:22:18 +03:00
videoio_registry.hpp Added depricated_backends list. Added new information masseges. It needs to inform user, when he tries to use depricated or not uses backend 2023-05-05 14:22:18 +03:00
wrl.h Fix modules/ typos 2019-08-16 17:34:29 +03:00