Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Specifying Video Constraints

You can customize the properties of the video produced by a TVIVideoCapturer using TVIVideoConstraints. Constraints allow you to choose at runtime between one of the many TVIVideoFormats that are supported by the capturer. The included TVICameraCapturer class exposes formats based upon what your device's cameras are capable of.

Constraints allow you to filter by several criteria including size, frame rate, and aspect ratio. While constraints are used to resolve the actual capture format, the video sent to other Participants may be downscaled (temporally or spatially) in response to network and device conditions.

In Swift

// Create camera object
let camera = TVICameraCapturer(source: .frontCamera)

// Setup the video constraints
let videoConstraints = TVIVideoConstraints { (constraints) in
    constraints.maxSize = TVIVideoConstraintsSize960x540
    constraints.minSize = TVIVideoConstraintsSize960x540
    constraints.maxFrameRate = TVIVideoConstraintsFrameRateNone
    constraints.minFrameRate = TVIVideoConstraintsFrameRateNone
}

// Add local video track with camera and video constraints
localVideoTrack = TVILocalVideoTrack(capturer: capturer,
                                     enabled: true,
                                     constraints: videoConstraints,
                                     name: "Screen")

// If the constraints are not satisfied, a nil track will be returned.
if (localVideoTrack == nil) {
    print ("Error: Failed to create a video track using the local camera.")
}

In Objective-C

// Create camera object
TVICameraCapturer *camera = = [[TVICameraCapturer alloc] init];

// Setup the video constraints
TVIVideoConstraints *videoConstraints = [TVIVideoConstraints constraintsWithBlock:
                              ^(TVIVideoConstraintsBuilder * _Nonnull builder) {
    builder.maxSize = TVIVideoConstraintsSize960x540;
    builder.minSize = TVIVideoConstraintsSize960x540;
    builder.maxFrameRate = TVIVideoConstraintsFrameRateNone;
    builder.minFrameRate = TVIVideoConstraintsFrameRateNone;
}];

// Add local video track with camera and video constraints
TVIVideoTrack *localVideoTrack = [TVILocalVideoTrack trackWithCapturer:camera
                                                               enabled:YES
                                                           constraints:videoConstraints
                                                                  name:@"Camera"];

// If the constraints are not satisfied, a nil track will be returned.
if (localVideoTrack == nil) {
    NSLog(@"Error: Failed to create a video track using the local camera.");
}

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

Loading Code Sample...