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

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: Avoid setting session preset twice. 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 {
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) const {
magjed_webrtc 2016/08/15 10:10:23 Structs are not allowed to have methods providing
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 #if TARGET_OS_IPHONE
42 cricket::VideoFormat::FpsToInterval(15), 42 static const AVCaptureSessionPresetResolution kAvailablePresets[] = {
43 cricket::FOURCC_NV12); 43 { AVCaptureSessionPreset352x288, 352, 288},
44 { AVCaptureSessionPreset640x480, 640, 480},
45 { AVCaptureSessionPreset1280x720, 1280, 720},
46 { AVCaptureSessionPreset1920x1080, 1920, 1080},
47 };
48 #else // macOS
49 static const AVCaptureSessionPresetResolution kAvailablePresets[] = {
50 { AVCaptureSessionPreset320x240, 320, 240},
51 { AVCaptureSessionPreset352x288, 352, 288},
52 { AVCaptureSessionPreset640x480, 640, 480},
53 { AVCaptureSessionPreset960x540, 960, 540},
54 { AVCaptureSessionPreset1280x720, 1280, 720},
55 };
56 #endif
57
58 // mapping from cricket::VideoFormat to AVCaptureSession presets
magjed_webrtc 2016/08/15 10:10:23 Super-nit: comments should have proper capitalizat
59 static NSString *GetSessionPresetForVideoFormat(
60 const cricket::VideoFormat& format) {
61 for (const auto preset : kAvailablePresets) {
62 // Check both orientations
63 if ((format.width == preset.width && format.height == preset.height) ||
64 (format.width == preset.height && format.height == preset.width)) {
65 return preset.sessionPreset;
66 }
67 }
68 // Nothing found, use default
69 return AVCaptureSessionPreset640x480;
70 }
44 71
45 // This class used to capture frames using AVFoundation APIs on iOS. It is meant 72 // 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 73 // to be owned by an instance of AVFoundationVideoCapturer. The reason for this
47 // because other webrtc objects own cricket::VideoCapturer, which is not 74 // because other webrtc objects own cricket::VideoCapturer, which is not
48 // ref counted. To prevent bad behavior we do not expose this class directly. 75 // ref counted. To prevent bad behavior we do not expose this class directly.
49 @interface RTCAVFoundationVideoCapturerInternal : NSObject 76 @interface RTCAVFoundationVideoCapturerInternal : NSObject
50 <AVCaptureVideoDataOutputSampleBufferDelegate> 77 <AVCaptureVideoDataOutputSampleBufferDelegate>
51 78
52 @property(nonatomic, readonly) AVCaptureSession *captureSession; 79 @property(nonatomic, readonly) AVCaptureSession *captureSession;
53 @property(nonatomic, readonly) dispatch_queue_t frameQueue; 80 @property(nonatomic, readonly) dispatch_queue_t frameQueue;
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 #pragma mark - Private 381 #pragma mark - Private
355 382
356 - (BOOL)setupCaptureSession { 383 - (BOOL)setupCaptureSession {
357 AVCaptureSession *captureSession = [[AVCaptureSession alloc] init]; 384 AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];
358 #if defined(__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0 385 #if defined(__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
359 NSString *version = [[UIDevice currentDevice] systemVersion]; 386 NSString *version = [[UIDevice currentDevice] systemVersion];
360 if ([version integerValue] >= 7) { 387 if ([version integerValue] >= 7) {
361 captureSession.usesApplicationAudioSession = NO; 388 captureSession.usesApplicationAudioSession = NO;
362 } 389 }
363 #endif 390 #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;
kthelgason 2016/08/15 08:43:37 I don't see why we set the sessionPreset here, and
magjed_webrtc 2016/08/15 10:10:23 sessionPreset used to be set only from here, so yo
375 391
376 // Add the output. 392 // Add the output.
377 AVCaptureVideoDataOutput *videoDataOutput = [self videoDataOutput]; 393 AVCaptureVideoDataOutput *videoDataOutput = [self videoDataOutput];
378 if (![captureSession canAddOutput:videoDataOutput]) { 394 if (![captureSession canAddOutput:videoDataOutput]) {
379 RTCLogError(@"Video data output unsupported."); 395 RTCLogError(@"Video data output unsupported.");
380 return NO; 396 return NO;
381 } 397 }
382 [captureSession addOutput:videoDataOutput]; 398 [captureSession addOutput:videoDataOutput];
383 399
384 // Get the front and back cameras. If there isn't a front camera 400 // 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
570 586
571 struct AVFoundationFrame { 587 struct AVFoundationFrame {
572 AVFoundationFrame(CVImageBufferRef buffer, int64_t time) 588 AVFoundationFrame(CVImageBufferRef buffer, int64_t time)
573 : image_buffer(buffer), capture_time(time) {} 589 : image_buffer(buffer), capture_time(time) {}
574 CVImageBufferRef image_buffer; 590 CVImageBufferRef image_buffer;
575 int64_t capture_time; 591 int64_t capture_time;
576 }; 592 };
577 593
578 AVFoundationVideoCapturer::AVFoundationVideoCapturer() 594 AVFoundationVideoCapturer::AVFoundationVideoCapturer()
579 : _capturer(nil), _startThread(nullptr) { 595 : _capturer(nil), _startThread(nullptr) {
580 // Set our supported formats. This matches preset. 596 // Set our supported formats. This matches kAvailablePresets
597 _capturer =
598 [[RTCAVFoundationVideoCapturerInternal alloc] initWithCapturer:this];
599
581 std::vector<cricket::VideoFormat> supported_formats; 600 std::vector<cricket::VideoFormat> supported_formats;
601 int framerate = 30;
602
582 #if TARGET_OS_IPHONE 603 #if TARGET_OS_IPHONE
583 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) { 604 if ([UIDevice deviceType] == RTCDeviceTypeIPhone4S) {
584 supported_formats.push_back(cricket::VideoFormat(kIPhone4SFormat));
585 set_enable_video_adapter(false); 605 set_enable_video_adapter(false);
586 } else { 606 framerate = 15;
587 supported_formats.push_back(cricket::VideoFormat(kDefaultFormat));
588 } 607 }
589 #else
590 supported_formats.push_back(cricket::VideoFormat(kDefaultFormat));
591 #endif 608 #endif
609
610 for (const auto preset : kAvailablePresets) {
611 if ([_capturer.captureSession canSetSessionPreset:preset.sessionPreset]) {
612 supported_formats.push_back(preset.getVideoFormat(framerate));
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);
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;
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
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
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