Skip to main content

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