Android: share the screen into the encoder without a copy
Screen sharing went MediaProjection -> ImageReader -> an owned Vec per frame -> RGBA to NV12 on the CPU -> MediaCodec's input buffers: two copies and a conversion of every frame on a phone's CPU. Now, while it is the only MediaCodec encoder in the process (one simulcast layer, one codec), an encoder takes GpuFrames: for the first it starts a codec with an input surface (COLOR_FormatSurface, max-fps-to-encoder at the stream's rate, repeat-previous-frame-after so a still screen answers keyframe requests) and publishes it, and the capture points its virtual display at that surface, resized to the layer's size. The compositor renders the screen into the encoder; a ticker at the stream's rate hands the sink GpuFrames that only collect what the codec encoded, on the capture's timeline. Simulcast keeps working by falling back: a second encoder (another layer, or another codec a viewer chose) turns the path off for every encoder and the display goes back to the reader, until it is the only one again. One display can only render into one surface, and a GL fan-out to several encoder surfaces is a lot of code for a case phones rarely need. An encoder whose surface fails stays on frames in memory. The fallback path no longer copies either: the provider's start_sink hands the reader's buffer to the stream's sink borrowed. A frame-rate change restarts the surface codec (its cap is fixed when it starts), and the display is detached while it is resized, so neither consumer sees a picture of the other's size. Compiled for aarch64-linux-android only: there is no device here, so frames per second and CPU use are not measured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ecb0e11668
commit
c79bb7dd07
10 files changed
+635
-59
No files matched your search
@@ -8,6 +8,7 @@ import android.content.pm.ResolveInfo
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.os.Build
|
||||
import android.view.Surface
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
@@ -69,6 +70,16 @@ object Bridge {
|
||||
ScreenCaptureService.stop(app)
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the shared screen into `surface` (an encoder's input,
|
||||
* `width` x `height`: the zero-copy path), or with null back into the
|
||||
* frame reader.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun setScreenSurface(surface: Surface?, width: Int, height: Int) {
|
||||
ScreenCaptureService.redirect(surface, width, height)
|
||||
}
|
||||
|
||||
/** Capture what the device plays; false without a running screen capture. */
|
||||
@JvmStatic
|
||||
fun startSystemAudio(): Boolean = ScreenCaptureService.startAudio()
|
||||
|
||||
@@ -36,9 +36,10 @@ import kotlin.math.roundToInt
|
||||
/**
|
||||
* Screen sharing: holds the MediaProjection (a `mediaProjection` foreground
|
||||
* service, as Android requires), mirrors the display into an ImageReader and
|
||||
* hands each RGBA frame to Native.onScreenFrame; optionally captures what
|
||||
* other apps play (AudioPlaybackCapture: all but us, or one app) for
|
||||
* Native.onSystemAudio and Native.onAudioInput.
|
||||
* hands each RGBA frame to Native.onScreenFrame, or (the zero-copy path,
|
||||
* Bridge.setScreenSurface) straight into an encoder's input surface;
|
||||
* optionally captures what other apps play (AudioPlaybackCapture: all but
|
||||
* us, or one app) for Native.onSystemAudio and Native.onAudioInput.
|
||||
*/
|
||||
class ScreenCaptureService : Service() {
|
||||
private var projection: MediaProjection? = null
|
||||
@@ -149,6 +150,32 @@ class ScreenCaptureService : Service() {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Point the virtual display at `surface` (an encoder's input surface,
|
||||
* `width` x `height`: the screen goes into the encoder without a copy),
|
||||
* or with null back at the reader, at the reader's size.
|
||||
*/
|
||||
@Synchronized
|
||||
private fun redirectTo(surface: Surface?, width: Int, height: Int) {
|
||||
val display = display ?: return
|
||||
val reader = reader ?: return
|
||||
val dpi = resources.displayMetrics.densityDpi
|
||||
try {
|
||||
// Detached while resizing, so neither consumer gets a picture of
|
||||
// the other's size.
|
||||
display.surface = null
|
||||
if (surface != null) {
|
||||
display.resize(width, height, dpi)
|
||||
display.surface = surface
|
||||
} else {
|
||||
display.resize(reader.width, reader.height, dpi)
|
||||
display.surface = reader.surface
|
||||
}
|
||||
} catch (e: RuntimeException) {
|
||||
Log.w(TAG, "cannot point the screen at ${if (surface != null) "the encoder" else "the reader"}", e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Capture media, game and unknown-usage playback as 48 kHz stereo float:
|
||||
* of app `uid`, or with null of every app but us (our voices and watched
|
||||
@@ -293,6 +320,11 @@ class ScreenCaptureService : Service() {
|
||||
context.stopService(Intent(context, ScreenCaptureService::class.java))
|
||||
}
|
||||
|
||||
/** See redirectTo; nothing without a running capture. */
|
||||
fun redirect(surface: Surface?, width: Int, height: Int) {
|
||||
instance?.redirectTo(surface, width, height)
|
||||
}
|
||||
|
||||
fun startAudio(): Boolean {
|
||||
val service = instance ?: return false
|
||||
return service.startAudioCapture(SYSTEM_AUDIO, null)
|
||||
|
||||
Reference in new issue
Block a user