iOS code example
Overview
This guide demonstrates how to use the iOS SDK to publish a screen-sharing video, using the device screen as the source for the stream's video.
Setting Up Your Project
The code for this section is in the screen-sharing branch of the learning-opentok-ios repo, so if you haven't already, you'll need to clone the repo into a local directory — this can be done using the command line:
Then check out the branch:
This branch shows you how to capture the screen (a UIView) using a custom video capturer. Open the project in XCode to follow along.
Important: Please note that your App ID is your API key.
Exploring the Code
This sample uses the initCapture, releaseCapture, startCapture, stopCapture, and isCaptureStarted methods of the OTVideoKit class to manage capture functions of the application.
The ViewController class creates a session, instantiates subscribers and sets up the publisher.
The OTKBasicVideoCapturer class creates a frame, captures a screenshot, tags the frame with a timestamp and saves it in an instance of consumer.
The publisher accesses the consumer to obtain the frame.
The initCapture method is used to initialize the capture and sets value for the pixel format of an OTVideoFrame object. In this example, it is set to ARGB.
The releaseCapture method clears the memory buffer:
The startCapture method creates a separate thread and calls the produceFrame method to start screen captures:
The produceFrame method:
- Defines the frame for captured images
- Creates a timestamp to tag a captured image
- Takes a screenshot
- Converts the screenshot to a readable format
- Tags the screenshot with a timestamp
- Calculates the size of the image
- Sets the consumeFrame with the image
- Calls itself 15 times per second once the capture starts
The frame for the captured images is set as an object of OTVideoFrame. Properties of OTVideoFrame define the planes, timestamp, orientation and format of a frame.
A timestamp is created to tag the image. Every image is tagged with a timestamp so both publisher and subscriber are able to create the same timeline and reference the frames in the same order.
The screenshot method is called to obtain an image of the screen.
The fillPixelBufferFromCGImage method converts the image data of a CGImage into a CVPixelBuffer.
The frame is tagged with a timestamp and capture rate in frames per second and delay between captures are set.
The number of bytes in a single row is multiplied with the height of the image to obtain the size of the image. Note, the single element array and bytes per row are based on a 4-byte, single plane specification of an RGB image.
The frame is saved in an instance of consumer. The publisher accesses captured images through the consumer instance.
The pixel buffer is cleared and a background-priority queue (separate from the queue used by the UI) is used to capture images. If image capture is in progress, the produceFrame method calls itself 15 times per second.
The screenshot method takes a screenshot and returns an image. This method is called by the produceFrame method.