servo_media_player/
lib.rs

1use std::ops::Range;
2use std::time::Duration;
3
4pub extern crate ipc_channel;
5#[macro_use]
6extern crate serde_derive;
7extern crate servo_media_streams as streams;
8extern crate servo_media_traits;
9
10pub mod audio;
11pub mod context;
12pub mod metadata;
13pub mod video;
14
15use ipc_channel::ipc::{self, IpcSender};
16use servo_media_traits::MediaInstance;
17use streams::registry::MediaStreamId;
18
19#[derive(Clone, Debug, Deserialize, Serialize)]
20pub enum PlaybackState {
21    Stopped,
22    Buffering,
23    Paused,
24    Playing,
25}
26
27#[derive(Debug, PartialEq)]
28pub enum PlayerError {
29    /// Backend specific error.
30    Backend(String),
31    /// Could not push buffer contents to the player.
32    BufferPushFailed,
33    /// The player cannot consume more data.
34    EnoughData,
35    /// Setting End Of Stream failed.
36    EOSFailed,
37    /// The media stream is not seekable.
38    NonSeekableStream,
39    /// Tried to seek out of range.
40    SeekOutOfRange,
41    /// Setting an audio or video stream failed.
42    /// Possibly because the type of source is not PlayerSource::Stream.
43    SetStreamFailed,
44    // Setting an audio or video track failed.
45    SetTrackFailed,
46}
47
48pub type SeekLockMsg = (bool, IpcSender<()>);
49
50#[derive(Clone, Debug, Deserialize, Serialize)]
51pub struct SeekLock {
52    pub lock_channel: IpcSender<SeekLockMsg>,
53}
54
55impl SeekLock {
56    pub fn unlock(&self, result: bool) {
57        let (ack_sender, ack_recv) = ipc::channel::<()>().expect("Could not create IPC channel");
58        self.lock_channel.send((result, ack_sender)).unwrap();
59        ack_recv.recv().unwrap()
60    }
61}
62
63#[derive(Clone, Debug, Deserialize, Serialize)]
64pub enum PlayerEvent {
65    EndOfStream,
66    /// The player has enough data. The client should stop pushing data into.
67    EnoughData,
68    Error(String),
69    VideoFrameUpdated,
70    MetadataUpdated(metadata::Metadata),
71    // The `None` value means the duration is unknown, in which case this is likely a live stream.
72    DurationChanged(Option<Duration>),
73    /// The internal player queue is running out of data. The client should start
74    /// pushing more data.
75    NeedData,
76    PositionChanged(f64),
77    /// The player needs the data to perform a seek to the given offset in bytes.
78    /// The next push_data should get the buffers from the new offset.
79    /// The player will be blocked until the user unlocks it through
80    /// the given SeekLock instance.
81    /// This event is only received for seekable stream types.
82    SeekData(u64, SeekLock),
83    /// The player has performed a seek to the given time offset in seconds.
84    SeekDone(f64),
85    StateChanged(PlaybackState),
86}
87
88#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
89pub enum StreamType {
90    /// No seeking is supported in the stream, such as a live stream.
91    Stream,
92    /// The stream is seekable.
93    Seekable,
94}
95
96pub trait Player: Send + MediaInstance {
97    fn play(&self) -> Result<(), PlayerError>;
98    fn pause(&self) -> Result<(), PlayerError>;
99    fn stop(&self) -> Result<(), PlayerError>;
100    fn seek(&self, time: f64) -> Result<(), PlayerError>;
101    fn seekable(&self) -> Result<Vec<Range<f64>>, PlayerError>;
102    fn set_mute(&self, val: bool) -> Result<(), PlayerError>;
103    fn set_volume(&self, value: f64) -> Result<(), PlayerError>;
104    fn set_input_size(&self, size: u64) -> Result<(), PlayerError>;
105    fn set_rate(&self, rate: f64) -> Result<(), PlayerError>;
106    fn push_data(&self, data: Vec<u8>) -> Result<(), PlayerError>;
107    fn end_of_stream(&self) -> Result<(), PlayerError>;
108    /// Get the list of time ranges in seconds that have been buffered.
109    fn buffered(&self) -> Result<Vec<Range<f64>>, PlayerError>;
110    /// Set the stream to be played by the player.
111    /// Only a single stream of the same type (audio or video) can be set.
112    /// Subsequent calls with a stream of the same type will override the previously
113    /// set stream.
114    /// This method requires the player to be constructed with StreamType::Stream.
115    /// It is important to give the correct value of `only_stream` indicating
116    /// that the audio or video stream being set is the only one expected.
117    /// Subsequent calls to `set_stream` after the `only_stream` flag has been
118    /// set to true will fail.
119    fn set_stream(&self, stream: &MediaStreamId, only_stream: bool) -> Result<(), PlayerError>;
120    /// If player's rendering draws using GL textures
121    fn render_use_gl(&self) -> bool;
122    fn set_audio_track(&self, stream_index: i32, enabled: bool) -> Result<(), PlayerError>;
123    fn set_video_track(&self, stream_index: i32, enabled: bool) -> Result<(), PlayerError>;
124}