Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable Video Params Config from React Native #604

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public class CustomTwilioVideoView extends View implements LifecycleEventListene
private boolean maintainVideoTrackInBackground = false;
private String cameraType = "";
private boolean enableH264Codec = false;
private VideoDimensions videoDimensions = VideoDimensions.CIF_VIDEO_DIMENSIONS;
private Integer frameRate = 15;

@Retention(RetentionPolicy.SOURCE)
@StringDef({Events.ON_CAMERA_SWITCHED,
Expand Down Expand Up @@ -247,7 +249,7 @@ public CustomTwilioVideoView(ThemedReactContext context) {
// ===== SETUP =================================================================================

private VideoFormat buildVideoFormat() {
return new VideoFormat(VideoDimensions.CIF_VIDEO_DIMENSIONS, 15);
return new VideoFormat(this.videoDimensions, this.frameRate);
}

private CameraCapturer createCameraCaputer(Context context, String cameraId) {
Expand Down Expand Up @@ -448,7 +450,9 @@ public void connectToRoomWrapper(
boolean dominantSpeakerEnabled,
boolean maintainVideoTrackInBackground,
String cameraType,
boolean enableH264Codec
boolean enableH264Codec,
VideoDimensions dimensions,
Integer frameRate
) {
this.roomName = roomName;
this.accessToken = accessToken;
Expand All @@ -459,6 +463,12 @@ public void connectToRoomWrapper(
this.cameraType = cameraType;
this.enableH264Codec = enableH264Codec;

if (dimensions != null)
this.videoDimensions = dimensions;

if (frameRate != null)
this.frameRate = frameRate;

// Share your microphone
localAudioTrack = LocalAudioTrack.create(getContext(), enableAudio);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_DOMINANT_SPEAKER_CHANGED;
import static com.twiliorn.library.CustomTwilioVideoView.Events.ON_LOCAL_PARTICIPANT_SUPPORTED_CODECS;

import com.twilio.video.VideoDimensions;

public class CustomTwilioVideoViewManager extends SimpleViewManager<CustomTwilioVideoView> {
public static final String REACT_CLASS = "RNCustomTwilioVideoView";

Expand Down Expand Up @@ -86,6 +88,9 @@ public void receiveCommand(CustomTwilioVideoView view, int commandId, @Nullable
String cameraType = args.getString(8);
ReadableMap encodingParameters = args.getMap(9);
boolean enableH264Codec = encodingParameters.hasKey("enableH264Codec") ? encodingParameters.getBoolean("enableH264Codec") : false;
ReadableMap dimensionParameters = args.getMap(10);
VideoDimensions dimensions = dimensionParameters.hasKey("height") && dimensionParameters.hasKey("width") ? new VideoDimensions(dimensionParameters.getInt("width"), dimensionParameters.getInt("height")) : null;
Integer frameRate = args.getInt(11);
view.connectToRoomWrapper(
roomName,
accessToken,
Expand All @@ -96,7 +101,9 @@ public void receiveCommand(CustomTwilioVideoView view, int commandId, @Nullable
dominantSpeakerEnabled,
maintainVideoTrackInBackground,
cameraType,
enableH264Codec
enableH264Codec,
dimensions,
frameRate
);
break;
case DISCONNECT:
Expand Down
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ declare module "react-native-twilio-video-webrtc" {
videoBitrate?: number;
};
enableNetworkQualityReporting?: boolean;
videoParams?: {width: number, height: number, frameRate: number};
};

type androidConnectParams = {
Expand All @@ -149,6 +150,8 @@ declare module "react-native-twilio-video-webrtc" {
};
enableNetworkQualityReporting?: boolean;
maintainVideoTrackInBackground?: boolean;
dimensions?: {width: number, height: number};
frameRate?: number;
};

class TwilioVideo extends React.Component<TwilioVideoProps> {
Expand Down
30 changes: 26 additions & 4 deletions ios/RCTTWVideoModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,37 @@
static NSString* statsReceived = @"statsReceived";
static NSString* networkQualityLevelsChanged = @"networkQualityLevelsChanged";

static const CMVideoDimensions kRCTTWVideoAppCameraSourceDimensions = (CMVideoDimensions){900, 720};
static CMVideoDimensions kRCTTWVideoAppCameraSourceDimensions = (CMVideoDimensions){900, 720};

static const int32_t kRCTTWVideoCameraSourceFrameRate = 15;
static int32_t kRCTTWVideoCameraSourceFrameRate = 15;

TVIVideoFormat *RCTTWVideoModuleCameraSourceSelectVideoFormatBySize(AVCaptureDevice *device, CMVideoDimensions targetSize) {
TVIVideoFormat *selectedFormat = nil;

BOOL passedParamsAreGood = kRCTTWVideoAppCameraSourceDimensions.width >= targetSize.width && kRCTTWVideoAppCameraSourceDimensions.height >= targetSize.height;
// Ordered from smallest to largest.
NSOrderedSet<TVIVideoFormat *> *formats = [TVICameraSource supportedFormatsForDevice:device];

for (TVIVideoFormat *format in formats) {
if (format.pixelFormat != TVIPixelFormatYUV420BiPlanarFullRange) {
continue;
}

selectedFormat = format;

if (passedParamsAreGood) {
selectedFormat.frameRate = kRCTTWVideoCameraSourceFrameRate;
selectedFormat.dimensions = kRCTTWVideoAppCameraSourceDimensions;
break;
}

// ^ Select whatever is available until we find one we like and short-circuit
CMVideoDimensions dimensions = format.dimensions;

if (dimensions.width >= targetSize.width && dimensions.height >= targetSize.height) {
break;
}

}
return selectedFormat;
}
Expand Down Expand Up @@ -397,7 +408,7 @@ -(NSMutableDictionary*)convertLocalVideoTrackStats:(TVILocalVideoTrackStats *)st
}
}

RCT_EXPORT_METHOD(connect:(NSString *)accessToken roomName:(NSString *)roomName enableAudio:(BOOL *)enableAudio enableVideo:(BOOL *)enableVideo encodingParameters:(NSDictionary *)encodingParameters enableNetworkQualityReporting:(BOOL *)enableNetworkQualityReporting dominantSpeakerEnabled:(BOOL *)dominantSpeakerEnabled cameraType:(NSString *)cameraType) {
RCT_EXPORT_METHOD(connect:(NSString *)accessToken roomName:(NSString *)roomName enableAudio:(BOOL *)enableAudio enableVideo:(BOOL *)enableVideo encodingParameters:(NSDictionary *)encodingParameters enableNetworkQualityReporting:(BOOL *)enableNetworkQualityReporting dominantSpeakerEnabled:(BOOL *)dominantSpeakerEnabled cameraType:(NSString *)cameraType videoParams:(NSDictionary *)videoParams) {
[self _setLocalVideoEnabled:enableVideo cameraType:cameraType];
if (self.localAudioTrack) {
[self.localAudioTrack setEnabled:enableAudio];
Expand All @@ -417,7 +428,7 @@ -(NSMutableDictionary*)convertLocalVideoTrackStats:(TVILocalVideoTrackStats *)st
if (self.localDataTrack) {
builder.dataTracks = @[self.localDataTrack];
}

builder.dominantSpeakerEnabled = dominantSpeakerEnabled ? YES : NO;

builder.roomName = roomName;
Expand All @@ -437,6 +448,17 @@ -(NSMutableDictionary*)convertLocalVideoTrackStats:(TVILocalVideoTrackStats *)st
builder.networkQualityConfiguration = [ [TVINetworkQualityConfiguration alloc] initWithLocalVerbosity:TVINetworkQualityVerbosityMinimal remoteVerbosity:TVINetworkQualityVerbosityMinimal];
}

if(videoParams[@"width"] && videoParams[@"height"]){
NSInteger width = [videoParams[@"width"] integerValue];
NSInteger height = [videoParams[@"height"] integerValue];
kRCTTWVideoAppCameraSourceDimensions.width = width;
kRCTTWVideoAppCameraSourceDimensions.height = height;
}

if (videoParams[@"frameRate"]) {
kRCTTWVideoCameraSourceFrameRate = [videoParams[@"frameRate"] integerValue];
}

}];

self.room = [TwilioVideoSDK connectWithOptions:connectOptions delegate:self];
Expand Down
8 changes: 6 additions & 2 deletions src/TwilioVideo.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ class CustomTwilioVideoView extends Component {
enableNetworkQualityReporting = false,
dominantSpeakerEnabled = false,
maintainVideoTrackInBackground = false,
encodingParameters = {}
encodingParameters = {},
dimensions = null,
frameRate = null
}) {
this.runCommand(nativeEvents.connectToRoom, [
roomName,
Expand All @@ -191,7 +193,9 @@ class CustomTwilioVideoView extends Component {
dominantSpeakerEnabled,
maintainVideoTrackInBackground,
cameraType,
encodingParameters
encodingParameters,
dimensions,
frameRate
])
}

Expand Down
6 changes: 4 additions & 2 deletions src/TwilioVideo.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ export default class TwilioVideo extends Component {
enableVideo = true,
encodingParameters = null,
enableNetworkQualityReporting = false,
dominantSpeakerEnabled = false
dominantSpeakerEnabled = false,
videoParams = {}
}) {
TWVideoModule.connect(accessToken,
roomName,
Expand All @@ -254,7 +255,8 @@ export default class TwilioVideo extends Component {
encodingParameters,
enableNetworkQualityReporting,
dominantSpeakerEnabled,
cameraType
cameraType,
videoParams
)
}

Expand Down