Skip to content

Commit

Permalink
Remove prepare buffer from SeekableInput
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Dec 16, 2024
1 parent 7fb2778 commit 878f81f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public abstract class BufferedSeekableInput(
check(read == length) { "readFileToBufferExact: Expected to read $length bytes, but read $read bytes, fileOffset=$fileOffset" }
}

override fun prepareBuffer() {
internal fun prepareBuffer() {
checkClosed()
fillBuffer()
}
Expand Down
2 changes: 0 additions & 2 deletions mediamp-api/src/commonMain/kotlin/io/SeekableInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ public interface SeekableInput : AutoCloseable {
position: @Range(from = 0L, to = Long.MAX_VALUE) Long,
)

public fun prepareBuffer() {}

/**
* Reads up to [length] bytes from the input source into [buffer] starting at [offset].
*
Expand Down
10 changes: 5 additions & 5 deletions mediamp-core/src/androidMain/kotlin/PlayerState.android.kt
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ internal class ExoPlayerState @UiThread constructor(
addListener(
object : Player.Listener {
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
state.value = PlaybackState.READY
playbackState.value = PlaybackState.READY
isBuffering.value = false
}

Expand Down Expand Up @@ -261,7 +261,7 @@ internal class ExoPlayerState @UiThread constructor(
}

override fun onPlayerError(error: PlaybackException) {
state.value = PlaybackState.ERROR
playbackState.value = PlaybackState.ERROR
println("ExoPlayer error: ${error.errorCodeName}") // TODO: 2024/12/16 error handling
error.printStackTrace()
}
Expand Down Expand Up @@ -305,7 +305,7 @@ internal class ExoPlayerState @UiThread constructor(
}

Player.STATE_ENDED -> {
state.value = PlaybackState.FINISHED
playbackState.value = PlaybackState.FINISHED
isBuffering.value = false
}

Expand All @@ -318,10 +318,10 @@ internal class ExoPlayerState @UiThread constructor(

override fun onIsPlayingChanged(isPlaying: Boolean) {
if (isPlaying) {
state.value = PlaybackState.PLAYING
playbackState.value = PlaybackState.PLAYING
isBuffering.value = false
} else {
state.value = PlaybackState.PAUSED
playbackState.value = PlaybackState.PAUSED
}
}

Expand Down
16 changes: 8 additions & 8 deletions mediamp-core/src/commonMain/kotlin/core/state/PlayerState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ interface PlayerState {
*
* State can be changed internally e.g. buffer exhausted or externally by e.g. [pause], [resume].
*/
val state: StateFlow<PlaybackState>
val playbackState: StateFlow<PlaybackState>

/**
* The video source that is currently being played.
Expand Down Expand Up @@ -188,7 +188,7 @@ interface PlayerState {
}

fun PlayerState.togglePause() {
if (state.value.isPlaying) {
if (playbackState.value.isPlaying) {
pause()
} else {
resume()
Expand Down Expand Up @@ -227,7 +227,7 @@ abstract class AbstractPlayerState<D : AbstractPlayerState.Data>(

override val videoSource: MutableStateFlow<VideoSource<*>?> = MutableStateFlow(null)

override val state: MutableStateFlow<PlaybackState> = MutableStateFlow(PlaybackState.PAUSED_BUFFERING)
override val playbackState: MutableStateFlow<PlaybackState> = MutableStateFlow(PlaybackState.PAUSED_BUFFERING)

/**
* Currently playing resource that should be closed when the controller is closed.
Expand All @@ -242,7 +242,7 @@ abstract class AbstractPlayerState<D : AbstractPlayerState.Data>(
)

override val isBuffering: Flow<Boolean> by lazy {
state.map { it == PlaybackState.PAUSED_BUFFERING }
playbackState.map { it == PlaybackState.PAUSED_BUFFERING }
}

final override val videoData: Flow<VideoData?> = openResource.map {
Expand Down Expand Up @@ -308,7 +308,7 @@ abstract class AbstractPlayerState<D : AbstractPlayerState.Data>(
}

try {
state.value = PlaybackState.PAUSED_BUFFERING
playbackState.value = PlaybackState.PAUSED_BUFFERING
startPlayer(opened)
} catch (e: CancellationException) {
opened.releaseResource()
Expand Down Expand Up @@ -398,7 +398,7 @@ enum class PlaybackState(
class DummyPlayerState(
parentCoroutineContext: CoroutineContext = EmptyCoroutineContext,
) : AbstractPlayerState<AbstractPlayerState.Data>(parentCoroutineContext) {
override val state: MutableStateFlow<PlaybackState> = MutableStateFlow(PlaybackState.PLAYING)
override val playbackState: MutableStateFlow<PlaybackState> = MutableStateFlow(PlaybackState.PLAYING)
override fun stopImpl() {

}
Expand Down Expand Up @@ -448,11 +448,11 @@ class DummyPlayerState(
override val bufferedPercentage: StateFlow<Int> = MutableStateFlow(50)

override fun pause() {
state.value = PlaybackState.PAUSED
playbackState.value = PlaybackState.PAUSED
}

override fun resume() {
state.value = PlaybackState.PLAYING
playbackState.value = PlaybackState.PLAYING
}

override val playbackSpeed: MutableStateFlow<Float> = MutableStateFlow(1.0f)
Expand Down
22 changes: 11 additions & 11 deletions mediamp-core/src/desktopMain/kotlin/core/VideoPlayer.desktop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ class VlcjVideoPlayerState(parentCoroutineContext: CoroutineContext) : PlayerSta
attach(player)
}

override val state: MutableStateFlow<PlaybackState> = MutableStateFlow(PlaybackState.PAUSED_BUFFERING)
override val playbackState: MutableStateFlow<PlaybackState> = MutableStateFlow(PlaybackState.PAUSED_BUFFERING)

init {
backgroundScope.launch {
state.collect {
playbackState.collect {
surface.enableRendering.value = it == PlaybackState.PLAYING
}
}
Expand Down Expand Up @@ -299,7 +299,7 @@ class VlcjVideoPlayerState(parentCoroutineContext: CoroutineContext) : PlayerSta
createVideoProperties()?.let {
videoProperties.value = it
}
state.value = PlaybackState.READY
playbackState.value = PlaybackState.READY
}
}
},
Expand Down Expand Up @@ -351,7 +351,7 @@ class VlcjVideoPlayerState(parentCoroutineContext: CoroutineContext) : PlayerSta
}

override fun playing(mediaPlayer: MediaPlayer) {
state.value = PlaybackState.PLAYING
playbackState.value = PlaybackState.PLAYING
player.submit { player.media().parsing().parse() }

reloadSubtitleTracks()
Expand All @@ -360,16 +360,16 @@ class VlcjVideoPlayerState(parentCoroutineContext: CoroutineContext) : PlayerSta
}

override fun paused(mediaPlayer: MediaPlayer) {
state.value = PlaybackState.PAUSED
playbackState.value = PlaybackState.PAUSED
}

override fun finished(mediaPlayer: MediaPlayer) {
state.value = PlaybackState.FINISHED
playbackState.value = PlaybackState.FINISHED
}

override fun error(mediaPlayer: MediaPlayer) {
logger.error { "vlcj player error" }
state.value = PlaybackState.ERROR
playbackState.value = PlaybackState.ERROR
}

override fun positionChanged(mediaPlayer: MediaPlayer?, newPosition: Float) {
Expand All @@ -385,7 +385,7 @@ class VlcjVideoPlayerState(parentCoroutineContext: CoroutineContext) : PlayerSta
var lastPosition = currentPositionMillis.value
while (true) {
delay(1500)
if (state.value == PlaybackState.PLAYING) {
if (playbackState.value == PlaybackState.PLAYING) {
isBuffering.value = lastPosition == currentPositionMillis.value
lastPosition = currentPositionMillis.value
}
Expand All @@ -395,7 +395,7 @@ class VlcjVideoPlayerState(parentCoroutineContext: CoroutineContext) : PlayerSta
backgroundScope.launch {
subtitleTracks.current.collect { track ->
try {
if (state.value == PlaybackState.READY) {
if (playbackState.value == PlaybackState.READY) {
return@collect
}
if (track == null) {
Expand Down Expand Up @@ -425,7 +425,7 @@ class VlcjVideoPlayerState(parentCoroutineContext: CoroutineContext) : PlayerSta
backgroundScope.launch {
audioTracks.current.collect { track ->
try {
if (state.value == PlaybackState.READY) {
if (playbackState.value == PlaybackState.READY) {
return@collect
}
if (track == null) {
Expand Down Expand Up @@ -535,7 +535,7 @@ class VlcjVideoPlayerState(parentCoroutineContext: CoroutineContext) : PlayerSta
}

override fun skip(deltaMillis: Long) {
if (state.value == PlaybackState.PAUSED) {
if (playbackState.value == PlaybackState.PAUSED) {
// 如果是暂停, 上面 positionChanged 事件不会触发, 所以这里手动更新
// 如果正在播放, 这里不能更新. 否则可能导致进度抖动 1 秒
currentPositionMillis.value = (currentPositionMillis.value + deltaMillis)
Expand Down

0 comments on commit 878f81f

Please sign in to comment.