From f3cb49f3ef944f953603d2a2e80f5f9447c30434 Mon Sep 17 00:00:00 2001 From: Tze Kwang Chin Date: Tue, 22 Mar 2016 10:57:40 -0700 Subject: [PATCH] Refactor some ObjC API init methods. initWithFactory: is clumsy and makes classes difficult to mock out in tests. By keeping methods on the factory, we can simply mock out the factory's methods instead. We can consider adding regular Obj-C like ctors if we move to making the factory a singleton, but that requires further discussion. BUG= R=haysc@webrtc.org, hjon@webrtc.org Review URL: https://codereview.webrtc.org/1820193002 . Cr-Commit-Position: refs/heads/master@{#12089} --- .../objc/RTCAVFoundationVideoSource+Private.h | 4 ++ webrtc/api/objc/RTCAVFoundationVideoSource.h | 3 +- webrtc/api/objc/RTCAudioTrack+Private.h | 4 ++ webrtc/api/objc/RTCAudioTrack.h | 4 -- webrtc/api/objc/RTCMediaStream+Private.h | 4 ++ webrtc/api/objc/RTCMediaStream.h | 4 -- webrtc/api/objc/RTCPeerConnection+Private.h | 13 ++++++ webrtc/api/objc/RTCPeerConnection.h | 13 ------ webrtc/api/objc/RTCPeerConnectionFactory.h | 41 +++++++++++++++++ webrtc/api/objc/RTCPeerConnectionFactory.mm | 46 +++++++++++++++++++ webrtc/api/objc/RTCVideoTrack+Private.h | 5 ++ webrtc/api/objc/RTCVideoTrack.h | 5 -- .../examples/objc/AppRTCDemo/ARDAppClient.m | 21 ++++----- 13 files changed, 126 insertions(+), 41 deletions(-) diff --git a/webrtc/api/objc/RTCAVFoundationVideoSource+Private.h b/webrtc/api/objc/RTCAVFoundationVideoSource+Private.h index 64da9aa5f4..067e5067c0 100644 --- a/webrtc/api/objc/RTCAVFoundationVideoSource+Private.h +++ b/webrtc/api/objc/RTCAVFoundationVideoSource+Private.h @@ -18,6 +18,10 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, readonly) webrtc::AVFoundationVideoCapturer *capturer; +/** Initialize an RTCAVFoundationVideoSource with constraints. */ +- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory + constraints:(nullable RTCMediaConstraints *)constraints; + @end NS_ASSUME_NONNULL_END diff --git a/webrtc/api/objc/RTCAVFoundationVideoSource.h b/webrtc/api/objc/RTCAVFoundationVideoSource.h index 1d1eac0fab..d7cdbef8ce 100644 --- a/webrtc/api/objc/RTCAVFoundationVideoSource.h +++ b/webrtc/api/objc/RTCAVFoundationVideoSource.h @@ -25,8 +25,7 @@ NS_ASSUME_NONNULL_BEGIN */ @interface RTCAVFoundationVideoSource : RTCVideoSource -- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory - constraints:(nullable RTCMediaConstraints *)constraints; +- (instancetype)init NS_UNAVAILABLE; /** Returns whether rear-facing camera is available for use. */ @property(nonatomic, readonly) BOOL canUseBackCamera; diff --git a/webrtc/api/objc/RTCAudioTrack+Private.h b/webrtc/api/objc/RTCAudioTrack+Private.h index ce3298ee67..bcedca65b6 100644 --- a/webrtc/api/objc/RTCAudioTrack+Private.h +++ b/webrtc/api/objc/RTCAudioTrack+Private.h @@ -20,6 +20,10 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, readonly) rtc::scoped_refptr nativeAudioTrack; +/** Initialize an RTCAudioTrack with an id. */ +- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory + trackId:(NSString *)trackId; + @end NS_ASSUME_NONNULL_END diff --git a/webrtc/api/objc/RTCAudioTrack.h b/webrtc/api/objc/RTCAudioTrack.h index 76036ccec8..284206e7bc 100644 --- a/webrtc/api/objc/RTCAudioTrack.h +++ b/webrtc/api/objc/RTCAudioTrack.h @@ -18,10 +18,6 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)init NS_UNAVAILABLE; -/** Initialize an RTCAudioTrack with an id. */ -- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory - trackId:(NSString *)trackId; - @end NS_ASSUME_NONNULL_END diff --git a/webrtc/api/objc/RTCMediaStream+Private.h b/webrtc/api/objc/RTCMediaStream+Private.h index 4c83288190..b03b091c11 100644 --- a/webrtc/api/objc/RTCMediaStream+Private.h +++ b/webrtc/api/objc/RTCMediaStream+Private.h @@ -23,6 +23,10 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, readonly) rtc::scoped_refptr nativeMediaStream; +/** Initialize an RTCMediaStream with an id. */ +- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory + streamId:(NSString *)streamId; + /** Initialize an RTCMediaStream from a native MediaStreamInterface. */ - (instancetype)initWithNativeMediaStream: (rtc::scoped_refptr)nativeMediaStream; diff --git a/webrtc/api/objc/RTCMediaStream.h b/webrtc/api/objc/RTCMediaStream.h index e3ab754f3a..50ae7df925 100644 --- a/webrtc/api/objc/RTCMediaStream.h +++ b/webrtc/api/objc/RTCMediaStream.h @@ -29,10 +29,6 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)init NS_UNAVAILABLE; -/** Initialize an RTCMediaStream with an id. */ -- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory - streamId:(NSString *)streamId; - /** Adds the given audio track to this media stream. */ - (void)addAudioTrack:(RTCAudioTrack *)audioTrack; diff --git a/webrtc/api/objc/RTCPeerConnection+Private.h b/webrtc/api/objc/RTCPeerConnection+Private.h index feac8be6d9..031631a37a 100644 --- a/webrtc/api/objc/RTCPeerConnection+Private.h +++ b/webrtc/api/objc/RTCPeerConnection+Private.h @@ -58,6 +58,19 @@ class PeerConnectionDelegateAdapter : public PeerConnectionObserver { @property(nonatomic, readonly) rtc::scoped_refptr nativePeerConnection; +/** Initialize an RTCPeerConnection with a configuration, constraints, and + * delegate. + */ +- (instancetype)initWithFactory: + (RTCPeerConnectionFactory *)factory + configuration: + (RTCConfiguration *)configuration + constraints: + (RTCMediaConstraints *)constraints + delegate: + (nullable id)delegate + NS_DESIGNATED_INITIALIZER; + + (webrtc::PeerConnectionInterface::SignalingState)nativeSignalingStateForState: (RTCSignalingState)state; diff --git a/webrtc/api/objc/RTCPeerConnection.h b/webrtc/api/objc/RTCPeerConnection.h index 2c5c19e00f..e0f9b78e8e 100644 --- a/webrtc/api/objc/RTCPeerConnection.h +++ b/webrtc/api/objc/RTCPeerConnection.h @@ -117,19 +117,6 @@ typedef NS_ENUM(NSInteger, RTCStatsOutputLevel) { - (instancetype)init NS_UNAVAILABLE; -/** Initialize an RTCPeerConnection with a configuration, constraints, and - * delegate. - */ -- (instancetype)initWithFactory: - (RTCPeerConnectionFactory *)factory - configuration: - (RTCConfiguration *)configuration - constraints: - (RTCMediaConstraints *)constraints - delegate: - (nullable id)delegate - NS_DESIGNATED_INITIALIZER; - /** Sets the PeerConnection's global configuration to |configuration|. * Any changes to STUN/TURN servers or ICE candidate policy will affect the * next gathering phase, and cause the next call to createOffer to generate diff --git a/webrtc/api/objc/RTCPeerConnectionFactory.h b/webrtc/api/objc/RTCPeerConnectionFactory.h index c427c1bb91..8897c999e0 100644 --- a/webrtc/api/objc/RTCPeerConnectionFactory.h +++ b/webrtc/api/objc/RTCPeerConnectionFactory.h @@ -12,7 +12,48 @@ NS_ASSUME_NONNULL_BEGIN +#if defined(WEBRTC_IOS) +@class RTCAVFoundationVideoSource; +#endif +@class RTCAudioTrack; +@class RTCConfiguration; +@class RTCMediaConstraints; +@class RTCMediaStream; +@class RTCPeerConnection; +@class RTCVideoSource; +@class RTCVideoTrack; +@protocol RTCPeerConnectionDelegate; + @interface RTCPeerConnectionFactory : NSObject + +- (instancetype)init NS_DESIGNATED_INITIALIZER; + +#if defined(WEBRTC_IOS) +/** Initialize an RTCAVFoundationVideoSource with constraints. */ +- (RTCAVFoundationVideoSource *)avFoundationVideoSourceWithConstraints: + (nullable RTCMediaConstraints *)constraints; +#endif + +/** Initialize an RTCAudioTrack with an id. */ +- (RTCAudioTrack *)audioTrackWithTrackId:(NSString *)trackId; + +/** Initialize an RTCVideoTrack with a source and an id. */ +- (RTCVideoTrack *)videoTrackWithSource:(RTCVideoSource *)source + trackId:(NSString *)trackId; + +/** Initialize an RTCMediaStream with an id. */ +- (RTCMediaStream *)mediaStreamWithStreamId:(NSString *)streamId; + +/** Initialize an RTCPeerConnection with a configuration, constraints, and + * delegate. + */ +- (RTCPeerConnection *)peerConnectionWithConfiguration: + (RTCConfiguration *)configuration + constraints: + (RTCMediaConstraints *)constraints + delegate: + (nullable id)delegate; + @end NS_ASSUME_NONNULL_END diff --git a/webrtc/api/objc/RTCPeerConnectionFactory.mm b/webrtc/api/objc/RTCPeerConnectionFactory.mm index 31f3dda640..a7f9c594dc 100644 --- a/webrtc/api/objc/RTCPeerConnectionFactory.mm +++ b/webrtc/api/objc/RTCPeerConnectionFactory.mm @@ -10,7 +10,16 @@ #import "RTCPeerConnectionFactory.h" +#if defined(WEBRTC_IOS) +#import "webrtc/api/objc/RTCAVFoundationVideoSource+Private.h" +#endif +#import "webrtc/api/objc/RTCAudioTrack+Private.h" +#import "webrtc/api/objc/RTCMediaStream+Private.h" +#import "webrtc/api/objc/RTCPeerConnection+Private.h" #import "webrtc/api/objc/RTCPeerConnectionFactory+Private.h" +#import "webrtc/api/objc/RTCVideoSource+Private.h" +#import "webrtc/api/objc/RTCVideoTrack+Private.h" +#import "webrtc/base/objc/NSString+StdString.h" @implementation RTCPeerConnectionFactory { rtc::scoped_ptr _signalingThread; @@ -35,4 +44,41 @@ return self; } +#if defined(WEBRTC_IOS) +- (RTCAVFoundationVideoSource *)avFoundationVideoSourceWithConstraints: + (nullable RTCMediaConstraints *)constraints { + return [[RTCAVFoundationVideoSource alloc] initWithFactory:self + constraints:constraints]; +} +#endif + +- (RTCAudioTrack *)audioTrackWithTrackId:(NSString *)trackId { + return [[RTCAudioTrack alloc] initWithFactory:self + trackId:trackId]; +} + +- (RTCVideoTrack *)videoTrackWithSource:(RTCVideoSource *)source + trackId:(NSString *)trackId { + return [[RTCVideoTrack alloc] initWithFactory:self + source:source + trackId:trackId]; +} + +- (RTCMediaStream *)mediaStreamWithStreamId:(NSString *)streamId { + return [[RTCMediaStream alloc] initWithFactory:self + streamId:streamId]; +} + +- (RTCPeerConnection *)peerConnectionWithConfiguration: + (RTCConfiguration *)configuration + constraints: + (RTCMediaConstraints *)constraints + delegate: + (nullable id)delegate { + return [[RTCPeerConnection alloc] initWithFactory:self + configuration:configuration + constraints:constraints + delegate:delegate]; +} + @end diff --git a/webrtc/api/objc/RTCVideoTrack+Private.h b/webrtc/api/objc/RTCVideoTrack+Private.h index cd7de48ebc..be041246c3 100644 --- a/webrtc/api/objc/RTCVideoTrack+Private.h +++ b/webrtc/api/objc/RTCVideoTrack+Private.h @@ -20,6 +20,11 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, readonly) rtc::scoped_refptr nativeVideoTrack; +/** Initialize an RTCVideoTrack with its source and an id. */ +- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory + source:(RTCVideoSource *)source + trackId:(NSString *)trackId; + @end NS_ASSUME_NONNULL_END diff --git a/webrtc/api/objc/RTCVideoTrack.h b/webrtc/api/objc/RTCVideoTrack.h index e8dad3b9e1..1d88376068 100644 --- a/webrtc/api/objc/RTCVideoTrack.h +++ b/webrtc/api/objc/RTCVideoTrack.h @@ -23,11 +23,6 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)init NS_UNAVAILABLE; -/** Initialize an RTCVideoTrack with its source and an id. */ -- (instancetype)initWithFactory:(RTCPeerConnectionFactory *)factory - source:(RTCVideoSource *)source - trackId:(NSString *)trackId; - /** Register a renderer that will render all frames received on this track. */ - (void)addRenderer:(id)renderer; diff --git a/webrtc/examples/objc/AppRTCDemo/ARDAppClient.m b/webrtc/examples/objc/AppRTCDemo/ARDAppClient.m index 32d237a36a..d8dc771424 100644 --- a/webrtc/examples/objc/AppRTCDemo/ARDAppClient.m +++ b/webrtc/examples/objc/AppRTCDemo/ARDAppClient.m @@ -502,10 +502,9 @@ static BOOL const kARDAppClientEnableTracing = NO; RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints]; RTCConfiguration *config = [[RTCConfiguration alloc] init]; config.iceServers = _iceServers; - _peerConnection = [[RTCPeerConnection alloc] initWithFactory:_factory - configuration:config - constraints:constraints - delegate:self]; + _peerConnection = [_factory peerConnectionWithConfiguration:config + constraints:constraints + delegate:self]; // Create AV media stream and add it to the peer connection. RTCMediaStream *localStream = [self createLocalMediaStream]; [_peerConnection addStream:localStream]; @@ -608,16 +607,14 @@ static BOOL const kARDAppClientEnableTracing = NO; } - (RTCMediaStream *)createLocalMediaStream { - RTCMediaStream *localStream = - [[RTCMediaStream alloc] initWithFactory:_factory streamId:@"ARDAMS"]; + RTCMediaStream *localStream = [_factory mediaStreamWithStreamId:@"ARDAMS"]; RTCVideoTrack *localVideoTrack = [self createLocalVideoTrack]; if (localVideoTrack) { [localStream addVideoTrack:localVideoTrack]; [_delegate appClient:self didReceiveLocalVideoTrack:localVideoTrack]; } RTCAudioTrack *localAudioTrack = - [[RTCAudioTrack alloc] initWithFactory:_factory - trackId:@"ARDAMSa0"]; + [_factory audioTrackWithTrackId:@"ARDAMSa0"]; [localStream addAudioTrack:localAudioTrack]; return localStream; } @@ -634,12 +631,10 @@ static BOOL const kARDAppClientEnableTracing = NO; RTCMediaConstraints *mediaConstraints = [self defaultMediaStreamConstraints]; RTCAVFoundationVideoSource *source = - [[RTCAVFoundationVideoSource alloc] initWithFactory:_factory - constraints:mediaConstraints]; + [_factory avFoundationVideoSourceWithConstraints:mediaConstraints]; localVideoTrack = - [[RTCVideoTrack alloc] initWithFactory:_factory - source:source - trackId:@"ARDAMSv0"]; + [_factory videoTrackWithSource:source + trackId:@"ARDAMSv0"]; } #endif return localVideoTrack;