heos 0.3.1

Rust bindings for HEOS ecosystem API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Abstraction around anything stateful that can play music.
//!
//! A "playable" is considered to be anything that can play music or otherwise has music "controls",
//! such as volume, next/prev, etc. A [player](super::player) is obviously a playable, but so is a
//! [group](super::group), since a group can play and control music as a group.
//!
//! For example, when a playable is a group, many aspects of the playable - such as the queue and
//! repeat/shuffle modes - are simply retrieved from the group's leader, while some aspects that a
//! group controls - such as the volume - are retrieved from the group itself.

use ahash::HashSet;
use educe::Educe;
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use url::Url;

use crate::command::CommandError;
use crate::data::common::*;
use crate::data::group::*;
use crate::data::player::*;
use crate::data::queue::QueuedTrackInfo;
use crate::data::source::SourceId;
use crate::state::group::{Group, GroupsIter};
use crate::state::player::{NowPlaying, Player, PlayersIter, Queue};
use crate::state::State;

/// An ID enumeration covering all ID types of things that can be "playable".
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum PlayableId {
    /// This playable is a player, and uses a player ID.
    Player(PlayerId),
    /// This playable is a group, and uses a group ID.
    Group(GroupId),
}

// Custom Debug impl so that pretty printing doesn't add unnecessary line breaks
impl Debug for PlayableId {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Player(id) => write!(f, "PlayableId({id:?})"),
            Self::Group(id) => write!(f, "PlayableId({id:?})"),
        }
    }
}

impl Display for PlayableId {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Player(id) => Display::fmt(id, f),
            Self::Group(id) => Display::fmt(id, f),
        }
    }
}

impl From<PlayerId> for PlayableId {
    #[inline]
    fn from(id: PlayerId) -> Self {
        Self::Player(id)
    }
}

impl From<GroupId> for PlayableId {
    #[inline]
    fn from(id: GroupId) -> Self {
        Self::Group(id)
    }
}

/// The information of a playable.
#[derive(Debug, Clone)]
pub enum PlayableInfo {
    /// This playable is a player, and uses player information.
    Player(PlayerInfo),
    /// This playable is a group, and uses group information.
    Group(GroupInfo),
}

/// A snapshot of a playable's state.
///
/// This snapshot allows full perusal of a playable's state without needing to continually await
/// asynchronous locks.
#[derive(Debug, Clone)]
pub struct PlayableSnapshot {
    /// ID enumeration of the playable.
    pub id: PlayableId,
    /// Information enumeration of the playable.
    pub info: PlayableInfo,
    /// State of the currently playing media.
    pub now_playing: NowPlaying,
    /// The queue of tracks to play next.
    pub queue: Vec<QueuedTrackInfo>,
    /// The play state.
    pub play_state: PlayState,
    /// The volume level.
    pub volume: Volume,
    /// The mute state.
    pub mute: MuteState,
    /// The repeat mode.
    pub repeat: RepeatMode,
    /// The shuffle mode.
    pub shuffle: ShuffleMode,
}

/// Live view of a group as a playable.
///
/// This combines live views of the group and the group's leader.
#[derive(Educe)]
#[educe(Deref)]
pub struct PlayableGroup<'a> {
    /// Group live view.
    #[educe(Deref)]
    pub group: Group<'a>,
    /// Leader live view.
    pub leader: Player<'a>,
}

/// Live view into a playable's state.
///
/// This provides methods to asynchronously retrieve the latest stateful data, as well as send
/// command requests relevant to this playable.
///
/// This view owns a read lock on the relevant lists of underlying states. For example, when the
/// playable is a player, it contains a read lock on the list of player states, and when the
/// playable is a group, it contains a read lock on _both_ the list of group states and player
/// states (since it keeps a live view of the group's leader as a player). This means that
/// individual playable state (including this playable) can be updated when relevant events come in,
/// but top-level change events will be delayed until this lock is released.
pub enum Playable<'a> {
    /// This playable is a player.
    Player(Player<'a>),
    /// This playable is a group.
    Group(PlayableGroup<'a>),
}

impl<'a> From<Player<'a>> for Playable<'a> {
    #[inline]
    fn from(player: Player<'a>) -> Self {
        Self::Player(player)
    }
}

