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 @@ -18,6 +18,7 @@ package com.google.android.samples.socialite.ui.home.timeline

import android.content.Context
import android.net.Uri
import android.util.Log
import androidx.annotation.OptIn
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -52,6 +53,9 @@ 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 TAG = TimelineViewModel::class.java.simpleName

private val videoSizeListener = object : Player.Listener {
override fun onVideoSizeChanged(videoSize: VideoSize) {
videoRatio = if (videoSize.height > 0 && videoSize.width > 0) {
Expand All @@ -63,6 +67,16 @@ 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

Log.d(TAG, "First frame rendered in $timeToFirstRenderFrame ms")
}
}

init {
viewModelScope.launch {
val allChats = repository.getChats().first()
Expand Down Expand Up @@ -105,6 +119,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 +144,7 @@ class TimelineViewModel @Inject constructor(
videoRatio = null
if (uri != null) {
setMediaItem(MediaItem.fromUri(uri))
timeAtPlayerPrepare = System.currentTimeMillis()
prepare()
} else {
clearMediaItems()
Expand Down
Loading