Subject: [PATCH] Use appropriate method for device discovery.

From: Romain Beauxis <[email-masked]>
Date: Fri, 14 Jan 2022 10:06:08 -0600

Signed-off-by: Romain Beauxis <[email-masked]>
---
 libavdevice/avfoundation.m | 77 ++++++++++++++++++++++++++++----------
 1 file changed, 57 insertions(+), 20 deletions(-)

diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m
index 5d013cc0eb..03ec49df86 100644
--- a/libavdevice/avfoundation.m
+++ b/libavdevice/avfoundation.m
@@ -27,6 +27,7 @@
 
 #import <AVFoundation/AVFoundation.h>
 #import <CoreMedia/CoreMedia.h>
+#include <Availability.h>
 
 #include "libavutil/channel_layout.h"
 #include "libavutil/pixdesc.h"
@@ -698,8 +699,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];
 
@@ -707,6 +734,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;
@@ -734,8 +776,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];
             const char *uniqueId = CLEANUP_DEVICE_ID([device uniqueID]);
             int index            = [devices indexOfObject:device];
@@ -885,9 +926,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;
         }
@@ -898,22 +937,20 @@ 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) {
-            const char *name = [[device localizedName] UTF8String];
-            if (!strncmp(ctx->audio_filename, name, 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;
+                }
 
-            const char *uniqueId = CLEANUP_DEVICE_ID([device uniqueID]);
-            if (!strncmp(ctx->audio_filename, uniqueId, strlen(ctx->audio_filename))) {
-                audio_device = device;
-                break;
+                const char *uniqueId = CLEANUP_DEVICE_ID([device uniqueID]);
+                if (!strncmp(ctx->audio_filename, uniqueId, strlen(ctx->audio_filename))) {
+                    audio_device = device;
+                    break;
+                }
             }
         }
-        }
 
         if (!audio_device) {
             av_log(ctx, AV_LOG_ERROR, "Audio device not found\n");
-- 
2.32.0 (Apple Git-132)