From patchwork Sun Jan 30 17:30:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Romain Beauxis X-Patchwork-Id: 33943 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp2123308iov; Sun, 30 Jan 2022 09:33:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJyMWtUo2I5mb9fK7PqXaXVtuQFM6eD0xeEpEKRtznwMvvZjl7BpVEQ1n/UFIQeizBCw6k03 X-Received: by 2002:aa7:d459:: with SMTP id q25mr17015784edr.212.1643563990436; Sun, 30 Jan 2022 09:33:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643563990; cv=none; d=google.com; s=arc-20160816; b=oSQzVLQdt3SFjWzYtR/trAvZYlvAjjNfQNmULixXlolLigZdwq83EwqOYiRm+aAzvu 1HsZgKAjEU0rlr6jWaqafkli0euQ7WEVKu7Zila/UcR8tn8agCsgTmuXI8QXWbsXCBO8 I8jEn6nljB08mGJB/m1ll36dvN9NTHeS+1RxiqFs2DD2xrFy4BloyXw+Lu+14dPtm66N 8yQrb8IGDWusNY01L9wcWHu9bOEeWDVk/mWqlEuRuxpNJbWUgIIwD6nNnzx72oKpivmw 6Zo9Y4PnsPpPV9tR8AdkOxRY5qS5vDWO+vmoFFKc7Lryzr7aR387FMWZEtIi4RqsLbpt vtmg== 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:delivered-to; bh=lkgqSPA7cLT0fJ5OU8a89S4ZQwdugluaRDBgjvYnjho=; b=sWmTVhq+/5BShPWm9GX/Z1tYy7h5hvrd6/zjdOjLwpvtnyCcnoLsxc8RFb4rqT1XSk +CiOSYwl2/FDzChs2sW1eCxRzqbscddqVjqGYDvA59vWvTp4W55estYZFcnLAGDUPkZ4 BODFa0InS6GRtzdqXyb8E/xc6j7ptnxW73Msw+vXdbFmAPFOVW94x3HsOSKZrWbj6Cst f0AcvKcXBQ/gqPzdm81tlxm6Jhx7N2OMeAF16uupG2vPeCAO5uDtNjvF9g+Z9I80hghe jyfLl0KYOm2PixZzrg5MJE1ryjMwiEj4hiXT+KFKnLRTWnYyCLCOsZwJBRKHzEsGPWlF wxWQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ho35si6772780ejc.428.2022.01.30.09.32.45; Sun, 30 Jan 2022 09:33:10 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3BD9A68B22E; Sun, 30 Jan 2022 19:32:43 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ot1-f53.google.com (mail-ot1-f53.google.com [209.85.210.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9DB24680394 for ; Sun, 30 Jan 2022 19:32:36 +0200 (EET) Received: by mail-ot1-f53.google.com with SMTP id i16-20020a056830011000b005a3cc8d20fbso4454860otp.9 for ; Sun, 30 Jan 2022 09:32:36 -0800 (PST) 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=5T9c2ukazCBvlJxjdb0Jd0ZLpnJswDwPPGXlRDcnbk8=; b=7bYlmspdY5wGxLPdP1dX+ZKDTs5EizAQ6yA5f56RsInNWRvVAuJBflttZCTl7GZ1vX Rx1+YQlKd1BHHoAC9Av6NEwY3O1/+m9X6wGnRbCVceJuWwdiCq3G29h+IlEoY0ee+UFC cUkZ6wYMwS6yaCDIO6utwxcHQElN7HGFtlStDtafH/T3Wo0YK9n+2E4/fl7B6mQUJoW8 2v+Zq/ia9CNlkJzh9xhTIvOKbfEkJTBPfeXxgX8cQbsX68iyqw/v8YKL+75qLkI2OuNW hY4HTk1DDlW/2f5V1qBzkU6XQNtBEH960PWqEr2AOLbZYZPdTfevDWFLLs5p4Zr00KwU GScA== X-Gm-Message-State: AOAM5308aA7r10LelHukh4eVUsP8EUrMEfzbmPu2bCueg3fctJjkxlcm 6Yde5Votw3c8ceTrq0TlFYBwmNiC1TI9tke9 X-Received: by 2002:a9d:5ccb:: with SMTP id r11mr9355924oti.263.1643563954342; Sun, 30 Jan 2022 09:32:34 -0800 (PST) Received: from localhost.localdomain (wsip-98-173-234-196.no.no.cox.net. [98.173.234.196]) by smtp.gmail.com with ESMTPSA id 71sm8303211otn.43.2022.01.30.09.32.32 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 30 Jan 2022 09:32:33 -0800 (PST) From: toots@rastageeks.org To: ffmpeg-devel@ffmpeg.org Date: Sun, 30 Jan 2022 11:30:43 -0600 Message-Id: <20220130173045.32690-2-toots@rastageeks.org> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220130173045.32690-1-toots@rastageeks.org> References: <20220130173045.32690-1-toots@rastageeks.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/4] Use appropriate method for device discovery, fix crash with bogus device index. 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: thilo.borgmann@mail.de, Romain Beauxis , epirat07@gmail.com Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: /bcDa1N8sGD6 From: Romain Beauxis This updates the code for avfoundation to use modern device lookup APIs and also adds a check to avoid querying the video devices array beyound its maximum size. --- libavdevice/avfoundation.m | 71 ++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m index 0cd6e646d5..d8bcd98f81 100644 --- a/libavdevice/avfoundation.m +++ b/libavdevice/avfoundation.m @@ -27,6 +27,7 @@ #import #include +#include #include "libavutil/channel_layout.h" #include "libavutil/pixdesc.h" @@ -764,8 +765,34 @@ static int avf_read_header(AVFormatContext *s) AVCaptureDevice *video_device = nil; AVCaptureDevice *audio_device = nil; // Find capture device - NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; - NSArray *devices_muxed = [AVCaptureDevice devicesWithMediaType:AVMediaTypeMuxed]; +#if defined(__MAC_10_15) || (TARGET_OS_IPHONE && defined(__IPHONE_10_0)) + AVCaptureDeviceDiscoverySession *discoverySession = + [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[ +#if TARGET_OS_IPHONE + AVCaptureDeviceTypeBuiltInDualCamera, + AVCaptureDeviceTypeBuiltInDualWideCamera, + AVCaptureDeviceTypeBuiltInUltraWideCamera, + AVCaptureDeviceTypeBuiltInTrueDepthCamera, + AVCaptureDeviceTypeBuiltInTelephotoCamera, +#endif + AVCaptureDeviceTypeBuiltInWideAngleCamera, + AVCaptureDeviceTypeExternalUnknown + ] + mediaType:NULL + position:AVCaptureDevicePositionUnspecified]; + + NSMutableArray *devices = [NSMutableArray array]; + NSMutableArray *devices_muxed = [NSMutableArray array]; + for (AVCaptureDevice *device in [discoverySession devices]) { + if ([device hasMediaType:AVMediaTypeVideo]) + [devices addObject:device]; + else if ([device hasMediaType:AVMediaTypeMuxed]) + [devices_muxed addObject:device]; + } +#else + NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; + NSArray *devices_muxed = [AVCaptureDevice devicesWithMediaType:AVMediaTypeMuxed]; +#endif ctx->num_video_devices = [devices count] + [devices_muxed count]; @@ -775,6 +802,21 @@ static int avf_read_header(AVFormatContext *s) CGGetActiveDisplayList(0, NULL, &num_screens); #endif + NSArray *audio_devices; +#if defined(__MAC_10_15) || (TARGET_OS_IPHONE && defined(__IPHONE_10_0)) + discoverySession = + [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[ + AVCaptureDeviceTypeBuiltInMicrophone, + AVCaptureDeviceTypeExternalUnknown + ] + mediaType:AVMediaTypeAudio + position:AVCaptureDevicePositionUnspecified]; + + audio_devices = [discoverySession devices]; +#else + audio_devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio]; +#endif + // List devices if requested if (ctx->list_devices) { int index = 0; @@ -800,8 +842,7 @@ static int avf_read_header(AVFormatContext *s) #endif av_log(ctx, AV_LOG_INFO, "AVFoundation audio devices:\n"); - devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio]; - for (AVCaptureDevice *device in devices) { + for (AVCaptureDevice *device in audio_devices) { const char *name = [[device localizedName] UTF8String]; int index = [devices indexOfObject:device]; av_log(ctx, AV_LOG_INFO, "[%d] %s\n", index, name); @@ -824,9 +865,12 @@ static int avf_read_header(AVFormatContext *s) if (ctx->video_device_index < ctx->num_video_devices) { if (ctx->video_device_index < [devices count]) { video_device = [devices objectAtIndex:ctx->video_device_index]; - } else { + } else if (ctx->video_device_index - [devices count] < [devices_muxed count]) { video_device = [devices_muxed objectAtIndex:(ctx->video_device_index - [devices count])]; ctx->video_is_muxed = 1; + } else { + av_log(ctx, AV_LOG_ERROR, "Invalid video device index\n"); + goto fail; } } else if (ctx->video_device_index < ctx->num_video_devices + num_screens) { #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 @@ -922,9 +966,7 @@ static int avf_read_header(AVFormatContext *s) // get audio device if (ctx->audio_device_index >= 0) { - NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio]; - - if (ctx->audio_device_index >= [devices count]) { + if (ctx->audio_device_index >= [audio_devices count]) { av_log(ctx, AV_LOG_ERROR, "Invalid audio device index\n"); goto fail; } @@ -935,15 +977,14 @@ static int avf_read_header(AVFormatContext *s) if (!strncmp(ctx->audio_filename, "default", 7)) { audio_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio]; } else { - NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio]; - - for (AVCaptureDevice *device in devices) { - if (!strncmp(ctx->audio_filename, [[device localizedName] UTF8String], strlen(ctx->audio_filename))) { - audio_device = device; - break; + for (AVCaptureDevice *device in audio_devices) { + const char *name = [[device localizedName] UTF8String]; + if (!strncmp(ctx->audio_filename, name, strlen(ctx->audio_filename))) { + audio_device = device; + break; + } } } - } if (!audio_device) { av_log(ctx, AV_LOG_ERROR, "Audio device not found\n");