From patchwork Mon Jun 7 23:03:45 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: 28152 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:b214:0:0:0:0:0 with SMTP id b20csp3889603iof; Mon, 7 Jun 2021 16:10:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxAAq1uWvSd7AvF0I9jspfRaGZOaZai2fitxk8/ukfIMmSnQIaNpCHWofKPmGjbdxE4xRcE X-Received: by 2002:a17:906:3b87:: with SMTP id u7mr20486231ejf.548.1623107417370; Mon, 07 Jun 2021 16:10:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623107417; cv=none; d=google.com; s=arc-20160816; b=yKfXz1BaqflGrxoETmnXSKFgDckUYtUz7oiohsSo07grlHRngfnq0zlaGIYi2dbpkX tZteI9OH2ikYY9a+CM4+ACi+PLT1EF0mITfrd+zDQYORDiumAW37bJ4nB4aZgWImM5Hp V9yk+ZrZ29ECMreCu3ovLVH2KQ62kOx9dtzQVd0lsZHvurMmALg7N+ERUfCCE8ANE9iW 9KjsiCdT7BW2a7sU/LMnE73BMUq2ySyTotvfcGEk/Jlip1Fnw19zzRX+WGTgZjZ/HIcS cV1Y1mwE+n2BHgCDbTfZ3159B7Su2/qQ//kj27IOUVmXtHp3KTrhH01jQ4mwmsbnkdKT cYZg== 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=tRdnltGSSWoMw9z882PquprilgkXdkVePSnsasPeAQmhMwIhwbOnIrLNJwibkxsj6Y osd0L068j/L+OFEQdn0++0cpeSV92IHaEFx8G+yzLCAEI0/oudZAyyvLAAV3XB7t5GQk LeyY9RhbEwO2I697qNmFF2rzQikhjPUtumGrd+GhnRNkJ9ufN7zMdrLMmKACDYbzOmHh YHtWjCBHSQHFQy2p3dbpwOjXzAAVi63Y8rjWhIFzWx8xoPgqfOj3vAh5giQcUw3k4O5E huSw5uE6CHL4M7XjaGdSC7ZOTm6E7aWY1Vm9zcEAy9c4t7WNlzdhFFG0ESDrxFDhpST5 grMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b="F/FOtq9n"; 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 e1si12566510edk.506.2021.06.07.16.10.17; Mon, 07 Jun 2021 16:10:17 -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="F/FOtq9n"; 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 0DF5F6802C7; Tue, 8 Jun 2021 02:10:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C471D6802C7 for ; Tue, 8 Jun 2021 02:10:13 +0300 (EEST) Received: by mail-ed1-f51.google.com with SMTP id r7so7941663edv.12 for ; Mon, 07 Jun 2021 16:10:13 -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=F/FOtq9nFWwI4v4RA375IPQjiarI3rkTav9BfmBI8dznMxuH/hJgbNEUp7zDdPg4qk Agl1BTZVr3FhU3Rfll7aiRUXe12UOJttk1YJPG2T2DUEEMMeUJCdi5YBQC0txmeo8e1X R0iIFHsHhLsvCCfQ3wGQEKNQnUeYqAa2mZOdlnL1e/JdfXn6Ct+zEFJi5YWPW0l8+fFh w/TDbpMtmlgPCFxrcMP/fnxNhFZ7nl1qdCKiM4zq02qeO1R1mIeR5L4XTguoHIAnOikA FRHsmMCwPyFPoRPHIx/QG4S/xFTVFJq6wpKLVDoTYlLQpkZYniqX8YbHeOkRcwHvl4gl 9cZA== 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=nxmjiSRjO5jFlYXmTL87Pu8iWxuV/pfxn1BGOJQxRvmmY4GWMw8LsU/0YWTkjIQlhe pBn1tsm9EbjpySeJXHCJqx+9YLE3JEjKyGVagzy58/XLZihcFQbiqzmbDP7rSM2oRZxQ F6rosCfo6GsxzuChRrzj3yftfZnHnR3rkG/hciycvpnTFVpu/nsnSn8BLIUSt/YHO5BZ BgcGd0xZtTIisS746pEPHPrFyJ5NEvLz4JbVD3WUc2kpRln1LMLrIs6thv12UHNTKz7O uByQv7VR3JBUekkAhrKd9G0fd17pWKbEcsozwn7NqktC+hvjrtFXsz7KJxTj/TTGzoMH U0SA== X-Gm-Message-State: AOAM530iNUJ0hjSdVWr4OWQZDvq8E1U9hoXivctApS6xSix85dLIZ3cL aTiirSHkVeCOzOgIBXVMK5Och/WcycSykw== X-Received: by 2002:a05:6512:3054:: with SMTP id b20mr5926087lfb.375.1623107072678; Mon, 07 Jun 2021 16:04:32 -0700 (PDT) Received: from localhost.localdomain (84-217-56-54.customers.ownit.se. [84.217.56.54]) by smtp.gmail.com with ESMTPSA id v9sm1999563ljv.131.2021.06.07.16.04.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 16:04:32 -0700 (PDT) From: Diederick Niehorster To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Jun 2021 01:03:45 +0200 Message-Id: <20210607230414.612-7-dcnieho@gmail.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20210607230414.612-1-dcnieho@gmail.com> References: <20210607230414.612-1-dcnieho@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 06/35] 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: xr9xNwaIiweG 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, };