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
@@ -113,6 +113,40 @@ pub fn set_screen_notification(notice: Option<&ScreenNotice>) -> Result<()> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Point the screen's virtual display at an encoder's input surface (the
|
||||
/// zero-copy path), or back at the frame reader with `None`.
|
||||
pub fn set_screen_surface(
|
||||
surface: Option<&voelin_media::codec::mediacodec::InputSurface>,
|
||||
) -> Result<()> {
|
||||
with_bridge(|env, class| {
|
||||
let (object, width, height) = match surface {
|
||||
Some(surface) => {
|
||||
// SAFETY: `env` is this thread's JNI environment, and the
|
||||
// window stays alive (`surface` holds a reference) while
|
||||
// Java makes a Surface of it.
|
||||
#[allow(unsafe_code)]
|
||||
let raw = unsafe { surface.window.to_surface(env.get_raw().cast()) };
|
||||
if raw.is_null() {
|
||||
return Err(Error::NullPtr("encoder surface"));
|
||||
}
|
||||
// SAFETY: `raw` is a new local reference to an
|
||||
// android.view.Surface of this frame.
|
||||
#[allow(unsafe_code)]
|
||||
let object = unsafe { JObject::from_raw(env, raw.cast()) };
|
||||
(object, surface.width, surface.height)
|
||||
}
|
||||
None => (JObject::null(), 0, 0),
|
||||
};
|
||||
env.call_static_method(
|
||||
class,
|
||||
jni_str!("setScreenSurface"),
|
||||
jni_sig!((surface: android.view.Surface, width: jint, height: jint) -> void),
|
||||
&[JValue::Object(&object), JValue::Int(width as jint), JValue::Int(height as jint)],
|
||||
)?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Ask the user to share the screen. The answer comes back through
|
||||
/// `Native.onScreenCaptureResult`.
|
||||
pub fn request_screen_capture(fps: u32, max_size: u32) -> Result<()> {
|
||||
@@ -362,9 +396,10 @@ fn on_screen_frame<'local>(
|
||||
return Err(Error::NullPtr("screen frame buffer"));
|
||||
}
|
||||
// SAFETY: a direct buffer's memory is `capacity` bytes at `address`, and
|
||||
// the `Image` it belongs to stays open until this call returns.
|
||||
// the `Image` it belongs to stays open until this call returns (the
|
||||
// frame is borrowed only during it).
|
||||
#[allow(unsafe_code)]
|
||||
let bytes = unsafe { std::slice::from_raw_parts(address, capacity) }.to_vec();
|
||||
let bytes = unsafe { std::slice::from_raw_parts(address, capacity) };
|
||||
Ok(crate::capture::on_frame(
|
||||
bytes,
|
||||
width.max(0) as u32,
|
||||
|
||||
Reference in new issue
Block a user