Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: webrtc/sdk/objc/Framework/Classes/avfoundationvideocapturer.mm

Issue 2231033002: Add support for more resolutions on iOS/macOS (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Represent capture format mappings with a struct Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 {
tkchin_webrtc 2016/08/11 16:51:52 Some notes about this file: ObjC++ is gnarly and
kthelgason 2016/08/11 17:42:04 That's good to know, thanks. I'll revise this. Per
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 cricket::VideoFormat getVideoFormat(int framerate) {
35 480, 35 return cricket::VideoFormat(height, width,
36 cricket::VideoFormat::FpsToInterval(30), 36 cricket::VideoFormat::FpsToInterval(framerate),
37 cricket::FOURCC_NV12); 37 cricket::FOURCC_NV12);
38 // iPhone4S is too slow to handle 30fps. 38 }
39 static cricket::VideoFormat const kIPhone4SFormat = 39 };
40 cricket::VideoFormat(352, 40
41 288, 41 static const AVCaptureSessionPresetResolution kSupportedPresets[] = {
tkchin_webrtc 2016/08/11 16:51:52 Not all of these is supported for every device. Th
kthelgason 2016/08/11 17:42:04 Acknowledged.
42 cricket::VideoFormat::FpsToInterval(15), 42 { AVCaptureSessionPreset352x288, 352, 288},
43 cricket::FOURCC_NV12); 43 { AVCaptureSessionPreset640x480, 640, 480},
44 { AVCaptureSessionPreset1280x720, 1280, 720}
45 };
46
47 static AVCaptureSessionPresetResolution const kDefaultPreset =
48 kSupportedPresets[1];
49 static AVCaptureSessionPresetResolution const kIPhone4SPreset =
50 kSupportedPresets[0];
51
52 // mapping from cricket::VideoFormat to AVCaptureSession presets
53 static NSString *GetSessionPresetForVideoFormat(
54 const cricket::VideoFormat& format) {
55 for (const auto preset : kSupportedPresets) {
56 if (format.width == preset.width && format.height == preset.height) {
57 return preset.sessionPreset;
58 }
59 }
60 // Nothing found, use default
61 return kDefaultPreset.sessionPreset;
62 }
44 63
45 // This class used to capture frames using AVFoundation APIs on iOS. It is meant 64 // 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 65 // to be owned by an instance of AVFoundationVideoCapturer. The reason for this
47 // because other webrtc objects own cricket::VideoCapturer, which is not 66 // because other webrtc objects own cricket::VideoCapturer, which is not
48 // ref counted. To prevent bad behavior we do not expose this class directly. 67 // ref counted. To prevent bad behavior we do not expose this class directly.
49 @interface RTCAVFoundationVideoCapturerInternal : NSObject 68 @interface RTCAVFoundationVideoCapturerInternal : NSObject
50 <AVCaptureVideoDataOutputSampleBufferDelegate> 69 <AVCaptureVideoDataOutputSampleBufferDelegate>
51 70
52 @property(nonatomic, readonly) AVCaptureSession *captureSession; 71 @property(nonatomic, readonly) AVCaptureSession *captureSession;
53 @property(nonatomic, readonly) dispatch_queue_t frameQueue; 72 @property(nonatomic, readonly) dispatch_queue_t frameQueue;
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 #pragma mark - Private 373 #pragma mark - Private
355 374
356 - (BOOL)setupCaptureSession { 375 - (BOOL)setupCaptureSession {
357 AVCaptureSession *captureSession = [[AVCaptureSession alloc] init]; 376 AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
358 #if defined(__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0 377 #if defined(__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
359 NSString *version = [[UIDevice currentDevice] systemVersion]; 378 NSString *version = [[UIDevice currentDevice] systemVersion];
360 if ([version integerValue] >= 7) { 379 if ([version integerValue] >= 7) {
361 captureSession.usesApplicationAudioSession = NO; 380 captureSession.usesApplicationAudioSession = NO;
362 } 381 }
363 #endif 382 #endif
364 NSString *preset = kDefaultPreset; 383 NSString *preset = kDefaultPreset.sessionPreset;
365 #if TARGET_OS_IPHONE 384 #if TARGET_OS_IPHONE
366 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { 385 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) {
367 preset = kIPhone4SPreset; 386 preset = kIPhone4SPreset.sessionPreset;
368 } 387 }
369 #endif 388 #endif
370 if (![captureSession canSetSessionPreset:preset]) { 389 if (![captureSession canSetSessionPreset:preset]) {
371 RTCLogError(@"Session preset unsupported."); 390 RTCLogError(@"Session preset unsupported.");
372 return NO; 391 return NO;
373 } 392 }
374 captureSession.sessionPreset = preset; 393 captureSession.sessionPreset = preset;
375 394
376 // Add the output. 395 // Add the output.
377 AVCaptureVideoDataOutput *videoDataOutput = [self videoDataOutput]; 396 AVCaptureVideoDataOutput *videoDataOutput = [self videoDataOutput];
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 589
571 struct AVFoundationFrame { 590 struct AVFoundationFrame {
572 AVFoundationFrame(CVImageBufferRef buffer, int64_t time) 591 AVFoundationFrame(CVImageBufferRef buffer, int64_t time)
573 : image_buffer(buffer), capture_time(time) {} 592 : image_buffer(buffer), capture_time(time) {}
574 CVImageBufferRef image_buffer; 593 CVImageBufferRef image_buffer;
575 int64_t capture_time; 594 int64_t capture_time;
576 }; 595 };
577 596
578 AVFoundationVideoCapturer::AVFoundationVideoCapturer() 597 AVFoundationVideoCapturer::AVFoundationVideoCapturer()
579 : _capturer(nil), _startThread(nullptr) { 598 : _capturer(nil), _startThread(nullptr) {
580 // Set our supported formats. This matches preset. 599 // Set our supported formats. This matches kSupportedPresets
600 _capturer =
601 [[RTCAVFoundationVideoCapturerInternal alloc] initWithCapturer:this];
602
581 std::vector<cricket::VideoFormat> supported_formats; 603 std::vector<cricket::VideoFormat> supported_formats;
604 int framerate = 30;
tkchin_webrtc 2016/08/11 16:51:52 There's no guarantee that you will get these frame
kthelgason 2016/08/11 17:42:04 I understand. I'd pretty much come to that conclus
605
582 #if TARGET_OS_IPHONE 606 #if TARGET_OS_IPHONE
583 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { 607 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) {
584 supported_formats.push_back(cricket::VideoFormat(kIPhone4SFormat));
585 set_enable_video_adapter(false); 608 set_enable_video_adapter(false);
586 } else { 609 framerate = 15;
587 supported_formats.push_back(cricket::VideoFormat(kDefaultFormat));
588 } 610 }
589 #else
590 supported_formats.push_back(cricket::VideoFormat(kDefaultFormat));
591 #endif 611 #endif
612
613 for (auto preset : kSupportedPresets) {
614 if ([_capturer.captureSession canSetSessionPreset:preset.sessionPreset]) {
tkchin_webrtc 2016/08/11 17:17:55 I'm not sure we want to go down the preset route j
615 supported_formats.push_back(preset.getVideoFormat(framerate));
616 }
617 }
618
592 SetSupportedFormats(supported_formats); 619 SetSupportedFormats(supported_formats);
593 _capturer =
594 [[RTCAVFoundationVideoCapturerInternal alloc] initWithCapturer:this];
595 } 620 }
596 621
597 AVFoundationVideoCapturer::~AVFoundationVideoCapturer() { 622 AVFoundationVideoCapturer::~AVFoundationVideoCapturer() {
598 _capturer = nil; 623 _capturer = nil;
599 } 624 }
600 625
601 cricket::CaptureState AVFoundationVideoCapturer::Start( 626 cricket::CaptureState AVFoundationVideoCapturer::Start(
602 const cricket::VideoFormat& format) { 627 const cricket::VideoFormat& format) {
603 if (!_capturer) { 628 if (!_capturer) {
604 LOG(LS_ERROR) << "Failed to create AVFoundation capturer."; 629 LOG(LS_ERROR) << "Failed to create AVFoundation capturer.";
605 return cricket::CaptureState::CS_FAILED; 630 return cricket::CaptureState::CS_FAILED;
606 } 631 }
607 if (_capturer.isRunning) { 632 if (_capturer.isRunning) {
608 LOG(LS_ERROR) << "The capturer is already running."; 633 LOG(LS_ERROR) << "The capturer is already running.";
609 return cricket::CaptureState::CS_FAILED; 634 return cricket::CaptureState::CS_FAILED;
610 } 635 }
611 if (format != kDefaultFormat && format != kIPhone4SFormat) { 636
612 LOG(LS_ERROR) << "Unsupported format provided."; 637 NSString *desiredPreset = GetSessionPresetForVideoFormat(format);
638
639 [_capturer.captureSession beginConfiguration];
640 if (![_capturer.captureSession canSetSessionPreset:desiredPreset]) {
641 LOG(LS_ERROR) << "Unsupported video format.";
tkchin_webrtc 2016/08/11 16:51:52 do you need beginconfiguration to check session pr
kthelgason 2016/08/11 17:42:04 From Apple's documentation: "After calling beginCo
613 return cricket::CaptureState::CS_FAILED; 642 return cricket::CaptureState::CS_FAILED;
614 } 643 }
644 _capturer.captureSession.sessionPreset = desiredPreset;
645 [_capturer.captureSession commitConfiguration];
615 646
616 // Keep track of which thread capture started on. This is the thread that 647 // Keep track of which thread capture started on. This is the thread that
617 // frames need to be sent to. 648 // frames need to be sent to.
618 RTC_DCHECK(!_startThread); 649 RTC_DCHECK(!_startThread);
619 _startThread = rtc::Thread::Current(); 650 _startThread = rtc::Thread::Current();
620 651
621 SetCaptureFormat(&format); 652 SetCaptureFormat(&format);
653
654
tkchin_webrtc 2016/08/11 16:51:52 nit: remove blank line
kthelgason 2016/08/11 17:42:04 Acknowledged.
622 // This isn't super accurate because it takes a while for the AVCaptureSession 655 // This isn't super accurate because it takes a while for the AVCaptureSession
623 // to spin up, and this call returns async. 656 // to spin up, and this call returns async.
624 // TODO(tkchin): make this better. 657 // TODO(tkchin): make this better.
625 [_capturer start]; 658 [_capturer start];
626 SetCaptureState(cricket::CaptureState::CS_RUNNING); 659 SetCaptureState(cricket::CaptureState::CS_RUNNING);
627 660
628 return cricket::CaptureState::CS_STARTING; 661 return cricket::CaptureState::CS_STARTING;
629 } 662 }
630 663
631 void AVFoundationVideoCapturer::Stop() { 664 void AVFoundationVideoCapturer::Stop() {
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 } 759 }
727 760
728 OnFrame(cricket::WebRtcVideoFrame(buffer, webrtc::kVideoRotation_0, 761 OnFrame(cricket::WebRtcVideoFrame(buffer, webrtc::kVideoRotation_0,
729 translated_camera_time_us, 0), 762 translated_camera_time_us, 0),
730 captured_width, captured_height); 763 captured_width, captured_height);
731 764
732 CVBufferRelease(image_buffer); 765 CVBufferRelease(image_buffer);
733 } 766 }
734 767
735 } // namespace webrtc 768 } // namespace webrtc
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698