[M120 Migration][MM][CAPI] Fix the logic for media using capi player.
[platform/framework/web/chromium-efl.git] / media / mojo / services / video_decode_stats_recorder.cc
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.
4
5 #include "media/mojo/services/video_decode_stats_recorder.h"
6
7 #include "base/functional/callback.h"
8 #include "base/memory/ptr_util.h"
9
10 #include "base/logging.h"
11
12 namespace media {
13
14 VideoDecodeStatsRecorder::VideoDecodeStatsRecorder(
15     VideoDecodePerfHistory::SaveCallback save_cb,
16     ukm::SourceId source_id,
17     learning::FeatureValue origin,
18     bool is_top_frame,
19     uint64_t player_id)
20     : save_cb_(std::move(save_cb)),
21       source_id_(source_id),
22       origin_(origin),
23       is_top_frame_(is_top_frame),
24       player_id_(player_id) {
25   DCHECK(save_cb_);
26 }
27
28 VideoDecodeStatsRecorder::~VideoDecodeStatsRecorder() {
29   DVLOG(2) << __func__ << " Finalize for IPC disconnect";
30   FinalizeRecord();
31 }
32
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);
38
39   // DO THIS FIRST! Finalize existing stats with the current state.
40   FinalizeRecord();
41
42   features_ = *features;
43
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;
49
50   // Reinitialize to defaults.
51   targets_ = mojom::PredictionTargets();
52 }
53
54 void VideoDecodeStatsRecorder::UpdateRecord(
55     mojom::PredictionTargetsPtr targets) {
56   DVLOG(3) << __func__ << " decoded:" << targets->frames_decoded
57            << " dropped:" << targets->frames_dropped;
58
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);
67
68   targets_ = *targets;
69 }
70
71 void VideoDecodeStatsRecorder::FinalizeRecord() {
72   if (features_.profile == VIDEO_CODEC_PROFILE_UNKNOWN ||
73       targets_.frames_decoded == 0) {
74     return;
75   }
76
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;
83
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());
88 }
89
90 }  // namespace media