Skip to main content

tauri_plugin_video/
models.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4
5/// Version of the JavaScript-to-Rust command, payload, response, and error
6/// contract. Additive diagnostic or capability fields do not change it.
7pub const VIDEO_PLUGIN_PROTOCOL_VERSION: u32 = 1;
8
9#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
10#[serde(rename_all = "camelCase")]
11pub struct NativePluginDiagnostics {
12    pub protocol_version: u32,
13    pub crate_name: String,
14    pub crate_version: String,
15}
16
17impl NativePluginDiagnostics {
18    pub fn current() -> Self {
19        Self {
20            protocol_version: VIDEO_PLUGIN_PROTOCOL_VERSION,
21            crate_name: env!("CARGO_PKG_NAME").to_owned(),
22            crate_version: env!("CARGO_PKG_VERSION").to_owned(),
23        }
24    }
25}
26
27#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq)]
28#[serde(rename_all = "lowercase")]
29pub enum TrackKind {
30    Video,
31    Audio,
32    Subtitle,
33}
34
35#[derive(Debug, Clone, Deserialize, Serialize)]
36#[serde(rename_all = "camelCase")]
37pub struct NativeOpenRequest {
38    /// Protocol and adapter version are required by protocol 1. They remain
39    /// optional while deserializing so legacy JavaScript receives a deliberate
40    /// protocol error instead of a generic invalid-payload rejection.
41    #[serde(default)]
42    pub protocol_version: Option<u32>,
43    #[serde(default)]
44    pub package_version: Option<String>,
45    /// Identifies the JavaScript controller that owns the singleton native surface.
46    /// Late cleanup from an older React render must not close a newer player.
47    #[serde(default)]
48    pub session_key: String,
49    pub uri: String,
50    /// Playback engine requested by the JavaScript controller. `auto` selects
51    /// the platform's primary native backend; alternatives must be explicit.
52    #[serde(default)]
53    pub backend: Option<String>,
54    #[serde(default)]
55    pub headers: BTreeMap<String, String>,
56    #[serde(default)]
57    pub cookies: Option<String>,
58    #[serde(default)]
59    pub user_agent: Option<String>,
60    #[serde(default)]
61    pub referrer: Option<String>,
62    #[serde(default)]
63    pub tls_ca_file: Option<String>,
64    pub x: f64,
65    pub y: f64,
66    #[serde(default)]
67    pub scroll_x: f64,
68    #[serde(default)]
69    pub scroll_y: f64,
70    pub width: f64,
71    pub height: f64,
72    #[serde(default = "default_true")]
73    pub autoplay: bool,
74    #[serde(default = "default_volume")]
75    pub volume: f64,
76    #[serde(default)]
77    pub muted: bool,
78    #[serde(default)]
79    pub min_buffer_ms: Option<u32>,
80    #[serde(default)]
81    pub max_buffer_ms: Option<u32>,
82    #[serde(default)]
83    pub playback_buffer_ms: Option<u32>,
84    #[serde(default)]
85    pub rebuffer_ms: Option<u32>,
86    #[serde(default)]
87    pub target_buffer_bytes: Option<u64>,
88    #[serde(default)]
89    pub decoder_fallback: Option<bool>,
90    #[serde(default)]
91    pub dolby_vision_mode: Option<String>,
92    #[serde(default)]
93    pub tunneling: Option<bool>,
94}
95
96const fn default_true() -> bool {
97    true
98}
99
100const fn default_volume() -> f64 {
101    1.0
102}
103
104#[derive(Debug, Clone, Deserialize, Serialize)]
105#[serde(rename_all = "camelCase")]
106pub struct NativeLayoutRequest {
107    #[serde(default)]
108    pub session_key: String,
109    pub x: f64,
110    pub y: f64,
111    #[serde(default)]
112    pub scroll_x: f64,
113    #[serde(default)]
114    pub scroll_y: f64,
115    pub width: f64,
116    pub height: f64,
117}
118
119#[derive(Debug, Clone, Deserialize, Serialize)]
120#[serde(rename_all = "camelCase")]
121pub struct NativeControlRequest {
122    #[serde(default)]
123    pub session_key: String,
124    pub action: String,
125    #[serde(default)]
126    pub value: f64,
127    #[serde(default = "default_native_track_index")]
128    pub index: i32,
129}
130
131#[derive(Debug, Clone, Default, Deserialize, Serialize)]
132#[serde(rename_all = "camelCase")]
133pub struct NativeSessionRequest {
134    #[serde(default)]
135    pub session_key: String,
136}
137
138const fn default_native_track_index() -> i32 {
139    -1
140}
141
142#[derive(Debug, Clone, Deserialize, Serialize)]
143#[serde(rename_all = "camelCase")]
144pub struct NativeTrackInfo {
145    pub id: String,
146    pub index: i32,
147    pub kind: TrackKind,
148    pub language: String,
149    pub label: String,
150    pub codec: String,
151    #[serde(default)]
152    pub selected: bool,
153}
154
155#[derive(Debug, Clone, Deserialize, Serialize)]
156#[serde(rename_all = "camelCase")]
157pub struct NativePlaybackSnapshot {
158    pub duration_seconds: f64,
159    pub current_time_seconds: f64,
160    pub buffered_seconds: f64,
161    pub playing: bool,
162    pub video_width: u32,
163    pub video_height: u32,
164    pub tracks: Vec<NativeTrackInfo>,
165    #[serde(default)]
166    pub presented_frames: u64,
167    #[serde(default)]
168    pub dropped_frames: u64,
169    #[serde(default)]
170    pub measured_fps: f64,
171    #[serde(default)]
172    pub hardware_backend: String,
173    #[serde(default)]
174    pub encoded_bytes_buffered: u64,
175    #[serde(default)]
176    pub average_frame_processing_us: f64,
177}