Skip to main content

browser_protocol/media/
mod.rs

1//! This domain allows detailed inspection of media elements.
2
3
4use serde::{Serialize, Deserialize};
5use serde_json::Value as JsonValue;
6use std::borrow::Cow;
7
8/// Players will get an ID that is unique within the agent context.
9
10pub type PlayerId<'a> = Cow<'a, str>;
11
12
13pub type Timestamp = f64;
14
15/// Have one type per entry in MediaLogRecord::Type
16/// Corresponds to kMessage
17
18#[derive(Debug, Clone, Serialize, Deserialize, Default)]
19#[serde(rename_all = "camelCase")]
20pub struct PlayerMessage<'a> {
21    /// Keep in sync with MediaLogMessageLevel
22    /// We are currently keeping the message level 'error' separate from the
23    /// PlayerError type because right now they represent different things,
24    /// this one being a DVLOG(ERROR) style log message that gets printed
25    /// based on what log level is selected in the UI, and the other is a
26    /// representation of a media::PipelineStatus object. Soon however we're
27    /// going to be moving away from using PipelineStatus for errors and
28    /// introducing a new error type which should hopefully let us integrate
29    /// the error log level into the PlayerError type.
30    level: Cow<'a, str>,
31    message: Cow<'a, str>,
32}
33
34impl<'a> PlayerMessage<'a> {
35    /// Creates a builder for this type with the required parameters:
36    /// * `level`: Keep in sync with MediaLogMessageLevel We are currently keeping the message level 'error' separate from the PlayerError type because right now they represent different things, this one being a DVLOG(ERROR) style log message that gets printed based on what log level is selected in the UI, and the other is a representation of a media::PipelineStatus object. Soon however we're going to be moving away from using PipelineStatus for errors and introducing a new error type which should hopefully let us integrate the error log level into the PlayerError type.
37    /// * `message`: 
38    pub fn builder(level: impl Into<Cow<'a, str>>, message: impl Into<Cow<'a, str>>) -> PlayerMessageBuilder<'a> {
39        PlayerMessageBuilder {
40            level: level.into(),
41            message: message.into(),
42        }
43    }
44    /// Keep in sync with MediaLogMessageLevel
45    /// We are currently keeping the message level 'error' separate from the
46    /// PlayerError type because right now they represent different things,
47    /// this one being a DVLOG(ERROR) style log message that gets printed
48    /// based on what log level is selected in the UI, and the other is a
49    /// representation of a media::PipelineStatus object. Soon however we're
50    /// going to be moving away from using PipelineStatus for errors and
51    /// introducing a new error type which should hopefully let us integrate
52    /// the error log level into the PlayerError type.
53    pub fn level(&self) -> &str { self.level.as_ref() }
54    pub fn message(&self) -> &str { self.message.as_ref() }
55}
56
57
58pub struct PlayerMessageBuilder<'a> {
59    level: Cow<'a, str>,
60    message: Cow<'a, str>,
61}
62
63impl<'a> PlayerMessageBuilder<'a> {
64    pub fn build(self) -> PlayerMessage<'a> {
65        PlayerMessage {
66            level: self.level,
67            message: self.message,
68        }
69    }
70}
71
72/// Corresponds to kMediaPropertyChange
73
74#[derive(Debug, Clone, Serialize, Deserialize, Default)]
75#[serde(rename_all = "camelCase")]
76pub struct PlayerProperty<'a> {
77    name: Cow<'a, str>,
78    value: Cow<'a, str>,
79}
80
81impl<'a> PlayerProperty<'a> {
82    /// Creates a builder for this type with the required parameters:
83    /// * `name`: 
84    /// * `value`: 
85    pub fn builder(name: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>) -> PlayerPropertyBuilder<'a> {
86        PlayerPropertyBuilder {
87            name: name.into(),
88            value: value.into(),
89        }
90    }
91    pub fn name(&self) -> &str { self.name.as_ref() }
92    pub fn value(&self) -> &str { self.value.as_ref() }
93}
94
95
96pub struct PlayerPropertyBuilder<'a> {
97    name: Cow<'a, str>,
98    value: Cow<'a, str>,
99}
100
101impl<'a> PlayerPropertyBuilder<'a> {
102    pub fn build(self) -> PlayerProperty<'a> {
103        PlayerProperty {
104            name: self.name,
105            value: self.value,
106        }
107    }
108}
109
110/// Corresponds to kMediaEventTriggered
111
112#[derive(Debug, Clone, Serialize, Deserialize, Default)]
113#[serde(rename_all = "camelCase")]
114pub struct PlayerEvent<'a> {
115    timestamp: Timestamp,
116    value: Cow<'a, str>,
117}
118
119impl<'a> PlayerEvent<'a> {
120    /// Creates a builder for this type with the required parameters:
121    /// * `timestamp`: 
122    /// * `value`: 
123    pub fn builder(timestamp: Timestamp, value: impl Into<Cow<'a, str>>) -> PlayerEventBuilder<'a> {
124        PlayerEventBuilder {
125            timestamp: timestamp,
126            value: value.into(),
127        }
128    }
129    pub fn timestamp(&self) -> &Timestamp { &self.timestamp }
130    pub fn value(&self) -> &str { self.value.as_ref() }
131}
132
133
134pub struct PlayerEventBuilder<'a> {
135    timestamp: Timestamp,
136    value: Cow<'a, str>,
137}
138
139impl<'a> PlayerEventBuilder<'a> {
140    pub fn build(self) -> PlayerEvent<'a> {
141        PlayerEvent {
142            timestamp: self.timestamp,
143            value: self.value,
144        }
145    }
146}
147
148/// Represents logged source line numbers reported in an error.
149/// NOTE: file and line are from chromium c++ implementation code, not js.
150
151#[derive(Debug, Clone, Serialize, Deserialize, Default)]
152#[serde(rename_all = "camelCase")]
153pub struct PlayerErrorSourceLocation<'a> {
154    file: Cow<'a, str>,
155    line: i64,
156}
157
158impl<'a> PlayerErrorSourceLocation<'a> {
159    /// Creates a builder for this type with the required parameters:
160    /// * `file`: 
161    /// * `line`: 
162    pub fn builder(file: impl Into<Cow<'a, str>>, line: i64) -> PlayerErrorSourceLocationBuilder<'a> {
163        PlayerErrorSourceLocationBuilder {
164            file: file.into(),
165            line: line,
166        }
167    }
168    pub fn file(&self) -> &str { self.file.as_ref() }
169    pub fn line(&self) -> i64 { self.line }
170}
171
172
173pub struct PlayerErrorSourceLocationBuilder<'a> {
174    file: Cow<'a, str>,
175    line: i64,
176}
177
178impl<'a> PlayerErrorSourceLocationBuilder<'a> {
179    pub fn build(self) -> PlayerErrorSourceLocation<'a> {
180        PlayerErrorSourceLocation {
181            file: self.file,
182            line: self.line,
183        }
184    }
185}
186
187/// Corresponds to kMediaError
188
189#[derive(Debug, Clone, Serialize, Deserialize, Default)]
190#[serde(rename_all = "camelCase")]
191pub struct PlayerError<'a> {
192    #[serde(rename = "errorType")]
193    error_type: Cow<'a, str>,
194    /// Code is the numeric enum entry for a specific set of error codes, such
195    /// as PipelineStatusCodes in media/base/pipeline_status.h
196    code: i64,
197    /// A trace of where this error was caused / where it passed through.
198    stack: Vec<PlayerErrorSourceLocation<'a>>,
199    /// Errors potentially have a root cause error, ie, a DecoderError might be
200    /// caused by an WindowsError
201    cause: Vec<Box<PlayerError<'a>>>,
202    /// Extra data attached to an error, such as an HRESULT, Video Codec, etc.
203    data: serde_json::Map<String, JsonValue>,
204}
205
206impl<'a> PlayerError<'a> {
207    /// Creates a builder for this type with the required parameters:
208    /// * `error_type`: 
209    /// * `code`: Code is the numeric enum entry for a specific set of error codes, such as PipelineStatusCodes in media/base/pipeline_status.h
210    /// * `stack`: A trace of where this error was caused / where it passed through.
211    /// * `cause`: Errors potentially have a root cause error, ie, a DecoderError might be caused by an WindowsError
212    /// * `data`: Extra data attached to an error, such as an HRESULT, Video Codec, etc.
213    pub fn builder(error_type: impl Into<Cow<'a, str>>, code: i64, stack: Vec<PlayerErrorSourceLocation<'a>>, cause: Vec<Box<PlayerError<'a>>>, data: serde_json::Map<String, JsonValue>) -> PlayerErrorBuilder<'a> {
214        PlayerErrorBuilder {
215            error_type: error_type.into(),
216            code: code,
217            stack: stack,
218            cause: cause,
219            data: data,
220        }
221    }
222    pub fn error_type(&self) -> &str { self.error_type.as_ref() }
223    /// Code is the numeric enum entry for a specific set of error codes, such
224    /// as PipelineStatusCodes in media/base/pipeline_status.h
225    pub fn code(&self) -> i64 { self.code }
226    /// A trace of where this error was caused / where it passed through.
227    pub fn stack(&self) -> &[PlayerErrorSourceLocation<'a>] { &self.stack }
228    /// Errors potentially have a root cause error, ie, a DecoderError might be
229    /// caused by an WindowsError
230    pub fn cause(&self) -> &[Box<PlayerError<'a>>] { &self.cause }
231    /// Extra data attached to an error, such as an HRESULT, Video Codec, etc.
232    pub fn data(&self) -> &serde_json::Map<String, JsonValue> { &self.data }
233}
234
235
236pub struct PlayerErrorBuilder<'a> {
237    error_type: Cow<'a, str>,
238    code: i64,
239    stack: Vec<PlayerErrorSourceLocation<'a>>,
240    cause: Vec<Box<PlayerError<'a>>>,
241    data: serde_json::Map<String, JsonValue>,
242}
243
244impl<'a> PlayerErrorBuilder<'a> {
245    pub fn build(self) -> PlayerError<'a> {
246        PlayerError {
247            error_type: self.error_type,
248            code: self.code,
249            stack: self.stack,
250            cause: self.cause,
251            data: self.data,
252        }
253    }
254}
255
256
257#[derive(Debug, Clone, Serialize, Deserialize, Default)]
258#[serde(rename_all = "camelCase")]
259pub struct Player<'a> {
260    #[serde(rename = "playerId")]
261    player_id: PlayerId<'a>,
262    #[serde(skip_serializing_if = "Option::is_none", rename = "domNodeId")]
263    dom_node_id: Option<crate::dom::BackendNodeId>,
264}
265
266impl<'a> Player<'a> {
267    /// Creates a builder for this type with the required parameters:
268    /// * `player_id`: 
269    pub fn builder(player_id: impl Into<PlayerId<'a>>) -> PlayerBuilder<'a> {
270        PlayerBuilder {
271            player_id: player_id.into(),
272            dom_node_id: None,
273        }
274    }
275    pub fn player_id(&self) -> &PlayerId<'a> { &self.player_id }
276    pub fn dom_node_id(&self) -> Option<&crate::dom::BackendNodeId> { self.dom_node_id.as_ref() }
277}
278
279
280pub struct PlayerBuilder<'a> {
281    player_id: PlayerId<'a>,
282    dom_node_id: Option<crate::dom::BackendNodeId>,
283}
284
285impl<'a> PlayerBuilder<'a> {
286    pub fn dom_node_id(mut self, dom_node_id: crate::dom::BackendNodeId) -> Self { self.dom_node_id = Some(dom_node_id); self }
287    pub fn build(self) -> Player<'a> {
288        Player {
289            player_id: self.player_id,
290            dom_node_id: self.dom_node_id,
291        }
292    }
293}
294
295#[derive(Debug, Clone, Serialize, Deserialize, Default)]
296pub struct EnableParams {}
297
298impl EnableParams { pub const METHOD: &'static str = "Media.enable"; }
299
300impl<'a> crate::CdpCommand<'a> for EnableParams {
301    const METHOD: &'static str = "Media.enable";
302    type Response = crate::EmptyReturns;
303}
304
305#[derive(Debug, Clone, Serialize, Deserialize, Default)]
306pub struct DisableParams {}
307
308impl DisableParams { pub const METHOD: &'static str = "Media.disable"; }
309
310impl<'a> crate::CdpCommand<'a> for DisableParams {
311    const METHOD: &'static str = "Media.disable";
312    type Response = crate::EmptyReturns;
313}