Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ fun String.fixHtmlWhitespace(): Spanned {
}

fun Long.formatDuration(): String {
// Negative durations show up for live streams seeked behind the seek-window start, or
// briefly while the player is reseating. Recurse on the absolute value so we get a clean
// `-MM:SS` instead of garbage like `00:-49`.
if (this < 0) return "-" + (-this).formatDuration()

val hours = this / 3600000
val minutes = (this % 3600000) / 60000
val seconds = (this % 60000) / 1000
Expand Down
137 changes: 120 additions & 17 deletions app/src/main/java/com/futo/platformplayer/views/video/FutoVideoPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.ImageButton
import android.widget.TextView
import androidx.annotation.OptIn
Expand Down Expand Up @@ -119,6 +120,15 @@ class FutoVideoPlayer : FutoVideoPlayerBase {
private val _control_duration_fullscreen: TextView;
private val _control_pause_fullscreen: ImageButton;

// LIVE pill: shown only when current media item is live; dot color reflects live-edge proximity.
private val _live_pill: LinearLayout
private val _live_pill_dot: View
private val _live_pill_fullscreen: LinearLayout
private val _live_pill_dot_fullscreen: View
private val _text_divider: TextView
private val _text_divider_fullscreen: TextView
private var _wasAtLiveEdge: Boolean = true

private val _title_fullscreen: TextView;
private val _author_fullscreen: TextView;
private var _shouldRestartHideJobOnPlaybackStateChange: Boolean = false;
Expand Down Expand Up @@ -189,6 +199,9 @@ class FutoVideoPlayer : FutoVideoPlayerBase {
_buttonPrevious = videoControls.findViewById(R.id.button_previous);
_control_time = videoControls.findViewById(R.id.text_position);
_control_duration = videoControls.findViewById(R.id.text_duration);
_live_pill = videoControls.findViewById(R.id.live_pill_container)
_live_pill_dot = videoControls.findViewById(R.id.live_pill_dot)
_text_divider = videoControls.findViewById(R.id.text_divider)

_videoControls_fullscreen = findViewById(R.id.video_player_controller_fullscreen);
_control_autoplay_fullscreen = _videoControls_fullscreen.findViewById(R.id.button_autoplay);
Expand All @@ -206,6 +219,9 @@ class FutoVideoPlayer : FutoVideoPlayerBase {
_control_time_fullscreen = _videoControls_fullscreen.findViewById(R.id.text_position);
_control_duration_fullscreen = _videoControls_fullscreen.findViewById(R.id.text_duration);
_control_pause_fullscreen = _videoControls_fullscreen.findViewById(R.id.button_pause);
_live_pill_fullscreen = _videoControls_fullscreen.findViewById(R.id.live_pill_container)
_live_pill_dot_fullscreen = _videoControls_fullscreen.findViewById(R.id.live_pill_dot)
_text_divider_fullscreen = _videoControls_fullscreen.findViewById(R.id.text_divider)

_loaderGame = findViewById(R.id.loader_overlay)
_loaderGame.visibility = View.GONE
Expand All @@ -225,24 +241,26 @@ class FutoVideoPlayer : FutoVideoPlayerBase {
_buttonNext.setOnClickListener { onNext.emit() };
_buttonPrevious_fullscreen.setOnClickListener { onPrevious.emit() };
_buttonNext_fullscreen.setOnClickListener { onNext.emit() };
_control_play.setOnClickListener {
exoPlayer?.player?.let {
if (it.contentPosition >= it.duration) {
it.seekTo(0)
}
exoPlayer?.player?.play();
}
updatePlayPause();
};
_control_play_fullscreen.setOnClickListener {
exoPlayer?.player?.let {
if (it.contentPosition >= it.duration) {
it.seekTo(0)
val playClickHandler = View.OnClickListener {
// Order matters:
// 1. If the player is stuck (STATE_IDLE after error, STATE_ENDED on a slipped live
// window) plain play() is a no-op until we re-prepare. Recover first.
// 2. Otherwise, if a VOD has played to its end, rewind to start (replay).
// 3. Then start playback.
val recovered = recoverFromStuck()
if (!recovered) {
exoPlayer?.player?.let {
val dur = it.duration
if (dur > 0 && it.contentPosition >= dur) {
it.seekTo(0)
}
it.play()
}
exoPlayer?.player?.play();
}
updatePlayPause();
};
updatePlayPause()
}
_control_play.setOnClickListener(playClickHandler)
_control_play_fullscreen.setOnClickListener(playClickHandler)
_control_pause.setOnClickListener {
exoPlayer?.player?.pause();
updatePlayPause();
Expand Down Expand Up @@ -460,7 +478,17 @@ class FutoVideoPlayer : FutoVideoPlayerBase {
updateAutoplayButton()

val progressUpdateListener = { position: Long, bufferedPosition: Long ->
val currentTime = position.formatDuration()
// For live streams that have been seeked behind, replace the running position with
// a -MM:SS "behind live" indicator (the videojs/HLS convention). At the live edge
// we keep showing the running position; this matches YouTube's web behaviour where
// the LIVE pill alone (red "caught up" / gray "behind") + a clear offset readout
// tell the whole story.
val behindMs = if (isLive) behindLiveMs else null
val currentTime = if (behindMs != null && behindMs > 0) {
"-" + behindMs.formatDuration()
} else {
position.formatDuration()
}
val currentDuration = duration.formatDuration()
_control_time.text = currentTime;
_control_time_fullscreen.text = currentTime;
Expand All @@ -473,6 +501,12 @@ class FutoVideoPlayer : FutoVideoPlayerBase {
_time_bar_fullscreen.setBufferedPosition(bufferedPosition);
_time_bar.setBufferedPosition(bufferedPosition);

// While live, refresh the LIVE pill's edge state so the dot reflects whether the user
// is at the live edge or seeked behind. Cheap and only updates when state actually changes.
if (isLive) {
updateLiveEdgeState()
}

onTimeBarChanged.emit(position, bufferedPosition);

if(!_currentChapterLoopActive)
Expand All @@ -499,6 +533,23 @@ class FutoVideoPlayer : FutoVideoPlayerBase {
}
}

// Toggle LIVE pill / time UI when the underlying media item changes liveness.
// The base class emits this on Timeline / MediaItem transitions.
onLiveChanged.subscribe { live ->
CoroutineScope(Dispatchers.Main).launch(Dispatchers.Main) {
applyLiveUI(live)
}
}

val jumpToLiveListener = View.OnClickListener {
seekToLiveEdge()
}
_live_pill.setOnClickListener(jumpToLiveListener)
_live_pill_fullscreen.setOnClickListener(jumpToLiveListener)

// Apply once at construction in case we attach to an already-live media item.
applyLiveUI(isLive)

updateLoopVideoUI();

if(!isInEditMode) {
Expand Down Expand Up @@ -895,6 +946,58 @@ class FutoVideoPlayer : FutoVideoPlayerBase {
}
}

/**
* Applies (or reverts) live-stream-specific control affordances:
* - shows/hides the LIVE pill
* - hides the duration text + divider when live (duration shown by the pill)
* - hides the loop button (looping a live stream is meaningless)
* - hides the chapter text (live streams from the source plugins do not provide chapters)
*
* Position text is kept visible because for HLS DVR streams it shows offset within the
* available seek window, which is useful information.
*/
private fun applyLiveUI(live: Boolean) {
val pillVis = if (live) View.VISIBLE else View.GONE
val timeVis = if (live) View.GONE else View.VISIBLE
_live_pill.visibility = pillVis
_live_pill_fullscreen.visibility = pillVis
_text_divider.visibility = timeVis
_text_divider_fullscreen.visibility = timeVis
_control_duration.visibility = timeVis
_control_duration_fullscreen.visibility = timeVis

if (live) {
// Loop / chapter UI is meaningless on live; hide and reset.
_control_loop.visibility = View.GONE
_control_loop_fullscreen.visibility = View.GONE
_control_chapter.visibility = View.GONE
_control_chapter_fullscreen.visibility = View.GONE
updateLiveEdgeState()
} else {
_control_loop.visibility = View.VISIBLE
_control_loop_fullscreen.visibility = View.VISIBLE
_control_chapter.visibility = View.VISIBLE
_control_chapter_fullscreen.visibility = View.VISIBLE
}
}

/**
* Updates the LIVE pill's dot + background to reflect whether playback is at the live edge.
* Idempotent: only mutates when state changes to avoid invalidations on every progress tick.
*/
private fun updateLiveEdgeState() {
val atEdge = isAtLiveEdge
if (atEdge == _wasAtLiveEdge) return
_wasAtLiveEdge = atEdge
Logger.i(TAG, "LIVE pill -> ${if (atEdge) "AT EDGE" else "BEHIND"} (offset=${liveOffsetMs}ms target=${targetLiveOffsetMs}ms)")
val bg = if (atEdge) R.drawable.background_live_pill else R.drawable.background_live_pill_behind
val dot = if (atEdge) R.drawable.dot_live_edge else R.drawable.dot_live_behind
_live_pill.setBackgroundResource(bg)
_live_pill_fullscreen.setBackgroundResource(bg)
_live_pill_dot.setBackgroundResource(dot)
_live_pill_dot_fullscreen.setBackgroundResource(dot)
}

fun setGestureSoundFactor(soundFactor: Float) {
gestureControl.setSoundFactor(soundFactor);
}
Expand Down
Loading