From patchwork Tue Dec 21 13:53:31 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: 32796 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5444730iog; Tue, 21 Dec 2021 05:55:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJxD7SM228dBowgkfVgoUfh3ExX0kdJetKITsyUC+diuTkEssNrEvxTtjMyQnNAeyzYpMNBD X-Received: by 2002:a17:907:7b9e:: with SMTP id ne30mr2869663ejc.24.1640094915094; Tue, 21 Dec 2021 05:55:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640094915; cv=none; d=google.com; s=arc-20160816; b=t3CdUdWV0H19T1IJZ5sX8FgXRj4gv4gYiEZJojP/8l+VivPIfNssRycgKvB10/HlU/ l8lXBe9DZq4QcNnxhj2yoJ59CD6XBvyNkuWAbnRhy0HgWWHywnbUc8sUNxyK7ktDQFf7 7clSbijE1q+0p5A7a7eF6fyTe+4dbLPZXV7dRR2xxhMr2qTlLFz/EgEsN+Xn0Ai/KlR7 qfN61HIKYgmqukDHECUPU48Y2iimoeVxyOEHOLkqDfEneaVRnxkATuobFc5XwA+F3TS2 gFdvwFxsbAjRiCjZBvXt6EtzqWvTAgtHNR9xa4wfS5eVI6NubA8BfPn8K6Q4DwEF6QVW thdA== 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=ROWVTtgqL6f8eW6i80Mb+pSJms4dTm0L0E2cqP4wDWHxb9gCdNUZ20vE7ep/52LP27 nKbxvo4pcY14FSOGWrh8sYY8YmYNY47aMc0/nrVPfa18Tqg7eRstankESYifj9bYm8E3 gLNLNRItiLWkk+DJspNMxJJhqMDcuIv1SfbMGTchOQ8DKfDYZpBAc+LzUpJCY4rvvfoL HXNQnZfj8qV+bxxxSGgnf5DYnh3kdJlme0/qbkowLRWjda2cetOjgN55wtFkASLRpN4P KAJAXnQ8q+UVb2d2PmGbOrTOIFPDV30gCvubgwKUAeBMY/6i4/78nFd1Qq1lwWuOJRql b9mQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=HJr4dSk4; 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 mq7si9734080ejc.19.2021.12.21.05.55.14; Tue, 21 Dec 2021 05:55:15 -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=HJr4dSk4; 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 A275768ADCE; Tue, 21 Dec 2021 15:54:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com [209.85.208.181]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DA23368AFDE for ; Tue, 21 Dec 2021 15:54:34 +0200 (EET) Received: by mail-lj1-f181.google.com with SMTP id m12so21532519ljj.6 for ; Tue, 21 Dec 2021 05:54: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=HJr4dSk4nVkrVOkOsJtQg6KasaX39pmvXChnPv31fDcRC9E+Z6oaFHt2PcgGmB2SiA 5VuqDIETLhuHFHu3v7nRXZMjmfRirX08CgAF0X/ED71Y0ZJubHA7ZAmbzxcQuwLEwLL0 F6i8yffxTGsVT3uKm5wU/hv+oHqTU4cp2lKCm4natCStARL15wu0M+UtSUW7xoVe6si6 HQkU9VuJ/owWOJYsnMmuhXYjcBagsosODWKpO5cJu20+JXx7uIE6ohEft+R840+lEtAH Lnd/qFe3SScSVS7kICmXhmDxs7ayq6VuNhm1ytLEKBuNQ9RNPSMmkRPxrzezRUGsDTaK r0CA== 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=Pxb2R+frkQfbT99beBOV7sbPh7Ic3825qzT+1WEPTsxZkGOGm6kCgNOmo/QBGHfjOa GSoLcauvHJaxupLvckQ4RHoqZY75mBkHAOqbaS2q6ylgnetVG/jZSzoXs+GTOzO9zqeV GfIEjZlTB1m05Qmk49q5Fp3AMKpkIK/PboiJ8WpmCi9+6NVMF/F6gbHn8/uaFxCngwUA mHEvgvoy4CqFaH42WMNvSqUili8+/0vvsFPl4maKJMm/cLDf27kiZo0qBBjFTLzNGnhA KV/Z6StZNDOSP73a2+1LILjSwt+6B7Px9+PlIXiHxN0ahzJ/XVwfTL5Dsav5z9HDPLxd 3KBA== X-Gm-Message-State: AOAM530XqKiKjmPaisrNcbbhXP8XLTYxOHHkSZCcW1oMIyW+fL/Geb1Z PL8z5zFf4Cd4GFDuqKTPO7mT5HJTfGnIPA== X-Received: by 2002:a2e:9b03:: with SMTP id u3mr1258989lji.490.1640094873688; Tue, 21 Dec 2021 05:54:33 -0800 (PST) Received: from localhost.localdomain ([196.244.192.13]) by smtp.gmail.com with ESMTPSA id m19sm943098lfu.267.2021.12.21.05.54.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Dec 2021 05:54:33 -0800 (PST) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Tue, 21 Dec 2021 14:53:31 +0100 Message-Id: <20211221135337.1348-7-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20211221135337.1348-1-dcnieho@gmail.com> References: <20211221135337.1348-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v7 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: Nr3xg4r5MhRv 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, };