mpvipc_async/
highlevel_api_extension.rs

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
//! High-level API extension for [`Mpv`].

use crate::{
    parse_property, IntoRawCommandPart, LoopProperty, Mpv, MpvCommand, MpvDataType, MpvError,
    Playlist, PlaylistAddOptions, Property, SeekOptions,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Generic high-level command for changing a number property.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum NumberChangeOptions {
    Absolute,
    Increase,
    Decrease,
}

impl IntoRawCommandPart for NumberChangeOptions {
    fn into_raw_command_part(self) -> String {
        match self {
            NumberChangeOptions::Absolute => "absolute".to_string(),
            NumberChangeOptions::Increase => "increase".to_string(),
            NumberChangeOptions::Decrease => "decrease".to_string(),
        }
    }
}

/// Generic high-level switch for toggling boolean properties.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum Switch {
    On,
    Off,
    Toggle,
}

/// Options for [`MpvExt::playlist_add`].
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum PlaylistAddTypeOptions {
    File,
    Playlist,
}

/// A set of typesafe high-level functions to interact with [`Mpv`].
// TODO: fix this
#[allow(async_fn_in_trait)]
pub trait MpvExt {
    // COMMANDS

    /// Seek to a specific position in the current video.
    async fn seek(&self, seconds: f64, option: SeekOptions) -> Result<(), MpvError>;

    /// Shuffle the current playlist.
    async fn playlist_shuffle(&self) -> Result<(), MpvError>;

    /// Remove an entry from the playlist.
    async fn playlist_remove_id(&self, id: usize) -> Result<(), MpvError>;

    /// Play the next entry in the playlist.
    async fn playlist_play_next(&self, id: usize) -> Result<(), MpvError>;

    /// Play a specific entry in the playlist.
    async fn playlist_play_id(&self, id: usize) -> Result<(), MpvError>;

    /// Move an entry in the playlist.
    ///
    /// The `from` parameter is the current position of the entry, and the `to` parameter is the new position.
    /// Mpv will then move the entry from the `from` position to the `to` position,
    /// shifting after `to` one number up. Paradoxically, that means that moving an entry further down the list
    /// will result in a final position that is one less than the `to` parameter.
    async fn playlist_move_id(&self, from: usize, to: usize) -> Result<(), MpvError>;

    /// Remove all entries from the playlist.
    async fn playlist_clear(&self) -> Result<(), MpvError>;

    /// Add a file or playlist to the playlist.
    async fn playlist_add(
        &self,
        file: &str,
        file_type: PlaylistAddTypeOptions,
        option: PlaylistAddOptions,
    ) -> Result<(), MpvError>;

    /// Start the current video from the beginning.
    async fn restart(&self) -> Result<(), MpvError>;

    /// Play the previous entry in the playlist.
    async fn prev(&self) -> Result<(), MpvError>;

    /// Notify mpv to send events whenever a property changes.
    /// See [`Mpv::get_event_stream`] and [`Property`](crate::Property) for more information.
    async fn observe_property(&self, id: usize, property: &str) -> Result<(), MpvError>;

    /// Stop observing a property.
    /// See [`Mpv::get_event_stream`] and [`Property`](crate::Property) for more information.
    async fn unobserve_property(&self, id: usize) -> Result<(), MpvError>;

    /// Skip to the next entry in the playlist.
    async fn next(&self) -> Result<(), MpvError>;

    /// Stop mpv completely, and kill the process.
    ///
    /// Note that this is different than forcefully killing the process using
    /// as handle to a subprocess, it will only send a command to mpv to ask
    /// it to exit itself. If mpv is stuck, it may not respond to this command.
    async fn kill(&self) -> Result<(), MpvError>;

    /// Stop the player completely (as opposed to pausing),
    /// removing the pointer to the current video.
    async fn stop(&self) -> Result<(), MpvError>;

    // SETTERS

    /// Set the volume of the player.
    async fn set_volume(
        &self,
        input_volume: f64,
        option: NumberChangeOptions,
    ) -> Result<(), MpvError>;

    /// Set the playback speed of the player.
    async fn set_speed(
        &self,
        input_speed: f64,
        option: NumberChangeOptions,
    ) -> Result<(), MpvError>;

    /// Toggle/set the pause state of the player.
    async fn set_playback(&self, option: Switch) -> Result<(), MpvError>;

    /// Toggle/set the mute state of the player.
    async fn set_mute(&self, option: Switch) -> Result<(), MpvError>;

    /// Toggle/set whether the player should loop the current playlist.
    async fn set_loop_playlist(&self, option: Switch) -> Result<(), MpvError>;

    /// Toggle/set whether the player should loop the current video.
    async fn set_loop_file(&self, option: Switch) -> Result<(), MpvError>;

    // GETTERS

    /// Get a list of all entries in the playlist.
    async fn get_playlist(&self) -> Result<Playlist, MpvError>;

