diff mbox series

[FFmpeg-devel,35/35] avdevice/dshow: capabilities query also works on opened device

Message ID 20210607230414.612-36-dcnieho@gmail.com
State Superseded, archived
Headers show
Series avdevice (mostly dshow) enhancements
Related show

Checks

Context Check Description
andriy/x86_make fail Make failed
andriy/PPC64_make warning Make failed

Commit Message

Diederick C. Niehorster June 7, 2021, 11:04 p.m. UTC
While the capabilities API is in principle meant to be used with an allocated format context belonging to an unopened device, small changes make it work for an opened dshow device as well. So hereby done.

Signed-off-by: Diederick Niehorster <dcnieho@gmail.com>
---
 libavdevice/dshow.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c
index 9af3360317..c0032ae726 100644
--- a/libavdevice/dshow.c
+++ b/libavdevice/dshow.c
@@ -834,7 +834,7 @@  dshow_cycle_formats(AVFormatContext *avctx, enum dshowDeviceType devtype,
     void *caps = NULL;
     int i, n, size, r;
     int wait_for_better = 0;
-    int use_default;
+    int use_default, already_opened;
 
     // format parameters requested by user
     // if none are requested by user, the values will below be set to
@@ -860,6 +860,9 @@  dshow_cycle_formats(AVFormatContext *avctx, enum dshowDeviceType devtype,
     if (!caps)
         goto end;
 
+    // get if device is already opened
+    already_opened = ctx->device_name[0] || ctx->device_name[1];
+
     /** 
      * If we should open the device with the default format,
      * then:
@@ -1173,7 +1176,7 @@  dshow_cycle_formats(AVFormatContext *avctx, enum dshowDeviceType devtype,
         // in ranges, try to apply in all cases, and store
         // caps if successfully applied
         if (!wait_for_better || ranges) {
-            if (IAMStreamConfig_SetFormat(config, type) != S_OK)
+            if (!already_opened && IAMStreamConfig_SetFormat(config, type) != S_OK) // skip if device already opened
                 goto next;
             else if (ranges) {
                 // format matched and could be set successfully.
@@ -1500,12 +1503,19 @@  dshow_list_device_options(AVFormatContext *avctx, ICreateDevEnum *devenum,
     char *device_unique_name = NULL;
     int r;
 
-    if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter, &device_unique_name, NULL)) < 0)
-        return r;
-    ctx->device_filter[devtype] = device_filter;
-    ctx->device_unique_name[devtype] = device_unique_name;
+    if (!ctx->device_filter[devtype]) {
+        if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter, &device_unique_name, NULL)) < 0)
+            return r;
+    } else
+        device_filter = ctx->device_filter[devtype];
     if ((r = dshow_cycle_pins(avctx, devtype, sourcetype, device_filter, ranges ? &device_pin : NULL, ranges, query_type)) < 0)
         return r;
+
+    if (!ctx->device_filter[devtype]) {
+        // put them in context so they'll be cleaned up again
+        ctx->device_filter[devtype] = device_filter;
+        ctx->device_unique_name[devtype] = device_unique_name;
+    }
     return 0;
 }
 
@@ -2175,6 +2185,7 @@  static int dshow_query_ranges(AVOptionRanges** ranges_arg, void* obj, const char
     AVDeviceCapabilitiesQuery *caps = obj;
     const AVFormatContext *avctx = caps->device_context;
     struct dshow_ctx *ctx = avctx->priv_data;
+    int already_opened;
 
     int backup_sample_size;
     int backup_sample_rate;
@@ -2214,13 +2225,11 @@  static int dshow_query_ranges(AVOptionRanges** ranges_arg, void* obj, const char
         goto fail1;
     }
 
-    if (ctx->device_name[0]) {
-        av_log(avctx, AV_LOG_ERROR, "You cannot query device capabilities on an opened device\n");
-        ret = AVERROR(EIO);
-        goto fail1;
-    }
+    already_opened = ctx->device_name[0] || ctx->device_name[1];
+    if (already_opened)
+        av_log(avctx, AV_LOG_WARNING, "Querying device capabilities on an opened device: may yield false positives\n");
 
-    if (!parse_device_name(avctx)) {
+    if (!already_opened && !parse_device_name(avctx)) {
         av_log(avctx, AV_LOG_ERROR, "You must set a device name (AVFormatContext url) to specify which device to query capabilities from\n");
         ret = AVERROR(EINVAL);
         goto fail1;
@@ -2336,7 +2345,8 @@  fail2:
 
     // clear state variables that may have been set during the above process
     // (e.g. frees device names, removes device_filters, etc)
-    dshow_read_close(avctx);
+    if (!already_opened)
+        dshow_read_close(avctx);
 
 fail1:
     if (ret < 0)