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