oximedia-cli 0.1.6

Command-line interface for OxiMedia
Documentation
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
//! NDI (Network Device Interface) CLI commands.
//!
//! Provides NDI source discovery, stream sending/receiving, and monitoring
//! using the `oximedia-ndi` crate.

use anyhow::{Context, Result};
use clap::Subcommand;
use colored::Colorize;
use std::path::PathBuf;

/// NDI command subcommands.
#[derive(Subcommand, Debug)]
pub enum NdiCommand {
    /// Discover NDI sources on the network
    Discover {
        /// Discovery timeout in seconds
        #[arg(long, default_value = "5")]
        timeout: u64,

        /// NDI group filter
        #[arg(long)]
        group: Option<String>,
    },

    /// Receive NDI stream and save to file
    Receive {
        /// Source name or IP address
        #[arg(short, long)]
        source: String,

        /// Output file path
        #[arg(short, long)]
        output: PathBuf,

        /// Recording duration in seconds (unlimited if omitted)
        #[arg(long)]
        duration: Option<f64>,

        /// Receive audio only (discard video)
        #[arg(long)]
        audio_only: bool,

        /// Receive video only (discard audio)
        #[arg(long)]
        video_only: bool,

        /// Low-bandwidth mode
        #[arg(long)]
        low_bandwidth: bool,
    },

    /// Send a media file as an NDI source
    Send {
        /// Input media file
        #[arg(short, long)]
        input: PathBuf,

        /// NDI source name
        #[arg(long)]
        name: Option<String>,

        /// Loop the input continuously
        #[arg(long, default_value = "false")]
        looping: bool,

        /// NDI group to join
        #[arg(long)]
        group: Option<String>,

        /// Enable tally light support
        #[arg(long)]
        tally: bool,
    },

    /// Monitor NDI sources and display stats
    Monitor {
        /// Specific source to monitor (all sources if omitted)
        #[arg(short, long)]
        source: Option<String>,

        /// Stats refresh interval in seconds
        #[arg(long, default_value = "2")]
        interval: u64,

        /// Show detailed per-frame statistics
        #[arg(long)]
        detailed: bool,
    },
}

/// Handle NDI command dispatch.
pub async fn handle_ndi_command(command: NdiCommand, json_output: bool) -> Result<()> {
    match command {
        NdiCommand::Discover { timeout, group } => {
            discover_sources(timeout, group.as_deref(), json_output).await
        }
        NdiCommand::Receive {
            source,
            output,
            duration,
            audio_only,
            video_only,
            low_bandwidth,
        } => {
            receive_stream(
                &source,
                &output,
                duration,
                audio_only,
                video_only,
                low_bandwidth,
                json_output,
            )
            .await
        }
        NdiCommand::Send {
            input,
            name,
            looping,
            group,
            tally,
        } => {
            send_stream(
                &input,
                name.as_deref(),
                looping,
                group.as_deref(),
                tally,
                json_output,
            )
            .await
        }
        NdiCommand::Monitor {
            source,
            interval,
            detailed,
        } => monitor_sources(source.as_deref(), interval, detailed, json_output).await,
    }
}

// ---------------------------------------------------------------------------
// Discover
// ---------------------------------------------------------------------------

