Skip to content

Commit

Permalink
use anni-player to download audio and handle cache
Browse files Browse the repository at this point in the history
  • Loading branch information
snylonue committed Feb 28, 2024
1 parent b40160e commit 3706dda
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 145 deletions.
36 changes: 30 additions & 6 deletions annix/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion annix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ uuid = "1"
crossbeam = { version = "0.8.2", features = ["crossbeam-channel"] }
anyhow = "1.0.75"
log = "0.4.20"
env_logger = "0.11"

anni-playback = { git = "https://github.com/ProjectAnni/anni", rev = "a91695e" }
anni-player = { git = "https://github.com/snylonue/anni-player", rev = "2afcc8c" }
anni-player = { git = "https://github.com/snylonue/anni-player", rev = "9edc57a" }

[build-dependencies]
flutter_rust_bridge_codegen = { version = "=1.81", features = ["uuid"] }
27 changes: 23 additions & 4 deletions annix/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,15 @@ CREATE TABLE IF NOT EXISTS store(

// Audio
pub use anni_playback::types::*;
use flutter_rust_bridge::StreamSink;
use flutter_rust_bridge::frb;
use flutter_rust_bridge::StreamSink;

// pub use crate::player::player::Player;
pub use crate::player::PlayerStateEvent;
pub use anni_player::AnniPlayer;
use anni_player::TypedPriorityProvider;
#[cfg(target_os = "windows")]
use once_cell::sync::OnceCell;

#[frb(mirror(ProgressState))]
pub struct _ProgressState {
Expand Down Expand Up @@ -263,7 +265,8 @@ fn update_player_state_stream(

pub type StreamWrapper<T> = Arc<OnceLock<RwLock<Option<StreamSink<T>>>>>;

// impl DartSafe for AnniPlayerWrapper {}
#[cfg(target_os = "windows")]
static LOGGER_INIT: OnceCell<()> = OnceCell::new();

pub struct AnnixPlayer {
pub player: RustOpaque<AnniPlayer>,
Expand All @@ -272,8 +275,12 @@ pub struct AnnixPlayer {
}

impl AnnixPlayer {
pub fn new() -> SyncReturn<AnnixPlayer> {
let (player, receiver) = AnniPlayer::new(TypedPriorityProvider::new(vec![]), "./cache".into());
pub fn new(cache_path: String) -> SyncReturn<AnnixPlayer> {
#[cfg(target_os = "windows")]
LOGGER_INIT.get_or_init(|| env_logger::init());

let (player, receiver) =
AnniPlayer::new(TypedPriorityProvider::new(vec![]), cache_path.into());
let progress = Arc::new(OnceLock::new());
let player_state = Arc::new(OnceLock::new());

Expand Down Expand Up @@ -320,6 +327,10 @@ impl AnnixPlayer {
self.player.open_file(path)
}

pub fn open(&self, identifier: String) -> anyhow::Result<()> {
self.player.open(identifier.parse()?)
}

pub fn set_volume(&self, volume: f32) {
self.player.set_volume(volume);
}
Expand All @@ -336,6 +347,14 @@ impl AnnixPlayer {
SyncReturn(self.player.player.is_playing())
}

pub fn add_provider(&self, url: String, auth: String, priority: i32) {
self.player.add_provider(url, auth, priority);
}

pub fn clear_provider(&self) {
self.player.clear_provider();
}

pub fn player_state_stream(&self, stream: StreamSink<PlayerStateEvent>) {
*self
._state
Expand Down
34 changes: 32 additions & 2 deletions annix/src/bridge_generated.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ pub extern "C" fn wire_clear__method__LocalStore(
}

#[no_mangle]
pub extern "C" fn wire_new__static_method__AnnixPlayer() -> support::WireSyncReturn {
wire_new__static_method__AnnixPlayer_impl()
pub extern "C" fn wire_new__static_method__AnnixPlayer(
cache_path: *mut wire_uint_8_list,
) -> support::WireSyncReturn {
wire_new__static_method__AnnixPlayer_impl(cache_path)
}

#[no_mangle]
Expand All @@ -141,6 +143,15 @@ pub extern "C" fn wire_open_file__method__AnnixPlayer(
wire_open_file__method__AnnixPlayer_impl(port_, that, path)
}

#[no_mangle]
pub extern "C" fn wire_open__method__AnnixPlayer(
port_: i64,
that: *mut wire_AnnixPlayer,
identifier: *mut wire_uint_8_list,
) {
wire_open__method__AnnixPlayer_impl(port_, that, identifier)
}

#[no_mangle]
pub extern "C" fn wire_set_volume__method__AnnixPlayer(
port_: i64,
Expand Down Expand Up @@ -171,6 +182,25 @@ pub extern "C" fn wire_is_playing__method__AnnixPlayer(
wire_is_playing__method__AnnixPlayer_impl(that)
}

#[no_mangle]
pub extern "C" fn wire_add_provider__method__AnnixPlayer(
port_: i64,
that: *mut wire_AnnixPlayer,
url: *mut wire_uint_8_list,
auth: *mut wire_uint_8_list,
priority: i32,
) {
wire_add_provider__method__AnnixPlayer_impl(port_, that, url, auth, priority)
}

#[no_mangle]
pub extern "C" fn wire_clear_provider__method__AnnixPlayer(
port_: i64,
that: *mut wire_AnnixPlayer,
) {
wire_clear_provider__method__AnnixPlayer_impl(port_, that)
}

#[no_mangle]
pub extern "C" fn wire_player_state_stream__method__AnnixPlayer(
port_: i64,
Expand Down
72 changes: 70 additions & 2 deletions annix/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,19 @@ fn wire_clear__method__LocalStore_impl(
},
)
}
fn wire_new__static_method__AnnixPlayer_impl() -> support::WireSyncReturn {
fn wire_new__static_method__AnnixPlayer_impl(
cache_path: impl Wire2Api<String> + UnwindSafe,
) -> support::WireSyncReturn {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync(
WrapInfo {
debug_name: "new__static_method__AnnixPlayer",
port: None,
mode: FfiCallMode::Sync,
},
move || Ok(AnnixPlayer::new()),
move || {
let api_cache_path = cache_path.wire2api();
Ok(AnnixPlayer::new(api_cache_path))
},
)
}
fn wire_play__method__AnnixPlayer_impl(
Expand Down Expand Up @@ -359,6 +364,24 @@ fn wire_open_file__method__AnnixPlayer_impl(
},
)
}
fn wire_open__method__AnnixPlayer_impl(
port_: MessagePort,
that: impl Wire2Api<AnnixPlayer> + UnwindSafe,
identifier: impl Wire2Api<String> + UnwindSafe,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap::<_, _, _, ()>(
WrapInfo {
debug_name: "open__method__AnnixPlayer",
port: Some(port_),
mode: FfiCallMode::Normal,
},
move || {
let api_that = that.wire2api();
let api_identifier = identifier.wire2api();
move |task_callback| AnnixPlayer::open(&api_that, api_identifier)
},
)
}
fn wire_set_volume__method__AnnixPlayer_impl(
port_: MessagePort,
that: impl Wire2Api<AnnixPlayer> + UnwindSafe,
Expand Down Expand Up @@ -426,6 +449,51 @@ fn wire_is_playing__method__AnnixPlayer_impl(
},
)
}
fn wire_add_provider__method__AnnixPlayer_impl(
port_: MessagePort,
that: impl Wire2Api<AnnixPlayer> + UnwindSafe,
url: impl Wire2Api<String> + UnwindSafe,
auth: impl Wire2Api<String> + UnwindSafe,
priority: impl Wire2Api<i32> + UnwindSafe,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap::<_, _, _, ()>(
WrapInfo {
debug_name: "add_provider__method__AnnixPlayer",
port: Some(port_),
mode: FfiCallMode::Normal,
},
move || {
let api_that = that.wire2api();
let api_url = url.wire2api();
let api_auth = auth.wire2api();
let api_priority = priority.wire2api();
move |task_callback| {
Ok(AnnixPlayer::add_provider(
&api_that,
api_url,
api_auth,
api_priority,
))
}
},
)
}
fn wire_clear_provider__method__AnnixPlayer_impl(
port_: MessagePort,
that: impl Wire2Api<AnnixPlayer> + UnwindSafe,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap::<_, _, _, ()>(
WrapInfo {
debug_name: "clear_provider__method__AnnixPlayer",
port: Some(port_),
mode: FfiCallMode::Normal,
},
move || {
let api_that = that.wire2api();
move |task_callback| Ok(AnnixPlayer::clear_provider(&api_that))
},
)
}
fn wire_player_state_stream__method__AnnixPlayer_impl(
port_: MessagePort,
that: impl Wire2Api<AnnixPlayer> + UnwindSafe,
Expand Down
17 changes: 16 additions & 1 deletion ios/Runner/bridge_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void wire_clear__method__LocalStore(int64_t port_,
struct wire_LocalStore *that,
struct wire_uint_8_list *category);

WireSyncReturn wire_new__static_method__AnnixPlayer(void);
WireSyncReturn wire_new__static_method__AnnixPlayer(struct wire_uint_8_list *cache_path);

void wire_play__method__AnnixPlayer(int64_t port_, struct wire_AnnixPlayer *that);

Expand All @@ -123,6 +123,10 @@ void wire_open_file__method__AnnixPlayer(int64_t port_,
struct wire_AnnixPlayer *that,
struct wire_uint_8_list *path);

void wire_open__method__AnnixPlayer(int64_t port_,
struct wire_AnnixPlayer *that,
struct wire_uint_8_list *identifier);

void wire_set_volume__method__AnnixPlayer(int64_t port_,
struct wire_AnnixPlayer *that,
float volume);
Expand All @@ -135,6 +139,14 @@ void wire_seek__method__AnnixPlayer(int64_t port_,

WireSyncReturn wire_is_playing__method__AnnixPlayer(struct wire_AnnixPlayer *that);

void wire_add_provider__method__AnnixPlayer(int64_t port_,
struct wire_AnnixPlayer *that,
struct wire_uint_8_list *url,
struct wire_uint_8_list *auth,
int32_t priority);

void wire_clear_provider__method__AnnixPlayer(int64_t port_, struct wire_AnnixPlayer *that);

void wire_player_state_stream__method__AnnixPlayer(int64_t port_, struct wire_AnnixPlayer *that);

void wire_progress_stream__method__AnnixPlayer(int64_t port_, struct wire_AnnixPlayer *that);
Expand Down Expand Up @@ -202,10 +214,13 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) wire_play__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) wire_pause__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) wire_open_file__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) wire_open__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) wire_set_volume__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) wire_stop__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) wire_seek__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) wire_is_playing__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) wire_add_provider__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) wire_clear_provider__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) wire_player_state_stream__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) wire_progress_stream__method__AnnixPlayer);
dummy_var ^= ((int64_t) (void*) new_AnniPlayer);
Expand Down
Loading

0 comments on commit 3706dda

Please sign in to comment.