Screen sharing refers to the process of sharing your screen with other users in the room during a video call or interactive live streaming. This feature can be applied to a video conference, live game streaming, an online class, and other scenarios. for example, the teacher can share the slides with the students in the room to enhance communication.
This feature only shares the whole screen. Sharing part of a specified screen or window is not supported for now.
Before implementing the screen sharing feature, make sure you complete the following steps:
ZEGOCLOUD uses the Replaykit framework to record video from the screen to implement screen sharing on the iOS platform.
Therefore, you will need to provide an additional Extension process for the current App (the main App process) to record the screen, and then make related API calls of the ZEGO Express SDK
for implementing the screen sharing feature.
The main App process and the Extension process are two different processes, so the userID
and streamID
in the Extension can't be the same as those in the main App process.
The following steps shows the main process of implementing the feature:
In your project, create a Target
of Broadcast Upload Extension
, and integrate the ZEGO Express SDK
into your project.
Add an App Groups
in the Target
of Broadcast Upload Extension
, which enables the Extension process to synchronize configurations with the main App process.
Use the Extension process to record your screen, and call the handleReplayKitSampleBuffer
method to send the recorded screen data to the ZEGO Express SDK
.
Manually enable the screen sharing feature.
The following sections explain each step of this process in more detail.
The memory limit of the Broadcast Upload Extension is 50 MB, make sure the memory usage of the Extension for screen sharing does not exceed 50 MB.
Don't need to check the Include UI Extension option.
In Xcode, select your target project, and select Signing & Capabilities > Capability > App Groups.
In the App Groups window, bind the Extension and the main App process.
Use the iOS system API NSUserDefaults
to synchronize configuration data between the Extension process and the main App process (such as AppID, roomID, etc.).
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:appGroup];
For the following methods, check out the source files in the /ZegoExpressExample-iOS-OC/Topics/ScreenCapture/ZegoExpressExample-iOS-OC-Broadcast/SampleHandler.m
file in the Sample codes.
Make sure in the Info.plist
file, you set the RPBroadcastProcessMode
to 1RPBroadcastProcessModeSampleBuffer`.
Integrate the ZEGO Express SDK
in the Extension process. For details, see Integration.
When the screen recording starts, the system sends out notification through the broadcastStartedWithSetupInfo
callback, and you will need to call the following ZEGO Express SDK methods to implement related features:
a. To create a ZegoExpressEngine, call the createEngine
method.
ZegoEngineConfig *engineConfig = [[ZegoEngineConfig alloc] init];
engineConfig.advancedConfig = @{
@"replaykit_handle_rotation": @"false",
// Specify not to process the screen rotation on the publisher side, but to process it on the player side, thereby reduce memory usage, but in this case, the player must not play this stream from the CDN but directly from the ZEGO server. If you need to play this stream from the CDN and the captured screen needs to be dynamically rotated, please comment on this line of code.
@"max_channels": @"0", // Specify the max number of streams to play as 0 (Because this extension only needs to publish stream)
@"max_publish_channels": @"1" // Specify the max number of streams to publish as 1
};
// Set engine config
[ZegoExpressEngine setEngineConfig:engineConfig];
ZegoEngineProfile *profile = [ZegoEngineProfile new];
// The AppID value you get from the ZEGO Admin console.
profile.appID = appID;
// Use the general scenario.
profile.scenario = ZegoScenarioGeneral;
// Create a ZegoExpressEngine instance
[ZegoExpressEngine createEngineWithProfile:profile eventHandler:self];
// Enable hardware encoder to reduce the memory usage when publishing stream.
[[ZegoExpressEngine sharedEngine] enableHardwareEncoder:YES];
b. To initialize the Express ReplayKit
module, call the prepareForReplayKit
method.
// Init SDK ReplayKit module
[[ZegoExpressEngine sharedEngine] prepareForReplayKit];
c. To log in to a room, call the loginRoom
method.
// Login room
[[ZegoExpressEngine sharedEngine] loginRoom:self.roomID user:[ZegoUser userWithUserID:self.userID userName:self.userName]];
d. To publish the screen sharing streams, call the startPublishingStream
method.
// Start Publishing
[[ZegoExpressEngine sharedEngine] startPublishingStream:self.streamID];
In the system callback processSampleBuffer
, call the handleReplayKitSampleBuffer
to send the recorded screen sharing streams to the ZEGO Express SDK.
[[ZegoExpressEngine sharedEngine] handleReplayKitSampleBuffer:sampleBuffer bufferType:sampleBufferType];
When screen recording ends, the system sends out notification through the broadcastFinished
callback, and then you can call the following ZEGO Express SDK methods to implement related features:
To stop publishing the screen sharing streams, call the stopPublishingStream
method.
[[ZegoExpressEngine sharedEngine] stopPublishingStream];
To log out from a room, call the logoutRoom
method.
[[ZegoExpressEngine sharedEngine] logoutRoom:self.roomID];
To destroy the ZegoExpressEngine, call the destroyEngine
method.
[ZegoExpressEngine destroyEngine];
After the preceding screen sharing operations are complete, you must manually enable the screen sharing feature.
You can choose either the following two methods to enable the feature:
In the control center of the iOS system, tap and hold the Screen Sharing button, and select the corresponding Extension process to start the screen recording.
RPSystemBroadcastPickerView
in iOS 12.0, which enables the starter to pop up when running an App, available for the user to decide whether to enable the screen sharing. But not yet RPSystemBroadcastPickerView
does not support customizing interface and no official method provided.RPSystemBroadcastPickerView
system class, set the BundleID
of the Extension process.RPSystemBroadcastPickerView
for UIButton, and trigger its click event.