async fn discover_sources(timeout_secs: u64, group: Option<&str>, json_output: bool) -> Result<()> {
    let timeout_dur = std::time::Duration::from_secs(timeout_secs);

    // Use the NDI discovery service
    let discovery =
        oximedia_ndi::DiscoveryService::new().context("Failed to create NDI discovery service")?;

    let sources = discovery
        .discover(timeout_dur)
        .await
        .context("NDI discovery failed")?;

    // Filter by group if specified
    let filtered: Vec<_> = if let Some(grp) = group {
        sources
            .into_iter()
            .filter(|s| s.groups.iter().any(|g| g == grp))
            .collect()
    } else {
        sources
    };

    if json_output {
        let source_list: Vec<serde_json::Value> = filtered
            .iter()
            .map(|s| {
                serde_json::json!({
                    "name": s.name,
                    "address": s.address.to_string(),
                    "groups": s.groups,
                })
            })
            .collect();
        let result = serde_json::json!({
            "command": "ndi discover",
            "timeout_seconds": timeout_secs,
            "group_filter": group,
            "sources_found": filtered.len(),
            "sources": source_list,
        });
        let json_str =
            serde_json::to_string_pretty(&result).context("Failed to serialize result")?;
        println!("{}", json_str);
    } else {
        println!("{}", "NDI Source Discovery".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {} seconds", "Timeout:", timeout_secs);
        if let Some(grp) = group {
            println!("{:20} {}", "Group filter:", grp);
        }
        println!();

        if filtered.is_empty() {
            println!("{}", "No NDI sources found on the network.".yellow());
        } else {
            println!("Found {} source(s):", filtered.len().to_string().cyan());
            println!("{}", "-".repeat(60));
            for (i, source) in filtered.iter().enumerate() {
                println!("  {}. {}", i + 1, source.name.green().bold());
                println!("     Address: {}", source.address);
                if !source.groups.is_empty() {
                    println!("     Groups:  {}", source.groups.join(", "));
                }
            }
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Receive
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
async fn receive_stream(
    source_name: &str,
    output: &PathBuf,
    duration: Option<f64>,
    audio_only: bool,
    video_only: bool,
    low_bandwidth: bool,
    json_output: bool,
) -> Result<()> {
    if audio_only && video_only {
        return Err(anyhow::anyhow!(
            "Cannot specify both --audio-only and --video-only"
        ));
    }

    // Configure the NDI receiver
    let config = oximedia_ndi::NdiConfig {
        name: format!("OxiMedia Receiver ({})", source_name),
        low_bandwidth,
        ..oximedia_ndi::NdiConfig::default()
    };

    if json_output {
        let result = serde_json::json!({
            "command": "ndi receive",
            "source": source_name,
            "output": output.display().to_string(),
            "duration": duration,
            "audio_only": audio_only,
            "video_only": video_only,
            "low_bandwidth": low_bandwidth,
            "config": {
                "name": config.name,
                "buffer_size": config.buffer_size,
                "enable_tally": config.enable_tally,
                "enable_ptz": config.enable_ptz,
            },
            "status": "ready",
            "message": "NDI receiver configured; awaiting connection to source",
        });
        let json_str =
            serde_json::to_string_pretty(&result).context("Failed to serialize result")?;
        println!("{}", json_str);
    } else {
        println!("{}", "NDI Receive".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Source:", source_name);
        println!("{:20} {}", "Output:", output.display());
        if let Some(dur) = duration {
            println!("{:20} {:.1}s", "Duration:", dur);
        } else {
            println!("{:20} unlimited", "Duration:");
        }
        println!("{:20} {}", "Audio only:", audio_only);
        println!("{:20} {}", "Video only:", video_only);
        println!("{:20} {}", "Low bandwidth:", low_bandwidth);
        println!("{:20} {}", "Buffer size:", config.buffer_size);
        println!();

        println!("{}", "Receiver Configuration".cyan().bold());
        println!("{}", "-".repeat(60));
        println!("  Name:          {}", config.name);
        println!("  Tally support: {}", config.enable_tally);
        println!("  PTZ support:   {}", config.enable_ptz);
        println!();

        println!(
            "{}",
            "NDI receiver configured and ready to connect.".dimmed()
        );
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Send
// ---------------------------------------------------------------------------

async fn send_stream(
    input: &PathBuf,
    name: Option<&str>,
    looping: bool,
    group: Option<&str>,
    tally: bool,
    json_output: bool,
) -> Result<()> {
    if !input.exists() {
        return Err(anyhow::anyhow!("Input file not found: {}", input.display()));
    }

    let source_name = name.unwrap_or("OxiMedia NDI Source");
    let groups = group.map_or_else(|| vec!["public".to_string()], |g| vec![g.to_string()]);

    let file_size = std::fs::metadata(input)
        .context("Failed to read file metadata")?
        .len();

    // Build NDI config
    let config = oximedia_ndi::NdiConfig {
        name: source_name.to_string(),
        groups: groups.clone(),
        enable_tally: tally,
        ..oximedia_ndi::NdiConfig::default()
    };

    // Validate that a sender can be created with this config
    let _sender_config = oximedia_ndi::SenderConfig::default();

    if json_output {
        let result = serde_json::json!({
            "command": "ndi send",
            "input": input.display().to_string(),
            "file_size": file_size,
            "source_name": source_name,
            "groups": groups,
            "looping": looping,
            "tally": tally,
            "config": {
                "name": config.name,
                "groups": config.groups,
                "enable_tally": config.enable_tally,
                "enable_ptz": config.enable_ptz,
                "buffer_size": config.buffer_size,
            },
            "status": "ready",
            "message": "NDI sender configured; awaiting frame decoding pipeline integration",
        });
        let json_str =
            serde_json::to_string_pretty(&result).context("Failed to serialize result")?;
        println!("{}", json_str);
    } else {
        println!("{}", "NDI Send".green().bold());
        println!("{}", "=".repeat(60));
        println!("{:20} {}", "Input:", input.display());
        println!("{:20} {} bytes", "File size:", file_size);
        println!("{:20} {}", "Source name:", source_name);
        println!("{:20} {}", "Groups:", groups.join(", "));
        println!("{:20} {}", "Looping:", looping);
        println!("{:20} {}", "Tally:", tally);
        println!();

        println!("{}", "Sender Configuration".cyan().bold());
        println!("{}", "-".repeat(60));
        println!("  Buffer size: {}", config.buffer_size);
        println!("  PTZ:         {}", config.enable_ptz);
        println!();

        println!(
            "{}",
            "Note: Frame decoding pipeline not yet integrated.".yellow()
        );
        println!(
            "{}",
            "NDI sender is configured; frame decoding will enable streaming.".dimmed()
        );
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Monitor
// ---------------------------------------------------------------------------

async fn monitor_sources(
    source_name: Option<&str>,
    interval: u64,
    detailed: bool,
    json_output: bool,
) -> Result<()> {
    let timeout_dur = std::time::Duration::from_secs(5);

    let discovery =
        oximedia_ndi::DiscoveryService::new().context("Failed to create NDI discovery service")?;

    let sources = discovery
        .discover(timeout_dur)
        .await
        .context("NDI discovery failed")?;

    // Filter to specific source if requested
    let monitored: Vec<_> = if let Some(name) = source_name {
        sources
            .into_iter()
            .filter(|s| s.name.contains(name))
            .collect()
    } else {
        sources
    };

    if json_output {
        let source_list: Vec<serde_json::Value> = monitored
            .iter()
            .map(|s| {
                serde_json::json!({
                    "name": s.name,
                    "address": s.address.to_string(),
                    "groups": s.groups,
                })
            })
            .collect();
        let result = serde_json::json!({
            "command": "ndi monitor",
            "source_filter": source_name,
            "refresh_interval_seconds": interval,
            "detailed": detailed,
            "sources_found": monitored.len(),
            "sources": source_list,
            "status": "monitoring",
        });
        let json_str =
            serde_json::to_string_pretty(&result).context("Failed to serialize result")?;
        println!("{}", json_str);
    } else {
        println!("{}", "NDI Monitor".green().bold());
        println!("{}", "=".repeat(60));
        if let Some(name) = source_name {
            println!("{:20} {}", "Source filter:", name);
        }
        println!("{:20} {}s", "Refresh interval:", interval);
        println!("{:20} {}", "Detailed stats:", detailed);
        println!();

        if monitored.is_empty() {
            println!("{}", "No NDI sources found to monitor.".yellow());
        } else {
            println!(
                "Monitoring {} source(s):",
                monitored.len().to_string().cyan()
            );
            println!("{}", "-".repeat(60));
            for source in &monitored {
                println!("  {} ({})", source.name.green(), source.address);
                if !source.groups.is_empty() {
                    println!("    Groups: {}", source.groups.join(", "));
                }
                if detailed {
                    println!("    Video:  pending connection");
                    println!("    Audio:  pending connection");
                    println!("    Tally:  unknown");
                }
            }
            println!();
            println!(
                "{}",
                "NDI monitor initialized; real-time stats require active connections.".dimmed()
            );
        }
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ndi_command_variants() {
        // Verify all command variants can be constructed
        let _discover = NdiCommand::Discover {
            timeout: 5,
            group: None,
        };
        let _receive = NdiCommand::Receive {
            source: "test".to_string(),
            output: PathBuf::from("out.mkv"),
            duration: Some(10.0),
            audio_only: false,
            video_only: false,
            low_bandwidth: false,
        };
        let _send = NdiCommand::Send {
            input: PathBuf::from("input.mkv"),
            name: Some("Test Source".to_string()),
            looping: false,
            group: None,
            tally: true,
        };
        let _monitor = NdiCommand::Monitor {
            source: None,
            interval: 2,
            detailed: false,
        };
    }

    #[test]
    fn test_receive_rejects_audio_and_video_only() {
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("failed to build runtime");
        let result = rt.block_on(receive_stream(
            "test",
            &PathBuf::from("out.mkv"),
            None,
            true,
            true,
            false,
            false,
        ));
        assert!(result.is_err());
        let err_msg = format!("{}", result.expect_err("should fail"));
        assert!(err_msg.contains("audio-only"));
    }

    #[test]
    fn test_send_rejects_missing_input() {
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("failed to build runtime");
        let result = rt.block_on(send_stream(
            &PathBuf::from("/nonexistent/file.mkv"),
            None,
            false,
            None,
            false,
            false,
        ));
        assert!(result.is_err());
    }

    #[test]
    fn test_ndi_config_defaults() {
        let config = oximedia_ndi::NdiConfig::default();
        assert!(config.enable_tally);
        assert!(config.enable_ptz);
        assert_eq!(config.buffer_size, 16);
        assert_eq!(config.groups, vec!["public".to_string()]);
    }

    #[test]
    fn test_video_format_construction() {
        let fmt = oximedia_ndi::VideoFormat::full_hd_60p();
        assert_eq!(fmt.width, 1920);
        assert_eq!(fmt.height, 1080);
        assert!((fmt.frame_rate() - 60.0).abs() < 0.001);
    }
}