Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 26
fix(vegas): keep plugin data and visuals fresh during Vegas scroll mode#291
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -90,6 +90,14 @@ def __init__( | ||
| self._interrupt_check: Optional[Callable[[], bool]] = None | ||
| self._interrupt_check_interval: int = 10 # Check every N frames | ||
| # Plugin update tick for keeping data fresh during Vegas mode | ||
| self._update_tick: Optional[Callable[[], Optional[List[str]]]] = None | ||
| self._update_tick_interval: float = 1.0 # Tick every 1 second | ||
| self._update_thread: Optional[threading.Thread] = None | ||
| self._update_results: Optional[List[str]] = None | ||
| self._update_results_lock = threading.Lock() | ||
| self._last_update_tick_time: float = 0.0 | ||
| # Config update tracking | ||
| self._config_version = 0 | ||
| self._pending_config_update = False | ||
| @@ -158,6 +166,25 @@ def set_interrupt_checker( | ||
| self._interrupt_check = checker | ||
| self._interrupt_check_interval = max(1, check_interval) | ||
| def set_update_tick( | ||
| self, | ||
| callback: Callable[[], Optional[List[str]]], | ||
| interval: float = 1.0 | ||
| ) -> None: | ||
| """ | ||
| Set the callback for periodic plugin update ticking during Vegas mode. | ||
| This keeps plugin data fresh while the Vegas render loop is running. | ||
| The callback should run scheduled plugin updates and return a list of | ||
| plugin IDs that were actually updated, or None/empty if no updates occurred. | ||
| Args: | ||
| callback: Callable that returns list of updated plugin IDs or None | ||
| interval: Seconds between update tick calls (default 1.0) | ||
| """ | ||
| self._update_tick = callback | ||
| self._update_tick_interval = max(0.5, interval) | ||
| def start(self) -> bool: | ||
| """ | ||
| Start Vegas mode operation. | ||
| @@ -210,6 +237,9 @@ def stop(self) -> None: | ||
| self.stats['total_runtime_seconds'] += time.time() - self._start_time | ||
| self._start_time = None | ||
| # Wait for in-flight background update before tearing down state | ||
| self._drain_update_thread() | ||
| # Cleanup components | ||
| self.render_pipeline.reset() | ||
| self.stream_manager.reset() | ||
| @@ -305,71 +335,83 @@ def run_iteration(self) -> bool: | ||
| last_fps_log_time = start_time | ||
| fps_frame_count = 0 | ||
| logger.info("Starting Vegas iteration for %.1fs", duration) | ||
| self._last_update_tick_time = start_time | ||
| while True: | ||
| # Check for STATIC mode plugin that should pause scroll | ||
| static_plugin = self._check_static_plugin_trigger() | ||
| if static_plugin: | ||
| if not self._handle_static_pause(static_plugin): | ||
| # Static pause was interrupted | ||
| return False | ||
| # After static pause, skip this segment and continue | ||
| self.stream_manager.get_next_segment() # Consume the segment | ||
| continue | ||
| # Run frame | ||
| if not self.run_frame(): | ||
| # Check why we stopped | ||
| with self._state_lock: | ||
| if self._should_stop: | ||
| return False | ||
| if self._is_paused: | ||
| # Paused for live priority - let caller handle | ||
| return False | ||
| # Sleep for frame interval | ||
| time.sleep(frame_interval) | ||
| # Increment frame count and check for interrupt periodically | ||
| frame_count += 1 | ||
| fps_frame_count += 1 | ||
| # Periodic FPS logging | ||
| current_time = time.time() | ||
| if current_time - last_fps_log_time >= fps_log_interval: | ||
| fps = fps_frame_count / (current_time - last_fps_log_time) | ||
| logger.info( | ||
| "Vegas FPS: %.1f (target: %d, frames: %d)", | ||
| fps, self.vegas_config.target_fps, fps_frame_count | ||
| ) | ||
| last_fps_log_time = current_time | ||
| fps_frame_count = 0 | ||
| logger.info("Starting Vegas iteration for %.1fs", duration) | ||
| if (self._interrupt_check and | ||
| frame_count % self._interrupt_check_interval == 0): | ||
| try: | ||
| if self._interrupt_check(): | ||
| logger.debug( | ||
| "Vegas interrupted by callback after %d frames", | ||
| frame_count | ||
| ) | ||
| try: | ||
| while True: | ||
| # Check for STATIC mode plugin that should pause scroll | ||
| static_plugin = self._check_static_plugin_trigger() | ||
| if static_plugin: | ||
| if not self._handle_static_pause(static_plugin): | ||
| # Static pause was interrupted | ||
| return False | ||
| except Exception: | ||
| # Log but don't let interrupt check errors stop Vegas | ||
| logger.exception("Interrupt check failed") | ||
| # Check elapsed time | ||
| elapsed = time.time() - start_time | ||
| if elapsed >= duration: | ||
| break | ||
| # Check for cycle completion | ||
| if self.render_pipeline.is_cycle_complete(): | ||
| break | ||
| # After static pause, skip this segment and continue | ||
| self.stream_manager.get_next_segment() # Consume the segment | ||
| continue | ||
| # Run frame | ||
| if not self.run_frame(): | ||
| # Check why we stopped | ||
| with self._state_lock: | ||
| if self._should_stop: | ||
| return False | ||
| if self._is_paused: | ||
| # Paused for live priority - let caller handle | ||
| return False | ||
| # Sleep for frame interval | ||
| time.sleep(frame_interval) | ||
| # Increment frame count and check for interrupt periodically | ||
| frame_count += 1 | ||
| fps_frame_count += 1 | ||
| # Periodic FPS logging | ||
| current_time = time.time() | ||
| if current_time - last_fps_log_time >= fps_log_interval: | ||
| fps = fps_frame_count / (current_time - last_fps_log_time) | ||
| logger.info( | ||
| "Vegas FPS: %.1f (target: %d, frames: %d)", | ||
| fps, self.vegas_config.target_fps, fps_frame_count | ||
| ) | ||
| last_fps_log_time = current_time | ||
| fps_frame_count = 0 | ||
| # Periodic plugin update tick to keep data fresh (non-blocking) | ||
| self._drive_background_updates() | ||
| if (self._interrupt_check and | ||
| frame_count % self._interrupt_check_interval == 0): | ||
| try: | ||
| if self._interrupt_check(): | ||
| logger.debug( | ||
| "Vegas interrupted by callback after %d frames", | ||
| frame_count | ||
| ) | ||
| return False | ||
| except Exception: | ||
| # Log but don't let interrupt check errors stop Vegas | ||
| logger.exception("Interrupt check failed") | ||
| # Check elapsed time | ||
| elapsed = time.time() - start_time | ||
| if elapsed >= duration: | ||
| break | ||
| # Check for cycle completion | ||
| if self.render_pipeline.is_cycle_complete(): | ||
| break | ||
| logger.info("Vegas iteration completed after %.1fs", time.time() - start_time) | ||
| return True | ||
| logger.info("Vegas iteration completed after %.1fs", time.time() - start_time) | ||
| return True | ||
| finally: | ||
| # Ensure background update thread finishes before the main loop | ||
| # resumes its own _tick_plugin_updates() calls, preventing concurrent | ||
| # run_scheduled_updates() execution. | ||
| self._drain_update_thread() | ||
| def _check_live_priority(self) -> bool: | ||
| """ | ||
| @@ -458,6 +500,71 @@ def _apply_pending_config(self) -> None: | ||
| if self._pending_config is None: | ||
| self._pending_config_update = False | ||
| def _run_update_tick_background(self) -> None: | ||
| """Run the plugin update tick in a background thread. | ||
| Stores results for the render loop to pick up on its next iteration, | ||
| so the scroll never blocks on API calls. | ||
| """ | ||
| try: | ||
| updated_plugins = self._update_tick() | ||
| if updated_plugins: | ||
| with self._update_results_lock: | ||
| # Accumulate rather than replace to avoid losing notifications | ||
| # if a previous result hasn't been picked up yet | ||
| if self._update_results is None: | ||
| self._update_results = updated_plugins | ||
| else: | ||
| self._update_results.extend(updated_plugins) | ||
| except Exception: | ||
| logger.exception("Background plugin update tick failed") | ||
| def _drain_update_thread(self, timeout: float = 2.0) -> None: | ||
| """Wait for any in-flight background update thread to finish. | ||
| Called when transitioning out of Vegas mode so the main-loop | ||
| ``_tick_plugin_updates`` call doesn't race with a still-running | ||
| background thread. | ||
| """ | ||
| if self._update_thread is not None and self._update_thread.is_alive(): | ||
| self._update_thread.join(timeout=timeout) | ||
| if self._update_thread.is_alive(): | ||
| logger.warning( | ||
| "Background update thread did not finish within %.1fs", timeout | ||
| ) | ||
ChuckBuilds marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def _drive_background_updates(self) -> None: | ||
| """Collect finished background update results and launch new ticks. | ||
| Safe to call from both the main render loop and the static-pause | ||
| wait loop so that plugin data stays fresh regardless of which | ||
| code path is active. | ||
| """ | ||
| # 1. Collect results from a previously completed background update | ||
| with self._update_results_lock: | ||
| ready_results = self._update_results | ||
| self._update_results = None | ||
| if ready_results: | ||
| for pid in ready_results: | ||
| self.mark_plugin_updated(pid) | ||
| # 2. Kick off a new background update if interval elapsed and none running | ||
| current_time = time.time() | ||
| if (self._update_tick and | ||
| current_time - self._last_update_tick_time >= self._update_tick_interval): | ||
| thread_alive = ( | ||
| self._update_thread is not None | ||
| and self._update_thread.is_alive() | ||
| ) | ||
| if not thread_alive: | ||
| self._last_update_tick_time = current_time | ||
| self._update_thread = threading.Thread( | ||
| target=self._run_update_tick_background, | ||
| daemon=True, | ||
| name="vegas-update-tick", | ||
| ) | ||
| self._update_thread.start() | ||
| def mark_plugin_updated(self, plugin_id: str) -> None: | ||
| """ | ||
| Notify that a plugin's data has been updated. | ||
| @@ -576,6 +683,9 @@ def _handle_static_pause(self, plugin: 'BasePlugin') -> bool: | ||
| logger.info("Static pause interrupted by live priority") | ||
| return False | ||
| # Keep plugin data fresh during static pause | ||
| self._drive_background_updates() | ||
| # Sleep in small increments to remain responsive | ||
| time.sleep(0.1) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.