Enabling Screen Sharing
TRTC supports two screen sharing schemes on iOS:
In-app sharing
With in-app sharing, sharing is limited to the views of the current app. This feature is supported on iOS 13 and later. As content outside the current app cannot be shared, this feature is suitable for scenarios with high requirements on privacy protection.
Cross-app sharing
Based on Apple's ReplayKit scheme, cross-app sharing allows the sharing of content across the system, but the steps required to implement this feature are more complicated than those for in-app sharing as an additional extension is needed.
Supported Platforms
iOS | Android | macOS | Windows | Electron | Chrome |
✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
In-App Sharing
You can implement in-app sharing simply by calling the startScreenCaptureInApp API of the TRTC SDK, passing in the encoding parameter
TRTCVideoEncParam
. If TRTCVideoEncParam
is set to nil
, the SDK will use the encoding parameters set previously.We recommend the following encoding settings for screen sharing on iOS:
Item | Parameter | Recommended Value for Regular Scenarios | Recommended Value for Text-based Teaching |
Resolution | videoResolution | 1280 × 720 | 1920 × 1080 |
Frame rate | videoFps | 10 fps | 8 fps |
Highest bitrate | videoBitrate | 1600 Kbps | 2000 Kbps |
Resolution adaption | enableAdjustRes | NO | NO |
As screen content generally does not change drastically, it is not economical to use a high frame rate. We recommend setting it to 10 fps.
If the screen you share contains a large amount of text, you can increase the resolution and bitrate accordingly.
The highest bitrate (
videoBitrate
) refers to the highest output bitrate when a shared screen changes dramatically. If the shared content does not change a lot, the actual encoding bitrate will be lower.Cross-App Sharing
To enable cross-app screen sharing on iOS, you need to add the screen recording process Broadcast Upload Extension, which works with the host app to push streams. A Broadcast Upload Extension is created by the system when screen sharing is requested and is responsible for receiving the screen images captured by the system. For this, you need to do the following:
1. Create an App Group and configure it in Xcode (optional) to enable communication between the Broadcast Upload Extension and host app.
2. Create a target of Broadcast Upload Extension in your project and import into it
TXLiteAVSDK_ReplayKitExt.framework
from the SDK package.3. Put the host app on standby to receive screen recording data from the Broadcast Upload Extension.
notice
If you skip step 1, that is, if you do not configure an App Group (passing in
nil
to the API), you can still enable screen sharing, but its stability will be compromised. We suggest you configure an App Group as described below.
Step 1. Create an App Group
Log in to https://developer.apple.com/ and do the following. You need to download the provisioning profile again afterwards.
1. Click Certificates, IDs & Profiles.
2. Click + next to Identifiers.
3. Select App Groups and click Continue.
4. Fill in the Description and Identifier boxes. For Identifier, type the
AppGroup
value passed in to the API. Click Continue.
5. Select Identifiers on the top left sidebar, and click your App ID (you need to configure the App ID for the host app and extension in the same way).
6. Select App Groups and click Edit.
7. Select the App Group you created, click Continue to return to the edit page, and click Save to save the settings.
8. Download the provisioning profile again and import it to Xcode.
Step 2. Create a Broadcast Upload Extension
1. Click File > New > Target... in the Xcode menu and select Broadcast Upload Extension.
2. In the dialog box that pops up, enter the information required. You don't need to select Include UI Extension. After entering the required information, click Finish.
3. Drag
TXLiteAVSDK_ReplayKitExt.framework
in the SDK package into the project and select the target created.4. Select the target you created, click + Capability, and double-click App Groups.
A file named target name.entitlements
will appear in the file list as shown below. Select it, click +, and enter the App Group created earlier.
5. Select the target of the host app and configure it in the same way as described above.
6. In the new target, Xcode will automatically create a file named
SampleHandler.h
. Replace the file content with the following code. You need to change APPGROUP
in the code to the App Group Identifier created earlier.#import "SampleHandler.h"@import TXLiteAVSDK_ReplayKitExt;#define APPGROUP @"group.com.tencent.liteav.RPLiveStreamShare"@interface SampleHandler() <TXReplayKitExtDelegate>@end@implementation SampleHandler// Note: Replace `APPGROUP` with the ID of the App Group created earlier.- (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo {[[TXReplayKitExt sharedInstance] setupWithAppGroup:APPGROUP delegate:self];}- (void)broadcastPaused {// User has requested to pause the broadcast. Samples will stop being delivered.}- (void)broadcastResumed {// User has requested to resume the broadcast. Samples delivery will resume.}- (void)broadcastFinished {[[TXReplayKitExt sharedInstance] finishBroadcast];// User has requested to finish the broadcast.}#pragma mark - TXReplayKitExtDelegate- (void)broadcastFinished:(TXReplayKitExt *)broadcast reason:(TXReplayKitExtReason)reason{NSString *tip = @"";switch (reason) {case TXReplayKitExtReasonRequestedByMain:tip = @"Screen sharing ended";break;case TXReplayKitExtReasonDisconnected:tip = @"Application disconnected";break;case TXReplayKitExtReasonVersionMismatch:tip = @"Integration error (SDK version mismatch)";break;}NSError *error = [NSError errorWithDomain:NSStringFromClass(self.class)code:0userInfo:@{NSLocalizedFailureReasonErrorKey:tip}];[self finishBroadcastWithError:error];}- (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType {switch (sampleBufferType) {case RPSampleBufferTypeVideo:[[TXReplayKitExt sharedInstance] sendVideoSampleBuffer:sampleBuffer];break;case RPSampleBufferTypeAudioApp:// Handle audio sample buffer for app audiobreak;case RPSampleBufferTypeAudioMic:// Handle audio sample buffer for mic audiobreak;default:break;}}@end
Step 3. Make the host app wait to receive data
Before screen sharing starts, the host application must be put on standby to receive screen recording data from the Broadcast Upload Extension. To do this, follow these steps:
1. Make sure that camera capturing has been disabled in
TRTCCloud
; if not, call stopLocalPreview to disable it.2. Call the startScreenCaptureByReplaykit:appGroup: API, passing in the
AppGroup
set in step 1 to put the SDK on standby.3. The SDK will then wait for a user to trigger screen sharing. If a "triggering button" is not added as described in step 4, users need to press and hold the screen recording button in the iOS Control Center to start screen sharing.
4. You can call stopScreenCapture to stop screen sharing at any time.
// Start screen sharing. You need to replace `APPGROUP` with the ID of the App Group created earlier.- (void)startScreenCapture {TRTCVideoEncParam *videoEncConfig = [[TRTCVideoEncParam alloc] init];videoEncConfig.videoResolution = TRTCVideoResolution_1280_720;videoEncConfig.videoFps = 10;videoEncConfig.videoBitrate = 2000;// You need to replace `APPGROUP` with the App Group Identifier created earlier.[[TRTCCloud sharedInstance] startScreenCaptureByReplaykit:videoEncConfigappGroup:APPGROUP];}// Stop screen sharing- (void)stopScreenCapture {[[TRTCCloud sharedInstance] stopScreenCapture];}// Event notification for the start of screen sharing, which can be received through `TRTCCloudDelegate`- (void)onScreenCaptureStarted[self showTip:@"Screen sharing started"];}
Step 4. Add a screen sharing triggering button (optional)
In step 3, users need to start screen sharing manually by pressing and holding the screen recording button in the Control Center. To make it possible to start screen sharing by tapping a button in your app as in VooV Meeting, follow these steps:
1. The TRTCBroadcastExtensionLauncher file in the Demo implements screen sharing. Find and add it to your project.
2. Add a button to your UI and call the
launch
function of TRTCBroadcastExtensionLauncher
in the response function of the button to trigger screen sharing.// Customize a response for button tapping- (IBAction)onScreenButtonTapped:(id)sender {[TRTCBroadcastExtensionLauncher launch];}
notice
Apple added
RPSystemBroadcastPickerView
to iOS 12.0, which can show a picker view in apps for users to select whether to start screen sharing. Currently, RPSystemBroadcastPickerView
does not support custom UI, and Apple does not provide an official triggering method.TRTCBroadcastExtensionLauncher
works by going through the subviews of RPSystemBroadcastPickerView
, finding the UI button, and triggering its tapping event.Note that this scheme is not recommended by Apple and may become invalid in its next update. We have therefore made step 4 optional. You need to bear the risks of using the scheme yourself.
Watching Shared Screen
Watch screens shared by macOS/Windows users
When a macOS/Windows user in a room starts screen sharing, the screen will be shared through the substream, and other users in the room will be notified via onUserSubStreamAvailable in
TRTCCloudDelegate
.
Users who want to watch the shared screen can start rendering the substream of the remote user by calling the startRemoteSubStreamView API.Watch screens shared by Android/iOS users
When an Android/iOS user starts screen sharing, the screen will be shared through the primary stream, and other users in the room will be notified via onUserVideoAvailable in
TRTCCloudDelegate
.
Users who want to watch the shared screen can start rendering the primary stream of the remote user by calling the startRemoteView API.