macro_rules! delegate_player {
    (
        $(#[$attr:meta])*
        $v:vis fn $fn_name:ident(&self) -> $ret_type:ty;
    ) => {
        $(#[$attr])*
        $v fn $fn_name(&self) -> $ret_type {
            match self {
                Self::Player(player) => player.$fn_name(),
                Self::Group(group) => group.leader.$fn_name(),
            }
        }
    };
    (
        $(#[$attr:meta])*
        $v:vis async fn $fn_name:ident(&self) -> $ret_type:ty;
    ) => {
        $(#[$attr])*
        $v async fn $fn_name(&self) -> $ret_type {
            match self {
                Self::Player(player) => player.$fn_name().await,
                Self::Group(group) => group.leader.$fn_name().await,
            }
        }
    };
    (
        $(#[$attr:meta])*
        $v:vis fn $fn_name:ident(&self, $($param:ident: $param_t:ty),*$(,)?) -> $ret_type:ty;
    ) => {
        $(#[$attr])*
        $v fn $fn_name(&self, $($param: $param_t),*) -> $ret_type {
            match self {
                Self::Player(player) => player.$fn_name($($param),*),
                Self::Group(group) => group.leader.$fn_name($($param),*),
            }
        }
    };
    (
        $(#[$attr:meta])*
        $v:vis async fn $fn_name:ident(&self, $($param:ident: $param_t:ty),*$(,)?) -> $ret_type:ty;
    ) => {
        $(#[$attr])*
        $v async fn $fn_name(&self, $($param: $param_t),*) -> $ret_type {
            match self {
                Self::Player(player) => player.$fn_name($($param),*).await,
                Self::Group(group) => group.leader.$fn_name($($param),*).await,
            }
        }
    };
}

macro_rules! delegate_both {
    (
        $(#[$attr:meta])*
        $v:vis fn $fn_name:ident(&self) -> $ret_type:ty;
    ) => {
        $v fn $fn_name(&self) -> $ret_type {
            match self {
                Self::Player(player) => player.$fn_name(),
                Self::Group(group) => group.group.$fn_name(),
            }
        }
    };
    (
        $(#[$attr:meta])*
        $v:vis async fn $fn_name:ident(&self) -> $ret_type:ty;
    ) => {
        $v async fn $fn_name(&self) -> $ret_type {
            match self {
                Self::Player(player) => player.$fn_name().await,
                Self::Group(group) => group.group.$fn_name().await,
            }
        }
    };
    (
        $(#[$attr:meta])*
        $v:vis fn $fn_name:ident(&self, $($param:ident: $param_t:ty),*$(,)?) -> $ret_type:ty;
    ) => {
        $v fn $fn_name(&self, $($param: $param_t),*) -> $ret_type {
            match self {
                Self::Player(player) => player.$fn_name($($param),*),
                Self::Group(group) => group.group.$fn_name($($param),*),
            }
        }
    };
    (
        $(#[$attr:meta])*
        $v:vis async fn $fn_name:ident(&self, $($param:ident: $param_t:ty),*$(,)?) -> $ret_type:ty;
    ) => {
        $v async fn $fn_name(&self, $($param: $param_t),*) -> $ret_type {
            match self {
                Self::Player(player) => player.$fn_name($($param),*).await,
                Self::Group(group) => group.group.$fn_name($($param),*).await,
            }
        }
    };
}

impl<'a> Playable<'a> {
    /// Create a playable live view from a group and it's leader.
    ///
    /// # Panics
    ///
    /// Panics if the player given as the "leader" does not have the same [PlayerId] as the group's
    /// actual leader.
    #[inline]
    pub fn from_group(group: Group<'a>, leader: Player<'a>) -> Self {
        assert_eq!(leader.info().player_id, group.leader_id());
        Self::Group(PlayableGroup {
            group,
            leader,
        })
    }

    /// Get the ID of this playable.
    pub fn id(&self) -> PlayableId {
        match self {
            Self::Player(player) => player.info().player_id.into(),
            Self::Group(group) => group.info().group_id.into(),
        }
    }

    /// Get general non-mutable information about this playable.
    pub fn info(&self) -> PlayableInfo {
        match self {
            Self::Player(player) => PlayableInfo::Player(player.info().clone()),
            Self::Group(group) => PlayableInfo::Group(group.info().clone()),
        }
    }

    delegate_player! {
        /// Retrieve the state of the currently playing media.
        pub async fn now_playing(&self) -> NowPlaying;
    }

    delegate_player! {
        /// Retrieve a view into the queue of tracks to play next.
        pub fn queue(&self) -> Queue<'_>;
    }

    delegate_player! {
        /// Retrieve the play state of this playable.
        pub async fn play_state(&self) -> PlayState;
    }

    delegate_player! {
        /// Set the play state of this player.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn set_play_state(&self, state: PlayState) -> Result<(), CommandError>;
    }

    delegate_both! {
        /// Retrieve the volume level of this playable.
        pub async fn volume(&self) -> Volume;
    }

    delegate_both! {
        /// Set the volume level of this playable.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn set_volume(&self, level: Volume) -> Result<(), CommandError>;
    }

    delegate_both! {
        /// Increment the volume level of this playable.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn volume_up(&self, step: Option<VolumeStep>) -> Result<(), CommandError>;
    }

    delegate_both! {
        /// Decrement the volume level of this playable.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn volume_down(&self, step: Option<VolumeStep>) -> Result<(), CommandError>;
    }

    delegate_both! {
        /// Retrieve the mute state of this playable.
        pub async fn mute(&self) -> MuteState;
    }

    delegate_both! {
        /// Set the mute state of this playable.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn set_mute(&self, state: MuteState) -> Result<(), CommandError>;
    }

    delegate_both! {
        /// Toggle the mute state of this playable.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn toggle_mute(&self) -> Result<(), CommandError>;
    }

    delegate_player! {
        /// Retrieve the repeat mode of this playable.
        pub async fn repeat(&self) -> RepeatMode;
    }

    delegate_player! {
        /// Retrieve the shuffle mode of this playable.
        pub async fn shuffle(&self) -> ShuffleMode;
    }

    delegate_player! {
        /// Set the play state of this player.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn set_play_mode(
            &self,
            repeat: Option<RepeatMode>,
            shuffle: Option<ShuffleMode>,
        ) -> Result<(), CommandError>;
    }

    delegate_player! {
        /// Play the next track in this playable's queue.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn play_next(&self) -> Result<(), CommandError>;
    }

    delegate_player! {
        /// Play the previous track in this playable's queue.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn play_previous(&self) -> Result<(), CommandError>;
    }

    delegate_player! {
        /// Play a music station on this playable.
        ///
        /// See [PlayStation](crate::command::browse::PlayStation) for details on the parameters.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn play_station(
            &self,
            source_id: SourceId,
            container_id: Option<String>,
            media_id: impl Into<String>,
            name: impl Into<String>,
        ) -> Result<(), CommandError>;
    }

    delegate_player! {
        /// Play a preset on this playable.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn play_preset(&self, preset: usize) -> Result<(), CommandError>;
    }

    delegate_player! {
        /// Play an input source on this playable.
        ///
        /// See [PlayInputSource](crate::command::browse::PlayInputSource) for details on the
        /// parameters.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn play_input_source(
            &self,
            src_player_id: Option<PlayerId>,
            input: impl Into<String>,
        ) -> Result<(), CommandError>;
    }

    delegate_player! {
        /// Play a remote stream URL on this playable.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn play_url(&self, url: impl Into<Url>) -> Result<(), CommandError>;
    }

    delegate_player! {
        /// Add media to this playable's queue.
        ///
        /// See [AddToQueue](crate::command::browse::AddToQueue) for details on the parameters.
        ///
        /// # Errors
        ///
        /// Errors if sending the underlying command errors.
        pub async fn add_to_queue(
            &self,
            source_id: SourceId,
            container_id: Option<String>,
            media_id: Option<String>,
            add_to_queue_type: AddToQueueType,
        ) -> Result<(), CommandError>;
    }

    /// Create a snapshot using the latest stateful data of this playable.
    ///
    /// This method will retrieve all latest stateful data and put it into a snapshot that can then
    /// be perused without further async awaiting.
    pub async fn snapshot(&self) -> PlayableSnapshot {
        PlayableSnapshot {
            id: self.id(),
            info: self.info(),
            now_playing: self.now_playing().await,
            queue: self.queue().data().await.clone(),
            play_state: self.play_state().await,
            volume: self.volume().await,
            mute: self.mute().await,
            repeat: self.repeat().await,
            shuffle: self.shuffle().await,
        }
    }
}

/// Iterator over [playables](Playable).
pub struct PlayablesIter<'a> {
    yielded_player_ids: HashSet<PlayerId>,
    groups: GroupsIter<'a>,
    players: PlayersIter<'a>,
}

impl<'a> PlayablesIter<'a> {
    pub(super) async fn new(state: &'a State) -> Self {
        Self {
            yielded_player_ids: HashSet::default(),
            groups: state.groups().await,
            players: state.players().await,
        }
    }
}

impl<'a> Iterator for PlayablesIter<'a> {
    type Item = Playable<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(group) = self.groups.next() {
            let player = self.players.get(&group.leader_id())?;

            for player in &group.info().players {
                self.yielded_player_ids.insert(player.player_id);
            }

            return Some(Playable::from_group(group, player))
        }

        while let Some(player) = self.players.next() {
            if !self.yielded_player_ids.contains(&player.info().player_id) {
                self.yielded_player_ids.insert(player.info().player_id);
                return Some(player.into())
            }
        }

        None
    }
}