|
| 1 | +# Video Call Screen Sharing |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document shows how screen sharing works within an active video call in Rocket.Chat Electron. |
| 6 | + |
| 7 | +## Screen Sharing Flow |
| 8 | + |
| 9 | +```mermaid |
| 10 | +flowchart TD |
| 11 | + %% Screen Sharing Initiation |
| 12 | + A[Video Call Active] --> B[User Requests Screen Share] |
| 13 | + B --> C[Webview Calls requestScreenSharing API] |
| 14 | + C --> D[Preload Script Receives Request] |
| 15 | + |
| 16 | + %% IPC Communication to Main Process |
| 17 | + D --> E[Send video-call-window/open-screen-picker IPC] |
| 18 | + E --> F[Main Process Receives Request] |
| 19 | + F --> G[Create Screen Picker Window] |
| 20 | + G --> H[Load ScreenSharePicker Component] |
| 21 | + |
| 22 | + %% Desktop Capturer Cache Check |
| 23 | + H --> I{Desktop Capturer Cache Valid?} |
| 24 | + I -->|Yes - Within 3s TTL| J[Return Cached Sources] |
| 25 | + I -->|No - Cache Expired/Empty| K[Fetch Available Sources] |
| 26 | + |
| 27 | + %% System Source Fetching |
| 28 | + K --> L[Call desktopCapturer.getSources] |
| 29 | + L --> M[System Returns Available Sources] |
| 30 | + M --> N[Filter & Process Sources] |
| 31 | + N --> O[Cache Sources with 3s TTL] |
| 32 | + O --> J[Return Sources to UI] |
| 33 | + |
| 34 | + %% Source Selection UI |
| 35 | + J --> P[Display Sources in Tabs] |
| 36 | + P --> Q[Windows Tab - Application Windows] |
| 37 | + P --> R[Screens Tab - Desktop Displays] |
| 38 | + Q --> S[User Selects Window] |
| 39 | + R --> T[User Selects Screen] |
| 40 | + S --> U[Validate Selected Source] |
| 41 | + T --> U |
| 42 | + |
| 43 | + %% Source Validation with Caching |
| 44 | + U --> V{Source in Validation Cache?} |
| 45 | + V -->|Yes - Within 30s TTL| W[Return Cached Validation] |
| 46 | + V -->|No - Not Cached/Expired| X[Validate Source Availability] |
| 47 | + X --> Y[Check Source Still Exists] |
| 48 | + Y --> Z{Source Valid?} |
| 49 | + Z -->|Yes| AA[Cache Validation with 30s TTL] |
| 50 | + Z -->|No| BB[Show Source Unavailable Error] |
| 51 | + AA --> W |
| 52 | + W --> CC[Return Source ID to Main Process] |
| 53 | + |
| 54 | + %% IPC Response to Webview |
| 55 | + CC --> DD[Send Source ID via IPC Response] |
| 56 | + DD --> EE[Preload Script Receives Source ID] |
| 57 | + EE --> FF[Return Source to Webview] |
| 58 | + FF --> GG[Webview Gets Stream Access] |
| 59 | + GG --> HH[User Sees Screen Share Active] |
| 60 | + |
| 61 | + %% Error Handling |
| 62 | + BB --> II[User Selects Different Source] |
| 63 | + II --> U |
| 64 | + |
| 65 | + %% Cache Cleanup on Window Close |
| 66 | + HH --> JJ[Video Call Window Closes] |
| 67 | + JJ --> KK[Schedule Cache Cleanup - 60s Delay] |
| 68 | + KK --> LL{Other Video Call Windows?} |
| 69 | + LL -->|Yes| MM[Keep Cache Active] |
| 70 | + LL -->|No| NN[Clear Desktop Capturer Cache] |
| 71 | + NN --> OO[Clear Source Validation Cache] |
| 72 | + OO --> PP[Cache Cleanup Complete] |
| 73 | + |
| 74 | + %% Cache Miss Performance Path |
| 75 | + K --> QQ[Background Promise Deduplication] |
| 76 | + QQ --> RR{Existing Fetch in Progress?} |
| 77 | + RR -->|Yes| SS[Wait for Existing Fetch] |
| 78 | + RR -->|No| TT[Start New Fetch Operation] |
| 79 | + SS --> J |
| 80 | + TT --> L |
| 81 | + |
| 82 | + %% Styling |
| 83 | + classDef mainProcess fill:#e1f5fe,stroke:#0277bd,stroke-width:2px |
| 84 | + classDef renderer fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px |
| 85 | + classDef webview fill:#e8f5e8,stroke:#2e7d32,stroke-width:2px |
| 86 | + classDef cache fill:#e3f2fd,stroke:#1976d2,stroke-width:2px |
| 87 | + classDef ui fill:#fff3e0,stroke:#f57c00,stroke-width:2px |
| 88 | + classDef validation fill:#f3e5f5,stroke:#8e24aa,stroke-width:2px |
| 89 | + classDef error fill:#ffebee,stroke:#c62828,stroke-width:2px |
| 90 | + classDef success fill:#e8f5e8,stroke:#388e3c,stroke-width:2px |
| 91 | + |
| 92 | + class F,G,L,M,N,CC,DD,KK,LL,NN,OO mainProcess |
| 93 | + class D,E,EE,FF renderer |
| 94 | + class A,C,GG,HH webview |
| 95 | + class I,J,O,V,W,AA,QQ,RR,SS,TT cache |
| 96 | + class H,P,Q,R,S,T ui |
| 97 | + class U,X,Y,Z,BB validation |
| 98 | + class BB,II error |
| 99 | + class CC,GG,HH,PP success |
| 100 | +``` |
| 101 | + |
| 102 | +## Explanation |
| 103 | + |
| 104 | +**What this diagram shows:** |
| 105 | +- How screen sharing works inside an active video call |
| 106 | +- The caching system that makes it fast and smooth |
| 107 | +- How users select what to share from their computer |
| 108 | +- Error handling when something goes wrong |
| 109 | + |
| 110 | +**Key parts:** |
| 111 | +1. **User Request** - User clicks screen share button in video call |
| 112 | +2. **Source Discovery** - App finds all windows and screens available |
| 113 | +3. **Caching System** - Remembers recent results to avoid slow lookups |
| 114 | +4. **User Selection** - Shows windows and screens in easy-to-use tabs |
| 115 | +5. **Validation** - Checks that selected window/screen still exists |
| 116 | +6. **Stream Access** - Gives video call access to share the screen |
| 117 | +7. **Cache Cleanup** - Cleans up memory when no longer needed |
| 118 | + |
| 119 | +**Performance Features:** |
| 120 | +- **3-second cache** for source lists (faster when user reopens picker) |
| 121 | +- **30-second validation cache** (remembers which sources work) |
| 122 | +- **60-second cleanup delay** (keeps cache active for quick re-use) |
| 123 | +- **Promise deduplication** (prevents multiple slow system calls) |
| 124 | + |
| 125 | +**Color Guide:** |
| 126 | +- **Blue** - Main process & Cache operations |
| 127 | +- **Purple** - Renderer process & Validation |
| 128 | +- **Green** - Webview & Success states |
| 129 | +- **Orange** - User interface elements |
| 130 | +- **Red** - Error states |
| 131 | + |
| 132 | +**Starting Point:** |
| 133 | +This flow starts from the "Video Call Active" state that you get after completing the window management flow. |
| 134 | + |
| 135 | +## Detailed Step-by-Step Explanation |
| 136 | + |
| 137 | +### 1. Starting Screen Sharing |
| 138 | +When you're in an active video call and want to share your screen, you click the screen share button in the video call interface. |
| 139 | + |
| 140 | +**What happens:** |
| 141 | +- The video call website detects your click on the screen share button |
| 142 | +- It calls a special function that Rocket.Chat provides for screen sharing |
| 143 | +- This request goes through a secure bridge between the video call and the main app |
| 144 | +- The main app receives the screen sharing request |
| 145 | + |
| 146 | +### 2. Opening the Source Picker |
| 147 | +The app needs to show you what you can share (windows and screens). |
| 148 | + |
| 149 | +**What happens:** |
| 150 | +- Creates a new small window specifically for choosing what to share |
| 151 | +- This picker window is separate from your video call window |
| 152 | +- Loads a user interface that will show you all available options |
| 153 | +- Prepares to fetch information about what's available on your computer |
| 154 | + |
| 155 | +### 3. Checking the Cache First |
| 156 | +Before asking your computer for available windows and screens, the app checks if it already knows. |
| 157 | + |
| 158 | +**What happens:** |
| 159 | +- Looks in its memory for a recent list of windows and screens |
| 160 | +- If it found a list less than 3 seconds ago, it uses that (much faster!) |
| 161 | +- If the information is older than 3 seconds, it needs to get fresh data |
| 162 | +- This caching makes screen sharing feel instant when you use it multiple times |
| 163 | + |
| 164 | +### 4. Getting Available Sources |
| 165 | +When the cache is empty or old, the app asks your operating system what you can share. |
| 166 | + |
| 167 | +**What happens:** |
| 168 | +- Asks your computer for a list of all open windows |
| 169 | +- Asks for a list of all monitors/screens you have |
| 170 | +- Your operating system provides thumbnails and names for each option |
| 171 | +- The app filters and organizes this information for display |
| 172 | + |
| 173 | +### 5. Caching for Performance |
| 174 | +To make future requests faster, the app remembers what it just learned. |
| 175 | + |
| 176 | +**What happens:** |
| 177 | +- Stores the list of windows and screens in memory |
| 178 | +- Sets a 3-second expiration time on this information |
| 179 | +- If multiple screen share requests come in quickly, they all use the same cached data |
| 180 | +- This prevents overwhelming your computer with repeated requests |
| 181 | + |
| 182 | +### 6. Showing Your Options |
| 183 | +Now you see a nice interface with everything you can share. |
| 184 | + |
| 185 | +**What happens:** |
| 186 | +- Creates tabs to organize your options: "Windows" and "Screens" |
| 187 | +- **Windows tab**: Shows all your open applications with preview thumbnails |
| 188 | +- **Screens tab**: Shows each monitor/display you have connected |
| 189 | +- Each option shows a preview image and the name of the window or screen |
| 190 | +- You can click on any option to select it |
| 191 | + |
| 192 | +### 7. Validating Your Choice |
| 193 | +When you click on something to share, the app double-checks it's still available. |
| 194 | + |
| 195 | +**What happens:** |
| 196 | +- Checks if the window or screen you selected still exists |
| 197 | +- Sometimes windows get closed between when the list was made and when you click |
| 198 | +- If it checked the same source recently (within 30 seconds), it uses that cached result |
| 199 | +- If not, it quickly verifies the source is still available |
| 200 | + |
| 201 | +### 8. Handling Unavailable Sources |
| 202 | +If what you selected is no longer available, the app helps you choose something else. |
| 203 | + |
| 204 | +**What happens:** |
| 205 | +- Shows an error message explaining the window or screen is no longer available |
| 206 | +- The source picker stays open so you can choose a different option |
| 207 | +- Your previous selection is cleared |
| 208 | +- You can try selecting a different window or screen |
| 209 | + |
| 210 | +### 9. Returning the Source |
| 211 | +When you've successfully selected something that's available, the app provides it to your video call. |
| 212 | + |
| 213 | +**What happens:** |
| 214 | +- Caches the validation result for 30 seconds (in case you want to share the same thing again soon) |
| 215 | +- Sends the source information back to the main app |
| 216 | +- The main app passes this information to your video call |
| 217 | +- The source picker window closes since you've made your choice |
| 218 | + |
| 219 | +### 10. Activating Screen Sharing |
| 220 | +Your video call now has permission to share your selected screen or window. |
| 221 | + |
| 222 | +**What happens:** |
| 223 | +- The video call website receives the source information |
| 224 | +- It can now access the stream from your selected window or screen |
| 225 | +- Other people in the call start seeing what you're sharing |
| 226 | +- The screen sharing is now active and working |
| 227 | + |
| 228 | +### 11. Cache Cleanup |
| 229 | +When you close your video call, the app cleans up the memory it was using. |
| 230 | + |
| 231 | +**What happens:** |
| 232 | +- Waits 60 seconds after the video call window closes (in case you open another call quickly) |
| 233 | +- Checks if any other video call windows are still open |
| 234 | +- If no other video calls are running, it clears all cached screen sharing data |
| 235 | +- This prevents the app from using memory unnecessarily |
| 236 | + |
| 237 | +### 12. Performance Optimization |
| 238 | +The app has special features to make screen sharing as fast as possible. |
| 239 | + |
| 240 | +**What happens:** |
| 241 | +- **Promise deduplication**: If multiple requests come in at the same time, it only asks the system once |
| 242 | +- **Background processing**: Fetching source information happens in the background |
| 243 | +- **Smart caching**: Keeps frequently used information readily available |
| 244 | +- **Efficient cleanup**: Only clears memory when it's actually not needed |
| 245 | + |
| 246 | +## Why This System Works Well |
| 247 | + |
| 248 | +### Speed and Responsiveness |
| 249 | +- **3-second source cache**: Makes the picker open almost instantly on repeat use |
| 250 | +- **30-second validation cache**: Avoids re-checking the same sources repeatedly |
| 251 | +- **Immediate response**: You see your options as quickly as your computer can provide them |
| 252 | +- **Background optimization**: Multiple requests don't slow each other down |
| 253 | + |
| 254 | +### Reliability |
| 255 | +- **Real-time validation**: Ensures what you select actually works |
| 256 | +- **Error recovery**: Helps you choose something else if your first choice isn't available |
| 257 | +- **Source verification**: Double-checks everything before committing |
| 258 | +- **Graceful degradation**: Still works even if some features are slow |
| 259 | + |
| 260 | +### Resource Management |
| 261 | +- **Smart caching**: Only keeps information as long as it's useful |
| 262 | +- **Memory cleanup**: Automatically frees up memory when not needed |
| 263 | +- **Efficient requests**: Doesn't overwhelm your computer with repeated system calls |
| 264 | +- **Multi-window support**: Handles multiple video calls without conflicts |
| 265 | + |
| 266 | +### User Experience |
| 267 | +- **Visual previews**: See exactly what you'll be sharing before you choose |
| 268 | +- **Organized tabs**: Easy to find windows vs. screens |
| 269 | +- **Clear feedback**: Know immediately if something isn't available |
| 270 | +- **Fast response**: Screen sharing starts quickly once you make a choice |
| 271 | + |
| 272 | +This system ensures that screen sharing is fast, reliable, and doesn't slow down your computer, even when you have many windows open or multiple monitors connected. |
0 commit comments