Skip to content

Commit

Permalink
refactor: exoplayer custom datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
hpp2334 committed Nov 23, 2024
1 parent f401790 commit 864c61c
Show file tree
Hide file tree
Showing 34 changed files with 436 additions and 481 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ android {
minSdk = 29
targetSdk = 34
versionCode = 1
versionName = "0.2.0-beta.2"
versionName = "0.2.0-beta.3"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fun EaseImage(
contentScale: ContentScale
) {
val bridge = UIBridgeController.current
var bitmap by remember { mutableStateOf<ImageBitmap?>(null) }
var bitmap by remember { mutableStateOf(bridge.bitmapDataSources.get(dataSourceKey)) }

LaunchedEffect(dataSourceKey) {
val data = bridge.bitmapDataSources.load(dataSourceKey)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,22 @@ class BitmapDataSources {
}
return false;
}

override fun hashCode(): Int {
return _key.hashCode()
}
}

private val _map = HashMap<K, ImageBitmap?>()

fun get(key: DataSourceKey): ImageBitmap? {
return this._map[K(key)]
}

suspend fun load(key: DataSourceKey): ImageBitmap? {
val cached = this._map[K(key)]
if (cached != null) {
return null
return cached
}

val data = apiLoadAsset(key)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.kutedev.easemusicplayer.utils

import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock

class Item(val data: ByteArray, var remaining: Int)

class ByteQueue {
private val queue = ConcurrentLinkedQueue<Item>()
private val lock = ReentrantLock()

fun put(bytes: ByteArray) {
lock.withLock {
queue.add(Item(bytes, bytes.size))
}
}

fun isEmpty(): Boolean {
lock.withLock {
return queue.isEmpty()
}
}

fun take(n: Int): ByteArray {
lock.withLock {
val result = ByteArray(n)
var resultOffset = 0

var takeRemaining = n
while (takeRemaining > 0 && queue.isNotEmpty()) {
val item = queue.peek()!!

val bytesToTake = minOf(takeRemaining, item.remaining)
item.data.copyInto(result, resultOffset, item.data.size - item.remaining, item.data.size - item.remaining + bytesToTake)
resultOffset += bytesToTake
takeRemaining -= bytesToTake
item.remaining -= bytesToTake

if (item.remaining == 0) {
queue.poll()
}
}
return result.copyOf(resultOffset)
}
}

fun clear() {
lock.withLock {
queue.clear()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@ private fun MusicSlider(
.fillMaxWidth()
.height(sliderContainerHeight)
.onSizeChanged { size ->
sliderWidth = size.width;
if (sliderWidth != size.width) {
sliderWidth = size.width;
}
}
.pointerInput(Unit) {
.pointerInput(totalDurationMS, sliderWidth) {
detectTapGestures { offset ->
var nextMS = (offset.x.toDouble() / sliderWidth.toDouble() * totalDurationMS.toDouble()).toLong()
nextMS = nextMS.coerceIn(0L, totalDurationMS.toLong())
Expand Down
2 changes: 1 addition & 1 deletion misty-vm/misty-async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
time::Duration,
};

use async_task::{Runnable, Task};
pub use async_task::{Runnable, Task};

pub use futures::future::{BoxFuture, LocalBoxFuture};

Expand Down
119 changes: 3 additions & 116 deletions rust-libs/Cargo.lock

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

1 change: 1 addition & 0 deletions rust-libs/ease-client-android/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ tracing = "0.1"
serde_bytes = "0.11.14"
uniffi = "=0.28.3"
tokio = { version = "1", features = ["rt-multi-thread", "time", "macros"] }
thiserror = "1.0.57"
44 changes: 29 additions & 15 deletions rust-libs/ease-client-android/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,16 @@ use tracing::subscriber::set_global_default;

use crate::{
backend_host::BackendHost,
error::AndroidFfiError,
foreigns::{
IAsyncAdapterForeign, IPermissionServiceForeign, IPlayerDelegateForeign,
IRouterServiceForeign, IToastServiceForeign, IViewStateServiceForeign,
PermissionServiceDelegate, PlayerDelegate, RouterServiceDelegate, ToastServiceDelegate,
ViewStateServiceDelegate,
AssetLoadDelegate, IAssetLoadDelegateForeign, IAsyncAdapterForeign,
IPermissionServiceForeign, IPlayerDelegateForeign, IRouterServiceForeign,
IToastServiceForeign, IViewStateServiceForeign, PermissionServiceDelegate, PlayerDelegate,
RouterServiceDelegate, ToastServiceDelegate, ViewStateServiceDelegate,
},
inst::{BACKEND, CLIENTS, RT},
};

static BACKEND: Lazy<Arc<BackendHost>> = Lazy::new(|| BackendHost::new());
static CLIENTS: Lazy<AppPods> = Lazy::new(|| AppPods::new());
static RT: Lazy<Runtime> = Lazy::new(|| {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(4)
.enable_all()
.build()
.unwrap()
});

pub struct AsyncAdapterDelegate {
inner: Arc<dyn IAsyncAdapterForeign>,
app_id: Option<u64>,
Expand Down Expand Up @@ -202,7 +194,7 @@ pub fn api_flush_backend_spawned_local() {
pub async fn api_load_asset(key: DataSourceKey) -> Option<Vec<u8>> {
RT.spawn(async move {
if let Some(backend) = BACKEND.try_backend() {
let file = backend.load_asset(key).await;
let file = backend.asset_server().load(key).await;
if let Ok(Some(file)) = file {
return file.bytes().await.ok().map(|v| v.to_vec());
}
Expand All @@ -213,6 +205,28 @@ pub async fn api_load_asset(key: DataSourceKey) -> Option<Vec<u8>> {
.unwrap()
}

#[uniffi::export]
pub fn api_open_asset(
key: DataSourceKey,
offset: u64,
listener: Arc<dyn IAssetLoadDelegateForeign>,
) -> u64 {
let _guard = RT.enter();
let backend = BACKEND.backend();
backend
.asset_server()
.open(key, offset as usize, AssetLoadDelegate::new(listener))
}

#[uniffi::export]
pub fn api_close_asset(id: u64) {
let _guard = RT.enter();
let backend = BACKEND.try_backend();
if let Some(backend) = backend {
backend.asset_server().close(id);
}
}

#[uniffi::export]
pub fn api_build_client(
permission: Arc<dyn IPermissionServiceForeign>,
Expand Down
3 changes: 0 additions & 3 deletions rust-libs/ease-client-android/src/backend_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ impl IConnectorHost for BackendHost {
})
}

fn port(&self) -> u16 {
self.backend().port()
}
fn storage_path(&self) -> String {
self.backend().storage_path()
}
Expand Down
Loading

0 comments on commit 864c61c

Please sign in to comment.