|
1 | | -use librespot_core::dealer::protocol::SkipTo; |
| 1 | +use crate::{ |
| 2 | + core::dealer::protocol::SkipTo, protocol::context_player_options::ContextPlayerOptionOverrides, |
| 3 | +}; |
2 | 4 |
|
| 5 | +use std::ops::Deref; |
| 6 | + |
| 7 | +/// Request for loading playback |
3 | 8 | #[derive(Debug)] |
4 | | -pub struct SpircLoadCommand { |
5 | | - pub context_uri: String, |
| 9 | +pub struct LoadRequest { |
| 10 | + pub(super) context_uri: String, |
| 11 | + pub(super) options: LoadRequestOptions, |
| 12 | +} |
| 13 | + |
| 14 | +impl Deref for LoadRequest { |
| 15 | + type Target = LoadRequestOptions; |
| 16 | + |
| 17 | + fn deref(&self) -> &Self::Target { |
| 18 | + &self.options |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/// The parameters for creating a load request |
| 23 | +#[derive(Debug, Default)] |
| 24 | +pub struct LoadRequestOptions { |
6 | 25 | /// Whether the given tracks should immediately start playing, or just be initially loaded. |
7 | 26 | pub start_playing: bool, |
| 27 | + /// Start the playback at a specific point of the track. |
| 28 | + /// |
| 29 | + /// The provided value is used as milliseconds. Providing a value greater |
| 30 | + /// than the track duration will start the track at the beginning. |
8 | 31 | pub seek_to: u32, |
9 | | - pub shuffle: bool, |
10 | | - pub repeat: bool, |
11 | | - pub repeat_track: bool, |
12 | | - /// Decides if the context or the autoplay of the context is played |
| 32 | + /// Options that decide how the context starts playing |
| 33 | + pub context_options: Option<LoadContextOptions>, |
| 34 | + /// Decides the starting position in the given context. |
13 | 35 | /// |
14 | | - /// ## Remarks: |
15 | | - /// If `true` is provided, the option values (`shuffle`, `repeat` and `repeat_track`) are ignored |
16 | | - pub autoplay: bool, |
17 | | - /// Decides the starting position in the given context |
| 36 | + /// If the provided item doesn't exist or is out of range, |
| 37 | + /// the playback starts at the beginning of the context. |
18 | 38 | /// |
19 | | - /// ## Remarks: |
20 | 39 | /// If `None` is provided and `shuffle` is `true`, a random track is played, otherwise the first |
21 | 40 | pub playing_track: Option<PlayingTrack>, |
22 | 41 | } |
23 | 42 |
|
| 43 | +/// The options which decide how the playback is started |
| 44 | +/// |
| 45 | +/// Separated into an `enum` to exclude the other variants from being used |
| 46 | +/// simultaneously, as they are not compatible. |
| 47 | +#[derive(Debug)] |
| 48 | +pub enum LoadContextOptions { |
| 49 | + /// Starts the context with options |
| 50 | + Options(Options), |
| 51 | + /// Starts the playback as the autoplay variant of the context |
| 52 | + /// |
| 53 | + /// This is the same as finishing a context and |
| 54 | + /// automatically continuing playback of similar tracks |
| 55 | + Autoplay, |
| 56 | +} |
| 57 | + |
| 58 | +/// The available options that indicate how to start the context |
| 59 | +#[derive(Debug, Default)] |
| 60 | +pub struct Options { |
| 61 | + /// Start the context in shuffle mode |
| 62 | + pub shuffle: bool, |
| 63 | + /// Start the context in repeat mode |
| 64 | + pub repeat: bool, |
| 65 | + /// Start the context, repeating the first track until skipped or manually disabled |
| 66 | + pub repeat_track: bool, |
| 67 | +} |
| 68 | + |
| 69 | +impl From<ContextPlayerOptionOverrides> for Options { |
| 70 | + fn from(value: ContextPlayerOptionOverrides) -> Self { |
| 71 | + Self { |
| 72 | + shuffle: value.shuffling_context.unwrap_or_default(), |
| 73 | + repeat: value.repeating_context.unwrap_or_default(), |
| 74 | + repeat_track: value.repeating_track.unwrap_or_default(), |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl LoadRequest { |
| 80 | + /// Create a load request from a `context_uri` |
| 81 | + /// |
| 82 | + /// For supported `context_uri` see [`SpClient::get_context`](librespot_core::spclient::SpClient::get_context) |
| 83 | + pub fn from_context_uri(context_uri: String, options: LoadRequestOptions) -> Self { |
| 84 | + Self { |
| 85 | + context_uri, |
| 86 | + options, |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/// An item that represent a track to play |
24 | 92 | #[derive(Debug)] |
25 | 93 | pub enum PlayingTrack { |
| 94 | + /// Represent the track at a given index. |
26 | 95 | Index(u32), |
| 96 | + /// Represent the uri of a track. |
27 | 97 | Uri(String), |
| 98 | + #[doc(hidden)] |
| 99 | + /// Represent an internal identifier from spotify. |
| 100 | + /// |
| 101 | + /// The internal identifier is not the id contained in the uri. And rather |
| 102 | + /// an unrelated id probably unique in spotify's internal database. But that's |
| 103 | + /// just speculation. |
| 104 | + /// |
| 105 | + /// This identifier is not available by any public api. It's used for varies in |
| 106 | + /// any spotify client, like sorting, displaying which track is currently played |
| 107 | + /// and skipping to a track. Mobile uses it pretty intensively but also web and |
| 108 | + /// desktop seem to make use of it. |
28 | 109 | Uid(String), |
29 | 110 | } |
30 | 111 |
|
|
0 commit comments