From patchwork Tue Dec 21 12:12:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Diederick C. Niehorster" X-Patchwork-Id: 32783 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5360993iog; Tue, 21 Dec 2021 04:14:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJzBmCUev3Vm+LXYEQv90ueMrYvqoBb2ir2GaQSnbu8eBIfKEC5nb2p8WF3pdskHpNWSRVhr X-Received: by 2002:a17:906:c444:: with SMTP id ck4mr2372839ejb.89.1640088852787; Tue, 21 Dec 2021 04:14:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640088852; cv=none; d=google.com; s=arc-20160816; b=ZIBOT5ZvNlpCki1TCclzaplZUJUw0p7rBRj66e0ld+2OG3YczkdwCZN7TJRmX2mUgH 9aJm2BFXd7On5p0GkA+PAyT8ud0l9EJVWfa6ICjnKCdUU2LzEPNdmRNsULC1cRrFIOl6 wyFPQx69jOS6PkFGGX4JcvTHr9n759rVJq3qRopssgSakZMi4nbD2tlRdp+ICIW0yuT5 F8skxd2neRxZ4gnWIY0SRCYvrOZugUc1hUdpiMVyKlAL5Km4DYROLVJMq/4czHgyk8S4 +jkdvBxSD1oUgubngTcEUuG6r9S0xwQSFQMbRZGv7X4UA7ptvwgbKTIMlVU1OE7rC17l 2jjg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=gwKU17KTV55I+hU7pJgZxuNPujnv+pcgLbvC+EZEKA8=; b=OIwL/GefXkJuMu+vz8u6KxponM73wSODsCaIc+IFrQfWmKoSNOjorrJebayUC8YsJr ILJcBy6ygMxJJ+SNkVosK3S1tLWiyZR7ONespf2n1JCqs0aQ9f0XD57IVdOfAVDivoLl fqRn4Ll+aVR5roBOoWkUudkajgAUtsEVpS1vd06FHzzYFJ0tNgc/SfOYfEln0eje7PK6 eAWPXJ/aLhJmMOi6lG5K78hVZf+GT7tLKQ0ba1ge9/rpJGxJw9Df2LMCEsI7KR+ndeeC HhqjSAxbwqL7gWTMzsDLF6WBqOMPDYqeYmJpA9R7ljQez+jmkewzKQEbbWuQEIQqENjS 8i4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=lH9NcQnW; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bm25si9998731edb.145.2021.12.21.04.14.12; Tue, 21 Dec 2021 04:14:12 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=lH9NcQnW; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E7FCC68AF9C; Tue, 21 Dec 2021 14:13:35 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 275A868AF87 for ; Tue, 21 Dec 2021 14:13:34 +0200 (EET) Received: by mail-lf1-f50.google.com with SMTP id k21so10725553lfu.0 for ; Tue, 21 Dec 2021 04:13:34 -0800 (PST) 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=tpWG988i7SyyWgEvuHi9I7+W8WU7xI2kxgYCv90LkdI=; b=lH9NcQnWhFJ/FvqbQYwP3NcinIR0GfY89wMPlbndCde47BdAup3cdNrxB0YymO7/wn acf3wOLrjJsyf3Mob0p51UKEmUT8Qr1w/+hgAzkqHSIcg2gjgURAxZYpzRhomb8x/+Vx xRcQYIGbp5bgFW7fYjlqR8oUCLdBl9DPhP7D1KNfhHbdhoiMfGWzNp2HBZA+MZdLPXcT DaGD4/QAvor+N5zpJ/O/PxLd+xw0b6hPm7TqYuGXfoqAcM0Xc2NwWoFz+1ceUwlCjQHr Q8lqEsPS4hoD518OPIh3FDtq6+9WQx2d+fVnobQr33byqYct58PvDpY3L6yLs9JtwPUx G/6Q== 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=tpWG988i7SyyWgEvuHi9I7+W8WU7xI2kxgYCv90LkdI=; b=ri0f8pYnoyHN6oIYZ0S0tyZRw9359mHRhvKaW9+u5ArmpU6LTi7DR0CCJaqgd5zyJe /MBiCFFtbfTUgIHRksdBZvGRZHXtlAGsEo50jLOJBTa/JCCDqUwEleT8EaoiQ2/KYuq9 v98W9fv1vynss0o6V5+PmKDOaHoflXnRNZjJH1MAN1snqVejv3iMSuwoNeqMMc5/ceD+ a62cBXZWQoyHuyO5jV+r1Bm9HGZGjQrjWbPQtxCeOztSYiknDR1FRNdbgTOrVfyD5MtV urNsqIFtd2gRXmxEvboPk74H8xCwvZsP4BchEo6l0+5R0sVQBoahg1dRK9X5J8HrUdNH VdRg== X-Gm-Message-State: AOAM531T2jq/+uGojRMJQ7z3OHZvBF4FRFxCN+A5aUFn5z5Rosn+6en6 SLpCuYVGBk/j5CIr+lj7fuzervWYoStgVA== X-Received: by 2002:a05:6512:238f:: with SMTP id c15mr2871796lfv.166.1640088813206; Tue, 21 Dec 2021 04:13:33 -0800 (PST) Received: from localhost.localdomain ([196.244.192.13]) by smtp.gmail.com with ESMTPSA id h9sm147165lft.5.2021.12.21.04.13.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 04:13:32 -0800 (PST) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Tue, 21 Dec 2021 13:12:33 +0100 Message-Id: <20211221121239.1201-7-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20211221121239.1201-1-dcnieho@gmail.com> References: <20211221121239.1201-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v6 06/12] avdevice/dshow: implement get_device_list X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Diederick Niehorster Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: jM9k/wHQA6bU Needed to enable programmatic discovery of DirectShow devices Signed-off-by: Diederick Niehorster --- libavdevice/dshow.c | 96 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 81 insertions(+), 15 deletions(-) diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c index 0ef3b3d13e..e1702c8519 100644 --- a/libavdevice/dshow.c +++ b/libavdevice/dshow.c @@ -202,11 +202,14 @@ fail: * retrieve the device with type specified by devtype and return the * pointer to the object found in *pfilter. * If pfilter is NULL, list all device names. + * If device_list is not NULL, populate it with found devices instead of + * outputting device names to log */ static int dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum, enum dshowDeviceType devtype, enum dshowSourceFilterType sourcetype, - IBaseFilter **pfilter, char **device_unique_name) + IBaseFilter **pfilter, char **device_unique_name, + AVDeviceInfoList **device_list) { struct dshow_ctx *ctx = avctx->priv_data; IBaseFilter *device_filter = NULL; @@ -238,18 +241,19 @@ dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum, IBindCtx *bind_ctx = NULL; LPOLESTR olestr = NULL; LPMALLOC co_malloc = NULL; + AVDeviceInfo *device = NULL; int i; r = CoGetMalloc(1, &co_malloc); if (r != S_OK) - goto fail1; + goto fail; r = CreateBindCtx(0, &bind_ctx); if (r != S_OK) - goto fail1; + goto fail; /* GetDisplayname works for both video and audio, DevicePath doesn't */ r = IMoniker_GetDisplayName(m, bind_ctx, NULL, &olestr); if (r != S_OK) - goto fail1; + goto fail; unique_name = dup_wchar_to_utf8(olestr); /* replace ':' with '_' since we use : to delineate between sources */ for (i = 0; i < strlen(unique_name); i++) { @@ -259,34 +263,62 @@ dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum, r = IMoniker_BindToStorage(m, 0, 0, &IID_IPropertyBag, (void *) &bag); if (r != S_OK) - goto fail1; + goto fail; var.vt = VT_BSTR; r = IPropertyBag_Read(bag, L"FriendlyName", &var, NULL); if (r != S_OK) - goto fail1; + goto fail; friendly_name = dup_wchar_to_utf8(var.bstrVal); if (pfilter) { if (strcmp(device_name, friendly_name) && strcmp(device_name, unique_name)) - goto fail1; + goto fail; if (!skip--) { r = IMoniker_BindToObject(m, 0, 0, &IID_IBaseFilter, (void *) &device_filter); if (r != S_OK) { av_log(avctx, AV_LOG_ERROR, "Unable to BindToObject for %s\n", device_name); - goto fail1; + goto fail; } *device_unique_name = unique_name; unique_name = NULL; // 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); + if (device_list) { + device = av_mallocz(sizeof(AVDeviceInfo)); + if (!device) + goto fail; + + device->device_name = av_strdup(friendly_name); + device->device_description = av_strdup(unique_name); + if (!device->device_name || !device->device_description) + goto fail; + + // make space in device_list for this new device + if (av_reallocp_array(&(*device_list)->devices, + (*device_list)->nb_devices + 1, + sizeof(*(*device_list)->devices)) < 0) + goto fail; + + // store device in list + (*device_list)->devices[(*device_list)->nb_devices] = device; + (*device_list)->nb_devices++; + device = NULL; // copied into array, make sure not freed below + } + else { + av_log(avctx, AV_LOG_INFO, " \"%s\"\n", friendly_name); + av_log(avctx, AV_LOG_INFO, " Alternative name \"%s\"\n", unique_name); + } } -fail1: +fail: + if (device) { + av_freep(&device->device_name); + av_freep(&device->device_description); + av_free(device); + } if (olestr && co_malloc) IMalloc_Free(co_malloc, olestr); if (bind_ctx) @@ -312,6 +344,39 @@ fail1: return 0; } +static int dshow_get_device_list(AVFormatContext *avctx, AVDeviceInfoList *device_list) +{ + struct dshow_ctx *ctx = avctx->priv_data; + ICreateDevEnum *devenum = NULL; + int r; + int ret = AVERROR(EIO); + + if (!device_list) + return AVERROR(EINVAL); + + CoInitialize(0); + + r = CoCreateInstance(&CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, + &IID_ICreateDevEnum, (void**)&devenum); + if (r != S_OK) { + av_log(avctx, AV_LOG_ERROR, "Could not enumerate system devices.\n"); + goto error; + } + + ret = dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, NULL, NULL, &device_list); + if (ret < S_OK) + goto error; + ret = dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL, NULL, &device_list); + +error: + if (devenum) + ICreateDevEnum_Release(devenum); + + CoUninitialize(); + + return ret; +} + /** * Cycle through available formats using the specified pin, * try to set parameters specified through AVOptions and if successful @@ -705,7 +770,7 @@ 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)) < 0) + 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; @@ -765,7 +830,7 @@ 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, &device_filter_unique_name)) < 0) { + if ((r = dshow_cycle_devices(avctx, devenum, devtype, sourcetype, &device_filter, &device_filter_unique_name, NULL)) < 0) { ret = r; goto error; } @@ -1122,9 +1187,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, NULL); + dshow_cycle_devices(avctx, devenum, VideoDevice, VideoSourceDevice, NULL, NULL, NULL); av_log(avctx, AV_LOG_INFO, "DirectShow audio devices\n"); - dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL, NULL); + dshow_cycle_devices(avctx, devenum, AudioDevice, AudioSourceDevice, NULL, NULL, NULL); ret = AVERROR_EXIT; goto error; } @@ -1329,6 +1394,7 @@ const AVInputFormat ff_dshow_demuxer = { .read_header = dshow_read_header, .read_packet = dshow_read_packet, .read_close = dshow_read_close, + .get_device_list= dshow_get_device_list, .flags = AVFMT_NOFILE | AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK, .priv_class = &dshow_class, };