From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTP id 62CFE4114E
	for <ffmpegdev@gitmailbox.com>; Fri, 25 Mar 2022 14:15:33 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id BC8A068B2CB;
	Fri, 25 Mar 2022 16:13:35 +0200 (EET)
Received: from mail-lj1-f175.google.com (mail-lj1-f175.google.com
 [209.85.208.175])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 984F368B2C6
 for <ffmpeg-devel@ffmpeg.org>; Fri, 25 Mar 2022 16:13:34 +0200 (EET)
Received: by mail-lj1-f175.google.com with SMTP id g24so10474697lja.7
 for <ffmpeg-devel@ffmpeg.org>; Fri, 25 Mar 2022 07:13:34 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
 h=from:to:cc:subject:date:message-id:in-reply-to:references
 :mime-version:content-transfer-encoding;
 bh=MoX3WE6sSUEUEPxyFw4Zpli4hgB9ZCym87zfR3vTSy0=;
 b=bwq8w2hu3rlS8AqCUhdQ45E7Vndw1fnDbKmfYXcPZp0PCxOTdC0WK78dQ+1Dwb+eBU
 yGskiGf8mGUlAjnOdc/rhltnz9AyfKaYwG7l521B0XgKmHM4pF1NJRyMapM3WZdaRpgu
 kZIr89c+Go1O0Wqkk2BXYmbXb3JBu3TfbfBxBzDfTG6/YXa8cvGdAuStpI7e4idWsEtf
 5QuBzO4bXGJuC0JS4GHJFVd8rJDyUIUyh8p3UopBu4mKZXqSUxSW8EA2nWMP/q6c6hhX
 NWH7RtUnL8xY9732kAyAP6b1KQBj4H7YMnOzSfaLi2BFwlhdMzo7NWiJM49iU9031COx
 yosg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20210112;
 h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
 :references:mime-version:content-transfer-encoding;
 bh=MoX3WE6sSUEUEPxyFw4Zpli4hgB9ZCym87zfR3vTSy0=;
 b=2AWcZ8ckQMoe8Wivdqvngi34fMBU16FDEVNmYlGY6HvhaI5nzOARryh7qfraGcrX3C
 RXEI7x55BPRb5ewdDh+QmRhSZEvljnp6Yi431CoHWvme+oTVGoIma54eTfTGjfBXCY3U
 qNzEG5xF6mwY+PugqScx4X7uqbBZ4/TPKckP8sqolelnLNxsl0CGcLV/98/wnKAFmkzX
 SxAQE8vZNhdwpqCPgNzXxc70fMdBahenlaTBZcIrXsAqxrXC4KKx5rfahdJxz5BJ1eDm
 fnMkHTfRkchIGJBGn2T5Tten4xItudfGdW/bGJU8dRCn4u5/kYwKNTM0JRSipBh7T4hQ
 qv5w==
X-Gm-Message-State: AOAM532dXylOcrlOquKCWiJPRAK1EtdCx8lLMmW16c7LDyr8SPqpzNde
 qsCjzBt8qXCtjOWcB+bJ9x6qjY3my5A=
X-Google-Smtp-Source: ABdhPJxlsY05xLQAJE0OOCoyf/Yu6b576Zgzm7EN4KaFUQCxBkDnp+KePWlKRXk+aaQHWdetMmj4tA==
X-Received: by 2002:a2e:9847:0:b0:238:eca:62fd with SMTP id
 e7-20020a2e9847000000b002380eca62fdmr8310519ljj.65.1648217613284; 
 Fri, 25 Mar 2022 07:13:33 -0700 (PDT)
Received: from localhost.localdomain (deedock.humlab.lu.se. [130.235.135.183])
 by smtp.gmail.com with ESMTPSA id
 x40-20020a056512132800b004489691436esm720853lfu.146.2022.03.25.07.13.32
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 25 Mar 2022 07:13:32 -0700 (PDT)
From: Diederick Niehorster <dcnieho@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Date: Fri, 25 Mar 2022 15:10:41 +0100
Message-Id: <20220325141041.1748-23-dcnieho@gmail.com>
X-Mailer: git-send-email 2.28.0.windows.1
In-Reply-To: <20220325141041.1748-1-dcnieho@gmail.com>
References: <20220325141041.1748-1-dcnieho@gmail.com>
MIME-Version: 1.0
Subject: [FFmpeg-devel] [PATCH v4 22/22] avdevice/dshow: capabilities query
 also works on opened device
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Cc: Diederick Niehorster <dcnieho@gmail.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/20220325141041.1748-23-dcnieho@gmail.com/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

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         | 110 +++++++++++++++++++++---------------
 libavdevice/dshow_capture.h |   3 +
 2 files changed, 66 insertions(+), 47 deletions(-)

diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c
index c426cef905..216c888bed 100644
--- a/libavdevice/dshow.c
+++ b/libavdevice/dshow.c
@@ -849,7 +849,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
@@ -875,6 +875,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:
@@ -1153,7 +1156,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.
@@ -1494,12 +1497,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;
+
+        // 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;
+    } 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;
+
     return 0;
 }
 
