From patchwork Tue Mar 22 13:39:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Romain Beauxis X-Patchwork-Id: 34893 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:ab0:5fda:0:0:0:0:0 with SMTP id g26csp680103uaj; Tue, 22 Mar 2022 06:40:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCS3AWRgpQnXOR9KOWt7tGcnJt6wF5prFjHBbyVUVXpaZENKYbm6TIVU39PcrKfK10bUVO X-Received: by 2002:a17:907:8d17:b0:6df:c072:2f66 with SMTP id tc23-20020a1709078d1700b006dfc0722f66mr18028111ejc.444.1647956441930; Tue, 22 Mar 2022 06:40:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647956441; cv=none; d=google.com; s=arc-20160816; b=e4SKovFajd6NlZWskD9B0BXdop2kDuEd27p8D7DPOFFwM4zK0kVmtbQK86x3l93uHL GCN/zvzAQ/bT1nbHHzg3rTbRyMzvgy4Fb0e/CSGNiLJ27jIQKFVGtqQqS8AuJPrqOV5O R2WCoW/bpoPQVB/c3HHzbte/pYdWo6tLP+z8i4zJ10358cpSV8UJDFpdBBTHWjltBoGV L3cQ/hboUR+ZHKdleDLMINGfB4o1WzVCPdwztCfiXBzpEdfHv4YpK2yLjJH3tjzn7Bgj ETj61QHOL4KkhK3LYj+zFe/2oupz01DVE0jpjV434oDPmQTuS/0H+Ut6i469qho7ttLh qgsA== 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=QKoAAvXU8DfellwxFZrCQwqYrhY7CSVwIlmmyOxVKdc=; b=W+mPbAwXVclDnql6bapYNrt2x5ItAmVLmlN5uFzNPsX0qZtDWvWCkPbIXbdrkXshNi xFIDfgOLJs3I0rvjk5DdnAuTYTMIvje3qhxNLtfNLF4ZJz3P81mYqcBD6f730kHHS+wW KBa21PCCEaRxYg00nXzmWZuVNpo3fmkOtTytTTolvQnkM5S9G/64DzUXgYwEwsCHDG4T ZakLuiIodvt+wW3HoAYeV475UPzfX+dJXXmJQAcETB2Q5BFjxIcpDS8Y6pLuSGe/ZwRA WfVwTDIUB8f4Ns2bADGVlTnFF6+ii4G3fZRzP2uECVbll2K6cU5u2sIVTdEgsDzsSN6n AXlg== 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 c1-20020a50e3c1000000b00418c2b5bf72si14657040edm.596.2022.03.22.06.40.41; Tue, 22 Mar 2022 06:40:41 -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; 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 EBDDC68B00F; Tue, 22 Mar 2022 15:40:26 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BB85668B0F6 for ; Tue, 22 Mar 2022 15:40:20 +0200 (EET) Received: by mail-ej1-f51.google.com with SMTP id pv16so36343008ejb.0 for ; Tue, 22 Mar 2022 06:40:20 -0700 (PDT) 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=/PO9ettZfVYrmzd6zHZ9yogWaJS/iD4eon3KI/ITwDk=; b=0X6FtOptc9qb8L7nJuUwRMcoo7z6c0aUajysloHzDKCoLTwWE4f8lU1SIGFNgRovGL TzXH33qMMnM9VRq2Hq4VqhGvtvQ0kLml9e3VMRddlYsTl7n9peoamgLET2RXdrT1R/Wk n2L1I+F2lr0Ddlw9Q87MDcOS7XZ1WOCG1WBVhglllZLZxmMUBhkw1PcNO2fH7W085/Tg rcu013/7CKpzKVnaAm8315Ud7kNEULASbh11rQcI6lJAzUmlbctjgbwG8w+wxotoarqW 7U0OT4xReeYvubGi9TgeO5vCXiWyx0rJQH16guKbDaTLRyzx1fI64ld4sb5Q5jlFCCCH 5a8A== X-Gm-Message-State: AOAM530z2yj6+0J6os0F5ZNr/F1laQs5cAY8CJWR7zZRFzbJqqiEunvN S3DNgvjcAKp2d5NsgCSxcMuuC0KQ61RZH4QA X-Received: by 2002:a17:907:7fa2:b0:6d8:2397:42 with SMTP id qk34-20020a1709077fa200b006d823970042mr25692212ejc.218.1647956419747; Tue, 22 Mar 2022 06:40:19 -0700 (PDT) Received: from localhost.localdomain (82-171-134-41.fixed.kpn.net. [82.171.134.41]) by smtp.gmail.com with ESMTPSA id z6-20020a056402274600b004194fc1b7casm2125829edd.48.2022.03.22.06.40.18 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 22 Mar 2022 06:40:19 -0700 (PDT) From: toots@rastageeks.org To: ffmpeg-devel@ffmpeg.org Date: Tue, 22 Mar 2022 14:39:53 +0100 Message-Id: <20220322133957.81743-3-toots@rastageeks.org> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220322133957.81743-1-toots@rastageeks.org> References: <20220322133957.81743-1-toots@rastageeks.org> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/6] 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: Romain Beauxis Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: TnEr63s9YzzY From: Romain Beauxis diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m index c9de93f774..719276cabf 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" @@ -771,8 +772,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]; @@ -782,6 +809,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; @@ -807,8 +849,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); @@ -833,9 +874,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 @@ -931,9 +975,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; } @@ -944,15 +986,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");