    /// Get metadata about the current video.
    async fn get_metadata(&self) -> Result<HashMap<String, MpvDataType>, MpvError>;

    /// Get the path of the current video.
    async fn get_file_path(&self) -> Result<String, MpvError>;

    /// Get the current volume of the player.
    async fn get_volume(&self) -> Result<f64, MpvError>;

    /// Get the playback speed of the player.
    async fn get_speed(&self) -> Result<f64, MpvError>;

    /// Get the current position in the current video.
    async fn get_time_pos(&self) -> Result<Option<f64>, MpvError>;

    /// Get the amount of time remaining in the current video.
    async fn get_time_remaining(&self) -> Result<Option<f64>, MpvError>;

    /// Get the total duration of the current video.
    async fn get_duration(&self) -> Result<f64, MpvError>;

    /// Get the current position in the playlist.
    async fn get_playlist_pos(&self) -> Result<usize, MpvError>;

    // BOOLEAN GETTERS

    /// Check whether the player is muted.
    async fn is_muted(&self) -> Result<bool, MpvError>;

    /// Check whether the player is currently playing.
    async fn is_playing(&self) -> Result<bool, MpvError>;

    /// Check whether the player is looping the current playlist.
    async fn playlist_is_looping(&self) -> Result<LoopProperty, MpvError>;

    /// Check whether the player is looping the current video.
    async fn file_is_looping(&self) -> Result<LoopProperty, MpvError>;
}

impl MpvExt for Mpv {
    // COMMANDS

    async fn seek(&self, seconds: f64, option: SeekOptions) -> Result<(), MpvError> {
        self.run_command(MpvCommand::Seek { seconds, option }).await
    }

    async fn playlist_shuffle(&self) -> Result<(), MpvError> {
        self.run_command(MpvCommand::PlaylistShuffle).await
    }

    async fn playlist_remove_id(&self, id: usize) -> Result<(), MpvError> {
        self.run_command(MpvCommand::PlaylistRemove(id)).await
    }

    async fn playlist_play_next(&self, id: usize) -> Result<(), MpvError> {
        let data = self.get_property("playlist-pos").await?;
        let current_id = match parse_property("playlist-pos", data)? {
            Property::PlaylistPos(Some(current_id)) => Ok(current_id),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }?;

        self.run_command(MpvCommand::PlaylistMove {
            from: id,
            to: current_id + 1,
        })
        .await
    }

    async fn playlist_play_id(&self, id: usize) -> Result<(), MpvError> {
        self.set_property("playlist-pos", id).await
    }

    async fn playlist_move_id(&self, from: usize, to: usize) -> Result<(), MpvError> {
        self.run_command(MpvCommand::PlaylistMove { from, to })
            .await
    }

    async fn playlist_clear(&self) -> Result<(), MpvError> {
        self.run_command(MpvCommand::PlaylistClear).await
    }

    async fn playlist_add(
        &self,
        file: &str,
        file_type: PlaylistAddTypeOptions,
        option: PlaylistAddOptions,
    ) -> Result<(), MpvError> {
        match file_type {
            PlaylistAddTypeOptions::File => {
                self.run_command(MpvCommand::LoadFile {
                    file: file.to_string(),
                    option,
                })
                .await
            }

            PlaylistAddTypeOptions::Playlist => {
                self.run_command(MpvCommand::LoadList {
                    file: file.to_string(),
                    option,
                })
                .await
            }
        }
    }

    async fn restart(&self) -> Result<(), MpvError> {
        self.run_command(MpvCommand::Seek {
            seconds: 0f64,
            option: SeekOptions::Absolute,
        })
        .await
    }

    async fn prev(&self) -> Result<(), MpvError> {
        self.run_command(MpvCommand::PlaylistPrev).await
    }

    async fn observe_property(&self, id: usize, property: &str) -> Result<(), MpvError> {
        self.run_command(MpvCommand::Observe {
            id,
            property: property.to_string(),
        })
        .await
    }

    async fn unobserve_property(&self, id: usize) -> Result<(), MpvError> {
        self.run_command(MpvCommand::Unobserve(id)).await
    }

    async fn next(&self) -> Result<(), MpvError> {
        self.run_command(MpvCommand::PlaylistNext).await
    }

    async fn kill(&self) -> Result<(), MpvError> {
        self.run_command(MpvCommand::Quit).await
    }

    async fn stop(&self) -> Result<(), MpvError> {
        self.run_command(MpvCommand::Stop).await
    }

    // SETTERS

    async fn set_volume(
        &self,
        input_volume: f64,
        option: NumberChangeOptions,
    ) -> Result<(), MpvError> {
        let volume = self.get_volume().await?;

        match option {
            NumberChangeOptions::Increase => {
                self.set_property("volume", volume + input_volume).await
            }
            NumberChangeOptions::Decrease => {
                self.set_property("volume", volume - input_volume).await
            }
            NumberChangeOptions::Absolute => self.set_property("volume", input_volume).await,
        }
    }

