Skip to content

Extract realtime input and media primitives#385

Merged
jarcherNV merged 5 commits into
mainfrom
dev/jarcher/media-refactor
Jul 20, 2026
Merged

Extract realtime input and media primitives#385
jarcherNV merged 5 commits into
mainfrom
dev/jarcher/media-refactor

Conversation

@jarcherNV

Copy link
Copy Markdown
Collaborator

Add a transport-neutral serving.realtime package for keyboard/input state, latest-frame publishing, tensor/array RGB conversion, and lazy JPEG encoding. Refactor WebRTC controls and media to consume the shared primitives while preserving existing import compatibility and BufferedVideoTrack behavior.

Add CPU tests covering input snapshots, frame-bus shutdown semantics, media conversion parity, and JPEG encoding.

Add a transport-neutral serving.realtime package for keyboard/input state,
latest-frame publishing, tensor/array RGB conversion, and lazy JPEG encoding.
Refactor WebRTC controls and media to consume the shared primitives while
preserving existing import compatibility and BufferedVideoTrack behavior.

Add CPU tests covering input snapshots, frame-bus shutdown semantics, media
conversion parity, and JPEG encoding.
@copy-pr-bot

copy-pr-bot Bot commented Jul 19, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@jarcherNV jarcherNV self-assigned this Jul 19, 2026
@greptile-apps

greptile-apps Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR extracts a transport-neutral serving.realtime package (frame_bus, input, media) from WebRTC-specific code, then refactors WebRTC controls/media and three integration packages (flashvsr, omnidreams MJPEG presenter, omnidreams gRPC utils) to consume the shared primitives. Backward-compatibility shims in webrtc/controls.py and webrtc/media.py preserve existing import paths.

  • realtime/media.py correctly promotes bfloat16 tensors to float32 before .numpy() and now raises an explicit error when a uint8 array is passed with a non-"uint8" value range, addressing both regressions flagged in earlier rounds.
  • streaming_presenter.py replaces the hand-rolled _frame_cond / _bev_frame_cond pair with LatestFrameBus; scene-change selection no longer triggers an immediate notify_all() wakeup on stream handlers, adding up to one second of latency before a connected browser observes the scene transition.

Confidence Score: 5/5

Safe to merge; all changed paths behave correctly under the documented contracts and the bfloat16/uint8-validation regressions flagged in earlier rounds are resolved.

The refactoring is mechanical and well-tested. The one behavioral change worth noting — scene-change in MJPEGStreamingPresenter no longer immediately wakes blocked stream handlers — adds up to one second of transition latency but does not affect correctness or data integrity. No changed path introduces wrong data, dropped state, or broken cleanup.

streaming_presenter.py — the scene-change wakeup removal is the only non-trivial behavioral delta in the PR.

Important Files Changed

Filename Overview
flashdreams/flashdreams/serving/realtime/frame_bus.py New single-slot latest-frame bus; thread-safe, correct timeout/close semantics, well-tested.
flashdreams/flashdreams/serving/realtime/input.py Transport-neutral input primitives extracted from webrtc/controls.py; logic unchanged, adds DRIVING_SUPPORTED_KEYS and sparse-snapshot helpers.
flashdreams/flashdreams/serving/realtime/media.py New shared media helpers; bfloat16 promoted via tensor.float(), uint8+wrong-value_range now raises, bvtchw channel dimension validated — all previously flagged regressions addressed.
flashdreams/flashdreams/serving/webrtc/controls.py Replaced 300-line implementation with a thin re-export shim from realtime.input; all preserves all previously public names.
flashdreams/flashdreams/serving/webrtc/media.py tensor_chunk_to_rgb_frames delegated to realtime.media; BufferedVideoTrack behavior preserved via _default_frame_converter wrapper.
integrations/omnidreams/omnidreams/interactive_drive/streaming_presenter.py MJPEGStreamingPresenter migrated to LatestFrameBus; scene-change no longer wakes stream handlers immediately, adding up to 1 s UX latency.
integrations/omnidreams/omnidreams/interactive_drive/input/keyboard.py KeyboardState now delegates to RealtimeKeyboardState with DRIVING_SUPPORTED_KEYS; arrow-key normalization now works correctly (ArrowUp→w), confirmed by new test.
integrations/omnidreams/omnidreams/grpc/utils.py JPEG path in encode_image delegates to encode_rgb_frame_to_jpeg; docstring already specifies [H,W,3] uint8 so the stricter interface is consistent with the documented contract.
flashdreams/tests/test_realtime_serving.py New CPU test suite covers frame-bus semantics, bfloat16 promotion, bvtchw channel rejection, and JPEG encoding; close-wakeup test now uses a patched condition.wait to eliminate the race window flagged in an earlier round.
integrations/flashvsr/flashvsr/grpc/streaming_view.py _encode_jpeg_rgb delegates to encode_rgb_frame_to_jpeg; ModuleNotFoundError guard correctly re-raises for non-Pillow import failures.
integrations/flashvsr/flashvsr/grpc/uplift_client.py encode_jpeg_frames refactored to use shared helper; io.BytesIO loop removed; ModuleNotFoundError handling is correct.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    subgraph realtime["serving.realtime (new)"]
        FB["frame_bus.py\nLatestFrameBus"]
        IN["input.py\nKeyboardState / KeyboardResampler\nCameraPoseIntegrator\nSparseInputSnapshot"]
        ME["media.py\n_as_numpy / _scale_rgb\nrgb_frame_to_uint8\ntensor_chunk_to_rgb_frames\nencode_rgb_frame_to_jpeg"]
    end

    subgraph webrtc["serving.webrtc (refactored)"]
        WC["controls.py\n(re-export shim)"]
        WM["media.py\n(re-export shim)"]
        MGR["manager.py"]
        BVT["BufferedVideoTrack"]
    end

    subgraph flashvsr["flashvsr integrations"]
        SV["grpc/streaming_view.py"]
        UC["grpc/uplift_client.py"]
    end

    subgraph omnidreams["omnidreams integrations"]
        KB["interactive_drive/input/keyboard.py"]
        SP["interactive_drive/streaming_presenter.py\nMJPEGStreamingPresenter"]
        GU["grpc/utils.py"]
    end

    IN --> WC
    ME --> WM
    ME --> SV
    ME --> UC
    ME --> GU
    ME --> SP
    IN --> KB
    FB --> SP

    WC --> MGR
    WM --> BVT
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    subgraph realtime["serving.realtime (new)"]
        FB["frame_bus.py\nLatestFrameBus"]
        IN["input.py\nKeyboardState / KeyboardResampler\nCameraPoseIntegrator\nSparseInputSnapshot"]
        ME["media.py\n_as_numpy / _scale_rgb\nrgb_frame_to_uint8\ntensor_chunk_to_rgb_frames\nencode_rgb_frame_to_jpeg"]
    end

    subgraph webrtc["serving.webrtc (refactored)"]
        WC["controls.py\n(re-export shim)"]
        WM["media.py\n(re-export shim)"]
        MGR["manager.py"]
        BVT["BufferedVideoTrack"]
    end

    subgraph flashvsr["flashvsr integrations"]
        SV["grpc/streaming_view.py"]
        UC["grpc/uplift_client.py"]
    end

    subgraph omnidreams["omnidreams integrations"]
        KB["interactive_drive/input/keyboard.py"]
        SP["interactive_drive/streaming_presenter.py\nMJPEGStreamingPresenter"]
        GU["grpc/utils.py"]
    end

    IN --> WC
    ME --> WM
    ME --> SV
    ME --> UC
    ME --> GU
    ME --> SP
    IN --> KB
    FB --> SP

    WC --> MGR
    WM --> BVT
Loading

Reviews (5): Last reviewed commit: "Fix Phase 2 lint issues" | Re-trigger Greptile

Comment thread flashdreams/tests/test_realtime_serving.py Outdated
Comment thread flashdreams/flashdreams/serving/realtime/media.py
Comment thread flashdreams/flashdreams/serving/realtime/media.py
Comment thread flashdreams/tests/test_realtime_serving.py
Validate RGB channel shape for bvtchw media chunks and reject inconsistent
uint8 value_range arguments instead of silently returning raw bytes.

Tighten realtime serving tests by removing the tautological WebRTC media
self-comparison, adding validation coverage for the new media errors, and
making the LatestFrameBus close-wakeup test deterministically block before
close() is called.
Comment thread flashdreams/flashdreams/serving/realtime/media.py
Promote floating torch tensors to float32 before numpy conversion in the
shared realtime media helper, preserving the old WebRTC converter behavior
for bfloat16 model outputs.

Add regression coverage for bfloat16 TCHW and BVTCHW tensor chunks.
Route OmniDreams interactive keyboard, MJPEG frame handoff, and JPEG encoding
through the shared realtime input, frame bus, and media helpers. Reuse the
shared JPEG encoder in FlashVSR and OmniDreams gRPC paths, and add focused CPU
coverage for the migrated behavior.
Apply the ruff import-order fixes required by the CPU pre-commit job and keep
the new Phase 2 files on the full Apache license header style.
sync_device=sync_device,
)
output = io.BytesIO()
image_from_array(rgb_uint8).save(output, format="JPEG", quality=quality)

@gtong-nv gtong-nv Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can add the nvjpeg path here if that HW unit is available ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I will add this to my TODO list.

@gtong-nv gtong-nv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@jarcherNV
jarcherNV added this pull request to the merge queue Jul 20, 2026
Merged via the queue into main with commit 12449c9 Jul 20, 2026
8 checks passed
@jarcherNV
jarcherNV deleted the dev/jarcher/media-refactor branch July 20, 2026 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants