From 5ad4639271cc739f19b755c4d554c2ad6fa6f71e Mon Sep 17 00:00:00 2001 From: Jonathan Chang Date: Wed, 11 Nov 2020 18:03:21 -0800 Subject: [PATCH] Rework local video initialization (#403) * Rework local video initialization * fix typo --- Example/ios/Podfile.lock | 10 +++---- ios/RCTTWVideoModule.m | 64 ++++++++++++++++------------------------ src/TwilioVideo.ios.js | 1 + 3 files changed, 32 insertions(+), 43 deletions(-) diff --git a/Example/ios/Podfile.lock b/Example/ios/Podfile.lock index e2aa60ef..e52b0348 100644 --- a/Example/ios/Podfile.lock +++ b/Example/ios/Podfile.lock @@ -10,7 +10,7 @@ PODS: - React/Core (= 0.59.9) - react-native-twilio-video-webrtc (1.0.2-1): - React - - TwilioVideo (~> 3.2.3) + - TwilioVideo (~> 3.7) - React/Core (0.59.9): - yoga (= 0.59.9.React) - React/CxxBridge (0.59.9): @@ -35,7 +35,7 @@ PODS: - React/cxxreact - React/jsi - React/jsinspector (0.59.9) - - TwilioVideo (3.2.5) + - TwilioVideo (3.7.1) - yoga (0.59.9.React) DEPENDENCIES: @@ -68,10 +68,10 @@ SPEC CHECKSUMS: Folly: de497beb10f102453a1afa9edbf8cf8a251890de glog: 1de0bb937dccdc981596d3b5825ebfb765017ded React: a86b92f00edbe1873a63e4a212c29b7a7ad5224f - react-native-twilio-video-webrtc: 5a1dc979bfe98c0f8dba110e9b5655d52278f862 - TwilioVideo: dec31712aab578a783703c8f699c838bea4adf06 + react-native-twilio-video-webrtc: c33953a98c71b8600ba321e01f52c62e0c20ab6e + TwilioVideo: 807526f68043a11f0e17e5c26ef0f0f73d5678e4 yoga: 03ff42a6f223fb88deeaed60249020d80c3091ee PODFILE CHECKSUM: a22eec28b45c6a20b9c752a17cdef0f608e13c10 -COCOAPODS: 1.9.2 +COCOAPODS: 1.10.0 diff --git a/ios/RCTTWVideoModule.m b/ios/RCTTWVideoModule.m index d5791c5d..88bf97d7 100644 --- a/ios/RCTTWVideoModule.m +++ b/ios/RCTTWVideoModule.m @@ -114,7 +114,9 @@ - (dispatch_queue_t)methodQueue { } - (void)addLocalView:(TVIVideoView *)view { - [self.localVideoTrack addRenderer:view]; + if (self.localVideoTrack != nil) { + [self.localVideoTrack addRenderer:view]; + } [self updateLocalViewMirroring:view]; } @@ -125,7 +127,9 @@ - (void)updateLocalViewMirroring:(TVIVideoView *)view { } - (void)removeLocalView:(TVIVideoView *)view { - [self.localVideoTrack removeRenderer:view]; + if (self.localVideoTrack != nil) { + [self.localVideoTrack removeRenderer:view]; + } } - (void)removeParticipantView:(TVIVideoView *)view sid:(NSString *)sid trackSid:(NSString *)trackSid { @@ -167,6 +171,12 @@ - (void)addParticipantView:(TVIVideoView *)view sid:(NSString *)sid trackSid:(NS return; } self.localVideoTrack = [TVILocalVideoTrack trackWithSource:self.camera enabled:YES name:@"camera"]; +} + +- (void)startCameraCapture { + if (self.camera == nil) { + return; + } AVCaptureDevice *camera = [TVICameraSource captureDeviceForPosition:AVCaptureDevicePositionFront]; [self.camera startCaptureWithDevice:camera completion:^(AVCaptureDevice *device, TVIVideoFormat *startFormat, @@ -185,8 +195,7 @@ - (void)addParticipantView:(TVIVideoView *)view sid:(NSString *)sid trackSid:(NS } RCT_EXPORT_METHOD(stopLocalVideo) { - self.localVideoTrack = nil; - self.camera = nil; + [self clearCameraInstance]; } RCT_EXPORT_METHOD(stopLocalAudio) { @@ -226,27 +235,19 @@ - (void)addParticipantView:(TVIVideoView *)view sid:(NSString *)sid trackSid:(NS RCT_REMAP_METHOD(setLocalVideoEnabled, enabled:(BOOL)enabled setLocalVideoEnabledWithResolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { - if(self.localVideoTrack != nil){ + if (self.localVideoTrack != nil) { [self.localVideoTrack setEnabled:enabled]; + if (self.camera && self.camera.device) { + if (enabled) { + [self startCameraCapture]; + } else { + [self clearCameraInstance]; + } + } resolve(@(enabled)); - } else if(enabled) { - [self createLocalVideoTrack]; - resolve(@true); - } else { - resolve(@false); - } -} - --(void)createLocalVideoTrack { - [self startLocalVideo]; - // Publish video so other Room Participants can subscribe - // This check is required when TVICameraSource return nil Eg: simulator - if(self.localVideoTrack != nil){ - [self.localParticipant publishVideoTrack:self.localVideoTrack]; } } - RCT_EXPORT_METHOD(flipCamera) { if (self.camera) { AVCaptureDevicePosition position = self.camera.device.position; @@ -376,22 +377,10 @@ -(NSMutableDictionary*)convertLocalVideoTrackStats:(TVILocalVideoTrackStats *)st } } --(void)enableLocalVideoAtCreationTime:(BOOL *)enableVideo { - if(enableVideo){ - if (self.localVideoTrack == nil) { - // We disabled video in a previous call, attempt to re-enable - [self startLocalVideo]; - } else { - [self.localVideoTrack setEnabled:true]; - } - } else { - [self stopLocalVideo]; - } -} - RCT_EXPORT_METHOD(connect:(NSString *)accessToken roomName:(NSString *)roomName enableVideo:(BOOL *)enableVideo encodingParameters:(NSDictionary *)encodingParameters enableNetworkQualityReporting:(BOOL *)enableNetworkQualityReporting) { - - [self enableLocalVideoAtCreationTime: enableVideo]; + if (enableVideo) { + [self startCameraCapture]; + } TVIConnectOptions *connectOptions = [TVIConnectOptions optionsWithToken:accessToken block:^(TVIConnectOptionsBuilder * _Nonnull builder) { if (self.localVideoTrack) { @@ -410,11 +399,11 @@ -(void)enableLocalVideoAtCreationTime:(BOOL *)enableVideo { } builder.roomName = roomName; - + if(encodingParameters[@"enableH264Codec"]){ builder.preferredVideoCodecs = @[ [TVIH264Codec new] ]; } - + if(encodingParameters[@"audioBitrate"] || encodingParameters[@"videoBitrate"]){ NSInteger audioBitrate = [encodingParameters[@"audioBitrate"] integerValue]; NSInteger videoBitrate = [encodingParameters[@"videoBitrate"] integerValue]; @@ -446,7 +435,6 @@ - (void)clearCameraInstance { // We are done with camera if (self.camera) { [self.camera stopCapture]; - self.camera = nil; } } diff --git a/src/TwilioVideo.ios.js b/src/TwilioVideo.ios.js index 590094c5..efd1a102 100644 --- a/src/TwilioVideo.ios.js +++ b/src/TwilioVideo.ios.js @@ -157,6 +157,7 @@ export default class extends Component { componentWillMount () { this._registerEvents() + this._startLocalVideo() this._startLocalAudio() }