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 // mapping from AVCaptureSession presets to cricket::VideoFormat |
31 static cricket::VideoFormat GetVideoFormatForSessionPreset( | |
32 NSString *preset, int framerate) { | |
33 | |
34 // Default parameter values | |
35 int w = 640; | |
36 int h = 480; | |
37 | |
38 if (preset == AVCaptureSessionPreset1280x720) { | |
magjed_webrtc
2016/08/11 12:34:07
I would prefer if this logic was data driven inste
kthelgason
2016/08/11 13:14:29
I like that, That's a way better idea. Thanks!
| |
39 w = 1280; | |
40 h = 720; | |
41 } else if (preset == AVCaptureSessionPreset352x288) { | |
42 w = 352; | |
43 h = 288; | |
44 } | |
45 return cricket::VideoFormat(w, h, | |
46 cricket::VideoFormat::FpsToInterval(framerate), | |
47 cricket::FOURCC_NV12); | |
48 } | |
49 | |
31 static NSString *const kDefaultPreset = AVCaptureSessionPreset640x480; | 50 static NSString *const kDefaultPreset = AVCaptureSessionPreset640x480; |
32 static NSString *const kIPhone4SPreset = AVCaptureSessionPreset352x288; | |
33 static cricket::VideoFormat const kDefaultFormat = | |
34 cricket::VideoFormat(640, | |
35 480, | |
36 cricket::VideoFormat::FpsToInterval(30), | |
37 cricket::FOURCC_NV12); | |
38 // iPhone4S is too slow to handle 30fps. | 51 // iPhone4S is too slow to handle 30fps. |
39 static cricket::VideoFormat const kIPhone4SFormat = | 52 static cricket::VideoFormat const kIPhone4SFormat = |
magjed_webrtc
2016/08/11 12:34:07
This variable is unused now so you can remove it.
kthelgason
2016/08/11 13:14:29
Acknowledged.
| |
40 cricket::VideoFormat(352, | 53 GetVideoFormatForSessionPreset(kDefaultPreset, 15); |
41 288, | 54 |
42 cricket::VideoFormat::FpsToInterval(15), | 55 // mapping from cricket::VideoFormat to AVCaptureSession presets |
43 cricket::FOURCC_NV12); | 56 static NSString *GetSessionPresetForVideoFormat( |
57 const cricket::VideoFormat& format) { | |
58 | |
59 if (format.width == 1280 && format.height == 720) { | |
60 return AVCaptureSessionPreset1280x720; | |
61 } else if (format.width == 352 && format.height == 288) { | |
62 return AVCaptureSessionPreset352x288; | |
63 } | |
64 // Nothing found, return default preset | |
65 return kDefaultPreset; | |
66 } | |
67 | |
68 static NSString *const kSupportedPresets[] = { | |
69 AVCaptureSessionPreset1280x720, | |
70 AVCaptureSessionPreset640x480, | |
71 AVCaptureSessionPreset352x288 | |
72 }; | |
73 | |
44 | 74 |
45 // This class used to capture frames using AVFoundation APIs on iOS. It is meant | 75 // 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 | 76 // to be owned by an instance of AVFoundationVideoCapturer. The reason for this |
47 // because other webrtc objects own cricket::VideoCapturer, which is not | 77 // because other webrtc objects own cricket::VideoCapturer, which is not |
48 // ref counted. To prevent bad behavior we do not expose this class directly. | 78 // ref counted. To prevent bad behavior we do not expose this class directly. |
49 @interface RTCAVFoundationVideoCapturerInternal : NSObject | 79 @interface RTCAVFoundationVideoCapturerInternal : NSObject |
50 <AVCaptureVideoDataOutputSampleBufferDelegate> | 80 <AVCaptureVideoDataOutputSampleBufferDelegate> |
51 | 81 |
52 @property(nonatomic, readonly) AVCaptureSession *captureSession; | 82 @property(nonatomic, readonly) AVCaptureSession *captureSession; |
53 @property(nonatomic, readonly) dispatch_queue_t frameQueue; | 83 @property(nonatomic, readonly) dispatch_queue_t frameQueue; |
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
570 | 600 |
571 struct AVFoundationFrame { | 601 struct AVFoundationFrame { |
572 AVFoundationFrame(CVImageBufferRef buffer, int64_t time) | 602 AVFoundationFrame(CVImageBufferRef buffer, int64_t time) |
573 : image_buffer(buffer), capture_time(time) {} | 603 : image_buffer(buffer), capture_time(time) {} |
574 CVImageBufferRef image_buffer; | 604 CVImageBufferRef image_buffer; |
575 int64_t capture_time; | 605 int64_t capture_time; |
576 }; | 606 }; |
577 | 607 |
578 AVFoundationVideoCapturer::AVFoundationVideoCapturer() | 608 AVFoundationVideoCapturer::AVFoundationVideoCapturer() |
579 : _capturer(nil), _startThread(nullptr) { | 609 : _capturer(nil), _startThread(nullptr) { |
580 // Set our supported formats. This matches preset. | 610 // Set our supported formats. This matches kSupportedPresets |
581 std::vector<cricket::VideoFormat> supported_formats; | 611 std::vector<cricket::VideoFormat> supported_formats; |
612 | |
582 #if TARGET_OS_IPHONE | 613 #if TARGET_OS_IPHONE |
583 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { | 614 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { |
584 supported_formats.push_back(cricket::VideoFormat(kIPhone4SFormat)); | |
585 set_enable_video_adapter(false); | 615 set_enable_video_adapter(false); |
586 } else { | 616 } |
587 supported_formats.push_back(cricket::VideoFormat(kDefaultFormat)); | 617 |
618 for (auto preset : kSupportedPresets) { | |
619 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { | |
magjed_webrtc
2016/08/11 12:34:07
Can we use '_capturer.captureSession canSetSession
kthelgason
2016/08/11 13:14:29
I did that initially but decided against it as I t
| |
620 // iPhone 4S does not support 720p | |
621 if (preset == AVCaptureSessionPreset1280x720) { | |
622 continue; | |
623 } | |
624 supported_formats.push_back(GetVideoFormatForSessionPreset(preset, 15)); | |
625 } else { | |
626 supported_formats.push_back(GetVideoFormatForSessionPreset(preset, 30)); | |
627 } | |
588 } | 628 } |
589 #else | 629 #else |
590 supported_formats.push_back(cricket::VideoFormat(kDefaultFormat)); | 630 supported_formats.push_back( |
631 GetVideoFormatForSessionPreset(kDefaultPreset, 30)); | |
591 #endif | 632 #endif |
592 SetSupportedFormats(supported_formats); | 633 SetSupportedFormats(supported_formats); |
593 _capturer = | |
594 [[RTCAVFoundationVideoCapturerInternal alloc] initWithCapturer:this]; | |
magjed_webrtc
2016/08/11 12:34:07
This looks like critical code we can't just remove
kthelgason
2016/08/11 13:14:29
Ugh, sorry about that. I'd fixed this locally but
| |
595 } | 634 } |
596 | 635 |
597 AVFoundationVideoCapturer::~AVFoundationVideoCapturer() { | 636 AVFoundationVideoCapturer::~AVFoundationVideoCapturer() { |
598 _capturer = nil; | 637 _capturer = nil; |
599 } | 638 } |
600 | 639 |
601 cricket::CaptureState AVFoundationVideoCapturer::Start( | 640 cricket::CaptureState AVFoundationVideoCapturer::Start( |
602 const cricket::VideoFormat& format) { | 641 const cricket::VideoFormat& format) { |
603 if (!_capturer) { | 642 if (!_capturer) { |
604 LOG(LS_ERROR) << "Failed to create AVFoundation capturer."; | 643 LOG(LS_ERROR) << "Failed to create AVFoundation capturer."; |
605 return cricket::CaptureState::CS_FAILED; | 644 return cricket::CaptureState::CS_FAILED; |
606 } | 645 } |
607 if (_capturer.isRunning) { | 646 if (_capturer.isRunning) { |
608 LOG(LS_ERROR) << "The capturer is already running."; | 647 LOG(LS_ERROR) << "The capturer is already running."; |
609 return cricket::CaptureState::CS_FAILED; | 648 return cricket::CaptureState::CS_FAILED; |
610 } | 649 } |
611 if (format != kDefaultFormat && format != kIPhone4SFormat) { | 650 |
612 LOG(LS_ERROR) << "Unsupported format provided."; | 651 NSString *desiredPreset = GetSessionPresetForVideoFormat(format); |
652 | |
653 [_capturer.captureSession beginConfiguration]; | |
654 if (![_capturer.captureSession canSetSessionPreset:desiredPreset]) { | |
655 LOG(LS_ERROR) << "Unsupported video format."; | |
613 return cricket::CaptureState::CS_FAILED; | 656 return cricket::CaptureState::CS_FAILED; |
614 } | 657 } |
658 _capturer.captureSession.sessionPreset = desiredPreset; | |
659 [_capturer.captureSession commitConfiguration]; | |
615 | 660 |
616 // Keep track of which thread capture started on. This is the thread that | 661 // Keep track of which thread capture started on. This is the thread that |
617 // frames need to be sent to. | 662 // frames need to be sent to. |
618 RTC_DCHECK(!_startThread); | 663 RTC_DCHECK(!_startThread); |
619 _startThread = rtc::Thread::Current(); | 664 _startThread = rtc::Thread::Current(); |
620 | 665 |
621 SetCaptureFormat(&format); | 666 SetCaptureFormat(&format); |
667 | |
668 | |
622 // This isn't super accurate because it takes a while for the AVCaptureSession | 669 // This isn't super accurate because it takes a while for the AVCaptureSession |
623 // to spin up, and this call returns async. | 670 // to spin up, and this call returns async. |
624 // TODO(tkchin): make this better. | 671 // TODO(tkchin): make this better. |
625 [_capturer start]; | 672 [_capturer start]; |
626 SetCaptureState(cricket::CaptureState::CS_RUNNING); | 673 SetCaptureState(cricket::CaptureState::CS_RUNNING); |
627 | 674 |
628 return cricket::CaptureState::CS_STARTING; | 675 return cricket::CaptureState::CS_STARTING; |
629 } | 676 } |
630 | 677 |
631 void AVFoundationVideoCapturer::Stop() { | 678 void AVFoundationVideoCapturer::Stop() { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
726 } | 773 } |
727 | 774 |
728 OnFrame(cricket::WebRtcVideoFrame(buffer, webrtc::kVideoRotation_0, | 775 OnFrame(cricket::WebRtcVideoFrame(buffer, webrtc::kVideoRotation_0, |
729 translated_camera_time_us, 0), | 776 translated_camera_time_us, 0), |
730 captured_width, captured_height); | 777 captured_width, captured_height); |
731 | 778 |
732 CVBufferRelease(image_buffer); | 779 CVBufferRelease(image_buffer); |
733 } | 780 } |
734 | 781 |
735 } // namespace webrtc | 782 } // namespace webrtc |
OLD | NEW |