Skip to content

Commit

Permalink
macOS で USB で接続されたカメラを取得するように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
torikizi committed Jul 11, 2024
1 parent b83b0ee commit b4af78f
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/mac_helper/mac_capturer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ - (void)capturer:(RTCVideoCapturer*)capturer
return selectedFormat;
}

static NSArray<AVCaptureDevice*>* captureDevices() {
if (@available(macOS 14, *)) {
// macOS 14 以上では、新しい API を使用して外部カメラも取得する
AVCaptureDeviceDiscoverySession *session = [AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:@[
AVCaptureDeviceTypeBuiltInWideAngleCamera,
AVCaptureDeviceTypeExternal ]
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified];
return session.devices;
} else {
// macOS 13 以下では、古い API を使用して内蔵カメラのみ取得する
return [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
}
}

} // namespace

MacCapturer::MacCapturer(size_t width,
Expand Down Expand Up @@ -100,7 +116,7 @@ - (void)capturer:(RTCVideoCapturer*)capturer
// https://www.ffmpeg.org/ffmpeg-devices.html#avfoundation

size_t capture_device_index = SIZE_T_MAX;
NSArray<AVCaptureDevice*>* devices = [RTCCameraVideoCapturer captureDevices];
NSArray<AVCaptureDevice*>* devices = captureDevices();
[devices enumerateObjectsUsingBlock:^(AVCaptureDevice* device, NSUInteger i,
BOOL* stop) {
// 便利なのでデバイスの一覧をログに出力しておく
Expand Down Expand Up @@ -136,8 +152,7 @@ - (void)capturer:(RTCVideoCapturer*)capturer
}

if (capture_device_index != SIZE_T_MAX) {
AVCaptureDevice* device = [[RTCCameraVideoCapturer captureDevices]
objectAtIndex:capture_device_index];
AVCaptureDevice* device = [devices objectAtIndex:capture_device_index];
RTC_LOG(LS_INFO) << "selected video device: [" << capture_device_index
<< "] device_name=" << [device.localizedName UTF8String];
return device;
Expand Down

0 comments on commit b4af78f

Please sign in to comment.