servo_media_player/
lib.rs

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