OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "avfoundationvideocapturer.h" | 11 #include "avfoundationvideocapturer.h" |
12 | 12 |
13 #import <AVFoundation/AVFoundation.h> | 13 #import <AVFoundation/AVFoundation.h> |
14 #import <Foundation/Foundation.h> | 14 #import <Foundation/Foundation.h> |
15 #if TARGET_OS_IPHONE | 15 #if TARGET_OS_IPHONE |
16 #import <UIKit/UIKit.h> | 16 #import <UIKit/UIKit.h> |
17 #endif | 17 #endif |
18 | 18 |
19 #import "RTCDispatcher+Private.h" | 19 #import "RTCDispatcher+Private.h" |
20 #import "WebRTC/RTCLogging.h" | 20 #import "WebRTC/RTCLogging.h" |
21 #if TARGET_OS_IPHONE | 21 #if TARGET_OS_IPHONE |
22 #import "WebRTC/UIDevice+RTCDevice.h" | 22 #import "WebRTC/UIDevice+RTCDevice.h" |
23 #endif | 23 #endif |
24 | 24 |
25 #include "webrtc/base/bind.h" | 25 #include "webrtc/base/bind.h" |
26 #include "webrtc/base/checks.h" | 26 #include "webrtc/base/checks.h" |
27 #include "webrtc/base/thread.h" | 27 #include "webrtc/base/thread.h" |
28 #include "webrtc/common_video/include/corevideo_frame_buffer.h" | 28 #include "webrtc/common_video/include/corevideo_frame_buffer.h" |
29 | 29 |
30 // TODO(tkchin): support other formats. | 30 struct AVCaptureSessionPresetResolution { |
31 static NSString *const kDefaultPreset = AVCaptureSessionPreset640x480; | 31 NSString *sessionPreset; |
32 static NSString *const kIPhone4SPreset = AVCaptureSessionPreset352x288; | 32 int width; |
33 static cricket::VideoFormat const kDefaultFormat = | 33 int height; |
34 cricket::VideoFormat(640, | 34 }; |
35 480, | 35 |
36 cricket::VideoFormat::FpsToInterval(30), | 36 #if TARGET_OS_IPHONE |
37 cricket::FOURCC_NV12); | 37 static const AVCaptureSessionPresetResolution kAvailablePresets[] = { |
38 // iPhone4S is too slow to handle 30fps. | 38 { AVCaptureSessionPreset352x288, 352, 288}, |
39 static cricket::VideoFormat const kIPhone4SFormat = | 39 { AVCaptureSessionPreset640x480, 640, 480}, |
40 cricket::VideoFormat(352, | 40 { AVCaptureSessionPreset1280x720, 1280, 720}, |
41 288, | 41 { AVCaptureSessionPreset1920x1080, 1920, 1080}, |
42 cricket::VideoFormat::FpsToInterval(15), | 42 }; |
43 cricket::FOURCC_NV12); | 43 #else // macOS |
44 static const AVCaptureSessionPresetResolution kAvailablePresets[] = { | |
45 { AVCaptureSessionPreset320x240, 320, 240}, | |
46 { AVCaptureSessionPreset352x288, 352, 288}, | |
47 { AVCaptureSessionPreset640x480, 640, 480}, | |
48 { AVCaptureSessionPreset960x540, 960, 540}, | |
49 { AVCaptureSessionPreset1280x720, 1280, 720}, | |
50 }; | |
51 #endif | |
52 | |
53 // Mapping from cricket::VideoFormat to AVCaptureSession presets. | |
54 static NSString *GetSessionPresetForVideoFormat( | |
55 const cricket::VideoFormat& format) { | |
56 for (const auto preset : kAvailablePresets) { | |
57 // Check both orientations | |
58 if ((format.width == preset.width && format.height == preset.height) || | |
59 (format.width == preset.height && format.height == preset.width)) { | |
60 return preset.sessionPreset; | |
61 } | |
62 } | |
63 // If no matching preset is found, use a default one. | |
64 return AVCaptureSessionPreset640x480; | |
65 } | |
44 | 66 |
45 // This class used to capture frames using AVFoundation APIs on iOS. It is meant | 67 // This class used to capture frames using AVFoundation APIs on iOS. It is meant |
46 // to be owned by an instance of AVFoundationVideoCapturer. The reason for this | 68 // to be owned by an instance of AVFoundationVideoCapturer. The reason for this |
47 // because other webrtc objects own cricket::VideoCapturer, which is not | 69 // because other webrtc objects own cricket::VideoCapturer, which is not |
48 // ref counted. To prevent bad behavior we do not expose this class directly. | 70 // ref counted. To prevent bad behavior we do not expose this class directly. |
49 @interface RTCAVFoundationVideoCapturerInternal : NSObject | 71 @interface RTCAVFoundationVideoCapturerInternal : NSObject |
50 <AVCaptureVideoDataOutputSampleBufferDelegate> | 72 <AVCaptureVideoDataOutputSampleBufferDelegate> |
51 | 73 |
52 @property(nonatomic, readonly) AVCaptureSession *captureSession; | 74 @property(nonatomic, readonly) AVCaptureSession *captureSession; |
53 @property(nonatomic, readonly) dispatch_queue_t frameQueue; | 75 @property(nonatomic, readonly) dispatch_queue_t frameQueue; |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
354 #pragma mark - Private | 376 #pragma mark - Private |
355 | 377 |
356 - (BOOL)setupCaptureSession { | 378 - (BOOL)setupCaptureSession { |
357 AVCaptureSession *captureSession = [[AVCaptureSession alloc] init]; | 379 AVCaptureSession *captureSession = [[AVCaptureSession alloc] init]; |
358 #if defined(__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0 | 380 #if defined(__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0 |
359 NSString *version = [[UIDevice currentDevice] systemVersion]; | 381 NSString *version = [[UIDevice currentDevice] systemVersion]; |
360 if ([version integerValue] >= 7) { | 382 if ([version integerValue] >= 7) { |
361 captureSession.usesApplicationAudioSession = NO; | 383 captureSession.usesApplicationAudioSession = NO; |
362 } | 384 } |
363 #endif | 385 #endif |
364 NSString *preset = kDefaultPreset; | |
365 #if TARGET_OS_IPHONE | |
366 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { | |
367 preset = kIPhone4SPreset; | |
368 } | |
369 #endif | |
370 if (![captureSession canSetSessionPreset:preset]) { | |
371 RTCLogError(@"Session preset unsupported."); | |
372 return NO; | |
373 } | |
374 captureSession.sessionPreset = preset; | |
375 | 386 |
376 // Add the output. | 387 // Add the output. |
377 AVCaptureVideoDataOutput *videoDataOutput = [self videoDataOutput]; | 388 AVCaptureVideoDataOutput *videoDataOutput = [self videoDataOutput]; |
378 if (![captureSession canAddOutput:videoDataOutput]) { | 389 if (![captureSession canAddOutput:videoDataOutput]) { |
379 RTCLogError(@"Video data output unsupported."); | 390 RTCLogError(@"Video data output unsupported."); |
380 return NO; | 391 return NO; |
381 } | 392 } |
382 [captureSession addOutput:videoDataOutput]; | 393 [captureSession addOutput:videoDataOutput]; |
383 | 394 |
384 // Get the front and back cameras. If there isn't a front camera | 395 // Get the front and back cameras. If there isn't a front camera |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
570 | 581 |
571 struct AVFoundationFrame { | 582 struct AVFoundationFrame { |
572 AVFoundationFrame(CVImageBufferRef buffer, int64_t time) | 583 AVFoundationFrame(CVImageBufferRef buffer, int64_t time) |
573 : image_buffer(buffer), capture_time(time) {} | 584 : image_buffer(buffer), capture_time(time) {} |
574 CVImageBufferRef image_buffer; | 585 CVImageBufferRef image_buffer; |
575 int64_t capture_time; | 586 int64_t capture_time; |
576 }; | 587 }; |
577 | 588 |
578 AVFoundationVideoCapturer::AVFoundationVideoCapturer() | 589 AVFoundationVideoCapturer::AVFoundationVideoCapturer() |
579 : _capturer(nil), _startThread(nullptr) { | 590 : _capturer(nil), _startThread(nullptr) { |
580 // Set our supported formats. This matches preset. | 591 // Set our supported formats. This matches kAvailablePresets. |
592 _capturer = | |
593 [[RTCAVFoundationVideoCapturerInternal alloc] initWithCapturer:this]; | |
tkchin_webrtc
2016/08/15 17:28:36
indent 4 not 2
| |
594 | |
581 std::vector<cricket::VideoFormat> supported_formats; | 595 std::vector<cricket::VideoFormat> supported_formats; |
596 int framerate = 30; | |
597 | |
582 #if TARGET_OS_IPHONE | 598 #if TARGET_OS_IPHONE |
583 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { | 599 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { |
584 supported_formats.push_back(cricket::VideoFormat(kIPhone4SFormat)); | |
585 set_enable_video_adapter(false); | 600 set_enable_video_adapter(false); |
586 } else { | 601 framerate = 15; |
587 supported_formats.push_back(cricket::VideoFormat(kDefaultFormat)); | |
588 } | 602 } |
589 #else | |
590 supported_formats.push_back(cricket::VideoFormat(kDefaultFormat)); | |
591 #endif | 603 #endif |
604 | |
605 for (const auto preset : kAvailablePresets) { | |
606 if ([_capturer.captureSession canSetSessionPreset:preset.sessionPreset]) { | |
607 const auto format = cricket::VideoFormat( | |
608 preset.width, | |
609 preset.height, | |
610 cricket::VideoFormat::FpsToInterval(framerate), | |
611 cricket::FOURCC_NV12); | |
612 supported_formats.push_back(format); | |
613 } | |
614 } | |
615 | |
592 SetSupportedFormats(supported_formats); | 616 SetSupportedFormats(supported_formats); |
593 _capturer = | |
594 [[RTCAVFoundationVideoCapturerInternal alloc] initWithCapturer:this]; | |
595 } | 617 } |
596 | 618 |
597 AVFoundationVideoCapturer::~AVFoundationVideoCapturer() { | 619 AVFoundationVideoCapturer::~AVFoundationVideoCapturer() { |
598 _capturer = nil; | 620 _capturer = nil; |
599 } | 621 } |
600 | 622 |
601 cricket::CaptureState AVFoundationVideoCapturer::Start( | 623 cricket::CaptureState AVFoundationVideoCapturer::Start( |
602 const cricket::VideoFormat& format) { | 624 const cricket::VideoFormat& format) { |
603 if (!_capturer) { | 625 if (!_capturer) { |
604 LOG(LS_ERROR) << "Failed to create AVFoundation capturer."; | 626 LOG(LS_ERROR) << "Failed to create AVFoundation capturer."; |
605 return cricket::CaptureState::CS_FAILED; | 627 return cricket::CaptureState::CS_FAILED; |
606 } | 628 } |
607 if (_capturer.isRunning) { | 629 if (_capturer.isRunning) { |
608 LOG(LS_ERROR) << "The capturer is already running."; | 630 LOG(LS_ERROR) << "The capturer is already running."; |
609 return cricket::CaptureState::CS_FAILED; | 631 return cricket::CaptureState::CS_FAILED; |
610 } | 632 } |
611 if (format != kDefaultFormat && format != kIPhone4SFormat) { | 633 |
612 LOG(LS_ERROR) << "Unsupported format provided."; | 634 NSString *desiredPreset = GetSessionPresetForVideoFormat(format); |
tkchin_webrtc
2016/08/15 17:28:36
dcheck that you get a preset?
| |
635 | |
636 [_capturer.captureSession beginConfiguration]; | |
637 if (![_capturer.captureSession canSetSessionPreset:desiredPreset]) { | |
638 LOG(LS_ERROR) << "Unsupported video format."; | |
613 return cricket::CaptureState::CS_FAILED; | 639 return cricket::CaptureState::CS_FAILED; |
tkchin_webrtc
2016/08/15 17:28:36
commit configuration before return
| |
614 } | 640 } |
641 _capturer.captureSession.sessionPreset = desiredPreset; | |
642 [_capturer.captureSession commitConfiguration]; | |
615 | 643 |
616 // Keep track of which thread capture started on. This is the thread that | 644 // Keep track of which thread capture started on. This is the thread that |
617 // frames need to be sent to. | 645 // frames need to be sent to. |
618 RTC_DCHECK(!_startThread); | 646 RTC_DCHECK(!_startThread); |
619 _startThread = rtc::Thread::Current(); | 647 _startThread = rtc::Thread::Current(); |
620 | 648 |
621 SetCaptureFormat(&format); | 649 SetCaptureFormat(&format); |
622 // This isn't super accurate because it takes a while for the AVCaptureSession | 650 // This isn't super accurate because it takes a while for the AVCaptureSession |
623 // to spin up, and this call returns async. | 651 // to spin up, and this call returns async. |
624 // TODO(tkchin): make this better. | 652 // TODO(tkchin): make this better. |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
726 } | 754 } |
727 | 755 |
728 OnFrame(cricket::WebRtcVideoFrame(buffer, webrtc::kVideoRotation_0, | 756 OnFrame(cricket::WebRtcVideoFrame(buffer, webrtc::kVideoRotation_0, |
729 translated_camera_time_us, 0), | 757 translated_camera_time_us, 0), |
730 captured_width, captured_height); | 758 captured_width, captured_height); |
731 | 759 |
732 CVBufferRelease(image_buffer); | 760 CVBufferRelease(image_buffer); |
733 } | 761 } |
734 | 762 |
735 } // namespace webrtc | 763 } // namespace webrtc |
OLD | NEW |