Skip to content

Commit

Permalink
Make loading slightly faster & add 128kbps option
Browse files Browse the repository at this point in the history
  • Loading branch information
LuftVerbot committed May 29, 2024
1 parent 16d58df commit 818bd34
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
12 changes: 8 additions & 4 deletions app/src/main/java/dev/brahmkshatriya/echo/extension/DeezerApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class DeezerApi {
jObject
}

suspend fun getMediaUrl(track: Track, useFlac: Boolean): JsonObject = withContext(Dispatchers.IO) {
suspend fun getMediaUrl(track: Track, useFlac: Boolean, use128: Boolean): JsonObject = withContext(Dispatchers.IO) {
val urlBuilder = HttpUrl.Builder()
.scheme("https")
.host("dzmedia.fly.dev")
Expand All @@ -325,10 +325,14 @@ class DeezerApi {

// Create request body
val requestBody = JSONObject(mapOf(
if(useFlac) {
"formats" to arrayOf("FLAC", "MP3_320", "MP3_128", "MP3_64", "MP3_MISC")
if(use128) {
"formats" to arrayOf("MP3_128", "MP3_64", "MP3_MISC")
} else {
"formats" to arrayOf("MP3_320", "MP3_128", "MP3_64", "MP3_MISC")
if (useFlac) {
"formats" to arrayOf("FLAC", "MP3_320", "MP3_128", "MP3_64", "MP3_MISC")
} else {
"formats" to arrayOf("MP3_320", "MP3_128", "MP3_64", "MP3_MISC")
}
},
"ids" to arrayOf(track.id.toLong())
)).toString().toRequestBody("application/json; charset=utf-8".toMediaType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class DeezerExtension : ExtensionClient, HomeFeedClient, TrackClient, SearchClie
"flac",
"Use FLAC if available (loading takes longer)",
false
),
SettingSwitch(
"Use 128kbps",
"128",
"Use 128kbps(overrides FLAC option)",
false
)
)

Expand All @@ -63,6 +69,9 @@ class DeezerExtension : ExtensionClient, HomeFeedClient, TrackClient, SearchClie
private val useFlac
get() = settings.getBoolean("flac") ?: false

private val use128
get() = settings.getBoolean("128") ?: false

private val arl: String
get() = DeezerCredentials.arl

Expand Down Expand Up @@ -371,7 +380,7 @@ class DeezerExtension : ExtensionClient, HomeFeedClient, TrackClient, SearchClie
val jsonObject = if (track.extras["FILESIZE_MP3_MISC"] != "0" && track.extras["FILESIZE_MP3_MISC"] != null) {
DeezerApi().getMP3MediaUrl(track)
} else {
DeezerApi().getMediaUrl(track, useFlac)
DeezerApi().getMediaUrl(track, useFlac, use128)
}
val key = Utils.createBlowfishKey(trackId = track.id)

Expand Down
32 changes: 20 additions & 12 deletions app/src/main/java/dev/brahmkshatriya/echo/extension/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import java.util.Arrays
import javax.crypto.Cipher
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec
import kotlin.math.min

object Utils {
private const val SECRET = "g4el58wc0zvf9na1"
Expand Down Expand Up @@ -94,17 +95,17 @@ fun getByteStreamAudio(streamable: Streamable, client: OkHttpClient): Streamable
println("Total bytes read: ${completeStreamBytes.size}")

// Determine chunk size based on decryption block size
val decryptionBlockSize = 2048 * 3072 // Increased decryption block size
val numChunks = (completeStreamBytes.size + decryptionBlockSize - 1) / decryptionBlockSize
val chunkSize = 2048 * 3072
val numChunks = (completeStreamBytes.size + chunkSize - 1) / chunkSize
println("Number of chunks: $numChunks")

// Measure decryption time
val startTime = System.nanoTime()

// Decrypt the chunks concurrently
val deferredChunks = (0 until numChunks).map { i ->
val start = i * decryptionBlockSize
val end = minOf((i + 1) * decryptionBlockSize, completeStreamBytes.size)
val start = i * chunkSize
val end = minOf((i + 1) * chunkSize, completeStreamBytes.size)
println("Chunk $i: start $start, end $end")
async(Dispatchers.Default) { decryptStreamChunk(completeStreamBytes.copyOfRange(start, end), key) }
}
Expand Down Expand Up @@ -134,16 +135,23 @@ private fun decryptStreamChunk(chunk: ByteArray, key: String): ByteArray {

while (place < chunk.size) {
val remainingBytes = chunk.size - place
val currentChunkSize = if (remainingBytes > 2048 * 3) 2048 * 3 else remainingBytes
val decryptingChunk = chunk.copyOfRange(place, place + currentChunkSize)
val blockSize = 2048
val encryptedBlock = blockSize * 3 // Every third block is encrypted

val currentChunkSize = min(remainingBytes, encryptedBlock)
val currentChunk = chunk.copyOfRange(place, place + currentChunkSize)
place += currentChunkSize

if (decryptingChunk.size > 2048) {
val decryptedChunk = Utils.decryptBlowfish(decryptingChunk.copyOfRange(0, 2048), key)
decryptedStream.write(decryptedChunk)
decryptedStream.write(decryptingChunk, 2048, decryptingChunk.size - 2048)
} else {
decryptedStream.write(decryptingChunk)
for (i in 0 until currentChunk.size step blockSize) {
val blockEnd = min(currentChunk.size, i + blockSize)
val block = currentChunk.copyOfRange(i, blockEnd)

if ((i / blockSize) % 3 == 0 && block.size == blockSize) {
val decryptedBlock = Utils.decryptBlowfish(block, key)
decryptedStream.write(decryptedBlock)
} else {
decryptedStream.write(block)
}
}
}

Expand Down

0 comments on commit 818bd34

Please sign in to comment.