Skip to content

Commit

Permalink
Input: Allow Restartable sources to be lazy
Browse files Browse the repository at this point in the history
This change is made with queue users in mind. Since sources
of this kind *know* how to (re)create themselves, they can
avoid being created at all until needed.

This also adds machinery to preload tracks *before* they are
needed, for gapless playback on queues and so on. Queues
make use of the event system to do this.
  • Loading branch information
FelixMcFelix committed Dec 28, 2020
1 parent c0d3cb3 commit 03ae0e7
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 100 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ version = "0.11"

[dependencies.rand]
optional = true
version = "0.7"
version = "0.8"

[dependencies.serenity]
optional = true
Expand Down
12 changes: 9 additions & 3 deletions examples/serenity/voice_events_queue/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ use serenity::{
};

use songbird::{
input,
input::{
self,
restartable::Restartable,
},
Event,
EventContext,
EventHandler as VoiceEventHandler,
Expand Down Expand Up @@ -477,7 +480,10 @@ async fn queue(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {

if let Some(handler_lock) = manager.get(guild_id) {
let mut handler = handler_lock.lock().await;
let source = match input::ytdl(&url).await {

// Here, we use lazy restartable sources to make sure that we don't pay
// for decoding, playback on tracks which aren't actually live yet.
let source = match Restartable::ytdl(url, true).await {
Ok(source) => source,
Err(why) => {
println!("Err starting source: {:?}", why);
Expand All @@ -488,7 +494,7 @@ async fn queue(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
},
};

handler.enqueue_source(source);
handler.enqueue_source(source.into());

check_msg(
msg.channel_id
Expand Down
2 changes: 1 addition & 1 deletion examples/twilight/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ async fn play(msg: Message, state: State) -> Result<(), Box<dyn Error + Send + S

let guild_id = msg.guild_id.unwrap();

if let Ok(song) = Restartable::ytdl(msg.content.clone()).await {
if let Ok(song) = Restartable::ytdl(msg.content.clone(), false).await {
let input = Input::from(song);

let content = format!(
Expand Down
9 changes: 9 additions & 0 deletions src/input/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ impl Reader {
_ => {},
}
}

#[allow(clippy::single_match)]
pub(crate) fn make_playable(&mut self) {
use Reader::*;
match self {
Restartable(r) => r.make_playable(),
_ => {},
}
}
}

impl Read for Reader {
Expand Down
Loading

0 comments on commit 03ae0e7

Please sign in to comment.