Skip to content

Commit

Permalink
address #129
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlett authored and Skarlett committed Sep 26, 2023
1 parent 443c103 commit 9dbfe06
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 32 deletions.
43 changes: 21 additions & 22 deletions crates/mockingbird/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub enum TrackAuthor {
// -> PreTrackRequest<T> [prefan]
//
// -> TrackRequest [prefan]
// -> TrackRequestFetched
// -> TrackRequestFetched [fanned]
// -> TrackRequestPreload<T> where T: AudioPlayer
// -> magic casting .*.*.~
// -> TrackRequestPreload<Box<dyn AudioPlayer>>
Expand All @@ -81,37 +81,31 @@ pub enum TrackAuthor {
// fn raw_metadata(&self) -> T;
// }
pub struct TrackRequest {
pub tranid: uuid::Uuid,
pub author: TrackAuthor,
pub uri: String,
}

impl TrackRequest {
pub fn new(uri: String, author: TrackAuthor) -> Self {
Self {
tranid: uuid::Uuid::new_v4(),
pub fn new(uri: String, author: TrackAuthor) -> (uuid::Uuid, Self) {
(uuid::Uuid::new_v4(), Self {
author,
uri
}
})
}

pub fn user(uri: String, author: UserId) -> Self {
Self {
tranid: uuid::Uuid::new_v4(),
author: TrackAuthor::User(author),
uri
}
pub fn user(uri: String, author: UserId) -> (uuid::Uuid, Self) {
Self::new(
uri,
TrackAuthor::User(author)
)
}

pub fn radio(uri: String) -> Self {
Self {
tranid: uuid::Uuid::new_v4(),
author: TrackAuthor::Radio,
uri
}
Self::new(
uri,
TrackAuthor::Radio,
)
}


}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -186,12 +180,17 @@ pub struct Radio {
pub seeds: VecDeque<MetadataType>,
}

pub struct Queue {
pub cold: VecDeque<TrackRequestFetched>,
pub struct Queue<T> {


pub cold: T,

// VecDeque<TrackRequestFetched>,
pub warm: VecDeque<TrackRequestPreload<Box<dyn AudioPlayer>>>,

pub has_played: VecDeque<TrackRecord>,
pub past_transactions: VecDeque<uuid::Uuid>,
pub past_transactions: HashMap<uuid::Uuid, TrackRequest>,
pub transactions_order: VecDeque<uuid::Uuid>,

pub killed: Vec<std::process::Child>,
pub radio: Option<Radio>,
Expand Down
70 changes: 60 additions & 10 deletions crates/mockingbird/src/routines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,70 @@ use std::{
sync::Arc,
};

use std::iter::Cycle;

use cutils::{availbytes, bigpipe, max_pipe_size};

use crate::{deemix::{DeemixMetadata, PreloadInput, DeemixError}, player::TrackRequestPreload};
use crate::{deemix::{DeemixMetadata, PreloadInput, DeemixError}, player::{TrackRequestPreload, TrackRequestFetched}};

use crate::player::{Queue, AudioPlayer, MetadataType, QueueContext, TrackRecord, EventEnd, TrackRequest};
use crate::ctrlerror::HandlerError;

/// Items moved from cold to warm queue
trait QueueStrategy {
fn next_track(&mut self) -> Option<TrackRequest>;

fn add_track(&mut self, track: TrackRequest) -> bool;
}

struct TraditionalQueue {
list: VecDeque<TrackRequestFetched>
}

impl QueueStrategy for TraditionalQueue {
fn next_track(&mut self) -> Option<TrackRequestFetched> {
self.list.pop_front()
}
}

struct RoundRobinQueue {
map: indexmap::IndexMap<UserId, VecDeque<TrackRequestFetched>>,
cyclic: Cycle<UserId>,
}

// remove all items from cycle
// maintaing the current order,
// and append new user to the end
fn update_cyclic(cyclic: &mut Cycle<UserId>, new_user: UserId ) {
let mut items = cyclic.clone().take(cyclic.len()).collect::<Vec<_>>();
items.push(new_user);

*cyclic = items.into_iter().cycle();
}

impl QueueStrategy for RoundRobinQueue {
fn next_track(&mut self, ) -> Option<TrackRequestFetched> {
loop {
if self.map.is_empty() { return None }

let user = self.cyclic.next();
let track = self.map.get_mut(&user).unwrap().pop_front();

if track.is_none() {
self.map.remove(&user);
continue
}
else { return track }
}

}
}


async fn next_track(queue: &mut VecDeque<TrackRequestPreload<Box<dyn AudioPlayer + Send>>> )
-> Option<Box<dyn AudioPlayer + Send>> {

let (mut radio, mut user): (VecDeque<_>, VecDeque<_>) = queue.iter().partition(|x|
let (mut radio, mut user): (VecDeque<_>, VecDeque<_>) = queue.iter_mut().partition(|x|
if let crate::player::TrackAuthor::User(_) = &x.request.author {
true
} else {
Expand All @@ -68,9 +120,8 @@ async fn next_track(queue: &mut VecDeque<TrackRequestPreload<Box<dyn AudioPlayer

pub async fn play_once_routine(
req: TrackRequest,
has_played: &mut VecDeque<TrackRecord>)
{

has_played: &mut VecDeque<TrackRecord>
){
has_played.push_front(
TrackRecord {
start: Instant::now(),
Expand All @@ -79,9 +130,9 @@ pub async fn play_once_routine(
req
}
);

}


pub async fn play_queue_routine(qctx: Arc<QueueContext>) -> Result<bool, HandlerError> {
let mut tries = 4;

Expand Down Expand Up @@ -115,11 +166,8 @@ pub async fn play_queue_routine(qctx: Arc<QueueContext>) -> Result<bool, Handler
let (track, trackhandle) = create_player(input);

call.enqueue(track);

// , metadata
};



Ok(true)
}

Expand Down Expand Up @@ -292,6 +340,8 @@ For the best experience, use 128kbps, & spotify links
warm: VecDeque::new(),
radio: None,
has_played: VecDeque::new(),
past_transactions: HashMap::new(),
transactions_order: VecDeque::new(),
killed: Vec::new(),
})),
// sfx: Arc::new(RwLock::new(todo!())),
Expand Down

0 comments on commit 9dbfe06

Please sign in to comment.