Share the Screen
Introduction
Screen sharing enables the host of an interactive live streaming broadcast or video call to display what is on their screen to other users in the channel. This technology has many obvious advantages for communicating information, particularly in the following scenarios:
- During video conferencing, the speaker can share a local image, web page, or full presentation with other participants.
- For online instruction, the teacher can share slides or notes with students.
Implementation
Screen sharing on iOS is implemented by using the native iOS ReplayKit framework in an Extension to record the screen and by treating the screen sharing stream as a user joining the channel. As Apple does not support screen capture in the main app process, you need to create a separate Extension for the screen sharing stream.
Implementations for v3.7.0 and later
This section describes how to implement screen sharing using the iOS SDK v3.7.0 and later.
Before you begin, ensure that you understand how to start a video call or start interactive video streaming. For details, see Start a Video Call or Start Interactive Video Streaming.
-
Ensure that
AgoraReplayKitExtension.xcframework
is integrated into the project. This dynamic library wraps the following functionality:-
Record the screen with Apple ReplayKit.
-
Use the custom video source feature of the SDK to capture the screen recording data and send it to other users in the channel.
-
-
Create a Broadcast Upload Extension to enable the screen sharing process:
-
In Xcode, click File > New > Target..., select Broadcast Upload Extension in the pop-up window, and click Next.
-
In the pop-up window, fill in Product Name and other fields, uncheck Include UI Extension, and click Finish. Xcode automatically creates the Extension folder, which contains the
SampleHandler.h
file. -
Modify the
SampleHandler.h
file to modify the code logic that implements screen sharing:-
If you only need to use the functionality in
AgoraReplayKitExtension.xcframework
provided by Agora, modify it by checkingTarget
for the Extension you created, and setting the Value of the NSExtension > NSExtensionPrincipalClass key in the Info settings from SampleHandler to AgoraReplayKitHandler. -
If you need to add some business logic in addition to using the functionality in
AgoraReplayKitExtension.xcframework
provided by Agora, modify it by replacing the code in theSampleHandler.h
file with the following code:
-
-
-
Call
startScreenCapture
, and prompt the user's manual action to make the app start screen sharing. You can choose either of the following two methods:-
Method 1: Prompt the user to tap and hold the Screen Sharing button in the control center of the iOS system, and select the Extension you created to start screen sharing.
-
Method 2: Use RPSystemBroadcastPickerView, which is added by Apple in iOS 12.0, to pop up the "Start screen sharing" button in the app interface. It prompts the user to start screen sharing by tapping this button.
RPSystemBroadcastPickerView
is limited in use and might fail in later versions of iOS systems. Therefore, use the method 2 at your discretion. -
Sample project
Agora provides an open-source sample project on GitHub, where you can refer to the following files:
/APIExample/Examples/Advanced/ScreenShare/ScreenShare.swift
/Agora-ScreenShare-Extension/SampleHandler.swift
API reference
Some restrictions and cautions exist for using the screen sharing feature, and charges can apply. Agora recommends that you read the following API references before calling the API:
Implementations earlier than v3.7.0
This section describes how to implement screen sharing using the iOS SDK earlier than v3.7.0.
Before you begin, ensure that you understand how to start a video call or start interactive video streaming. For details, see Start a Video Call or Start Interactive Video Streaming.
1. Create an Extension for screen sharing
Create a Broadcast Upload Extension with the following steps:
-
Open the project file with Xcode, and select Editor > Add Target... in the menu bar.
-
In the pop-up window, choose Broadcast Upload Extension on the iOS page, and click Next.
-
In the Product Name field, enter a name for the Extension, for example, Agora-ScreenShare, and click Finish. You should now see a folder for the Extension in your project; this folder holds the code for implementing screen sharing.
-
Open the Podfile in the project, and add dependencies to the Extension.
-
Run the
pod install
command in the project root directory to install the dependencies.
2. Use the Extension for screen sharing in the project
Add RPSystemBroadcastPickerView
to the view of the app as a button to start screen recording.
3. Set up a custom video source
Use the custom video source function of the Agora SDK to send the system-recorded screen data to remote users for screen sharing.
Create an instance of AgoraRtcEngineKit
for the screen sharing stream, and set up the custom video source function.
4. Get screen data
Use Apple's native ReplayKit framework for screen recording.
-
In the
Info.plist
file in the Extension for screen sharing, set theRPBroadcastProcessMode
asRPBroadcastProcessModeSampleBuffer.
-
Start screen recording in the
broadcastStarted
callback.The channel name in the code example is set asScreenShare
. If you want the user to enter the channel name, you must use App Group to pass the parameter value of the main process to the Extension. -
Send the system-collected data to the Agora SDK.
Use
pushExternalVideoFrame
to enablesendVideoBuffer
. -
Call
joinChannelByToken
in theAgoraRtcEngineKit
instance corresponding to the screen sharing stream to join the channel, and then start screen sharing.
Sample project
Implementing screen sharing using the iOS SDK earlier than v3.7.0 is complicated. To reduce the difficulty of using the sample project, this section describes the files or folders used in the sample project.
File/Folder | Description |
---|---|
/iOS/Agora-ScreenShare-Extension | The Extension for the screen sharing process. The main code files are as follows:
|
/iOS/APIExample/Examples/Advanced/ScreenShare/ScreenShare.swift | The main code of the screen sharing app, which allows the local user to join a channel and enable screen sharing. |
/iOS/Agora-ScreenShare-Extension/AgoraAudioTube.mm | Shares both the screen and audio of the app at the same time by using the custom audio source function. |
Considerations
-
If you use Cocoapods, add the following lines to the
Podfile
file to add dependencies for your screen sharing Extension. Remember to replaceScreenSharing
with the target name of your screen sharing Extension. -
The memory limit of a Broadcast Upload Extension is 50 MB. Ensure that your memory usage does not exceed this limit.
-
In the screen sharing process, you need to call
muteAllRemoteVideoStreams
andmuteAllRemoteAudioStreams
to cancel receiving streams from remote users and avoid double subscriptions. -
For the main process, the screen sharing stream is equivalent to a remote user in the channel. To avoid unnecessary costs, you can exclude the screen sharing stream when setting up the remote user view.