From patchwork Tue Jul 6 09:19:52 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: 28793 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5d:965a:0:0:0:0:0 with SMTP id d26csp4919869ios; Tue, 6 Jul 2021 02:22:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzYg3br+SIrFDVzhpTMw05QeUYV2KrdEEB9r5hboG8giHxu1ztqeG2Uk4J5DbF4JF20OVNv X-Received: by 2002:a17:907:60d4:: with SMTP id hv20mr17273853ejc.261.1625563332706; Tue, 06 Jul 2021 02:22:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625563332; cv=none; d=google.com; s=arc-20160816; b=u7AfpIpEGNG8tuj0NX6UfsI27flJV/6uzS91r/BNoJzWzE/lae3TBAz7YITuN96KYt fBl3m6sZQsfCAsGcZWTqQQOAzParAznVSgOMKheLsAD+HyMkHMgdsre5OneNfzDnvFSL NjvtGdmC1kTLUxvBR3Rl/H7hOAVlTIgbnZ+h2LS2qDkymDE7eLFFXgmwRncKDPxKoHWr LFPtaFTAyfyJ+fRapSQNMUPbaQMUv0kLr7TAagvM3Wo9+Rs9dzUwxg+9DeJM9cU7MFTD vtE4XQRh6/IQa/n+8DOkTJ/JQ0zu/e01hJlnI8YyphdGVCJBAArxEfLASlOkIxQpD1Rz y5nQ== 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=c58zCNFxeu/uoUmlzUOM1hC3IXccJZaE5xrwkjZOTxc=; b=Y+GaxMOwEw4R8hPWGdIw38e7rQe+BaZwjOFj88KkfYqXCYNteBevJL0z4wEpjlGaNQ xw2Zk0sO2AhfdlK5wwddHx1dryQ0PZGE1YP7EqOe4SNKc5BPU9nNCoc5FlX2MsTri4nH g2Q2uc4tmahOlYabA+ys2diRRIobUasBvQeX9XF1jw52YK8djxBPiAbnFkfJLbfkS5xX K7nuLvR9Wnuvimauuib96jO9/GC6bTGT0OimBCHEmirEii6QKXdhZQ8W7gdvN2IdAmub r4KHid2TRSpgHm49QbtFpr5a366miU5uXZg3edW3A4SoWQBaDeUUv/5t3WSrXhS0oZtc sVxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=eK5JJgpO; 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 nb24si16077598ejc.662.2021.07.06.02.22.12; Tue, 06 Jul 2021 02:22:12 -0700 (PDT) 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=20161025 header.b=eK5JJgpO; 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 781C368A723; Tue, 6 Jul 2021 12:22:06 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f169.google.com (mail-lj1-f169.google.com [209.85.208.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C387268A716 for ; Tue, 6 Jul 2021 12:22:04 +0300 (EEST) Received: by mail-lj1-f169.google.com with SMTP id w11so28322292ljh.0 for ; Tue, 06 Jul 2021 02:22:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BrTK3X3u/wxoTVDT2kQRQncJycwAhH4CuYt0VViKbQU=; b=eK5JJgpOUxpmsq95E1Mf93uwcPQ/NP1iyn6OYwybzxNLyaG2vy5Y6ydhTl4beO7R1b 5X2WS1mDnX2wl4W0oItBDRuQo48OVUKrGoHI47QIW+TDtFfp/cDZininJWUwxaShB7Px 5NXtGHxaHxXMI/VTu6fLqA3k3Ys8nF2RriEH2fUF9fI+LqGY73+zXWsfg81f/LRWMs59 hnNvMcA8cXtT0KGnIs89IFGz5InO4HaPH0T2GWKLoVb8zNVKaulT3sF11tIFNnThopNk gRWr6AJu0ncvGDu9PsqFH+aXhOF4Rx3HmV54CiE3nDbYKYrCrZCRw18mUovVxaD1Z3BP 4IxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BrTK3X3u/wxoTVDT2kQRQncJycwAhH4CuYt0VViKbQU=; b=Y7PvEf8gp0heWrfvz5q0akcLpOP1XaRxNNxxh7s99ovhp4zirfqWh9ZAJf2OZgcfXt SRC9a43I1s1mBduC1X8CyjeCxNUu69vMxysc38gvenRjdoEn0qoMJ19DUdkc6IsIIam3 s4s6BxMdf5oDo/lbMM7rGfal/3EoVLZdPdd3wsiixMg1fKA9l5G6UO5jq1F6F+bjgzOq lVqTvJdPPrK/9rGhA6GWcTGjf47qnlNSPo4bVGf/MG5Q32nj0GfbUIWm+twhMu9htegW R0jq/6uhLmuknQXa5s3fkdUUfVkAsuvBtkNDsby9DKhLgEh18VH9npHKFX1EEx6ydZjU u1Og== X-Gm-Message-State: AOAM531iwgvzfBF/gTRtn8fZuenST1knbFOnexGrnd2DLeNoyWAMxkX2 x5e65lLxvsUrNfVKKaVbcH9LwHXtK/s= X-Received: by 2002:a2e:8195:: with SMTP id e21mr10330219ljg.17.1625563323668; Tue, 06 Jul 2021 02:22:03 -0700 (PDT) Received: from localhost.localdomain (deedock.humlab.lu.se. [130.235.135.183]) by smtp.gmail.com with ESMTPSA id g22sm1338474lfr.227.2021.07.06.02.22.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jul 2021 02:22:03 -0700 (PDT) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Tue, 6 Jul 2021 11:19:52 +0200 Message-Id: <20210706092020.1057-7-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20210706092020.1057-1-dcnieho@gmail.com> References: <20210706092020.1057-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 06/34] 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: ysLeb2vSWCqM 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 389daa6380..25de6b1acd 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 @@ -713,7 +778,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; if ((r = dshow_cycle_pins(avctx, devtype, sourcetype, device_filter, NULL)) < 0) @@ -773,7 +838,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; } @@ -1130,9 +1195,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; } @@ -1336,6 +1401,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, };