Skip to content

Commit

Permalink
Rework local video initialization (blackuy#403)
Browse files Browse the repository at this point in the history
* Rework local video initialization

* fix typo
  • Loading branch information
slycoder authored Nov 12, 2020
1 parent 416b601 commit 5ad4639
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
10 changes: 5 additions & 5 deletions Example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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
64 changes: 26 additions & 38 deletions ios/RCTTWVideoModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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];
Expand Down Expand Up @@ -446,7 +435,6 @@ - (void)clearCameraInstance {
// We are done with camera
if (self.camera) {
[self.camera stopCapture];
self.camera = nil;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/TwilioVideo.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export default class extends Component {

componentWillMount () {
this._registerEvents()
this._startLocalVideo()
this._startLocalAudio()
}

Expand Down

0 comments on commit 5ad4639

Please sign in to comment.