Skip to main content

Screen share

The local camera stream can be replaced with a capture of the screen. Either as a simple replacement of the own video, or as a full screen presentation.

In order to start the capturing, you need to acquire a media projection token. For that we will use the API from the Jetpack Fragments library.

Media projection token

val requestScreenCapturePermission =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == AppCompatActivity.RESULT_OK) {
// Start screen share for running meeting
viewModel.startScreenShare(
result.data ?: return@registerForActivityResult,
...
)
}
}

val manager = requireContext().getSystemService(MediaProjectionManager::class.java)
requestScreenCapturePermission.launch(manager.createScreenCaptureIntent())

Start screen share

From here you can join a meeting with the captured screen as your local video or switch from the local camera to the screen capture for an already running meeting. The SDK will start a foreground service on your behalf and post the provided notification.

Screen share info

private fun generateScreenShareNotification(context: Context): Notification {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.getSystemService(NotificationManager::class.java).apply {
createNotificationChannel(
NotificationChannel(
SCREEN_SHARE_CHANNEL_ID,
SCREEN_SHARE_CHANNEL_NAME,
NotificationManager.IMPORTANCE_DEFAULT
)
)
}
}

return NotificationCompat.Builder(context, SCREEN_SHARE_CHANNEL_ID)
.setOngoing(true)
.setSilent(true)
.setContentText(context.getText(R.string.your_screen_is_currently_being_recorded))
.setContentTitle(context.getText(R.string.screen_capture))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSmallIcon(R.drawable.cast_24)
.setCategory(Notification.CATEGORY_SERVICE)
.build()
}

val screenShareInfo = EyesonMeeting.ScreenShareInfo(
mediaProjectionPermissionResultData,
SCREEN_SHARE_NOTIFICATION_ID,
generateScreenShareNotification(context)
)

Join meeting

eyesonMeeting.join(
accessKey = accessKey,
frontCamera = true,
audioOnly = false,
local = local,
remote = remote,
microphoneEnabledOnStart = true,
videoEnabledOnStart = true,
screenShareInfo = screenShareInfo
)

Switch to screen share (running meeting)

Either as full screen asPresentation = true or as a replacement of your local video.

eyesonMeeting.startScreenShare(screenShareInfo, asPresentation)

Permission

For Android API >= 33 make sure to add the POST_NOTIFICATIONS permission to your manifest and request the runtime permission as well.

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

Stop screen share

In order to stop sharing call stopScreenShare or leave. This has to be done at some point, otherwise the foreground service continues to run, even if the user swipes the app from the Recents screen. To prevent this, call leave from your Activity /Fragment onDestroy

override fun onDestroy() {
if (requireActivity().isFinishing) {
eyesonMeeting.leave()
}
super.onDestroy()
}