|
| 1 | +#[cfg(target_os = "macos")] |
| 2 | +mod macos; |
| 3 | +#[cfg(not(any( |
| 4 | + target_os = "macos" |
| 5 | +)))] |
| 6 | +mod null; |
1 | 7 |
|
| 8 | +pub trait MediaBackend { |
| 9 | + fn set_title(&self, title: &str); |
| 10 | + fn set_artist(&self, artist: &str); |
| 11 | + fn set_album(&self, album: &str); |
| 12 | + fn set_genre(&self, genre: &str); |
| 13 | + fn set_media_type(&self, media_type: MediaType); |
| 14 | + fn set_playback_duration(&self, duration: f64); |
| 15 | + fn set_elapsed_duration(&self, duration: f64); |
| 16 | + fn set_playback_rate(&self, rate: f64); |
| 17 | + fn start_session(&self); |
| 18 | + fn stop_session(&self); |
| 19 | +} |
| 20 | + |
| 21 | +// Defines the type of media being presented |
| 22 | +#[derive(Clone, Copy, PartialEq)] |
| 23 | +pub enum MediaType { |
| 24 | + Audio, |
| 25 | + Video |
| 26 | +} |
2 | 27 |
|
3 | | -#[unsafe(no_mangle)] |
4 | | -pub extern "C" fn rust_hello() { |
5 | | - println!("Hello from Rust!"); |
| 28 | +pub struct Metadata { |
| 29 | + title: String, |
| 30 | + artist: String, |
| 31 | + album: String, |
| 32 | + genre: String, |
| 33 | + media_type: MediaType, |
| 34 | + duration: f64, |
| 35 | + playback_rate: f64, |
6 | 36 | } |
7 | 37 |
|
8 | | -unsafe extern "C" { |
9 | | - fn hello_world(); |
| 38 | +pub struct MediaSession { |
| 39 | + backend: Box<dyn MediaBackend>, |
| 40 | + metadata: Metadata, |
10 | 41 | } |
11 | 42 |
|
| 43 | +impl MediaSession { |
12 | 44 |
|
| 45 | + pub fn new() -> Self { |
| 46 | + #[cfg(target_os = "macos")] |
| 47 | + let backend = Box::new(macos::NowPlayingBackend::new()); |
13 | 48 |
|
14 | | -use std::thread; |
15 | | -use std::time::Duration; |
| 49 | + #[cfg(not(any( |
| 50 | + target_os = "macos" |
| 51 | + )))] |
| 52 | + let backend = Box::new(null::NullBackend::new()); |
16 | 53 |
|
17 | | -pub fn call_swift() { |
18 | | - unsafe { |
19 | | - hello_world(); |
| 54 | + Self { |
| 55 | + backend, |
| 56 | + metadata: Metadata { |
| 57 | + title: "".to_string(), |
| 58 | + artist: "".to_string(), |
| 59 | + album: "".to_string(), |
| 60 | + genre: "".to_string(), |
| 61 | + media_type: MediaType::Audio, |
| 62 | + duration: 0.0f64, |
| 63 | + playback_rate: 0.0f64, |
| 64 | + }, |
| 65 | + } |
20 | 66 | } |
21 | 67 |
|
22 | | - thread::sleep(Duration::from_secs(10)); |
23 | | -} |
| 68 | + pub fn set_title(&mut self, title: &str) { |
| 69 | + self.metadata.title = title.to_string(); |
| 70 | + self.backend.set_title(title); |
| 71 | + } |
| 72 | + |
| 73 | + pub fn title(&self) -> &str { |
| 74 | + &self.metadata.title |
| 75 | + } |
| 76 | + |
| 77 | + pub fn set_artist(&mut self, artist: &str) { |
| 78 | + self.metadata.artist = artist.to_string(); |
| 79 | + self.backend.set_artist(artist); |
| 80 | + } |
| 81 | + |
| 82 | + pub fn artist(&self) -> &str { |
| 83 | + &self.metadata.artist |
| 84 | + } |
| 85 | + |
| 86 | + pub fn set_album(&mut self, album: &str) { |
| 87 | + self.metadata.album = album.to_string(); |
| 88 | + self.backend.set_album(album); |
| 89 | + } |
| 90 | + |
| 91 | + pub fn album(&self) -> &str { |
| 92 | + &self.metadata.album |
| 93 | + } |
| 94 | + |
| 95 | + pub fn set_genre(&mut self, genre: &str) { |
| 96 | + self.metadata.genre = genre.to_string(); |
| 97 | + self.backend.set_album(genre); |
| 98 | + } |
| 99 | + |
| 100 | + pub fn genre(&self) -> &str { |
| 101 | + &self.metadata.genre |
| 102 | + } |
| 103 | + |
| 104 | + pub fn set_media_type(&mut self, media_type: MediaType) { |
| 105 | + self.metadata.media_type = media_type; |
| 106 | + self.backend.set_media_type(media_type); |
| 107 | + } |
| 108 | + |
| 109 | + pub fn media_type(&self) -> MediaType { |
| 110 | + self.metadata.media_type |
| 111 | + } |
| 112 | + |
| 113 | + pub fn set_playback_duration(&mut self, duration: f64) { |
| 114 | + self.metadata.duration = duration; |
| 115 | + self.backend.set_playback_duration(duration); |
| 116 | + } |
| 117 | + |
| 118 | + pub fn duration(&self) -> f64 { |
| 119 | + self.metadata.duration |
| 120 | + } |
| 121 | + |
| 122 | + pub fn set_elapsed_duration(&self, duration: f64) { |
| 123 | + self.backend.set_elapsed_duration(duration); |
| 124 | + } |
| 125 | + |
| 126 | + pub fn set_playback_rate(&mut self, rate: f64) { |
| 127 | + self.metadata.playback_rate = rate; |
| 128 | + self.backend.set_playback_rate(rate); |
| 129 | + } |
| 130 | + |
| 131 | + pub fn playback_rate(&self) -> f64 { |
| 132 | + self.metadata.playback_rate |
| 133 | + } |
24 | 134 |
|
| 135 | + pub fn start(&self) { |
| 136 | + self.backend.start_session(); |
| 137 | + } |
| 138 | + |
| 139 | + pub fn stop(&self) { |
| 140 | + self.backend.stop_session(); |
| 141 | + } |
25 | 142 |
|
| 143 | +} |
26 | 144 |
|
27 | 145 |
|
28 | 146 | #[cfg(test)] |
|
0 commit comments