Skip to content

Commit

Permalink
[telemetry] Add telemetry to SbPlayerWriteSamples
Browse files Browse the repository at this point in the history
Adding additional Media Telemetry to cover SbPlayerWriteSamples.

The rough range of each function:
1. When batched write is enabled (Media.MaxAudioSamplesPerWrite=256):
  1. SbPlayerWriteSamples()/audio: 1us to 98us
  2. SbPlayerWriteSamples()/video: 1us to 3.4ms
2. When batched write is disabled (Media.MaxAudioSamplesPerWrite=1):
  1. SbPlayerWriteSamples()/audio: 9us to 65.47ms
  2. SbPlayerWriteSamples()/video: 8us to 28.07ms

b/303328238
  • Loading branch information
borongc committed Sep 9, 2024
1 parent 727a2b7 commit 75d2d7c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
27 changes: 27 additions & 0 deletions cobalt/media/base/metrics_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ void MediaMetricsProvider::EndTrackingAction(MediaAction action) {
tracked_actions_start_times_.erase(action);
}

void MediaMetricsProvider::EndTrackingAction(MediaAction action,
int number_of_samples) {
DCHECK(IsActionCurrentlyTracked(action));
base::AutoLock scoped_lock(mutex_);

auto duration = clock_->NowTicks() - tracked_actions_start_times_[action];
auto duration_per_sample = duration / number_of_samples;
ReportActionLatencyUMA(action, duration_per_sample);
tracked_actions_start_times_.erase(action);
}

