1use std::fmt;
5
6use async_trait::async_trait;
7use tokio::io::AsyncRead;
8
9use crate::{Failure, LoadResult, Track};
10
11#[derive(Debug, Clone, PartialEq, Eq)]
13pub enum Format {
14 Pcm48,
16 Encoded { hint: Option<String> },
20}
21
22impl Format {
23 pub fn mp3() -> Self {
24 Format::Encoded {
25 hint: Some("mp3".into()),
26 }
27 }
28}
29
30pub struct Stream {
32 pub body: Box<dyn AsyncRead + Send + Unpin>,
33 pub format: Format,
34 pub start_sample: i64,
38 pub live: bool,
40}
41
42impl fmt::Debug for Stream {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 f.debug_struct("Stream")
45 .field("format", &self.format)
46 .field("start_sample", &self.start_sample)
47 .field("live", &self.live)
48 .finish_non_exhaustive()
49 }
50}
51
52#[async_trait]
54pub trait Source: Send + Sync + 'static {
55 fn name(&self) -> &str;
57
58 fn search_prefix(&self) -> Option<&str> {
60 None
61 }
62
63 async fn search(&self, _query: &str) -> Result<Vec<Track>, Failure> {
65 Ok(vec![])
66 }
67
68 fn matches_url(&self, _url: &str) -> bool {
70 false
71 }
72
73 async fn load_url(&self, url: &str) -> Result<LoadResult, Failure> {
75 Err(Failure::common(
76 self.name(),
77 "links are not supported",
78 Some(url.to_string()),
79 ))
80 }
81
82 async fn track(&self, identifier: &str) -> Result<Track, Failure>;
84
85 fn playable(&self) -> Option<&dyn Playable> {
87 None
88 }
89
90 fn isrc_lookup(&self) -> Option<&dyn IsrcLookup> {
92 None
93 }
94
95 fn recommender(&self) -> Option<&dyn Recommender> {
97 None
98 }
99}
100
101#[async_trait]
103pub trait Playable: Send + Sync {
104 async fn open(&self, track: &Track, start_ms: i64) -> Result<Stream, Failure>;
106
107 fn seekable(&self) -> bool {
109 false
110 }
111
112 async fn warm(&self, _track: &Track) {}
114}
115
116#[async_trait]
117pub trait IsrcLookup: Send + Sync {
118 async fn by_isrc(&self, isrc: &str) -> Result<Track, Failure>;
119}
120
121#[async_trait]
122pub trait Recommender: Send + Sync {
123 async fn recommend(&self, identifier: &str, limit: usize) -> Result<Vec<Track>, Failure>;
124}