Kotlin

Starting the capture

startCapture() is the next method we need to implement. It is called by the SDK when the publisher begins capturing (e.g. after Session.publish(publisher)). It should start your capture loop or listener. As you'll see the frames here, are delivered by ImageReader.setOnImageAvailableListener, so startCapture() will mainly signal that capture is active.

Lifecycle:

  1. init() – Create ImageReader and start a background thread. The ImageReader provides a Surface that VirtualDisplay renders into.
  2. createVirtualDisplay() – Create a VirtualDisplay backed by the ImageReader's surface. Screen content is rendered into this display.
  3. startCapture() – Called when the publisher starts. Mark capturing as active.
  4. stopCapture() – Release the virtual display, stop media projection, and clean up.

Feeding Frames to the SDK

When the system renders a new frame into the VirtualDisplay, ImageReader delivers it via onImageAvailable:

The provideIntArrayFrame method, defined by the BaseVideoCapturer class, sends an integer array of data to the publisher, to be used for the next video frame published.

If the publisher is still capturing video, the thread starts again after another 1/15 of a second, so that the capturer continues to supply the publisher with new video frames to publish.