From patchwork Sun Dec 19 19:21:28 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: 32732 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp3695682iog; Sun, 19 Dec 2021 11:23:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJxnObp22oCZ6r5diB6LxB2rBDpL9nnToIIbcrXM1siGnmpovE0zzDp3UlXFiqXpiHYB5+31 X-Received: by 2002:a17:907:1b24:: with SMTP id mp36mr10804268ejc.487.1639941795744; Sun, 19 Dec 2021 11:23:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639941795; cv=none; d=google.com; s=arc-20160816; b=aO4xWNBUaX6TK2+c2c2UTpxSc7wi+iWEWLRLnUBIoEuk5dg4eMUavgwYyneM7MybOh 6pWzSZaXYBlsvsvXiri2pJMcm9faJUSRZjfnKH9j7t7oaYHiYN23wshFKCEbXoxLEg6o zENuuFd9p5hYx8u2T7dmLck06cmIGzVDreKs3XcsvVV5Y0qCQnbwfpWa+QwQxE0l+34G VqgRnMDmYxl+PlWtYsS1koADSDOLCT7NT2JM5jdIeVhJ8Uv3Sb32XU7b7q1/kUmWzL7l yi5i7jhZmQnife+onkLtFJBXIFEzxUzNKGJZHGu1hSdzzRX55hV+H1AglGhiX7IW23vh AG6w== 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=tG8Xe64fCv/CzNO/1KPJRE+HrHmRsg92GLBzOohtaK8=; b=JKi2ld5lSnF6NvZc+rOjVew3ecrn+0SqgPJuiSldS98FlOiThzYFJWLnl3RXCZD2cY bIdx5RXsIrKDesCH0fKW0BJcBVdhQjS0WKmL9jpoHAoTt5KDOppisr2gs0rXq7T5DhKO /WGe9pJjpCDdkjtggWPD/qpH6Yx3Nqhdyowjn2o6quHoXbeTqG5rwswUPAR757mF2xWZ F3PqR/5qCyrGXpF1t9V2ymPVOGzQas9Un51M7WPb39gOAR0i4NGeYFOQUB87dU9Pz88n +6SP1wt0i44jX9Jq8Da+AuadAbpN4th6I2qMZLOy/Whv/QjVggJ8iHbTtS7P59dinK+0 mcTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=fdjS6zb0; 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 f14si3701068ejx.663.2021.12.19.11.23.15; Sun, 19 Dec 2021 11:23: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=fdjS6zb0; 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 0419268AF97; Sun, 19 Dec 2021 21:22:46 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f173.google.com (mail-lj1-f173.google.com [209.85.208.173]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 36E0A68AF86 for ; Sun, 19 Dec 2021 21:22:39 +0200 (EET) Received: by mail-lj1-f173.google.com with SMTP id z8so12470531ljz.9 for ; Sun, 19 Dec 2021 11:22:39 -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=Rce8u6K7LN/Um/IxRb+ZbL4sJ3RSDKU2XLCuNLPgW6Q=; b=fdjS6zb0MyC5Y5z57nEYYdajMUhieGjPXUMFEOooXhfjObZ9Q1o59qllPgA2Kyi7sc yMuSaRValPlKI6UalE/Dh2j92tg9+5MtAVyRBGrX96STR3VOIZxuQsiYrMumsLU3oATC HXbKalE5HohM1D/AeLc6Ah+JJ/odc6LV/kHoRGBzfS8vQu9fBE6xLu4hTbJC5rjKwL+F eW4y9ozxQOWB4gFN/gVV0Jb0djIoj6hxiS0SJgWDH8oACgzKzHu6G+GRhKvWdS35fb4w ClPPpRwxby5d+xlLqqoEwcq3NNezikEhxuUN6b5fF33DCJ4Hn+7O8IYyyzL9Ui1N0IwL 0iIQ== 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=Rce8u6K7LN/Um/IxRb+ZbL4sJ3RSDKU2XLCuNLPgW6Q=; b=TwfdDOfAJeF2Z15g2VoD3EapjIGZDQDnGB/2Q4VjUC9LEjnukcs44ZIApsKes0ABgv KGJCUKza/j46YPpD+VZ/9nw1DEoLewYvPruSlsXjwOOlf9Bov0XgcOluHjtKZoHNf0rz auH7d3iDd3GOoAx/0eUf5jk3EZbPr/ipRJtxAKXT0YDh36263O78W7NVz8dU+BKU20IW MvmS2/k2PoEwTv9KqPHhQUCz7az+V5V3Uh3ywIwCoGipX+ehuPacHOsfS0QsVydxyYjm 12U/c4qbdnNWBxPFeUVh0xSp+be952EejLczCR25Ef6Nynwtrd+P8wZWNAdNwKXuL617 gF0Q== X-Gm-Message-State: AOAM533r74+ZJH2kTDanUpaAG20G82QUw3eOjvQ3toxF/oJ9aPUxCeoV VUIQPeAM3GU0FxHPGQhYj+8/cyQbrpS0IY7s X-Received: by 2002:a2e:99c7:: with SMTP id l7mr11895084ljj.126.1639941758286; Sun, 19 Dec 2021 11:22:38 -0800 (PST) Received: from localhost.localdomain (84-217-56-54.customers.ownit.se. [84.217.56.54]) by smtp.gmail.com with ESMTPSA id c36sm191115lfv.193.2021.12.19.11.22.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Dec 2021 11:22:37 -0800 (PST) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Sun, 19 Dec 2021 20:21:28 +0100 Message-Id: <20211219192134.1296-8-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20211219192134.1296-1-dcnieho@gmail.com> References: <20211219192134.1296-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 07/13] 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: L4k7ESyXv0PC Needed to enable programmatic discovery of DirectShow devices Signed-off-by: Diederick Niehorster --- libavdevice/dshow.c | 80 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c index 0ef3b3d13e..8c257ca8e7 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,6 +241,7 @@ 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); @@ -282,11 +286,39 @@ dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum, // 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 fail1; + + device->device_name = av_strdup(friendly_name); + device->device_description = av_strdup(unique_name); + if (!device->device_name || !device->device_description) + goto fail1; + + // store to device_list output + if (av_reallocp_array(&(*device_list)->devices, + (*device_list)->nb_devices + 1, + sizeof(*(*device_list)->devices)) < 0) + goto fail1; + (*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: + if (device) { + if (device->device_name) + av_freep(&device->device_name); + if (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, };