-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ktor ByteChannel Support for audio (#28)
- Loading branch information
1 parent
ada193b
commit 319e104
Showing
5 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
app/src/main/java/dev/brahmkshatriya/echo/playback/ByteChannelDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package dev.brahmkshatriya.echo.playback | ||
|
||
import androidx.annotation.OptIn | ||
import androidx.core.net.toUri | ||
import androidx.media3.common.C | ||
import androidx.media3.common.util.UnstableApi | ||
import androidx.media3.datasource.BaseDataSource | ||
import androidx.media3.datasource.DataSource | ||
import androidx.media3.datasource.DataSpec | ||
import dev.brahmkshatriya.echo.common.models.Streamable | ||
import io.ktor.utils.io.ByteReadChannel | ||
import io.ktor.utils.io.cancel | ||
import kotlinx.coroutines.runBlocking | ||
import java.io.IOException | ||
|
||
@OptIn(UnstableApi::class) | ||
class ByteChannelDataSource : BaseDataSource(true) { | ||
|
||
class Factory : DataSource.Factory { | ||
override fun createDataSource() = ByteChannelDataSource() | ||
} | ||
|
||
private var audio: Streamable.Audio.Channel? = null | ||
|
||
override fun read(buffer: ByteArray, offset: Int, length: Int): Int { | ||
val channel = audio!!.channel | ||
return runBlocking { | ||
val bytesRead = channel.readAvailable(buffer, offset, length) | ||
if (bytesRead == -1) C.RESULT_END_OF_INPUT else bytesRead | ||
} | ||
} | ||
|
||
override fun open(dataSpec: DataSpec): Long { | ||
val audio = dataSpec.customData as Streamable.Audio.Channel | ||
val requestedPosition = dataSpec.position | ||
runBlocking { | ||
audio.channel.seek(requestedPosition) | ||
} | ||
this.audio = audio | ||
return audio.totalBytes | ||
} | ||
|
||
override fun getUri() = audio?.hashCode().toString().toUri() | ||
|
||
override fun close() { | ||
runBlocking { | ||
audio?.channel?.cancel() | ||
} | ||
audio = null | ||
} | ||
|
||
private suspend fun ByteReadChannel.seek(requestedPosition: Long) { | ||
val discarded = discard(requestedPosition) | ||
if (discarded < requestedPosition) { | ||
throw IOException("Reached end of stream before desired position") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters