-
Notifications
You must be signed in to change notification settings - Fork 17
Refactor Live Streaming Architecture with Dedicated LiveViewController and WebRTC Support #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: copilot/implement-automated-affine-calibration
Are you sure you want to change the base?
Conversation
Co-authored-by: beniroquai <4345528+beniroquai@users.noreply.github.com>
…ller Co-authored-by: beniroquai <4345528+beniroquai@users.noreply.github.com>
Co-authored-by: beniroquai <4345528+beniroquai@users.noreply.github.com>
Co-authored-by: beniroquai <4345528+beniroquai@users.noreply.github.com>
Co-authored-by: beniroquai <4345528+beniroquai@users.noreply.github.com>
Co-authored-by: beniroquai <4345528+beniroquai@users.noreply.github.com>
Replaces ImConWidgetController with LiveUpdatedController in LiveViewController, removes widget-specific logic and signals, and deletes LiveViewWidget.py. API methods are renamed for clarity, and streaming parameter handling is improved. This refactor streamlines live streaming management, especially for headless mode operation.
@copilot, I have made the following observations, can you please fix them?
meta_message = {
"name": "frame_meta",
"detectorname": detector_name,
"pixelsize": int(pixel_size),
"format": "binary",
"metadata": metadata
} |
…pilot/refactor-live-streaming-architecture
…teImage, disable headless timer Co-authored-by: beniroquai <4345528+beniroquai@users.noreply.github.com>
All requested changes have been implemented in commit d87b366:
All changes validated with syntax checks. |
@copilot, actually, I think you can revert most of the changes in the last commit again as we want to remove the econding inside the noqt.py framework (e.g. _emit_jpeg_frame and _emit_binary_frame) and do it inside the LiveViewController to have it a single-source of truth. |
…truth Co-authored-by: beniroquai <4345528+beniroquai@users.noreply.github.com>
Done. Moved all encoding from noqt.py to LiveViewController as single source of truth: LiveViewController now owns encoding:
noqt.py updated:
All changes validated with syntax checks ✓ |
Streaming workers now emit pre-formatted messages via sigStreamFrame, which are directly handled by noqt's SignalInstance for socket.io emission. Legacy frame handling code is removed from LiveViewController, and ImConMainController connects LiveViewController's streaming signal in headless mode. This improves modularity and simplifies the streaming pipeline for both binary and JPEG protocols.
@copilot can you complete the webrtc example? Perhaps based on this aiortc example including the website streaming |
Completed WebRTC implementation based on aiortc webcam example: Implementation includes:
Key features:
Open Requires: |
Co-authored-by: beniroquai <4345528+beniroquai@users.noreply.github.com>
Refactored streaming logic for better separation of concerns and thread safety, centralizing frame encoding in LiveViewController and updating noqt.py to use a fallback queue for socket emission. Enhanced WebRTC offer handling with proper event loop management and improved client-side HTML for ICE gathering reliability. Added detailed documentation for streaming refactoring, thread safety fixes, and WebRTC implementation/debugging. Also updated API parameter handling and removed a test image file.
Introduces new documentation for LiveViewController acquisition safety and streaming protocol API. Adds protocol tracking endpoints to LiveViewController, improves thread safety and reference counting for detector acquisition, refactors WebRTC event loop handling, and fixes logic for stream parameter updates. Minor changes to SettingsController and DetectorsManager for improved initialization and debugging.
Overview
This PR implements a comprehensive refactoring of ImSwitch's live streaming architecture by introducing a dedicated
LiveViewController
that centralizes all streaming functionality and serves as the single source of truth for frame encoding. The implementation now includes full WebRTC streaming support based on aiortc for low-latency, real-time video streaming.Key Architecture Changes
LiveViewController as Single Source of Truth
All frame encoding now happens in LiveViewController:
BinaryStreamWorker
: Performs LZ4/Zstandard compression using BinaryFrameEncoderJPEGStreamWorker
: Performs JPEG encoding using cv2.imencodeMJPEGStreamWorker
: Performs MJPEG encoding for HTTP streamingWebRTCStreamWorker
: Manages WebRTC streaming with aiortc VideoStreamTrack_onFrameReady()
method withframe_meta
formatnoqt.py framework updated:
_emit_binary_frame()
and_emit_jpeg_frame()
marked as LEGACYImplementation Details
1. ✅ Fixed Timer Issue
time.sleep()
for consistent frame rate2. ✅ Stop by Detector Only
stopLiveView()
now only requiresdetectorName
parameterdetectorName
is None, stops the first active stream3. ✅ One Protocol at a Time
_activeStreams
from{(detectorName, protocol): worker}
to{detectorName: (protocol, worker)}
4. ✅ Centralized Encoding in LiveViewController
sigFrameReady = Signal(str, bytes, dict)
- emits (detectorName, encodedData, metadata)_onFrameReady()
method handles direct socket.io emissionframe_meta
format5. ✅ Disabled DetectorsManager Timer in Headless
startAcquisition()
in DetectorsManager to checkIS_HEADLESS
enableLV and not IS_HEADLESS
6. ✅ Removed Duplicate Processing
7. ✅ Complete WebRTC Implementation
WebRTCStreamWorker
: Captures and queues detector frames for WebRTC streamingDetectorVideoTrack
: Custom aiortc VideoStreamTrack that converts numpy arrays to av.VideoFramewebrtc_offer()
endpoint: Complete WebRTC signaling with SDP offer/answer negotiationwebrtc_stream.html
: Full-featured browser client with real-time statisticsArchitecture Diagram
Benefits
frame_meta
formatWebRTC Features
Testing
frame_meta
formatRequirements
New Dependencies for WebRTC
API Usage
Start WebRTC Stream
Use Browser Client
webrtc_stream.html
in browserDocumentation
Complete WebRTC documentation added in
docs/WebRTC_Streaming.md
:All changes maintain backward compatibility while establishing LiveViewController as the definitive location for all streaming encoding logic and providing modern WebRTC streaming capabilities.
Original prompt
This section details on the original issue you should resolve
<issue_title>Refactor Live Streaming Architecture with Dedicated LiveViewController</issue_title>
<issue_description>The current streaming implementation is fragmented across multiple controllers and uses a confusing signal/slot architecture that makes it difficult to maintain and extend. Key issues include:
setStreamParams
/getStreamParams
) are inSettingsController
, MJPEG streaming is inRecordingController
, and frame acquisition timing is handled byDetectorsManager
DetectorManager.updateLatestFrame
→DetectorsManager.sigImageUpdated
→MasterController
→ socket/backendDetectorsManager
polls frames even when not needed, wasting resourcesvideo_feeder
) is mixed with recording logic inRecordingController
Proposed Solution: Dedicated LiveViewController
Create a new
LiveViewController
that centralizes all live streaming concerns and provides a clean, extensible architecture.Core Responsibilities
Stream Configuration Management
setStreamParams()
andgetStreamParams()
fromSettingsController
toLiveViewController
Per-Detector Frame Polling
sigUpdateImage
IS_HEADLESS
) - the machanism in DetectorsManager is kept for non-headless modeMulti-Protocol Streaming
RecordingController.video_feeder
)aiortc
libraryStart/Stop Live View
startLiveView(detectorName, streamType, streamParams)
: Start streaming for specific detectorstopLiveView(detectorName, streamType)
: Stop streaming for specific detectorDetailed Architecture
we should listen to
to starT/stop livestreaming
Implementation Details
1. LiveViewController Class Structure...
Fixes #185
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.