[FFmpeg-devel] PATCH: dshow: don't add two instances of same device to graphs

Roger Pack rogerdpack2 at gmail.com
Tue Jun 21 16:06:09 CEST 2016


On 6/17/16, Roger Pack <rogerdpack2 at gmail.com> wrote:
> On 6/15/16, Clément Bœsch <u at pkh.me> wrote:
>> On Wed, Jun 15, 2016 at 02:54:09AM -0600, Roger Pack wrote:
>>> A handful of devices don't support this, and the rest work fine with it
>>> :)
>>>
>>> -roger-
>>
>>> From e724d7f169bcae3217455cd88f9c023d275d367a Mon Sep 17 00:00:00 2001
>>> From: rogerdpack <rogerpack2005 at gmail.com>
>>> Date: Wed, 15 Jun 2016 02:17:11 -0600
>>> Subject: [PATCH] dshow: don't add two instances of same device to graphs
>>>
>>> Signed-off-by: rogerdpack <rogerpack2005 at gmail.com>
>>> ---
>>>  libavdevice/dshow.c         | 37 ++++++++++++++++++++++++++++++-------
>>>  libavdevice/dshow_capture.h |  2 ++
>>>  2 files changed, 32 insertions(+), 7 deletions(-)
>>>
>>> diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c
>>> index 5f2cad7..e1ac855 100644
>>> --- a/libavdevice/dshow.c
>>> +++ b/libavdevice/dshow.c
>>> @@ -108,6 +108,10 @@ dshow_read_close(AVFormatContext *s)
>>>          av_freep(&ctx->device_name[0]);
>>>      if (ctx->device_name[1])
>>>          av_freep(&ctx->device_name[1]);
>>
>>> +    if (ctx->device_unique_name[0])
>>> +        av_freep(&ctx->device_unique_name[0]);
>>> +    if (ctx->device_unique_name[1])
>>> +        av_freep(&ctx->device_unique_name[1]);
>>
>> I can't comment the rest of the patch, but please remove the ifs. They
>> are,
>> and always were, totally useless. Just like those above.
>
> OK see also attached patch, to be applied after patch 1.
> Thanks, will wait three days and if no objections, request these
> patches be committed.

OK would some kind soul please commit these for me?
Thanks!
-roger-
-------------- next part --------------
From e724d7f169bcae3217455cd88f9c023d275d367a Mon Sep 17 00:00:00 2001
From: rogerdpack <rogerpack2005 at gmail.com>
Date: Wed, 15 Jun 2016 02:17:11 -0600
Subject: [PATCH 1/2] dshow: don't add two instances of same device to graphs

Signed-off-by: rogerdpack <rogerpack2005 at gmail.com>
---
 libavdevice/dshow.c         | 37 ++++++++++++++++++++++++++++++-------
 libavdevice/dshow_capture.h |  2 ++
 2 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c
index 5f2cad7..e1ac855 100644
--- a/libavdevice/dshow.c
+++ b/libavdevice/dshow.c
@@ -108,6 +108,10 @@ dshow_read_close(AVFormatContext *s)
         av_freep(&ctx->device_name[0]);
     if (ctx->device_name[1])
         av_freep(&ctx->device_name[1]);
+    if (ctx->device_unique_name[0])
+        av_freep(&ctx->device_unique_name[0]);
+    if (ctx->device_unique_name[1])
+        av_freep(&ctx->device_unique_name[1]);
 
     if(ctx->mutex)
         CloseHandle(ctx->mutex);
@@ -205,7 +209,8 @@ fail:
  */
 static int
 dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
-                    enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype, IBaseFilter **pfilter)
+                    enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype,
+                    IBaseFilter **pfilter, char **device_unique_name)
 {
     struct dshow_ctx *ctx = avctx->priv_data;
     IBaseFilter *device_filter = NULL;
@@ -276,10 +281,13 @@ dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
                     av_log(avctx, AV_LOG_ERROR, "Unable to BindToObject for %s\n", device_name);
                     goto fail1;
                 }
+                *device_unique_name = unique_name;
+                // success, loop will end now
             }
         } else {
             av_log(avctx, AV_LOG_INFO, " \"%s\"\n", friendly_name);
             av_log(avctx, AV_LOG_INFO, "    Alternative name \"%s\"\n", unique_name);
+            av_free(unique_name);
         }
 
 fail1:
@@ -288,7 +296,6 @@ fail1:
         if (bind_ctx)
             IBindCtx_Release(bind_ctx);
         av_free(friendly_name);
-        av_free(unique_name);
         if (bag)
             IPropertyBag_Release(bag);
         IMoniker_Release(m);
@@ -706,14 +713,15 @@ dshow_list_device_options(AVFormatContext *avctx, ICreateDevEnum *devenum,
 {
     struct dshow_ctx *ctx = avctx->priv_data;
     IBaseFilter *device_filter = NULL;
+    char *device_unique_name = NULL;
     int r;
 
-    if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter)) < 0)
+    if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter, &device_unique_name)) < 0)
         return r;
     ctx->device_filter[devtype] = device_filter;
     if ((r = dshow_cycle_pins(avctx, devtype, sourcetype, device_filter, NULL)) < 0)
         return r;
-
+    av_freep(&device_unique_name);
     return 0;
 }
 
@@ -723,6 +731,7 @@ dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
 {
     struct dshow_ctx *ctx = avctx->priv_data;
     IBaseFilter *device_filter = NULL;
+    char *device_filter_unique_name = NULL;
     IGraphBuilder *graph = ctx->graph;
     IPin *device_pin = NULL;
     libAVPin *capture_pin = NULL;
@@ -733,6 +742,7 @@ dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
     IStream *ifile_stream = NULL;
     IStream *ofile_stream = NULL;
     IPersistStream *pers_stream = NULL;
+    enum dshowDeviceType otherDevType = (devtype == VideoDevice) ? AudioDevice : VideoDevice;
 
     const wchar_t *filter_name[2] = { L"Audio capture filter", L"Video capture filter" };
 
@@ -766,13 +776,26 @@ dshow_open_device(AVFormatContext *avctx, ICreateDevEnum *devenum,
         av_log(avctx, AV_LOG_INFO, "Capture filter loaded successfully from file \"%s\".\n", filename);
     } else {
 
-        if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter)) < 0) {
+        if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter, &device_filter_unique_name)) < 0) {
             ret = r;
             goto error;
         }
     }
+	if (ctx->device_filter[otherDevType]) {
+        // avoid adding add two instances of the same device to the graph, one for video, one for audio
+        // a few devices don't support this (could also do this check earlier to avoid double crossbars, etc. but they seem OK)
+        if (strcmp(device_filter_unique_name, ctx->device_unique_name[otherDevType]) == 0) {
+          av_log(avctx, AV_LOG_DEBUG, "reusing previous graph capture filter... %s\n", device_filter_unique_name);
+          IBaseFilter_Release(device_filter);
+          device_filter = ctx->device_filter[otherDevType];
+          IBaseFilter_AddRef(ctx->device_filter[otherDevType]);
+        } else {
+            av_log(avctx, AV_LOG_DEBUG, "not reusing previous graph capture filter %s != %s\n", device_filter_unique_name, ctx->device_unique_name[otherDevType]);
+        }
+    }
 
     ctx->device_filter [devtype] = device_filter;
+    ctx->device_unique_name [devtype] = device_filter_unique_name;
 
     r = IGraphBuilder_AddFilter(graph, device_filter, NULL);
     if (r != S_OK) {
@@ -1101,9 +1124,9 @@ static int dshow_read_header(AVFormatContext *avctx)
 
     if (ctx->list_devices) {
         av_log(avctx, AV_LOG_INFO, "DirectShow video devices (some may be both video and audio devices)\n");
-        dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, NULL);
+        dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, NULL, NULL);
         av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n");
-        dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL);
+        dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL, NULL);
         ret = AVERROR_EXIT;
         goto error;
     }
diff --git a/libavdevice/dshow_capture.h b/libavdevice/dshow_capture.h
index f26eaf9..475d62b 100644
--- a/libavdevice/dshow_capture.h
+++ b/libavdevice/dshow_capture.h
@@ -289,6 +289,8 @@ struct dshow_ctx {
     IGraphBuilder *graph;
 
     char *device_name[2];
+    char *device_unique_name[2];
+
     int video_device_number;
     int audio_device_number;
 
-- 
2.8.3.windows.1

-------------- next part --------------
From 61e07cc85785345122832b1b5333412b5272c545 Mon Sep 17 00:00:00 2001
From: rogerdpack <rogerpack2005 at gmail.com>
Date: Fri, 17 Jun 2016 14:04:34 -0600
Subject: [PATCH 2/2] dshow: avoid duplicate NULL check before free

Signed-off-by: rogerdpack <rogerpack2005 at gmail.com>
---
 libavdevice/dshow.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c
index e1ac855..127e4c2 100644
--- a/libavdevice/dshow.c
+++ b/libavdevice/dshow.c
@@ -104,14 +104,10 @@ dshow_read_close(AVFormatContext *s)
     if (ctx->device_filter[AudioDevice])
         IBaseFilter_Release(ctx->device_filter[AudioDevice]);
 
-    if (ctx->device_name[0])
-        av_freep(&ctx->device_name[0]);
-    if (ctx->device_name[1])
-        av_freep(&ctx->device_name[1]);
-    if (ctx->device_unique_name[0])
-        av_freep(&ctx->device_unique_name[0]);
-    if (ctx->device_unique_name[1])
-        av_freep(&ctx->device_unique_name[1]);
+    av_freep(&ctx->device_name[0]);
+    av_freep(&ctx->device_name[1]);
+    av_freep(&ctx->device_unique_name[0]);
+    av_freep(&ctx->device_unique_name[1]);
 
     if(ctx->mutex)
         CloseHandle(ctx->mutex);
-- 
2.8.3.windows.1



More information about the ffmpeg-devel mailing list