1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/mojo/services/video_decode_stats_recorder.h"
7 #include "base/functional/callback.h"
8 #include "base/memory/ptr_util.h"
10 #include "base/logging.h"
14 VideoDecodeStatsRecorder::VideoDecodeStatsRecorder(
15 VideoDecodePerfHistory::SaveCallback save_cb,
16 ukm::SourceId source_id,
17 learning::FeatureValue origin,
20 : save_cb_(std::move(save_cb)),
21 source_id_(source_id),
23 is_top_frame_(is_top_frame),
24 player_id_(player_id) {
28 VideoDecodeStatsRecorder::~VideoDecodeStatsRecorder() {
29 DVLOG(2) << __func__ << " Finalize for IPC disconnect";
33 void VideoDecodeStatsRecorder::StartNewRecord(
34 mojom::PredictionFeaturesPtr features) {
35 DCHECK_NE(features->profile, VIDEO_CODEC_PROFILE_UNKNOWN);
36 DCHECK_GT(features->frames_per_sec, 0);
37 DCHECK(features->video_size.width() > 0 && features->video_size.height() > 0);
39 // DO THIS FIRST! Finalize existing stats with the current state.
42 features_ = *features;
44 DVLOG(2) << __func__ << "profile: " << features_.profile
45 << " sz:" << features_.video_size.ToString()
46 << " fps:" << features_.frames_per_sec
47 << " key_system:" << features_.key_system
48 << " use_hw_secure_codecs:" << features_.use_hw_secure_codecs;
50 // Reinitialize to defaults.
51 targets_ = mojom::PredictionTargets();
54 void VideoDecodeStatsRecorder::UpdateRecord(
55 mojom::PredictionTargetsPtr targets) {
56 DVLOG(3) << __func__ << " decoded:" << targets->frames_decoded
57 << " dropped:" << targets->frames_dropped;
59 // Dropped can never exceed decoded.
60 DCHECK_LE(targets->frames_dropped, targets->frames_decoded);
61 // Power efficient frames can never exceed decoded frames.
62 DCHECK_LE(targets->frames_power_efficient, targets->frames_decoded);
63 // Should never go backwards.
64 DCHECK_GE(targets->frames_decoded, targets_.frames_decoded);
65 DCHECK_GE(targets->frames_dropped, targets_.frames_dropped);
66 DCHECK_GE(targets->frames_power_efficient, targets_.frames_power_efficient);
71 void VideoDecodeStatsRecorder::FinalizeRecord() {
72 if (features_.profile == VIDEO_CODEC_PROFILE_UNKNOWN ||
73 targets_.frames_decoded == 0) {
77 DVLOG(2) << __func__ << " profile: " << features_.profile
78 << " size:" << features_.video_size.ToString()
79 << " fps:" << features_.frames_per_sec
80 << " decoded:" << targets_.frames_decoded
81 << " dropped:" << targets_.frames_dropped
82 << " power efficient decoded:" << targets_.frames_power_efficient;
84 // Final argument is an empty save-done-callback. No action to take if save
85 // fails (DB already records UMAs on failure). Callback mainly used by tests.
86 save_cb_.Run(source_id_, origin_, is_top_frame_, features_, targets_,
87 player_id_, base::OnceClosure());