videocall_cli/
cli_args.rs

1/*
2 * Copyright 2025 Security Union LLC
3 *
4 * Licensed under either of
5 *
6 * * Apache License, Version 2.0
7 *   (http://www.apache.org/licenses/LICENSE-2.0)
8 * * MIT license
9 *   (http://opensource.org/licenses/MIT)
10 *
11 * at your option.
12 *
13 * Unless you explicitly state otherwise, any contribution intentionally
14 * submitted for inclusion in the work by you, as defined in the Apache-2.0
15 * license, shall be dual licensed as above, without any additional terms or
16 * conditions.
17 */
18
19use std::str::FromStr;
20
21use clap::{ArgGroup, Args, Parser, Subcommand};
22use thiserror::Error;
23use url::Url;
24use videocall_nokhwa::utils::FrameFormat;
25
26/// Video Call CLI
27///
28/// This cli connects to the videocall.rs and streams audio and video to the specified meeting.
29///
30/// You can watch the video at https://videocall.rs/meeting/{meeting_id}
31#[derive(Parser, Debug)]
32#[clap(name = "client")]
33pub struct Opt {
34    #[clap(subcommand)]
35    pub mode: Mode,
36}
37
38#[derive(Clone, Debug)]
39pub enum IndexKind {
40    String(String),
41    Index(u32),
42}
43
44#[derive(Error, Debug)]
45pub enum ParseIndexKindError {
46    #[error("Invalid index value: {0}")]
47    InvalidIndex(String),
48}
49
50impl FromStr for IndexKind {
51    type Err = ParseIndexKindError;
52
53    fn from_str(s: &str) -> Result<Self, Self::Err> {
54        if let Ok(index) = s.parse::<u32>() {
55            Ok(IndexKind::Index(index))
56        } else {
57            Ok(IndexKind::String(s.to_string()))
58        }
59    }
60}
61
62#[derive(Subcommand, Debug)]
63pub enum Mode {
64    /// Stream audio and video to the specified meeting.
65    Stream(Stream),
66
67    /// Information mode to list cameras, formats, and resolutions.
68    Info(Info),
69}
70
71#[derive(Args, Debug, Clone)]
72pub struct Stream {
73    /// URL to connect to.
74    #[clap(long = "url", default_value = "https://transport.rustlemania.com")]
75    pub url: Url,
76
77    #[clap(long = "user-id")]
78    pub user_id: String,
79
80    #[clap(long = "meeting-id")]
81    pub meeting_id: String,
82
83    /// Specify which camera to use, either by index number or name.
84    ///
85    /// Examples:
86    ///   --video-device-index 0    # Use first camera
87    ///   --video-device-index "HD WebCam"  # Use camera by name
88    ///
89    /// If not provided, the program will list all available cameras.
90    /// You can also see available cameras by running:
91    ///   videocall-cli info --list-cameras
92    ///
93    /// Note for MacOS users: You must use the device UUID instead of the human-readable name.
94    /// The UUID can be found in the "Extras" field when listing cameras.
95    #[clap(long = "video-device-index", short = 'v')]
96    pub video_device_index: Option<IndexKind>,
97
98    #[clap(long = "audio-device", short = 'a')]
99    pub audio_device: Option<String>,
100
101    /// Resolution in WIDTHxHEIGHT format (e.g., 1920x1080)
102    #[clap(long = "resolution", short = 'r')]
103    #[clap(default_value = "1280x720")]
104    pub resolution: String,
105
106    /// Frame rate for the video stream.
107    #[clap(long = "fps")]
108    #[clap(default_value = "30")]
109    pub fps: u32,
110
111    #[clap(long = "bitrate-kbps")]
112    #[clap(default_value = "500")]
113    pub bitrate_kbps: u32,
114
115    /// Controls the speed vs. quality tradeoff for VP9 encoding.
116    ///
117    /// The value ranges from `0` (slowest, best quality) to `15` (fastest, lowest quality).
118    ///
119    /// The cli does not allow selecting values below 4 because they are useless for realtime streaming.
120    ///
121    /// ## Valid Values:
122    /// - `4` to `8`: **Fast encoding**, lower quality (good for real-time streaming, live video).
123    /// - `9` to `15`: **Very fast encoding**, lowest quality, largest files (for ultra-low-latency applications).
124    ///
125    /// videocall-cli --vp9-cpu-used 5  # Fast encoding, good for live streaming
126    #[arg(long, default_value_t = 5, value_parser = clap::value_parser!(u8).range(4..=15))]
127    pub vp9_cpu_used: u8,
128
129    /// Frame format to use for the video stream.
130    /// Different cameras support different formats.
131    /// Please use the `info` subcommand to list supported formats for a specific camera.
132    #[arg(long, default_value_t = FrameFormat::NV12, value_parser = parse_frame_format)]
133    pub frame_format: FrameFormat,
134
135    /// Perform NSS-compatible TLS key logging to the file specified in `SSLKEYLOGFILE`.
136    #[clap(long = "debug-keylog")]
137    pub keylog: bool,
138
139    /// Send test pattern instead of camera video.
140    #[clap(long = "debug-send-test-pattern")]
141    pub send_test_pattern: bool,
142
143    /// This is for ensuring that we can open the camera and encode video
144    #[clap(long = "debug-offline-streaming-test")]
145    pub local_streaming_test: bool,
146}
147
148fn parse_frame_format(s: &str) -> Result<FrameFormat, String> {
149    match s {
150        "NV12" => Ok(FrameFormat::NV12),
151        // TODO: Merge MR with MacOS BGRA support
152        // "BGRA" => Ok(FrameFormat::BGRA),
153        "YUYV" => Ok(FrameFormat::YUYV),
154        _ => Err("Invalid frame format, please use one of [NV12, BGRA, YUYV]".to_string()),
155    }
156}
157
158#[derive(Args, Debug)]
159#[clap(group = ArgGroup::new("info").required(true))]
160pub struct Info {
161    /// List available cameras.
162    #[clap(long = "list-cameras", group = "info")]
163    pub list_cameras: bool,
164
165    /// List supported formats for a specific camera using the index from `list-cameras`
166    #[clap(long = "list-formats", group = "info")]
167    pub list_formats: Option<IndexKind>, // Camera index
168
169    /// List supported resolutions for a specific camera using the index from `list-cameras`
170    #[clap(long = "list-resolutions", group = "info")]
171    pub list_resolutions: Option<IndexKind>, // Camera index
172}
173
174#[derive(Args, Debug, Clone)]
175pub struct TestCamera {
176    #[clap(long = "video-device-index")]
177    pub video_device_index: IndexKind,
178}