Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add latency metrics for video playback #89

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class TimelineViewModel @Inject constructor(
// Width/Height ratio of the current media item, used to properly size the Surface
var videoRatio by mutableStateOf<Float?>(null)

var timeAtPlayerPrepare: Long = 0

private val videoSizeListener = object : Player.Listener {
override fun onVideoSizeChanged(videoSize: VideoSize) {
videoRatio = if (videoSize.height > 0 && videoSize.width > 0) {
Expand All @@ -63,6 +65,15 @@ class TimelineViewModel @Inject constructor(
}
}

// Used to track performance of preload manager
private val firstFrameRenderedListener = object : Player.Listener {
override fun onRenderedFirstFrame() {
super.onRenderedFirstFrame()
val timeToFirstRenderFrame = System.currentTimeMillis() - timeAtPlayerPrepare
// Use this value in future to compare performance with and without preload manager
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be useful to add a log statement here to print out this value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. Added a log statement

}
}

init {
viewModelScope.launch {
val allChats = repository.getChats().first()
Expand Down Expand Up @@ -105,6 +116,7 @@ class TimelineViewModel @Inject constructor(
it.repeatMode = ExoPlayer.REPEAT_MODE_ONE
it.playWhenReady = true
it.addListener(videoSizeListener)
it.addListener(firstFrameRenderedListener)
}

videoRatio = null
Expand All @@ -129,6 +141,7 @@ class TimelineViewModel @Inject constructor(
videoRatio = null
if (uri != null) {
setMediaItem(MediaItem.fromUri(uri))
timeAtPlayerPrepare = System.currentTimeMillis()
prepare()
} else {
clearMediaItems()
Expand Down
Loading