The TRTC SDK supports screen sharing on Android. This means you can share your screen with other users in the same room. Pay attention to the following points regarding this feature:
To start screen sharing on Android, simply call startScreenCapture() in TRTCCloud
. However, to ensure the stability and video quality of screen sharing, you need to do the following.
Copy the activity below and paste it in the manifest file. You can skip this if the activity is already included in your project code.
<activity
android:name="com.tencent.rtmp.video.TXScreenCapture$TXScreenCaptureAssistantActivity"
android:theme="@android:style/Theme.Translucent"/>
By setting the first parameter encParams
in startScreenCapture(), you can specify the encoding quality of screen sharing. If encParams
is set to null
, the SDK will use the encoding parameters set previously. We recommend the following settings:
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 |
Note:
- 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.
Since Android 7.0, apps running in the background tend to be killed by the system if they consume CPU. To prevent your app from being killed when it is sharing the screen in the background, you need to create a floating window when screen sharing starts, which also serves the purpose of reminding the user to avoid displaying personal information as his or her screen is being shared.
The code in tool.dart offers an example of how to create a mini floating window similar to the one in VooV Meeting:
// Create a floating window when screen sharing starts to prevent the app from being killed when running in the background
static void showOverlayWindow() {
SystemWindowHeader header = SystemWindowHeader(
title: SystemWindowText(
text: "Screen being shared", fontSize: 14, textColor: Colors.black45),
decoration: SystemWindowDecoration(startColor: Colors.grey[100]),
);
SystemAlertWindow.showSystemWindow(
width: 18,
height: 95,
header: header,
margin: SystemWindowMargin(top: 200),
gravity: SystemWindowGravity.TOP,
);
}
You can implement in-app sharing simply by calling the startScreenCapture API of the TRTC SDK, passing in the encoding parameter TRTCVideoEncParam
, and setting the appGroup
parameter to ''
. If TRTCVideoEncParam
is set to null
, 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 |
Note:
- 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.
You can find the sample code for cross-app sharing in the ios directory of the TRTC demo. The directory contains the following files:
├── Broadcast.Upload // Code for the screen recording process Broadcast Upload Extension. For details, see step 2 below.
│ ├── Broadcast.Upload.entitlements // Code for configuring an App Group to enable communication between processes
│ ├── Broadcast.UploadDebug.entitlements // Code for configuring an App Group to enable communication between processes (debug environment)
│ ├── Info.plist
│ └── SampleHandler.swift // Code for receiving screen recording data from the system
├── Resource // Resource file
├── Runner // A simple TRTC demo
├── TXLiteAVSDK_ReplayKitExt.framework //TXLiteAVSDK_ReplayKitExt SDK
You can run the demo as instructed in README.
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 a screen needs to be shared and is responsible for receiving the screen images captured by the system. For this, you need to do the following:
TXLiteAVSDK_ReplayKitExt.framework
from the SDK package, which is tailored for the extension module.pubspec.yaml
file and import the replay_kit_launcher
plugin to make it possible to start screen sharing by tapping a button (optional), as in TRTC Demo Screen.# Import the TRTC SDK and `replay_kit_launcher`
dependencies:
tencent_trtc_cloud: ^0.2.1
replay_kit_launcher: ^0.2.0+1
Note:If you skip step 1, that is, if you do not configure an App Group (by passing
null
in the API), you can still enable the screen sharing feature, but its stability will be compromised. Therefore, to ensure the stability of screen sharing, we suggest that you configure an App Group as described in this document.
Log in to https://developer.apple.com/ and do the following. You need to download the provisioning profile again afterwards.
AppGroup
value passed in to the API. After this, click Continue.TXLiteAVSDK_ReplayKitExt.framework
in the SDK package into the project and select the target created.target name.entitlements
will appear in the file list as shown below. Select it, click "+", and enter the App Group
created earlier.SampleHandler.swift
file. Replace the file content with the following code. You need to change APPGROUP
in the code to the App Group Identifier created earlier.import ReplayKit
import TXLiteAVSDK_ReplayKitExt
let APPGROUP = "group.com.tencent.comm.trtc.demo"
class SampleHandler: RPBroadcastSampleHandler, TXReplayKitExtDelegate {
let recordScreenKey = Notification.Name.init("TRTCRecordScreenKey")
override func broadcastStarted(withSetupInfo setupInfo: [String : NSObject]?) {
// User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.
TXReplayKitExt.sharedInstance().setup(withAppGroup: APPGROUP, delegate: self)
}
override func broadcastPaused() {
// User has requested to pause the broadcast. Samples will stop being delivered.
}
override func broadcastResumed() {
// User has requested to resume the broadcast. Samples delivery will resume.
}
override func broadcastFinished() {
// User has requested to finish the broadcast.
TXReplayKitExt.sharedInstance() .finishBroadcast()
}
func broadcastFinished(_ broadcast: TXReplayKitExt, reason: TXReplayKitExtReason) {
var tip = ""
switch reason {
case TXReplayKitExtReason.requestedByMain:
tip = "Screen sharing ended"
break
case TXReplayKitExtReason.disconnected:
tip = "App was disconnected"
break
case TXReplayKitExtReason.versionMismatch:
tip = "Integration error (SDK version mismatch)"
break
default:
break
}
let error = NSError(domain: NSStringFromClass(self.classForCoder), code: 0, userInfo: [NSLocalizedFailureReasonErrorKey:tip])
finishBroadcastWithError(error)
}
override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) {
switch sampleBufferType {
case RPSampleBufferType.video:
// Handle video sample buffer
TXReplayKitExt.sharedInstance() .sendVideoSampleBuffer(sampleBuffer)
break
case RPSampleBufferType.audioApp:
// Handle audio sample buffer for app audio
break
case RPSampleBufferType.audioMic:
// Handle audio sample buffer for mic audio
break
@unknown default:
// Handle other sample buffer types
fatalError("Unknown type of sample buffer")
}
}
}
Before screen sharing starts, the host app must be on standby to receive screen recording data from the Broadcast Upload Extension. To achieve this, follow these steps:
Make sure that camera capturing is disabled in TRTCCloud
; if not, call stopLocalPreview to disable it.
Call startScreenCapture, passing in the AppGroup
set in step 1 to put the SDK on standby.
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.
You can call stopScreenCapture to stop screen sharing at any time.
// Start screen sharing. You need to replace `APPGROUP` with the App Group created in the steps above.
trtcCloud.startScreenCapture(
TRTCVideoEncParam(
videoFps: 10,
videoResolution: TRTCCloudDef.TRTC_VIDEO_RESOLUTION_1280_720,
videoBitrate: 1600,
videoResolutionMode: TRTCCloudDef.TRTC_VIDEO_RESOLUTION_MODE_PORTRAIT,
),
iosAppGroup,
);
// Stop screen sharing
await trtcCloud.stopScreenCapture();
// Event notification for the start of screen sharing, which can be received through `TRTCCloudListener`
onRtcListener(type, param){
if (type == TRTCCloudListener.onScreenCaptureStarted) {
// Screen sharing starts.
}
}
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 TRTC Demo Screen, follow these steps:
replay_kit_launcher
plugin to your project.ReplayKitLauncher.launchReplayKitBroadcast(iosExtensionName);
in the response function of the button to activate the screen sharing feature.// Customize a response for button tapping.
onShareClick() async {
if (Platform.isAndroid) {
if (await SystemAlertWindow.requestPermissions) {
MeetingTool.showOverlayWindow();
}
} else {
// The screen sharing feature can only be tested on a real device.
ReplayKitLauncher.launchReplayKitBroadcast(iosExtensionName);
}
}
TRTCCloudListener
. Can there be multiple channels of screen sharing streams in a room at the same time?
Currently, each TRTC room can have only one channel of screen sharing stream.
Was this page helpful?