2025-12-06 13:37:42 +01:00
|
|
|
//! Internal (local) queue implementation for PMOControl.
|
|
|
|
|
//!
|
|
|
|
|
//! This module provides a concrete implementation of the generic
|
|
|
|
|
//! `QueueBackend` trait for queues that are fully managed inside the
|
|
|
|
|
//! ControlPoint, without delegating playlist management to a remote
|
|
|
|
|
//! backend (like OpenHome).
|
|
|
|
|
//!
|
|
|
|
|
//! In this design, each queue instance is associated to exactly one
|
|
|
|
|
//! renderer. The queue does not need to know the renderer identifier:
|
|
|
|
|
//! it is "bound" to the renderer by construction, and will be stored
|
|
|
|
|
//! directly in the runtime (inside a higher-level `MusicQueue` enum).
|
|
|
|
|
//!
|
|
|
|
|
//! This internal queue:
|
|
|
|
|
//! - owns its list of `PlaybackItem`s,
|
|
|
|
|
//! - maintains a `current_index`,
|
|
|
|
|
//! - never starts playback (transport control is handled elsewhere).
|
|
|
|
|
|
2025-12-28 17:45:06 +01:00
|
|
|
use crate::{
|
2025-12-30 16:50:26 +01:00
|
|
|
DeviceId, DeviceIdentity, RendererInfo,
|
2025-12-28 17:45:06 +01:00
|
|
|
errors::ControlPointError,
|
2025-12-30 16:50:26 +01:00
|
|
|
queue::{MusicQueue, PlaybackItem, QueueBackend, QueueFromRendererInfo, QueueSnapshot},
|
2025-12-28 17:45:06 +01:00
|
|
|
};
|
2025-12-06 13:37:42 +01:00
|
|
|
|
|
|
|
|
/// Internal/local queue implementation.
|
|
|
|
|
///
|
|
|
|
|
/// This is the simplest possible queue backend:
|
|
|
|
|
/// - a `Vec<PlaybackItem>`
|
|
|
|
|
/// - plus an optional `current_index`.
|
|
|
|
|
///
|
|
|
|
|
/// It does not talk to any remote service. All operations are pure
|
|
|
|
|
/// structural mutations on in-memory data.
|
2025-12-28 17:45:06 +01:00
|
|
|
#[derive(Clone, Debug)]
|
2025-12-06 13:37:42 +01:00
|
|
|
pub struct InternalQueue {
|
2025-12-28 17:45:06 +01:00
|
|
|
renderer_id: DeviceId,
|
2025-12-06 13:37:42 +01:00
|
|
|
items: Vec<PlaybackItem>,
|
|
|
|
|
current_index: Option<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl InternalQueue {
|
|
|
|
|
/// Creates an empty internal queue.
|
2025-12-28 17:45:06 +01:00
|
|
|
pub fn new(renderer_id: DeviceId) -> Self {
|
2025-12-06 13:37:42 +01:00
|
|
|
Self {
|
2025-12-28 17:45:06 +01:00
|
|
|
renderer_id,
|
2025-12-06 13:37:42 +01:00
|
|
|
items: Vec::new(),
|
|
|
|
|
current_index: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 16:50:26 +01:00
|
|
|
pub fn from_renderer_info(info: &RendererInfo) -> Result<InternalQueue, ControlPointError> {
|
|
|
|
|
Ok(InternalQueue::new(info.id()))
|
2025-12-06 13:37:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Exposes a read-only view of the underlying items.
|
|
|
|
|
pub fn items(&self) -> &[PlaybackItem] {
|
|
|
|
|
&self.items
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl QueueBackend for InternalQueue {
|
2025-12-30 16:50:26 +01:00
|
|
|
fn len(&self) -> Result<usize, ControlPointError> {
|
|
|
|
|
Ok(self.items.len())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn track_ids(&self) -> Result<Vec<u32>, ControlPointError> {
|
|
|
|
|
let ids: Vec<u32> = (0..self.len()?).map(|i| i as u32).collect();
|
|
|
|
|
Ok(ids)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn id_to_position(&self, id: u32) -> Result<usize, ControlPointError> {
|
|
|
|
|
Ok(id as usize)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn position_to_id(&self, id: usize) -> Result<u32, ControlPointError> {
|
2026-01-09 14:30:53 +01:00
|
|
|
u32::try_from(id)
|
|
|
|
|
.map_err(|_| ControlPointError::QueueError(format!("Position {} exceeds u32::MAX", id)))
|
2025-12-30 16:50:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn current_track(&self) -> Result<Option<u32>, ControlPointError> {
|
|
|
|
|
match self.current_index {
|
|
|
|
|
None => Ok(None),
|
2026-01-09 14:30:53 +01:00
|
|
|
Some(i) => u32::try_from(i).map(Some).map_err(|_| {
|
|
|
|
|
ControlPointError::QueueError(format!("Current index {} exceeds u32::MAX", i))
|
|
|
|
|
}),
|
2025-12-30 16:50:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn current_index(&self) -> Result<Option<usize>, ControlPointError> {
|
|
|
|
|
Ok(self.current_index)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 17:45:06 +01:00
|
|
|
fn queue_snapshot(&self) -> Result<QueueSnapshot, ControlPointError> {
|
|
|
|
|
let mut items = self.items.clone();
|
|
|
|
|
for (i, item) in items.iter_mut().enumerate() {
|
|
|
|
|
item.backend_id = i;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-06 13:37:42 +01:00
|
|
|
Ok(QueueSnapshot {
|
2025-12-28 17:45:06 +01:00
|
|
|
items,
|
2025-12-06 13:37:42 +01:00
|
|
|
current_index: self.current_index,
|
2026-01-09 21:35:00 +01:00
|
|
|
playlist_id: None,
|
2025-12-06 13:37:42 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 17:45:06 +01:00
|
|
|
fn set_index(&mut self, index: Option<usize>) -> Result<(), ControlPointError> {
|
2025-12-06 13:37:42 +01:00
|
|
|
match index {
|
|
|
|
|
None => {
|
|
|
|
|
self.current_index = None;
|
|
|
|
|
}
|
|
|
|
|
Some(i) => {
|
|
|
|
|
if i < self.items.len() {
|
|
|
|
|
self.current_index = Some(i);
|
|
|
|
|
} else {
|
2025-12-30 16:50:26 +01:00
|
|
|
return Err(ControlPointError::QueueError(format!(
|
|
|
|
|
"Index out of bound {} >= {}",
|
|
|
|
|
i,
|
|
|
|
|
self.items.len()
|
|
|
|
|
)));
|
2025-12-06 13:37:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn replace_queue(
|
|
|
|
|
&mut self,
|
|
|
|
|
items: Vec<PlaybackItem>,
|
|
|
|
|
current_index: Option<usize>,
|
2025-12-28 17:45:06 +01:00
|
|
|
) -> Result<(), ControlPointError> {
|
2025-12-06 13:37:42 +01:00
|
|
|
self.items = items;
|
|
|
|
|
self.current_index = current_index.filter(|&i| i < self.items.len());
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-09 14:30:53 +01:00
|
|
|
fn sync_queue(&mut self, items: Vec<PlaybackItem>) -> Result<(), ControlPointError> {
|
2025-12-30 16:50:26 +01:00
|
|
|
if items.is_empty() {
|
|
|
|
|
return self.replace_queue(Vec::new(), None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Récupérer l'item actuel
|
2026-01-09 14:30:53 +01:00
|
|
|
let current = self
|
|
|
|
|
.current_index
|
2025-12-30 16:50:26 +01:00
|
|
|
.and_then(|idx| self.items.get(idx).map(|item| (idx, item.uri.clone())));
|
|
|
|
|
|
|
|
|
|
if let Some((_current_idx, current_uri)) = current {
|
|
|
|
|
// Chercher l'item actuel dans la nouvelle liste (par URI)
|
|
|
|
|
let new_idx = items.iter().position(|item| item.uri == current_uri);
|
|
|
|
|
|
|
|
|
|
if let Some(new_idx) = new_idx {
|
|
|
|
|
// Item trouvé dans la nouvelle liste
|
|
|
|
|
self.replace_queue(items, Some(new_idx))
|
|
|
|
|
} else {
|
|
|
|
|
// Item pas trouvé, le garder comme premier
|
|
|
|
|
let current_item = self.items[self.current_index.unwrap()].clone();
|
|
|
|
|
let mut new_items = Vec::with_capacity(items.len() + 1);
|
|
|
|
|
new_items.push(current_item);
|
|
|
|
|
new_items.extend(items);
|
|
|
|
|
self.replace_queue(new_items, Some(0))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Pas d'item actuel
|
|
|
|
|
self.replace_queue(items, None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn enqueue_items(
|
|
|
|
|
&mut self,
|
|
|
|
|
items: Vec<PlaybackItem>,
|
|
|
|
|
mode: crate::queue::EnqueueMode,
|
|
|
|
|
) -> Result<(), ControlPointError> {
|
|
|
|
|
use crate::queue::EnqueueMode;
|
|
|
|
|
|
|
|
|
|
match mode {
|
|
|
|
|
EnqueueMode::AppendToEnd => {
|
|
|
|
|
self.items.extend(items);
|
|
|
|
|
}
|
|
|
|
|
EnqueueMode::InsertAfterCurrent => {
|
2026-01-09 14:30:53 +01:00
|
|
|
let insert_pos = self
|
|
|
|
|
.current_index
|
2025-12-30 16:50:26 +01:00
|
|
|
.map(|i| (i + 1).min(self.items.len()))
|
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
|
|
|
|
|
for (offset, item) in items.into_iter().enumerate() {
|
|
|
|
|
self.items.insert(insert_pos + offset, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
EnqueueMode::ReplaceAll => {
|
|
|
|
|
self.items = items;
|
|
|
|
|
self.current_index = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 17:45:06 +01:00
|
|
|
fn get_item(&self, index: usize) -> Result<Option<PlaybackItem>, ControlPointError> {
|
2025-12-30 16:50:26 +01:00
|
|
|
if index < self.items.len() {
|
|
|
|
|
Ok(self.items.get(index).cloned())
|
|
|
|
|
} else {
|
|
|
|
|
Err(ControlPointError::QueueError(format!(
|
|
|
|
|
"get_item index out of bound {} >= {}",
|
|
|
|
|
index,
|
|
|
|
|
self.items.len()
|
|
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Optimized helpers for InternalQueue
|
|
|
|
|
fn clear_queue(&mut self) -> Result<(), ControlPointError> {
|
|
|
|
|
self.items.clear();
|
|
|
|
|
self.current_index = None;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_empty(&self) -> Result<bool, ControlPointError> {
|
|
|
|
|
Ok(self.items.is_empty())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn upcoming_len(&self) -> Result<usize, ControlPointError> {
|
|
|
|
|
let len = self.items.len();
|
|
|
|
|
match self.current_index {
|
|
|
|
|
None => Ok(len),
|
|
|
|
|
Some(idx) => Ok(len.saturating_sub(idx + 1)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn upcoming_items(&self) -> Result<Vec<PlaybackItem>, ControlPointError> {
|
|
|
|
|
let items = match self.current_index {
|
|
|
|
|
None => self.items.clone(),
|
|
|
|
|
Some(idx) => self.items.iter().skip(idx + 1).cloned().collect(),
|
|
|
|
|
};
|
|
|
|
|
Ok(items)
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-09 14:30:53 +01:00
|
|
|
fn peek_current(&mut self) -> Result<Option<(PlaybackItem, usize)>, ControlPointError> {
|
2025-12-30 16:50:26 +01:00
|
|
|
if self.items.is_empty() {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let len = self.items.len();
|
|
|
|
|
let (item, resolved_index) = match self.current_index {
|
|
|
|
|
Some(idx) if idx < len => (self.items.get(idx).cloned(), Some(idx)),
|
2026-01-09 14:30:53 +01:00
|
|
|
_ => {
|
|
|
|
|
// Si current_index est None ou invalide, initialiser à 0
|
|
|
|
|
self.current_index = Some(0);
|
|
|
|
|
(self.items.first().cloned(), Some(0))
|
|
|
|
|
}
|
2025-12-30 16:50:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let item = match item {
|
|
|
|
|
Some(item) => item,
|
|
|
|
|
None => return Ok(None),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let remaining = match resolved_index {
|
|
|
|
|
Some(idx) => len.saturating_sub(idx + 1),
|
|
|
|
|
None => len,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(Some((item, remaining)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn dequeue_next(&mut self) -> Result<Option<(PlaybackItem, usize)>, ControlPointError> {
|
|
|
|
|
if self.items.is_empty() {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let len = self.items.len();
|
|
|
|
|
let next_index = match self.current_index {
|
|
|
|
|
None => 0,
|
|
|
|
|
Some(idx) => {
|
|
|
|
|
let candidate = idx + 1;
|
|
|
|
|
if candidate >= len {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
candidate
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let Some(item) = self.items.get(next_index).cloned() else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let remaining = len.saturating_sub(next_index + 1);
|
|
|
|
|
self.current_index = Some(next_index);
|
|
|
|
|
Ok(Some((item, remaining)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn append_or_init_index(&mut self, items: Vec<PlaybackItem>) -> Result<(), ControlPointError> {
|
|
|
|
|
let was_empty = self.items.is_empty();
|
|
|
|
|
self.items.extend(items);
|
|
|
|
|
|
|
|
|
|
if was_empty && !self.items.is_empty() {
|
|
|
|
|
self.current_index = Some(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2025-12-06 13:37:42 +01:00
|
|
|
}
|
|
|
|
|
|
2025-12-28 17:45:06 +01:00
|
|
|
fn replace_item(&mut self, index: usize, item: PlaybackItem) -> Result<(), ControlPointError> {
|
2025-12-06 13:37:42 +01:00
|
|
|
if index < self.items.len() {
|
|
|
|
|
self.items[index] = item;
|
2025-12-30 16:50:26 +01:00
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(ControlPointError::QueueError(format!(
|
|
|
|
|
"Index out of bound {} >= {}",
|
|
|
|
|
index,
|
|
|
|
|
self.items.len()
|
|
|
|
|
)))
|
2025-12-06 13:37:42 +01:00
|
|
|
}
|
2025-12-30 16:50:26 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl QueueFromRendererInfo for InternalQueue {
|
|
|
|
|
fn from_renderer_info(renderer: &RendererInfo) -> Result<Self, ControlPointError> {
|
|
|
|
|
InternalQueue::from_renderer_info(renderer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_backend(self) -> MusicQueue {
|
|
|
|
|
MusicQueue::Internal(self)
|
2025-12-06 13:37:42 +01:00
|
|
|
}
|
|
|
|
|
}
|