bool MediaMetricsProvider::IsActionCurrentlyTracked(MediaAction action) {
base::AutoLock scoped_lock(mutex_);
return tracked_actions_start_times_.find(action) !=
Expand Down Expand Up @@ -151,6 +162,22 @@ void MediaMetricsProvider::ReportActionLatencyUMA(
UMA_HISTOGRAM_TIMES("Cobalt.Media.SbPlayer.Destroy.LatencyTiming",
action_duration);
break;
case MediaAction::SBPLAYER_WRITE_SAMPLES_AUDIO:
// TODO(borongchen): clean up logs
// LOG(ERROR) << "Brown SBPLAYER_WRITE_SAMPLES_AUDIO " << action_duration;
UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"Cobalt.Media.SbPlayer.WriteSamples.Audio.LatencyTiming",
action_duration, base::TimeDelta::FromMicroseconds(1),
base::TimeDelta::FromMilliseconds(20), 50);
break;
case MediaAction::SBPLAYER_WRITE_SAMPLES_VIDEO:
// TODO(borongchen): clean up logs
// LOG(ERROR) << "Brown SBPLAYER_WRITE_SAMPLES_VIDEO " << action_duration;
UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"Cobalt.Media.SbPlayer.WriteSamples.Video.LatencyTiming",
action_duration, base::TimeDelta::FromMicroseconds(1),
base::TimeDelta::FromMilliseconds(20), 50);
break;
case MediaAction::SBDRM_DESTROY:
UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"Cobalt.Media.SbDrm.Destroy.LatencyTiming", action_duration,
Expand Down
3 changes: 3 additions & 0 deletions cobalt/media/base/metrics_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ enum class MediaAction : uint8_t {
SBPLAYER_CREATE,
SBPLAYER_CREATE_URL_PLAYER,
SBPLAYER_DESTROY,
SBPLAYER_WRITE_SAMPLES_AUDIO,
SBPLAYER_WRITE_SAMPLES_VIDEO,
SBDRM_DESTROY,
SBDRM_GENERATE_SESSION_UPDATE_REQUEST,
SBDRM_UPDATE_SESSION,
Expand Down Expand Up @@ -87,6 +89,7 @@ class MediaMetricsProvider {
// Used to record the latency of an action in the WebMediaPlayer.
void StartTrackingAction(MediaAction action);
void EndTrackingAction(MediaAction action);
void EndTrackingAction(MediaAction action, int number_of_samples);
bool IsActionCurrentlyTracked(MediaAction action);

void Reset();
Expand Down
22 changes: 22 additions & 0 deletions cobalt/media/base/metrics_provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ TEST_F(MediaMetricsProviderTest, SbPlayerDestroy) {
std::string(kUmaPrefix) + "SbPlayer.Destroy.LatencyTiming", 100, 1);
}

TEST_F(MediaMetricsProviderTest, SbPlayerWriteSamplesAudio) {
metrics_.StartTrackingAction(MediaAction::SBPLAYER_WRITE_SAMPLES_AUDIO);

clock_.Advance(base::TimeDelta::FromMicroseconds(570));
metrics_.EndTrackingAction(MediaAction::SBPLAYER_WRITE_SAMPLES_AUDIO);

histogram_tester_.ExpectUniqueSample(
std::string(kUmaPrefix) + "SbPlayer.WriteSamples.Audio.LatencyTiming",
570, 1);
}

TEST_F(MediaMetricsProviderTest, SbPlayerWriteSamplesVideoAudio) {
metrics_.StartTrackingAction(MediaAction::SBPLAYER_WRITE_SAMPLES_VIDEO);

clock_.Advance(base::TimeDelta::FromMicroseconds(570));
metrics_.EndTrackingAction(MediaAction::SBPLAYER_WRITE_SAMPLES_VIDEO);

histogram_tester_.ExpectUniqueSample(
std::string(kUmaPrefix) + "SbPlayer.WriteSamples.Video.LatencyTiming",
570, 1);
}

TEST_F(MediaMetricsProviderTest, SbDrmCloseSession) {
metrics_.StartTrackingAction(MediaAction::SBDRM_CLOSE_SESSION);

Expand Down
6 changes: 6 additions & 0 deletions cobalt/media/base/sbplayer_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,19 @@ void DefaultSbPlayerInterface::WriteSamples(
SbPlayer player, SbMediaType sample_type,
const SbPlayerSampleInfo* sample_infos, int number_of_sample_infos) {
DCHECK(!IsEnhancedAudioExtensionEnabled());
auto media_action = (sample_type == kSbMediaTypeAudio)
? MediaAction::SBPLAYER_WRITE_SAMPLES_AUDIO
: MediaAction::SBPLAYER_WRITE_SAMPLES_VIDEO;
media_metrics_provider_.StartTrackingAction(media_action);
#if SB_API_VERSION >= 15
SbPlayerWriteSamples(player, sample_type, sample_infos,
number_of_sample_infos);
#else // SB_API_VERSION >= 15
SbPlayerWriteSample2(player, sample_type, sample_infos,
number_of_sample_infos);
#endif // SB_API_VERSION >= 15
media_metrics_provider_.EndTrackingAction(media_action,
number_of_sample_infos);
}

void DefaultSbPlayerInterface::WriteSamples(
Expand Down
18 changes: 18 additions & 0 deletions tools/metrics/histograms/metadata/cobalt/histograms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,24 @@ Always run the pretty print utility on this file after editing:
<summary>Timing data for the destruction of SbPlayer.</summary>
</histogram>

<histogram name="Cobalt.Media.SbPlayer.WriteSamples.Audio.LatencyTiming"
units="microseconds" expires_after="never">
<!-- expires-never: Needed for long-term tracking of SbPlayer latencies. -->

<owner>[email protected]</owner>
<owner>[email protected]</owner>
<summary>Timing data for writing audio samples.</summary>
</histogram>

<histogram name="Cobalt.Media.SbPlayer.WriteSamples.Video.LatencyTiming"
units="microseconds" expires_after="never">
<!-- expires-never: Needed for long-term tracking of SbPlayer latencies. -->

<owner>[email protected]</owner>
<owner>[email protected]</owner>
<summary>Timing data for writing video samples.</summary>
</histogram>

<histogram name="Cobalt.Media.SDR.Matrix" enum="MatrixID" expires_after="never">
<!-- expires-never: Needed for baseline Media pipeline health metric. -->

Expand Down

0 comments on commit 75d2d7c

Please sign in to comment.