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.
Before you begin, make sure you complete the following:
Create a project in ZEGOCLOUD Admin Console and get the AppID and AppSign of your project.
Refer to the Quick Start doc to complete the SDK integration and basic function implementation.
Before recording the screen on Android, a dialog prompt will appear asking the user for permission to allow the app to record the screen. Please grant the permission.
If the target Android SDK version is 34.0.0 or later, you need to navigate to the "app/src/main" directory of your project and open the "AndroidManifest.xml" file. Then, add the following permission declarations for FOREGROUND_SERVICE and FOREGROUND_SERVICE_MEDIA_PROJECTION:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION"/>
The video source that the SDK captured for publishing streams is from the camera by default. If you need to publish the streams that the screen sharing captured, you need to switch the source through the setVideoSource.
ZegoExpressEngine.instance().setVideoSource(ZegoVideoSourceType.ScreenCapture, ZegoPublishChannel.Aux);
The audio source that the SDK captured for publishing streams is from the microphone by default. If you need to publish the streams that the screen sharing captured, you need to switch the source through the setAudioSource.
ZegoExpressEngine.instance().setAudioSource(ZegoAudioSourceType.ScreenCapture, ZegoPublishChannel.Aux);
If you only share the screen and sound within the application, call the startScreenCaptureInApp method to start screen sharing.
ZegoExpressEngine.instance().startScreenCaptureInApp();
If you need to share the screen and sound of the whole system, call the startScreenCapture interface to start Screen Sharing.
ZegoExpressEngine.instance().startScreenCapture();
Cross-app screen sharing is recorded by the iOS system through the Extension process, so an additional process needs to be created and started.
The memory usage of the Broadcast Upload Extension is limited to 50 MB, please do not allocate additional memory to the Screen Sharing Extension.
)
)
ScreenShare. After selecting Team, Language and other information, click Finish.There is no need to check the Include UI Extension.
)
After the creation is complete, you will see the Extension folder in the project. The structure is similar to the following. This folder is used to store the implementation code of the Screen Sharing function:
)
The implementation of the following system callbacks can be viewed in the file /ZegoExpressExample-iOS-OC/Topics/ScreenCapture/ZegoExpressExample-iOS-OC-Broadcast/SampleHandler.m in Sample codes:
RPBroadcastProcessMode is set to RPBroadcastProcessModeSampleBuffer in the Info.plist file of the Extension.ZegoExpressEngine.xcframework to add.
)
ZegoReplayKit class to create a data transmission channel in this callback:[ZegoReplayKitExt.sharedInstance setupWithDelegate:self];
In the processSampleBuffer system callback, send it to the Video Call SDK through the sendSampleBuffer method in the ZegoReplayKit class.
[ZegoReplayKitExt.sharedInstance sendSampleBuffer:sampleBuffer withType:sampleBufferType];
The system notifies the Extension that the screen recording has ended through the broadcastFinished callback. In this callback, you can call the following method in the ZegoReplayKit class to stop screen sharing and disconnect the data transmission channel:
[ZegoReplayKitExt.sharedInstance finished];
After the capturing process of the above screen-sharing source is completed, the captured source is published to the cloud server. (The channel for publishing the source must be consistent with the channel for setting the capturingsource)
ZegoExpressEngine.instance().startPublishingStream(streamID, ZegoPublishChannel.Aux);
After completing the above steps, other users can use the startPlayingStream method to play the screen-sharing streams you published.
// To play streams, the streamID used by the user who initiated the Screen Sharing needs to be passed in when publishing streams.
ZegoExpressEngine.instance().startPlayingStream(streamID, {"reactTag": findNodeHandle(this.refs.zego_play_view), "viewMode": 0, "backgroundColor": 0});
The iOS system only supports sharing the entire screen yet.
)
If you use the Screen Sharing function to capture audio and publish audio streams, and use the stream-playing function on the device at the same time, it will cause the iOS system to repeatedly capture and play audio streams, resulting in abnormal audio playback. It is recommended to use muteAllPlayStreamAudio to cancel all audio stream-playing operations.
