MacOS开发

ios/macOS 视频采集-设备(3)

2021-04-06  本文已影响0人  yxibng

参考demo AppleVideoCapturer

获取设备

//video capture device with specified positon
+ (AVCaptureDevice *)videoCaptureDeviceWithPosition:(AVCaptureDevicePosition)position
{
    AVCaptureDevice *videoDevice;
#if TARGET_OS_IOS
    
    if (@available(iOS 10.0, *)) {
        NSArray<AVCaptureDeviceType> *deviceType_13_0;
        if (@available(iOS 13.0, *)) {
            deviceType_13_0 = @[AVCaptureDeviceTypeBuiltInUltraWideCamera,
                                AVCaptureDeviceTypeBuiltInDualWideCamera,
                                AVCaptureDeviceTypeBuiltInTripleCamera];
        } else {
            deviceType_13_0 = @[];
        }
        
        NSArray<AVCaptureDeviceType> *deviceType_11_1;
        if (@available(iOS 11.1, *)) {
            deviceType_11_1 = @[AVCaptureDeviceTypeBuiltInTrueDepthCamera];
        } else {
            deviceType_11_1 = @[];
        }
        
        NSArray<AVCaptureDeviceType> *deviceType_10_2;
        if (@available(iOS 10.2, *)) {
            deviceType_10_2 = @[AVCaptureDeviceTypeBuiltInDualCamera];
        } else {
            deviceType_10_2 = @[];
        }
        
        NSArray<AVCaptureDeviceType> *deviceType_10_0;
        if (@available(iOS 10.0, *)) {
            deviceType_10_0 = @[AVCaptureDeviceTypeBuiltInWideAngleCamera,
                                AVCaptureDeviceTypeBuiltInTelephotoCamera];
        } else {
            deviceType_10_0 = @[];
        }
        
        NSArray *osTypes = @[deviceType_13_0, deviceType_11_1, deviceType_10_2, deviceType_10_0];
        NSMutableArray<AVCaptureDeviceType> *deviceTypes = [NSMutableArray array];
        for (NSArray *types in osTypes) {
            [deviceTypes addObjectsFromArray:types];
        }
        AVCaptureDeviceDiscoverySession *session = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:deviceTypes
                                                                                                          mediaType:AVMediaTypeVideo
                                                                                                           position:position];
        for (AVCaptureDevice *device in session.devices) {
            if (device.position == position) {
                videoDevice = device;
                break;
            }
        }
    } else {
        NSArray *cameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        for (AVCaptureDevice *device in cameras) {
            if (device.position == position) {
                videoDevice = device;
                break;
            }
        }
    }
#elif TARGET_OS_OSX
    videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
#endif
    return videoDevice;
}

添加视频采集设备

  1. 获取AVCaptureDevice
  2. AVCaptureDevice获取AVCaptureDeviceInput
  3. AVCaptureDeviceInput添加到AVCaptureSession
// Remove the existing device input first, because AVCaptureSession doesn't support
// simultaneous use of the rear and front cameras.
self.session.removeInput(self.videoDeviceInput)
if self.session.canAddInput(videoDeviceInput) {    
    self.session.addInput(videoDeviceInput)
    self.videoDeviceInput = videoDeviceInput
} else {
    self.session.addInput(self.videoDeviceInput)
}
上一篇下一篇

猜你喜欢

热点阅读