@@ -2319,7 +2329,8 @@ fail1:
     return ret;
 }
 
-// fake class to point av_opt_query_ranges to our query_ranges function
+// fake class to point av_opt functions to capabilities that can be queried,
+// and av_opt_query_ranges to our query_ranges function
 static const AVClass dshow_dev_caps_class = {
     .class_name   = "",
     .item_name    = av_default_item_name,
@@ -2337,49 +2348,51 @@ static int dshow_create_device_capabilities(struct AVFormatContext *avctx, AVDev
     // set class so queries work
     caps->av_class = &dshow_dev_caps_class;
 
-    if (ctx->device_name[0] || ctx->device_name[1]) {
-        av_log(avctx, AV_LOG_ERROR, "You cannot query device capabilities on an opened device\n");
-        ret = AVERROR(EIO);
-        goto fail;
-    }
+    // check if device setup is needed or we will be querying capabilities of an already opened device
+    ctx->cap_query_already_opened = ctx->device_name[0] || ctx->device_name[1];
+    if (ctx->cap_query_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)) {
-        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 fail;
-    }
-
-    CoInitialize(0);
-    if (CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
-                         &IID_ICreateDevEnum, (void **) &devenum)) {
-        av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
-        ret = AVERROR(EIO);
-        goto fail;
-    }
+    // if device not already opened, check that what user specified can be opened
+    if (!ctx->cap_query_already_opened) {
+        if (!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 fail;
+        }
 
-    // check devices can be found
-    if (ctx->device_name[VideoDevice]) {
-        IBaseFilter *device_filter = NULL;
-        char *device_unique_name = NULL;
-        if ((ret = dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, &device_filter, &device_unique_name, NULL)) < 0)
-            return ret;
-
-        if (ret >= 0) {
-            ctx->device_filter[VideoDevice] = device_filter;
-            ctx->device_unique_name[VideoDevice] = device_unique_name;
+        CoInitialize(0);
+        if (CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
+                             &IID_ICreateDevEnum, (void **) &devenum)) {
+            av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n");
+            ret = AVERROR(EIO);
+            goto fail;
         }
-    }
-    if (ctx->device_name[AudioDevice]) {
-        IBaseFilter *device_filter = NULL;
-        char *device_unique_name = NULL;
-        if (dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, &device_filter, &device_unique_name, NULL) < 0) {
-            /* try to access audio from combined video+audio sources as fallback */
-            if ((ret = dshow_cycle_devices(avctx, devenum, AudioDevice, VideoSourceDevice, &device_filter, &device_unique_name, NULL)) < 0)
-                goto fail;
+
+        // check devices can be found
+        if (ctx->device_name[VideoDevice]) {
+            IBaseFilter *device_filter = NULL;
+            char *device_unique_name = NULL;
+            if ((ret = dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, &device_filter, &device_unique_name, NULL)) < 0)
+                return ret;
+
+            if (ret >= 0) {
+                ctx->device_filter[VideoDevice] = device_filter;
+                ctx->device_unique_name[VideoDevice] = device_unique_name;
+            }
         }
-        if (ret >= 0) {
-            ctx->device_filter[AudioDevice] = device_filter;
-            ctx->device_unique_name[AudioDevice] = device_unique_name;
+        if (ctx->device_name[AudioDevice]) {
+            IBaseFilter *device_filter = NULL;
+            char *device_unique_name = NULL;
+            if (dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, &device_filter, &device_unique_name, NULL) < 0) {
+                /* try to access audio from combined video+audio sources as fallback */
+                if ((ret = dshow_cycle_devices(avctx, devenum, AudioDevice, VideoSourceDevice, &device_filter, &device_unique_name, NULL)) < 0)
+                    goto fail;
+            }
+            if (ret >= 0) {
+                ctx->device_filter[AudioDevice] = device_filter;
+                ctx->device_unique_name[AudioDevice] = device_unique_name;
+            }
         }
     }
 
@@ -2395,9 +2408,12 @@ fail:
 
 static int dshow_free_device_capabilities(struct AVFormatContext *avctx, AVDeviceCapabilitiesQuery *caps)
 {
+    struct dshow_ctx *ctx = avctx->priv_data;
+
     // clear state variables that may have been set during the querying process
     // (e.g. frees device names, removes device_filters, etc)
-    dshow_read_close(avctx);
+    if (!ctx->cap_query_already_opened)
+        dshow_read_close(avctx);
 
     return 0;
 }
diff --git a/libavdevice/dshow_capture.h b/libavdevice/dshow_capture.h
index 94ba9896b7..a2c107b19c 100644
--- a/libavdevice/dshow_capture.h
+++ b/libavdevice/dshow_capture.h
@@ -345,6 +345,9 @@ struct dshow_ctx {
     int sample_rate;
     int sample_size;
     int channels;
+
+    // for capabilities query
+    int cap_query_already_opened;
 };
 
 /*****************************************************************************
-- 
2.28.0.windows.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".