    async fn set_speed(
        &self,
        input_speed: f64,
        option: NumberChangeOptions,
    ) -> Result<(), MpvError> {
        let speed = self.get_speed().await?;

        match option {
            NumberChangeOptions::Increase => self.set_property("speed", speed + input_speed).await,
            NumberChangeOptions::Decrease => self.set_property("speed", speed - input_speed).await,
            NumberChangeOptions::Absolute => self.set_property("speed", input_speed).await,
        }
    }

    async fn set_playback(&self, option: Switch) -> Result<(), MpvError> {
        let enabled = match option {
            Switch::On => "no",
            Switch::Off => "yes",
            Switch::Toggle => {
                if self.is_playing().await? {
                    "no"
                } else {
                    "yes"
                }
            }
        };
        self.set_property("pause", enabled).await
    }

    async fn set_mute(&self, option: Switch) -> Result<(), MpvError> {
        let enabled = match option {
            Switch::On => "yes",
            Switch::Off => "no",
            Switch::Toggle => {
                if self.is_muted().await? {
                    "no"
                } else {
                    "yes"
                }
            }
        };
        self.set_property("mute", enabled).await
    }

    async fn set_loop_playlist(&self, option: Switch) -> Result<(), MpvError> {
        let enabled = match option {
            Switch::On => "inf",
            Switch::Off => "no",
            Switch::Toggle => match self.playlist_is_looping().await? {
                LoopProperty::Inf => "no",
                LoopProperty::N(_) => "no",
                LoopProperty::No => "inf",
            },
        };
        self.set_property("loop-playlist", enabled).await
    }

    async fn set_loop_file(&self, option: Switch) -> Result<(), MpvError> {
        let enabled = match option {
            Switch::On => "inf",
            Switch::Off => "no",
            Switch::Toggle => match self.file_is_looping().await? {
                LoopProperty::Inf => "no",
                LoopProperty::N(_) => "no",
                LoopProperty::No => "inf",
            },
        };
        self.set_property("loop-file", enabled).await
    }

    // GETTERS

    async fn get_playlist(&self) -> Result<Playlist, MpvError> {
        let data = self.get_property("playlist").await?;
        match parse_property("playlist", data)? {
            Property::Playlist(value) => Ok(Playlist(value)),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    async fn get_metadata(&self) -> Result<HashMap<String, MpvDataType>, MpvError> {
        let data = self.get_property("metadata").await?;
        match parse_property("metadata", data)? {
            Property::Metadata(Some(value)) => Ok(value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    async fn get_file_path(&self) -> Result<String, MpvError> {
        let data = self.get_property("path").await?;
        match parse_property("path", data)? {
            Property::Path(Some(value)) => Ok(value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    async fn get_volume(&self) -> Result<f64, MpvError> {
        let data = self.get_property("volume").await?;
        match parse_property("volume", data)? {
            Property::Volume(value) => Ok(value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    async fn get_speed(&self) -> Result<f64, MpvError> {
        let data = self.get_property("speed").await?;
        match parse_property("speed", data)? {
            Property::Speed(value) => Ok(value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    async fn get_time_pos(&self) -> Result<Option<f64>, MpvError> {
        let data = self.get_property("time-pos").await?;
        match parse_property("time-pos", data)? {
            Property::TimePos(value) => Ok(value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    async fn get_time_remaining(&self) -> Result<Option<f64>, MpvError> {
        let data = self.get_property("time-remaining").await?;
        match parse_property("time-remaining", data)? {
            Property::TimeRemaining(value) => Ok(value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    async fn get_duration(&self) -> Result<f64, MpvError> {
        let data = self.get_property("duration").await?;
        match parse_property("duration", data)? {
            Property::Duration(Some(value)) => Ok(value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    async fn get_playlist_pos(&self) -> Result<usize, MpvError> {
        let data = self.get_property("playlist-pos").await?;
        match parse_property("playlist-pos", data)? {
            Property::PlaylistPos(Some(value)) => Ok(value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    // BOOLEAN GETTERS

    async fn is_muted(&self) -> Result<bool, MpvError> {
        let data = self.get_property("mute").await?;
        match parse_property("mute", data)? {
            Property::Mute(value) => Ok(value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    async fn is_playing(&self) -> Result<bool, MpvError> {
        let data = self.get_property("pause").await?;
        match parse_property("pause", data)? {
            Property::Pause(value) => Ok(!value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    async fn playlist_is_looping(&self) -> Result<LoopProperty, MpvError> {
        let data = self.get_property("loop-playlist").await?;
        match parse_property("loop-playlist", data)? {
            Property::LoopPlaylist(value) => Ok(value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }

    async fn file_is_looping(&self) -> Result<LoopProperty, MpvError> {
        let data = self.get_property("loop-file").await?;
        match parse_property("loop-file", data)? {
            Property::LoopFile(value) => Ok(value),
            prop => Err(MpvError::UnexpectedProperty(prop)),
        }
    }
}