Share the Screen
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.
As of v3.7.0, Agora provides the React Native API for screen sharing. This page describes how to implement screen sharing on Android and iOS platforms using the React Native SDK v3.7.0 and later.
Prerequisites
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.
Implement screen sharing on Android
To implement screen sharing on the Android platform, you only need to call startScreenCapture
to enable screen sharing. You can refer to example/src/examples/advanced/ScreenSharing/ScreenSharing.tsx
in react-native-agora to implement screen sharing.
Implement screen sharing on iOS
Understand the tech
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
-
Go to your project directory, and open
ios/.xcodeproj
with Xcode. -
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:
a. In Xcode, click File > New > Target..., select Broadcast Upload Extension in the pop-up window, and click Next.
b. 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.c. Under Target, select the Extension you have created, click General, and set the iOS version to 12.0 or later under Deployment Info.
d. 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: 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.
- Method 2: 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 1 is the default method in the React Native SDK, butRPSystemBroadcastPickerView
is limited in use and might fail in later versions of iOS systems. Therefore, if Method 1 fails, use Method 2 instead.
Sample project
Agora provides an example of screen sharing in react-native-agora, where you can refer to the following files to implement screen sharing:
example/ios/ScreenSharing/SampleHandler.m
example/src/examples/advanced/ScreenSharing/ScreenSharing.tsx
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.Ensure that the pod dependencies version is consistent with the SDK dependencies version in thenode_modules/react-native-agora/react-native-agora.podspec
file. -
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.
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: