Swift

Initializing capture

In this example, the app uses a custom video capturer to publish random pixels (white noise). This is done simply to illustrate the basic principles of setting up a custom video capturer.

In the main VonageVideoManager, after calling session.publish(publisher, error: &error) to initiate publishing of an audio-video stream, the videoCapture property of the OTPublisher object is set to an instance of BasicVideoCapturer:

BasicVideoCapturer is a custom class that implements the OTVideoCapture protocol (defined in the Vonage iOS SDK). This protocol lets you define a custom video capturer to be used by an OpenTok publisher.

The initCapture method initializes capture settings to be used by the custom video capturer. In this sample's custom implementation of OTVideoCapture (OTKBasicVideoCapturer), the initCapture method sets properties of the videoFormat property:

The OTVideoFormat class is defined by the Vonage iOS SDK. In this sample code, the format of the video capturer is set to use ARGB as the pixel format, with a specific number of bytes per row, height, and width.

The videoCaptureConsumer property sets an OTVideoCaptureConsumer object that the video consumer uses to transmit video frames to the publisher's stream. In BasicVideoCapturer, this property is synthesized from the protocol:

Starting capture

The start method is called when a publisher starts capturing video to send as a stream to the Vonage session. This will occur after the Session.publish(_:error:) method is called. In the BasicVideoCapturer, this method triggers produceFrame on a background queue:

Frame customization

The produceFrame method generates an OTVideoFrame object that represents a frame of video. In this case, the frame contains random pixels filling the defined height and width:

The method passes the frame to the consumeFrame method of the OTVideoCaptureConsumer. This causes the publisher to send the frame of data to the video stream in the session.