|
| 1 | +# ADR-0004: Source Preview Feature |
| 2 | + |
| 3 | +- Status: `Proposed` |
| 4 | +- Deciders: @itallix @leoll2 |
| 5 | +- Date: 2025-08-28 |
| 6 | + |
| 7 | +Technical Story: #4595 |
| 8 | + |
| 9 | +## Context and Problem Statement |
| 10 | + |
| 11 | +Users need to preview the source of a pipeline (e.g., video file, camera stream) without running the entire pipeline. |
| 12 | +This allows validation that the source is correctly configured and accessible before enabling processing and inference |
| 13 | +through the full pipeline. |
| 14 | + |
| 15 | +## Decision Drivers |
| 16 | + |
| 17 | +- Preview feature should be designed around existing concept of pipelines. |
| 18 | +- Only one pipeline can be active at any time (either preview or full). |
| 19 | +- The preview pipeline outputs to a dedicated WebRTC stream. |
| 20 | + |
| 21 | +## Considered Options |
| 22 | + |
| 23 | +1. **Preview thread:** |
| 24 | + |
| 25 | + Reuse existing pipeline infrastructure. Introduces a thread that links the frame queue to the WebRTC preview stream. |
| 26 | + |
| 27 | + - Good: |
| 28 | + |
| 29 | + - Uses established IPC components (queues and event). |
| 30 | + - Minimal additional code outside thread management (relaxed validation for pipeline). |
| 31 | + - No changes to existing worker routines or API. |
| 32 | + |
| 33 | + - Bad: |
| 34 | + |
| 35 | + - Idle thread consumes some resources when preview is not active due to preemptive multitasking. |
| 36 | + - More threads mean slightly increased debugging and maintenance complexity. |
| 37 | + |
| 38 | +2. **Dummy configs:** |
| 39 | + |
| 40 | + Run pipeline with "dummy" sink/model configuration, so inference and dispatch routines are started but don't perform work. |
| 41 | + |
| 42 | + - Good: |
| 43 | + |
| 44 | + - No new threading/IPC. |
| 45 | + - Relies on full pipeline logic for preview state transitions. |
| 46 | + |
| 47 | + - Bad: |
| 48 | + |
| 49 | + - Introduces more complexity: workers logic should be extended, new configuration types. |
| 50 | + - New orchestration phases should consider dummy types. |
| 51 | + - Unintended data flow (it will still write to the queues that are not needed for preview flow). |
| 52 | + |
| 53 | +3. **Preview source only:** |
| 54 | + |
| 55 | + Create a separate preview abstraction around the source component, bypassing pipeline logic. |
| 56 | + |
| 57 | + - Good: |
| 58 | + |
| 59 | + - Better fits conceptually as it's totally separated from pipeline logic. |
| 60 | + |
| 61 | + - Bad: |
| 62 | + |
| 63 | + - Introduces more complexity: requires DB schema update, new APIs, new lifecycle management. |
| 64 | + - Does not meat decision drivers. |
| 65 | + |
| 66 | +## Decision Outcome |
| 67 | + |
| 68 | +**Chosen option:** Preview thread |
| 69 | + |
| 70 | +- Meets all decision drivers. |
| 71 | +- Requires minimal changes and keeps pipeline abstraction intact. |
| 72 | +- Outputs to existing WebRTC stream. |
| 73 | +- No effects on the main pipeline workers or APIs. |
| 74 | + |
| 75 | +### Positive Consequences |
| 76 | + |
| 77 | +- No changes to core workers routines. |
| 78 | +- No additional IPC primitives required: queues and events reused. |
| 79 | +- Pipeline abstraction remains consistent. |
| 80 | +- No new API endpoints needed; existing logic covers preview functionality. |
| 81 | + |
| 82 | +### Negative Consequences |
| 83 | + |
| 84 | +- Idle preview thread may use some CPU/memory when no preview is active. |
| 85 | +- Multithreading complexity increases. |
| 86 | + |
| 87 | +### Implementation Details |
| 88 | + |
| 89 | +The Scheduler is responsible for creating and cleaning up processes, threads, IPC resources, and related components. |
| 90 | +The Scheduler should create a new thread called "Preview": |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +**Preview Thread Behavior:** |
| 95 | + |
| 96 | +The Preview thread runs only when the pipeline is in preview mode. |
| 97 | +It reads frames from the source frame queue and pushes them to the existing WebRTC queue. |
| 98 | + |
| 99 | +**Preview Mode Conditions:** |
| 100 | + |
| 101 | +- The pipeline has a source configured. |
| 102 | +- The pipeline does not have a model configured. |
| 103 | +- The pipeline does not have a sink configured. |
| 104 | +- The pipeline is in the running state. |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +**Note:** |
| 109 | +The Preview thread operates only alongside the "Stream loader" process, as other workers are inactive in preview mode. |
| 110 | + |
| 111 | +### Open Questions? |
| 112 | + |
| 113 | +- Is there any benefit to having a separate WebRTC queue for preview mode, or should we reuse the existing queue? |
| 114 | +- Should the Preview thread be created on demand when the pipeline enters preview mode, rather than always running after |
| 115 | + being created by the Scheduler during server startup? |
| 116 | + |
| 117 | +## Links <!-- optional --> |
| 118 | + |
| 119 | +- [MADR project template](https://github.com/joelparkerhenderson/architecture-decision-record/tree/main/locales/en/templates/decision-record-template-of-the-madr-project) |
| 120 | +- Implementation: see [Scheduler](../../../backend/app/core/scheduler.py), [Pipeline](../../../backend/app/schemas/pipeline.py) |
0 commit comments