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

Side by Side Diff: webrtc/examples/objc/AppRTCMobile/ARDAppClient.m

Issue 2778163005: Use new RTCCameraVideoCapturer in AppRTCMobile. (Closed)
Patch Set: Fix tests 2. Created 3 years, 8 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
OLDNEW
1 /* 1 /*
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2014 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 #import "ARDAppClient+Internal.h" 11 #import "ARDAppClient+Internal.h"
12 12
13 #import "WebRTC/RTCAVFoundationVideoSource.h" 13 #import "WebRTC/RTCAVFoundationVideoSource.h"
14 #import "WebRTC/RTCAudioTrack.h" 14 #import "WebRTC/RTCAudioTrack.h"
15 #import "WebRTC/RTCCameraVideoCapturer.h"
15 #import "WebRTC/RTCConfiguration.h" 16 #import "WebRTC/RTCConfiguration.h"
16 #import "WebRTC/RTCFileLogger.h" 17 #import "WebRTC/RTCFileLogger.h"
17 #import "WebRTC/RTCIceServer.h" 18 #import "WebRTC/RTCIceServer.h"
18 #import "WebRTC/RTCLogging.h" 19 #import "WebRTC/RTCLogging.h"
19 #import "WebRTC/RTCMediaConstraints.h" 20 #import "WebRTC/RTCMediaConstraints.h"
20 #import "WebRTC/RTCMediaStream.h" 21 #import "WebRTC/RTCMediaStream.h"
21 #import "WebRTC/RTCPeerConnectionFactory.h" 22 #import "WebRTC/RTCPeerConnectionFactory.h"
22 #import "WebRTC/RTCRtpSender.h" 23 #import "WebRTC/RTCRtpSender.h"
23 #import "WebRTC/RTCTracing.h" 24 #import "WebRTC/RTCTracing.h"
25 #import "WebRTC/RTCVideoTrack.h"
24 26
25 #import "ARDAppEngineClient.h" 27 #import "ARDAppEngineClient.h"
26 #import "ARDJoinResponse.h" 28 #import "ARDJoinResponse.h"
27 #import "ARDMessageResponse.h" 29 #import "ARDMessageResponse.h"
28 #import "ARDSDPUtils.h" 30 #import "ARDSDPUtils.h"
29 #import "ARDSettingsModel.h" 31 #import "ARDSettingsModel.h"
30 #import "ARDSignalingMessage.h" 32 #import "ARDSignalingMessage.h"
31 #import "ARDTURNClient+Internal.h" 33 #import "ARDTURNClient+Internal.h"
32 #import "ARDUtilities.h" 34 #import "ARDUtilities.h"
33 #import "ARDWebSocketChannel.h" 35 #import "ARDWebSocketChannel.h"
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 - (void)timerDidFire:(NSTimer *)timer { 95 - (void)timerDidFire:(NSTimer *)timer {
94 _timerHandler(); 96 _timerHandler();
95 } 97 }
96 98
97 @end 99 @end
98 100
99 @implementation ARDAppClient { 101 @implementation ARDAppClient {
100 RTCFileLogger *_fileLogger; 102 RTCFileLogger *_fileLogger;
101 ARDTimerProxy *_statsTimer; 103 ARDTimerProxy *_statsTimer;
102 ARDSettingsModel *_settings; 104 ARDSettingsModel *_settings;
105 RTCCameraVideoCapturer *_capturer;
106 RTCVideoTrack *_localVideoTrack;
107 BOOL _usingFrontCamera;
103 } 108 }
104 109
105 @synthesize shouldGetStats = _shouldGetStats; 110 @synthesize shouldGetStats = _shouldGetStats;
106 @synthesize state = _state; 111 @synthesize state = _state;
107 @synthesize delegate = _delegate; 112 @synthesize delegate = _delegate;
108 @synthesize roomServerClient = _roomServerClient; 113 @synthesize roomServerClient = _roomServerClient;
109 @synthesize channel = _channel; 114 @synthesize channel = _channel;
110 @synthesize loopbackChannel = _loopbackChannel; 115 @synthesize loopbackChannel = _loopbackChannel;
111 @synthesize turnClient = _turnClient; 116 @synthesize turnClient = _turnClient;
112 @synthesize peerConnection = _peerConnection; 117 @synthesize peerConnection = _peerConnection;
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 } 298 }
294 if (_channel) { 299 if (_channel) {
295 if (_channel.state == kARDSignalingChannelStateRegistered) { 300 if (_channel.state == kARDSignalingChannelStateRegistered) {
296 // Tell the other client we're hanging up. 301 // Tell the other client we're hanging up.
297 ARDByeMessage *byeMessage = [[ARDByeMessage alloc] init]; 302 ARDByeMessage *byeMessage = [[ARDByeMessage alloc] init];
298 [_channel sendMessage:byeMessage]; 303 [_channel sendMessage:byeMessage];
299 } 304 }
300 // Disconnect from collider. 305 // Disconnect from collider.
301 _channel = nil; 306 _channel = nil;
302 } 307 }
308 if (_capturer) {
309 [_capturer stopCapture];
310 _capturer = nil;
311 }
303 _clientId = nil; 312 _clientId = nil;
304 _roomId = nil; 313 _roomId = nil;
305 _isInitiator = NO; 314 _isInitiator = NO;
306 _hasReceivedSdp = NO; 315 _hasReceivedSdp = NO;
307 _messageQueue = [NSMutableArray array]; 316 _messageQueue = [NSMutableArray array];
317 _localVideoTrack = nil;
308 #if defined(WEBRTC_IOS) 318 #if defined(WEBRTC_IOS)
309 [_factory stopAecDump]; 319 [_factory stopAecDump];
310 [_peerConnection stopRtcEventLog]; 320 [_peerConnection stopRtcEventLog];
311 #endif 321 #endif
312 _peerConnection = nil; 322 _peerConnection = nil;
313 self.state = kARDAppClientStateDisconnected; 323 self.state = kARDAppClientStateDisconnected;
314 #if defined(WEBRTC_IOS) 324 #if defined(WEBRTC_IOS)
315 if (kARDAppClientEnableTracing) { 325 if (kARDAppClientEnableTracing) {
316 RTCStopInternalCapture(); 326 RTCStopInternalCapture();
317 } 327 }
318 #endif 328 #endif
319 } 329 }
320 330
331 - (void)switchCamera {
332 _usingFrontCamera = !_usingFrontCamera;
333 [self startCapture];
334 }
335
321 #pragma mark - ARDSignalingChannelDelegate 336 #pragma mark - ARDSignalingChannelDelegate
322 337
323 - (void)channel:(id<ARDSignalingChannel>)channel 338 - (void)channel:(id<ARDSignalingChannel>)channel
324 didReceiveMessage:(ARDSignalingMessage *)message { 339 didReceiveMessage:(ARDSignalingMessage *)message {
325 switch (message.type) { 340 switch (message.type) {
326 case kARDSignalingMessageTypeOffer: 341 case kARDSignalingMessageTypeOffer:
327 case kARDSignalingMessageTypeAnswer: 342 case kARDSignalingMessageTypeAnswer:
328 // Offers and answers must be processed before any other message, so we 343 // Offers and answers must be processed before any other message, so we
329 // place them at the front of the queue. 344 // place them at the front of the queue.
330 _hasReceivedSdp = YES; 345 _hasReceivedSdp = YES;
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
659 }]; 674 }];
660 } else { 675 } else {
661 [_channel sendMessage:message]; 676 [_channel sendMessage:message];
662 } 677 }
663 } 678 }
664 679
665 - (RTCRtpSender *)createVideoSender { 680 - (RTCRtpSender *)createVideoSender {
666 RTCRtpSender *sender = 681 RTCRtpSender *sender =
667 [_peerConnection senderWithKind:kRTCMediaStreamTrackKindVideo 682 [_peerConnection senderWithKind:kRTCMediaStreamTrackKindVideo
668 streamId:kARDMediaStreamId]; 683 streamId:kARDMediaStreamId];
669 RTCVideoTrack *track = [self createLocalVideoTrack]; 684 _localVideoTrack = [self createLocalVideoTrack];
670 if (track) { 685 if (_localVideoTrack) {
671 sender.track = track; 686 sender.track = _localVideoTrack;
672 [_delegate appClient:self didReceiveLocalVideoTrack:track]; 687 [_delegate appClient:self didReceiveLocalVideoTrack:_localVideoTrack];
673 } 688 }
689 [_delegate appClient:self didReceiveLocalVideoTrack:_localVideoTrack];
daniela-webrtc 2017/04/03 10:16:50 Not sure if we should invoke the delegate with nil
sakal 2017/04/04 09:18:23 Oops, this was added by accident.
674 690
675 return sender; 691 return sender;
676 } 692 }
677 693
678 - (void)setMaxBitrateForPeerConnectionVideoSender { 694 - (void)setMaxBitrateForPeerConnectionVideoSender {
679 for (RTCRtpSender *sender in _peerConnection.senders) { 695 for (RTCRtpSender *sender in _peerConnection.senders) {
680 if (sender.track != nil) { 696 if (sender.track != nil) {
681 if ([sender.track.kind isEqualToString:kARDVideoTrackKind]) { 697 if ([sender.track.kind isEqualToString:kARDVideoTrackKind]) {
682 [self setMaxBitrate:[_settings currentMaxBitrateSettingFromStore] forVid eoSender:sender]; 698 [self setMaxBitrate:[_settings currentMaxBitrateSettingFromStore] forVid eoSender:sender];
683 } 699 }
(...skipping 18 matching lines...) Expand all
702 RTCAudioSource *source = [_factory audioSourceWithConstraints:constraints]; 718 RTCAudioSource *source = [_factory audioSourceWithConstraints:constraints];
703 RTCAudioTrack *track = [_factory audioTrackWithSource:source 719 RTCAudioTrack *track = [_factory audioTrackWithSource:source
704 trackId:kARDAudioTrackId]; 720 trackId:kARDAudioTrackId];
705 RTCRtpSender *sender = 721 RTCRtpSender *sender =
706 [_peerConnection senderWithKind:kRTCMediaStreamTrackKindAudio 722 [_peerConnection senderWithKind:kRTCMediaStreamTrackKindAudio
707 streamId:kARDMediaStreamId]; 723 streamId:kARDMediaStreamId];
708 sender.track = track; 724 sender.track = track;
709 return sender; 725 return sender;
710 } 726 }
711 727
728 - (void)startCapture {
daniela-webrtc 2017/04/03 10:16:50 This should be exposed in the interface. Also, it
sakal 2017/04/04 09:18:23 Moved to CaptureController as discussed.
729 AVCaptureDevicePosition position =
730 _usingFrontCamera ? AVCaptureDevicePositionFront : AVCaptureDevicePosition Back;
731 AVCaptureDevice *device = [self findDeviceForPosition:position];
732 AVCaptureDeviceFormat *format = [self selectFormatForDevice:device];
733 int fps = [self selectFpsForFormat:format];
734
735 [_capturer startCaptureWithDevice:device format:format fps:fps];
736 }
737
738 - (AVCaptureDevice *)findDeviceForPosition:(AVCaptureDevicePosition)position {
739 NSArray<AVCaptureDevice *> *captureDevices = [RTCCameraVideoCapturer captureDe vices];
740 for (AVCaptureDevice *device in captureDevices) {
741 if (device.position == position) {
742 return device;
743 }
744 }
745 return captureDevices[0];
746 }
747
748 - (AVCaptureDeviceFormat *)selectFormatForDevice:(AVCaptureDevice *)device {
749 NSArray<AVCaptureDeviceFormat *> *formats =
750 [RTCCameraVideoCapturer supportedFormatsForDevice:device];
751 int targetWidth = [_settings currentVideoResolutionWidthFromStore];
752 int targetHeight = [_settings currentVideoResolutionHeightFromStore];
753 AVCaptureDeviceFormat *selectedFormat;
daniela-webrtc 2017/04/03 10:16:50 Preferably explicitly set pointers to nil.
sakal 2017/04/04 09:18:23 Done.
754 int currentDiff = INT_MAX;
755
756 for (AVCaptureDeviceFormat *format in formats) {
757 CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.f ormatDescription);
758 int diff = abs(targetWidth - dimension.width) + abs(targetHeight - dimension .height);
759 if (diff < currentDiff) {
760 selectedFormat = format;
761 currentDiff = diff;
762 }
763 }
764
765 NSAssert(selectedFormat != nil, @"No suitable capture format found.");
766 return selectedFormat;
767 }
768
769 - (int)selectFpsForFormat:(AVCaptureDeviceFormat *)format {
770 Float64 maxFramerate = 0;
771 for (AVFrameRateRange *fpsRange in format.videoSupportedFrameRateRanges) {
772 maxFramerate = fmax(maxFramerate, fpsRange.maxFrameRate);
773 }
774 return maxFramerate;
775 }
776
712 - (RTCVideoTrack *)createLocalVideoTrack { 777 - (RTCVideoTrack *)createLocalVideoTrack {
713 RTCVideoTrack* localVideoTrack = nil; 778 RTCVideoTrack* localVideoTrack = nil;
714 // The iOS simulator doesn't provide any sort of camera capture 779 // The iOS simulator doesn't provide any sort of camera capture
715 // support or emulation (http://goo.gl/rHAnC1) so don't bother 780 // support or emulation (http://goo.gl/rHAnC1) so don't bother
716 // trying to open a local stream. 781 // trying to open a local stream.
717 #if !TARGET_IPHONE_SIMULATOR 782 #if !TARGET_IPHONE_SIMULATOR
718 if (!_isAudioOnly) { 783 if (!_isAudioOnly) {
719 RTCMediaConstraints *cameraConstraints = 784 RTCVideoSource *source = [_factory videoSource];
720 [self cameraConstraints]; 785 _capturer = [[RTCCameraVideoCapturer alloc] initWithDelegate:source];
721 RTCAVFoundationVideoSource *source = 786 [_delegate appClient:self didCreateLocalCapturer:_capturer];
722 [_factory avFoundationVideoSourceWithConstraints:cameraConstraints]; 787 _usingFrontCamera = true;
788 [self startCapture];
daniela-webrtc 2017/04/03 10:16:50 [startCapture] should not be here but rather expos
sakal 2017/04/04 09:18:23 Done.
723 localVideoTrack = 789 localVideoTrack =
724 [_factory videoTrackWithSource:source 790 [_factory videoTrackWithSource:source
725 trackId:kARDVideoTrackId]; 791 trackId:kARDVideoTrackId];
726 } 792 }
727 #endif 793 #endif
728 return localVideoTrack; 794 return localVideoTrack;
729 } 795 }
730 796
731 #pragma mark - Collider methods 797 #pragma mark - Collider methods
732 798
(...skipping 24 matching lines...) Expand all
757 - (RTCMediaConstraints *)defaultMediaAudioConstraints { 823 - (RTCMediaConstraints *)defaultMediaAudioConstraints {
758 NSString *valueLevelControl = _shouldUseLevelControl ? 824 NSString *valueLevelControl = _shouldUseLevelControl ?
759 kRTCMediaConstraintsValueTrue : kRTCMediaConstraintsValueFalse; 825 kRTCMediaConstraintsValueTrue : kRTCMediaConstraintsValueFalse;
760 NSDictionary *mandatoryConstraints = @{ kRTCMediaConstraintsLevelControl : va lueLevelControl }; 826 NSDictionary *mandatoryConstraints = @{ kRTCMediaConstraintsLevelControl : va lueLevelControl };
761 RTCMediaConstraints *constraints = 827 RTCMediaConstraints *constraints =
762 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstr aints 828 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstr aints
763 optionalConstraints:nil]; 829 optionalConstraints:nil];
764 return constraints; 830 return constraints;
765 } 831 }
766 832
767 - (RTCMediaConstraints *)cameraConstraints {
768 RTCMediaConstraints *cameraConstraints = [[RTCMediaConstraints alloc]
769 initWithMandatoryConstraints:nil
770 optionalConstraints:[_settings currentMediaConstraintFromStoreAsR TCDictionary]];
771 return cameraConstraints;
772 }
773
774 - (RTCMediaConstraints *)defaultAnswerConstraints { 833 - (RTCMediaConstraints *)defaultAnswerConstraints {
775 return [self defaultOfferConstraints]; 834 return [self defaultOfferConstraints];
776 } 835 }
777 836
778 - (RTCMediaConstraints *)defaultOfferConstraints { 837 - (RTCMediaConstraints *)defaultOfferConstraints {
779 NSDictionary *mandatoryConstraints = @{ 838 NSDictionary *mandatoryConstraints = @{
780 @"OfferToReceiveAudio" : @"true", 839 @"OfferToReceiveAudio" : @"true",
781 @"OfferToReceiveVideo" : @"true" 840 @"OfferToReceiveVideo" : @"true"
782 }; 841 };
783 RTCMediaConstraints* constraints = 842 RTCMediaConstraints* constraints =
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
851 code:kARDAppClientErrorInvalidRoom 910 code:kARDAppClientErrorInvalidRoom
852 userInfo:@{ 911 userInfo:@{
853 NSLocalizedDescriptionKey: @"Invalid room.", 912 NSLocalizedDescriptionKey: @"Invalid room.",
854 }]; 913 }];
855 break; 914 break;
856 } 915 }
857 return error; 916 return error;
858 } 917 }
859 918
860 @end 919 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698