getDisplayMedia

open suspend override fun getDisplayMedia(constraints: Map<String, Any?>): MediaStream

Captures the screen for sharing using MediaProjection API.

This method creates a screen capture stream using Android's MediaProjection API. The caller must have already obtained MediaProjection permission via Activity.startActivityForResult with MediaProjectionManager.createScreenCaptureIntent().

Required constraints:

constraints = mapOf(
"mediaProjection" to mapOf(
"resultCode" to Activity.RESULT_OK,
"data" to intent // The Intent returned from onActivityResult
),
"video" to mapOf(
"width" to mapOf("ideal" to 1920),
"height" to mapOf("ideal" to 1080),
"frameRate" to mapOf("ideal" to 15, "max" to 30)
)
)

Usage from Activity:

// 1. Request screen capture permission
val mediaProjectionManager = getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), SCREEN_CAPTURE_REQUEST_CODE)

// 2. In onActivityResult, pass data to getDisplayMedia
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == SCREEN_CAPTURE_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
val constraints = mapOf(
"mediaProjection" to mapOf("resultCode" to resultCode, "data" to data),
"video" to mapOf("width" to mapOf("ideal" to 1920), "height" to mapOf("ideal" to 1080))
)
lifecycleScope.launch {
val stream = webRtcDevice.getDisplayMedia(constraints)
// Use stream for screen sharing
}
}
}

Return

MediaStream containing the screen capture video track

Parameters

constraints

Map containing mediaProjection data and optional video constraints

Throws

If mediaProjection data is missing or invalid

If screen capture initialization fails