servo_media_player/
lib.rs

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