1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
|
# Vimix Rendering Architecture Overview
## Executive Summary
Vimix is a real-time video mixing application with a sophisticated three-tier rendering pipeline:
1. **Source Rendering Layer** - Individual sources render to frame buffers via GStreamer
2. **Composition Layer** - Session compositing sources together into a single output frame
3. **UI/Display Layer** - OpenGL window rendering with ImGui UI overlay
The main rendering loop operates at a fixed ~62 FPS framerate with callbacks for each stage.
---
## 1. Main Application Loop
### Loop Structure
```cpp
// Main rendering loop (lines 261-262)
while ( Rendering::manager().isActive() )
Rendering::manager().draw();
```
### Initialization Flow (lines 209-247)
```
1. Rendering::manager().init() // OpenGL/GLFW initialization
2. UserInterface::manager().Init() // ImGui setup
3. Audio::manager().initialize() // Audio system
4. pushBackDrawCallback(prepare) // Register update callback
5. pushBackDrawCallback(drawScene) // Register scene rendering callback
6. pushBackDrawCallback(renderGUI) // Register UI rendering callback
7. Rendering::manager().show() // Display windows
8. Mixer::manager().load(_openfile) // Load session
9. Main loop starts
```
---
## 2. Main Rendering Loop Implementation
### Location
- **Method**: `Rendering::draw()` (lines 608-673)
### Rendering Pipeline Flow
```
Rendering::draw()
├─ glfwPollEvents() // Poll input/window events
│
├─ TabletInput::instance().pollEvents() // Tablet pressure support
│
├─ main_.changeFullscreen_() // Handle fullscreen requests
│
├─ OUTPUT WINDOWS RENDERING
│ └─ for (output windows):
│ └─ output.draw(Mixer::session()->frame()) // Render to output windows
│
├─ MAIN WINDOW CONTEXT
│ └─ main_.makeCurrent() // Switch to main window GL context
│
├─ EXECUTE DRAW CALLBACKS (key stage)
│ └─ for (draw_callbacks_):
│ 1. prepare()
│ └─ Control::manager().update()
│ └─ Mixer::manager().update()
│ └─ UserInterface::manager().NewFrame()
│
│ 2. drawScene()
│ └─ Mixer::manager().draw()
│
│ 3. renderGUI()
│ └─ UserInterface::manager().Render()
│
├─ SCREENSHOT CAPTURE
│ └─ if (request_screenshot_):
│ └─ screenshot_.captureGL()
│
└─ glfwSwapBuffers() // Display frame
└─ FPS LIMITER (15600 microseconds) // Soft-cap at ~62 FPS
static GTimer for frame timing
```
### Framerate Control
- **Location**: Lines 666-672 in RenderingManager.cpp
- **Method**: GLib GTimer (g_timer_*)
- **Target**: ~62 FPS (15600 microseconds between frames)
- **Implementation**: Busy-sleep if frame completes too quickly
---
## 3. Scene Update and Rendering (Mixer)
### Location
- **Methods**: `Mixer::update()` (lines 86-276), `Mixer::draw()` (lines 278-283)
### Update Phase (`prepare()` callback)
Executed in `Mixer::update()`:
```
Mixer::update(dt_)
├─ Session::update(dt_) // Update all sources
│ └─ for (each source):
│ └─ Source::update(dt) // Update individual source
│
├─ FrameGrabbing::grabFrame() // Grab frames for recording
│
├─ Process failed sources
│ ├─ FAIL_RETRY: Attempt to recreate
│ ├─ FAIL_CRITICAL: Detach from mixer
│ └─ FAIL_FATAL: Delete source
│
└─ Update views for deep refresh
└─ View::update(dt) for dirty views
Framerate tracking:
dt_ = elapsed time since last frame (ms)
dt__ = stabilized dt (EMA: 5% new + 95% old)
fps = 1000 / dt__
```
### Draw Phase (`drawScene()` callback)
Executed in `Mixer::draw()`:
```
Mixer::draw()
└─ current_view_->draw() // Render current view
```
The actual rendering is delegated to the View's draw method.
---
## 4. Source Rendering Pipeline
### Source Architecture
**Base Class**: `vimix/src/Source/Source.h`
Source hierarchy:
```
Source (base class)
├─ MediaSource // Video/image files via GStreamer
├─ DeviceSource // Camera/webcam input
├─ StreamSource // Generic GStreamer pipeline
├─ PatternSource // Generated patterns
├─ TextSource // Rendered text
├─ ShaderSource // GLSL shader output
├─ RenderSource // Renders nested session
├─ CloneSource // References another source
├─ SessionSource // Embedded session file
├─ ScreenCaptureSource // Screen capture
├─ MultiFileSource // Image sequence
├─ NetworkSource // Network stream
└─ SrtReceiverSource // SRT protocol input
```
### Source Update/Render Cycle
**Location**: Source.h (lines 162-169)
```cpp
// Each source has:
virtual void update(float dt); // Called once per frame
virtual void render(); // Render to frame buffer
virtual FrameBuffer *frame() const; // Get output texture
```
### MediaSource (Primary Video Source) Flow
**File**: `vimix/src/Source/MediaSource.cpp`
```
MediaSource::update(dt)
└─ mediaplayer_->update() // Update GStreamer pipeline
MediaSource::init()
└─ Create FrameBuffer matching media resolution
└─ Link texture from MediaPlayer to source framebuffer
MediaSource::render()
└─ Render media texture to source's frame buffer
```
---
## 5. GStreamer Integration
### MediaPlayer Class
**Location**: `/vimix/src/MediaPlayer.h` (Lines 59-434)
Key components:
```cpp
struct MediaInfo {
guint width, height, bitrate;
guint framerate_n, framerate_d;
std::string codec_name;
bool isimage, interlaced, seekable;
GstClockTime dt, end; // Frame duration and end time
};
class MediaPlayer {
GstElement *pipeline_; // GStreamer pipeline
GstBus *bus_; // Pipeline message bus
GstAppSink *appsink_; // Video output sink
Frame frame_[N_VFRAME]; // Frame buffer stack
uint textureid_; // OpenGL texture
struct TimeCounter {
GTimer *timer; // FPS measurement
gdouble fps;
} timecount_;
};
```
### Pipeline Structure
**File**: `/vimix/src/MediaPlayer.cpp`
```
GStreamer Pipeline:
[uridecodebin]
├─ Video path:
│ ├─ [videoscale]
│ ├─ [videoconvert]
│ ├─ [videofilter (optional)]
│ └─ [glsinkbin] (GL rendering) or [appsink] (CPU)
│
└─ Audio path:
└─ [audioconvert]
└─ [appsink]
```
### GStreamer OpenGL Integration
**Location**: RenderingManager.cpp (lines 42-52, 172-221)
```cpp
// GL context sharing with GStreamer
#ifdef USE_GST_OPENGL_SYNC_HANDLER
static GstGLContext *global_gl_context = NULL;
static GstGLDisplay *global_display = NULL;
// Linking pipeline to rendering instance
void Rendering::LinkPipeline(GstPipeline *pipeline)
{
GstBus* m_bus = gst_pipeline_get_bus(pipeline);
gst_bus_set_sync_handler(m_bus, bus_sync_handler, pipeline, NULL);
}
```
### GPU Hardware Decoding
**Location**: RenderingManager.cpp (lines 500-515)
```cpp
// Enable GPU decoding if available
std::list<std::string> gpuplugins =
GstToolkit::enable_gpu_decoding_plugins(
Settings::application.render.gpu_decoding);
// Supported plugins logged
if (Settings::application.render.gpu_decoding)
Log::Info("Hardware decoding enabled.");
```
### Frame Handling
**File**: MediaPlayer.cpp
```cpp
// Frame stack with mutex protection
struct Frame {
GstBuffer *buffer;
FrameStatus status; // SAMPLE, PREROLL, EOS, INVALID
bool is_new;
GstClockTime position;
std::mutex access;
};
Frame frame_[N_VFRAME]; // N_VFRAME = 5 frames
// Callback when new sample arrives
static GstFlowReturn callback_new_sample(GstAppSink *sink, gpointer data)
{
// Get sample, map to GPU texture
// Fill texture via PBO (Pixel Buffer Object) upload
}
```
---
## 6. Session Composition and Output
### Session Class
**Location**: `/vimix/src/Session.h`
```cpp
class Session {
SourceList sources_; // All sources in session
RenderView render_; // Composition renderer
// Main output framebuffer
FrameBuffer *frame() const {
return render_.frame();
}
void update(float dt); // Update all sources
};
```
### RenderView (Composition)
**Location**: `/vimix/src/View/RenderView.h`
```cpp
class RenderView : public View {
FrameBuffer *frame_buffer_; // Output framebuffer
Surface *fading_overlay_; // Fading effect overlay
void draw() override; // Composite all sources
};
```
### Composition Flow
**RenderView::draw()** (called from current View)
```
RenderView::draw()
├─ frame_buffer_->begin() // Bind FBO
│
├─ DrawVisitor traversal
│ └─ Visit and draw all source nodes
│ └─ Each source renders its texture to composition
│
├─ Apply blending/effects
│ └─ ImageProcessingShader
│ └─ BlendingShader
│
├─ Apply fading overlay
│ └─ fading_overlay_->draw()
│
└─ frame_buffer_->end() // Unbind FBO
Result: frame() returns the composed image
```
---
## 7. GUI Rendering (ImGui)
### Location
- **Init**: `UserInterface::Init()` (lines 117-150)
- **Frame Start**: `UserInterface::NewFrame()` (lines 866-871)
- **Render**: `UserInterface::Render()` (lines 1046-1047)
### ImGui Integration
```cpp
// Initialization
ImGui::CreateContext();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(VIMIX_GLSL_VERSION);
// Per-frame sequence (in renderGUI callback)
void UserInterface::NewFrame()
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
void UserInterface::Render()
{
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
```
### UI Rendering Stages
```
renderGUI() callback
├─ UserInterface::NewFrame()
│ └─ Poll input, start ImGui frame
│
├─ Build UI (in UserInterface::Render())
│ ├─ Main menu
│ ├─ View navigator
│ ├─ Source panels
│ ├─ Output windows
│ └─ Various controls
│
└─ ImGui::Render() + RenderDrawData()
└─ Draw ImGui command buffer to OpenGL
```
---
## 8. Existing Timing and Performance Measurement
### GLib GTimer Usage
**Location**: Multiple files
```cpp
// RenderingManager.cpp (lines 667-671)
static GTimer *timer = g_timer_new();
double elapsed = g_timer_elapsed(timer, NULL) * 1000000.0;
if (elapsed < 15600.0 && elapsed > 0.0)
g_usleep(15600 - (gulong)elapsed);
g_timer_start(timer);
// Mixer.cpp (lines 201-206)
static GTimer *timer = g_timer_new();
dt_ = g_timer_elapsed(timer, NULL) * 1000.0; // Frame delta time in ms
dt__ = 0.05f * dt_ + 0.95f * dt__; // Smoothed delta time (EMA)
g_timer_start(timer);
```
### FPS Calculation
**Location**: Mixer.h (line 87)
```cpp
inline int fps() const { return int(roundf(1000.f/dt__)); }
```
Also in MediaPlayer for individual sources:
```cpp
// MediaPlayer.h (lines 369-378)
struct TimeCounter {
GTimer *timer;
gdouble fps;
public:
TimeCounter();
~TimeCounter();
void tic();
inline gdouble frameRate() const { return fps; }
};
```
### UI Metrics Display
**Location**: UserInterfaceManager.cpp
```cpp
#define PLOT_ARRAY_SIZE 180 // Rolling window of 180 frames
// RenderMetrics() method displays:
- Current FPS
- Frame time plot
- GPU memory usage
- Render statistics
```
### Memory Monitoring
**Location**: RenderingManager.cpp (lines 786-850)
```cpp
static glm::ivec2 getGPUMemoryInformation()
{
// NVIDIA: GL_NVX_gpu_memory_info extension
// ATI: GL_ATI_meminfo extension
return { current_available_MB, total_available_MB }
}
static bool shouldHaveEnoughMemory(glm::vec3 resolution, int flags)
{
// Check if enough VRAM before creating framebuffer
}
```
### Source Update Timing
**Location**: Mixer.cpp (lines 200-206)
```cpp
// Detailed frametime tracking
dt_ = g_timer_elapsed(timer, NULL) * 1000.0; // Instantaneous
dt__ = 0.05f * dt_ + 0.95f * dt__; // Exponential Moving Average
// Accessible via
Mixer::manager().dt() // Raw delta
Mixer::manager().dt__() // Smoothed delta
Mixer::manager().fps() // Calculated FPS
```
---
## 9. Callback Architecture
### Rendering Callbacks
**Location**: RenderingManager.h (lines 129-131)
```cpp
typedef void (* RenderingCallback)(void);
void pushBackDrawCallback(RenderingCallback function);
// Callbacks are executed in sequence each frame
std::list<RenderingCallback> draw_callbacks_;
```
### Current Registered Callbacks
**Location**: main.cpp (lines 241-243)
```cpp
1. prepare() // Update phase
2. drawScene() // Render scene phase
3. renderGUI() // Render UI phase
```
Each callback is invoked in order during `Rendering::draw()` (lines 651-655).
---
## 10. Rendering Call Flow Diagram
```
┌─ Main Loop (main.cpp:261)
│
└─ Rendering::draw() [RenderingManager.cpp:608]
│
├─ Event handling
│ ├─ glfwPollEvents()
│ ├─ TabletInput::pollEvents()
│ └─ Handle fullscreen/window state changes
│
├─ Output Windows Rendering
│ └─ For each output window:
│ └─ RenderingWindow::draw(frame)
│ └─ Render session()->frame() to monitor
│
├─ Main Window Context Activation
│ └─ main_.makeCurrent()
│
├─ Execute Draw Callbacks (in sequence)
│ │
│ ├─ [1] prepare() [main.cpp:46]
│ │ └─ Mixer::update(dt) [Mixer.cpp:86]
│ │ └─ Session::update(dt) [Session.h:96]
│ │ ├─ For each source:
│ │ │ ├─ Source::update(dt) [Source.h:162]
│ │ │ │ ├─ MediaSource::update(dt) [MediaSource.cpp:181]
│ │ │ │ │ └─ MediaPlayer::update() [MediaPlayer.cpp]
│ │ │ │ │ └─ Poll GStreamer pipeline
│ │ │ │ │ └─ Update texture from latest frame
│ │ │ │ │ └─ Measure FPS via TimeCounter
│ │ │ │ │
│ │ │ │ └─ SourceCallback execution
│ │ │ │
│ │ │ ├─ RenderView internal update
│ │ │ │ └─ Update mixing nodes positions/transforms
│ │ │ │
│ │ │ └─ View::update(dt) [View.h:38]
│ │ │
│ │ ├─ Frame delta time calculation
│ │ │ └─ GTimer: dt_, dt__ (smoothed), fps
│ │ │
│ │ └─ Handle failed sources
│ │
│ │ └─ UserInterface::NewFrame() [UserInterfaceManager.cpp:866]
│ │ ├─ ImGui_ImplOpenGL3_NewFrame()
│ │ ├─ ImGui_ImplGlfw_NewFrame()
│ │ └─ ImGui::NewFrame()
│ │
│ ├─ [2] drawScene() [main.cpp:53]
│ │ └─ Mixer::draw() [Mixer.cpp:278]
│ │ └─ current_view_->draw() [View.h:39]
│ │ │
│ │ └─ RenderView::draw() [RenderView.cpp]
│ │ ├─ frame_buffer_->begin() [FrameBuffer.cpp:58]
│ │ │ └─ Bind FBO, set viewport, clear
│ │ │
│ │ ├─ DrawVisitor traversal
│ │ │ ├─ Visit scene root
│ │ │ └─ Draw all source nodes
│ │ │ └─ For each source:
│ │ │ ├─ Source::render() [Source.h:169]
│ │ │ │ └─ Render texture to composition FB
│ │ │ │
│ │ │ └─ Apply blending shader
│ │ │ ├─ ImageProcessingShader
│ │ │ └─ BlendingShader effects
│ │ │
│ │ ├─ Apply fading overlay
│ │ │ └─ fading_overlay_->draw()
│ │ │
│ │ └─ frame_buffer_->end()
│ │ └─ Unbind FBO
│ │
│ │ Result: Session frame buffer updated
│ │
│ └─ [3] renderGUI() [main.cpp:58]
│ └─ UserInterface::Render() [UserInterfaceManager.cpp:1046]
│ ├─ Build ImGui window hierarchy
│ │ ├─ Menu bar
│ │ ├─ View navigator
│ │ ├─ Source control panels
│ │ ├─ Inspector windows
│ │ └─ Other UI elements
│ │
│ └─ ImGui::Render() + RenderDrawData()
│ └─ Execute OpenGL draw calls for UI
│
├─ Screenshot Capture (if requested)
│ └─ screenshot_.captureGL()
│
├─ Buffer Swap
│ └─ glfwSwapBuffers()
│
└─ Framerate Limiting
└─ GTimer: sleep until 15600 microseconds elapsed (~62 FPS)
```
---
## 11. Key Files and Their Roles
| Component | File | Key Responsibility |
|-----------|------|-------------------|
| **Main Loop** | `main.cpp` | Application entry, callback registration |
| **Rendering Manager** | `RenderingManager.cpp/.h` | Window management, GL context, callback execution |
| **Mixer** | `Mixer.cpp/.h` | Scene management, source updating, FPS calculation |
| **Session** | `Session.cpp/.h` | Source composition, session state |
| **RenderView** | `View/RenderView.cpp/.h` | Final composition/rendering to FBO |
| **Source Base** | `Source/Source.cpp/.h` | Source interface, framebuffer attachment |
| **MediaSource** | `Source/MediaSource.cpp` | Video/image file handling |
| **MediaPlayer** | `MediaPlayer.cpp/.h` | GStreamer pipeline control, texture management |
| **Stream** | `Stream.cpp/.h` | Generic GStreamer stream handling |
| **FrameBuffer** | `FrameBuffer.cpp/.h` | OpenGL FBO creation and management |
| **UI Manager** | `UserInterfaceManager.cpp/.h` | ImGui initialization and rendering |
| **DrawVisitor** | `Visitor/DrawVisitor.cpp/.h` | Scene graph traversal for rendering |
---
## 12. Profiling Opportunities
### Existing Instrumentation Points
1. **GLib GTimer** - Used for frame timing
- Location: `RenderingManager.cpp`, `Mixer.cpp`
- Already measures: Frame delta time, FPS, framerate limiting
2. **MediaPlayer TimeCounter** - FPS per source
- Location: `MediaPlayer.h` (lines 370-378)
- Already measures: Update framerate per media player
3. **GPU Memory** - VRAM usage queries
- Location: `RenderingManager.cpp` (getGPUMemoryInformation)
- Uses: NVIDIA/ATI GPU memory extensions
### Recommended Profiling Points
1. **Per-stage timing** (prepare, draw, GUI)
- Insert GTimer at start/end of each callback
- Track: Update time, render time, UI time
2. **Source-level profiling**
- Time each `Source::update()` and `Source::render()`
- Track: GStreamer decoding, texture upload, GPU rendering
3. **View composition timing**
- Time RenderView::draw() phases
- Track: DrawVisitor traversal, shader execution
4. **GStreamer pipeline monitoring**
- Query pipeline state and buffer queue depth
- Track: Frame drops, decoding lag
5. **ImGui rendering stats**
- Use ImGui metrics window
- Track: Draw call count, vertex count, UI layout time
---
## 13. Architecture Summary
**Vimix's rendering is organized as:**
1. **Event-Driven Main Loop**
- Framerate-limited (~62 FPS)
- Callback-based architecture
2. **Three-Stage Pipeline Per Frame**
- Update phase: Process all sources and state
- Render phase: Compose sources to session framebuffer
- UI phase: Render ImGui overlay
3. **GStreamer-Based Source Handling**
- Asynchronous decoding via GStreamer pipelines
- GPU texture management (GL interop)
- Hardware decoding support
4. **Layered Rendering Model**
- Individual source → source framebuffer
- All sources → session framebuffer (composition)
- Session → output windows (display)
- UI overlay on top (ImGui)
5. **Visitor Pattern for Scene Traversal**
- DrawVisitor renders scene graph
- Depth-sorted source ordering
- Flexible for different rendering modes
This architecture enables efficient real-time video mixing with flexible source types and advanced effects processing.
|