iOS application example to capture stream from device screen
This example can be used to publish WebRTC stream from device screen with system audio or microphone audio capturing. The example works with iOS SDK 2.6.82 and newer.
The main application view is shown below. Inputs:
- WCS Websocket URL
- screen video stream name to publish
Application view when screen sharing is started
A special extension process is used to capture video from screen. This pocess works until device is locked or screen cpturing is stopped manually.
Analyzing example code
To analyze the code take ScreenCapturer example version which is available on GitHub.
- main application view class: ScreenCapturerViewController (implementation file ScreenCapturerViewController.swift)
- extension implementation class: ScreenCapturerExtensionHandler (implementation file ScreenCapturerExtensionHandler.swift)
1. Import API
2. Screen capturer extension parameters setup
UserDefaults.suiteName parameter must be equal to extension application group id
3. Receiving screen capture parameters in extension code
4. Screen capturer object setup to capture audio
5. Session creation to publish screen stream
WCSSession, WCSSession.connect code
6. Screen stream publishing
WCSSession.createStream, WCSStream.publish code
The following parameters are passed to createStream method:
- stream name to publish
- ScreenRTCVideoCapturer object to capture video from screen
7. ScreenRTCVideoCapturer class initialization
8. System audio capturing in extension code
9. Microphone audio capturing in extension code
1. Music from iTunes will not play when system audio capturing is active.
2. ScreenCapturerSwift extension will receive a silence in
sampleBuffer both from microphone and system audio if some other application uses the microphone. When microphone is released by other application, it is necessary to stop screen publishing and start it again to receive any audio.