From patchwork Fri Jun 11 20:30:37 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: 28209 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5e:c91a:0:0:0:0:0 with SMTP id z26csp763091iol; Fri, 11 Jun 2021 13:33:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvDv8tK239/KGIY/JgjbsTcdBPQtu2m16O8o/ZIOHrnPJAbyQEE2yCPnNWjTYKlNXXM1Dy X-Received: by 2002:a17:906:63d2:: with SMTP id u18mr5252458ejk.186.1623443606727; Fri, 11 Jun 2021 13:33:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623443606; cv=none; d=google.com; s=arc-20160816; b=Vvg32kL5hnPk2NwHP1VWRuTmW4frMObECP1M7hdof+0U7/uSBkrmZJL8gNhunYBTma gvOcUogtDD5nGCZy+QYiyCTjL5iMGqsycRkH3xfgaEGWzDPK9suu11i4+j0vTIkonF8P IaUoRFvvec/QcRFazZCndiHSG1+1JRdDjiFNtqXeTcYIERudndStNCWoYw9ayZ7h1r0m EPewGI6KNPMPKo71GidFGNHA+qWs0jEaC/WDkWY7klwzfQVg0P6mrRAv3iE0sYt8OPSm tEPtA3dIVMKT4D0Jt4Td3Yek1ER1kHDjj1uuu1NcwP4+KWntKdvh+wkZNMcLIrAX7bmH MYLg== 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=Eqj7QXhjgnZDD83bsooYF65FfmZ/rOuDozjb/N/cT5d+vPEqhD/uChP+WFqhnTdZ6x JGm2Vx3nIt3S9xWzi7kUBOJBNloQhKi+Qh1vr4QcFNXqNUkZ3ahePbaer1GuVLJwhkRP 853w4huxDVDqJ28jsMGm1bZapqUm/A0BqH49RpWuhZpcJb7Kjgf8f7mn0lMhLS5nb/gt HLbKt9GgqdVgki4+6XCMFt3kZHf4KgdMKYZzlonpr/ojHDDBfQjaNjowM8ZHM5OhNXqx bbosEnjzUOZegiuQ8jYWBAVmxNy/XsjuScBdAZxdBpgMMnOMrS6Jn3MiNLLt/UcXvQMs nDvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=Z85hLjPe; 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 g4si5777009ejo.319.2021.06.11.13.33.25; Fri, 11 Jun 2021 13:33:26 -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=Z85hLjPe; 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 681C0680331; Fri, 11 Jun 2021 23:33:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3ED1D680CED for ; Fri, 11 Jun 2021 23:33:20 +0300 (EEST) Received: by mail-lj1-f177.google.com with SMTP id 131so11325209ljj.3 for ; Fri, 11 Jun 2021 13:33:20 -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=Z85hLjPe00d/oK9xv11/3fAzd84zUjpmbXg/Y31UWrfUqM7DW6BHuBW2he6mj2bicF 9LBcBpBm0Bd2MGTotLGWYDD1/OGlUNy7/x3oyXJ4kF9mhELpdxX60FG5RZC94Tqb9tcP tK71MOfn88+xh3g9MZRxQDFkpcg4KmpEWJkUtrmtayQtfGlVgZ7fInQ9o8ylj6EsEZCX sfsyfDY/sT0GlPBKz8ZIRrut9IkOfqthMXni2YH35xuHVIt40qgsSIYxn6wtBlRlucMu HsY0r9tFwfGUK2Lwtxyl6TXM68D+GPxTiFDGlQygnszKbWC5j8G5AW9wufDkDSeJU5Tw ca7w== 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=Z+QiXinDHPIwYsHyrKvKFEoW4Hb3XycgasbESfECZotyv6WJYs7/uRqIWOqhJ6hFMj zvx90dmhk52hVB3l9nfKTsH5JUW5IvfU0X771vBrSF2JZrMT4VPbdJ7sWWIiSPOBnBIa u8SGWz1dioEkTNW3ZfVINGloTavAGkZkMIaO5/XGI2faqdgu/d8XBWgqk7Xqe7lXrbAO fZSNQgHCKDxe9FQpazaaxUf71NGObmQT/XxQuAd9d0q52b0rLcQlkOKxTrZ5eeNdqj1S +EErigT6D5/P65XUYPDkM62omzE4dO3OzhF8V0yHvX2DS5bu6u0UMPe9NbBao89YTFWb dyPA== X-Gm-Message-State: AOAM531sxtShkqzuqVlJ1necLPW9lcshq3K8WHgV1fhrRq4/5wRJEoB+ HLkdWuDyiVJhXd472aRm5rAkzbr0ZEJjnA== X-Received: by 2002:a2e:b01a:: with SMTP id y26mr4399238ljk.278.1623443599680; Fri, 11 Jun 2021 13:33:19 -0700 (PDT) Received: from localhost.localdomain ([196.196.203.214]) by smtp.gmail.com with ESMTPSA id l17sm685055lfc.238.2021.06.11.13.33.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Jun 2021 13:33:19 -0700 (PDT) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Fri, 11 Jun 2021 22:30:37 +0200 Message-Id: <20210611203104.1692-7-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20210611203104.1692-1-dcnieho@gmail.com> References: <20210611203104.1692-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 06/33] 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: JB9pGODf80DA 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, };