[M120 Migration][MM][CAPI] Fix the logic for media using capi player.
[platform/framework/web/chromium-efl.git] / media / mojo / services / video_decode_perf_history.h
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 #ifndef MEDIA_MOJO_SERVICES_VIDEO_DECODE_PERF_HISTORY_H_
6 #define MEDIA_MOJO_SERVICES_VIDEO_DECODE_PERF_HISTORY_H_
7
8 #include <stdint.h>
9 #include <memory>
10 #include <queue>
11
12 #include "base/functional/callback.h"
13 #include "base/metrics/field_trial_params.h"
14 #include "base/sequence_checker.h"
15 #include "base/supports_user_data.h"
16 #include "media/base/video_codecs.h"
17 #include "media/capabilities/video_decode_stats_db.h"
18 #include "media/capabilities/video_decode_stats_db_provider.h"
19 #include "media/learning/impl/feature_provider.h"
20 #include "media/mojo/mojom/video_decode_perf_history.mojom.h"
21 #include "media/mojo/services/media_mojo_export.h"
22 #include "mojo/public/cpp/bindings/pending_receiver.h"
23 #include "mojo/public/cpp/bindings/receiver_set.h"
24 #include "services/metrics/public/cpp/ukm_source_id.h"
25 #include "ui/gfx/geometry/size.h"
26
27 namespace media {
28
29 class LearningHelper;
30
31 // This class saves and retrieves video decode performance statistics on behalf
32 // of the MediaCapabilities API. It also helps to grade the accuracy of the API
33 // by comparing its history-based assessment of smoothness/power-efficiency to
34 // the observed performance as new stats are saved.
35 //
36 // The database is lazily initialized/loaded upon the first API call requiring
37 // DB access. DB implementations must take care to perform work on a separate
38 // task runner.
39 //
40 // Retrieving stats is triggered by calls to the GetPerfInfo() Mojo interface.
41 // The raw values are reduced to booleans (is_smooth, is_power_efficient) which
42 // are sent along the Mojo callback.
43 //
44 // Saving stats is performed by SavePerfRecord(), where a record is defined as a
45 // continuous playback of a stream with fixed decode characteristics (profile,
46 // natural size, frame rate).
47 //
48 // THREAD SAFETY:
49 // This class is not thread safe. All API calls should be made on the same
50 // sequence.
51 class MEDIA_MOJO_EXPORT VideoDecodePerfHistory
52     : public mojom::VideoDecodePerfHistory,
53       public VideoDecodeStatsDBProvider,
54       public base::SupportsUserData::Data {
55  public:
56   static const char kMaxSmoothDroppedFramesPercentParamName[];
57   static const char kEmeMaxSmoothDroppedFramesPercentParamName[];
58
59   explicit VideoDecodePerfHistory(
60       std::unique_ptr<VideoDecodeStatsDB> db,
61       learning::FeatureProviderFactoryCB feature_factory_cb =
62           learning::FeatureProviderFactoryCB());
63
64   VideoDecodePerfHistory(const VideoDecodePerfHistory&) = delete;
65   VideoDecodePerfHistory& operator=(const VideoDecodePerfHistory&) = delete;
66
67   ~VideoDecodePerfHistory() override;
68
69   // Bind the mojo receiver to this instance. Single instance will be used to
70   // serve multiple receivers.
71   void BindReceiver(
72       mojo::PendingReceiver<mojom::VideoDecodePerfHistory> receiver);
73
74   // mojom::VideoDecodePerfHistory implementation:
75   void GetPerfInfo(mojom::PredictionFeaturesPtr features,
76                    GetPerfInfoCallback got_info_cb) override;
77
78   // Provides a callback for saving a stats record for the described stream.
79   // This callback will silently fail if called after |this| is destroyed.
80   // Saving is generally fire-and-forget, but |save_done_cb| may be provided
81   // for tests to know the save is complete.
82   using SaveCallback =
83       base::RepeatingCallback<void(ukm::SourceId source_id,
84                                    learning::FeatureValue origin,
85                                    bool is_top_frame,
86                                    mojom::PredictionFeatures features,
87                                    mojom::PredictionTargets targets,
88                                    uint64_t player_id,
89                                    base::OnceClosure save_done_cb)>;
90   SaveCallback GetSaveCallback();
91
92   // Clear all history from the underlying database. Run |clear_done_cb| when
93   // complete.
94   void ClearHistory(base::OnceClosure clear_done_cb);
95
96   // From VideoDecodeStatsDBProvider. |cb| receives a pointer to the
97   // *initialized* VideoDecodeStatsDB, or null in case of error.
98   void GetVideoDecodeStatsDB(GetCB cb) override;
99
100  private:
101   friend class VideoDecodePerfHistoryTest;
102
103   // Decode capabilities will be described as "smooth" whenever the percentage
104   // of dropped frames is less-than-or-equal-to this value.
105   static double GetMaxSmoothDroppedFramesPercent(bool is_eme);
106
107   // Returns current feature params.
108   static base::FieldTrialParams GetFieldTrialParams();
109
110   // Track the status of database lazy initialization.
111   enum InitStatus {
112     UNINITIALIZED,
113     PENDING,
114     COMPLETE,
115     FAILED,
116   };
117
118   // Decode capabilities will be described as "power efficient" whenever the
119   // percentage of power efficient decoded frames is higher-than-or-equal-to
120   // this value.
121   static constexpr double kMinPowerEfficientDecodedFramePercent = .50;
122
123   // Initialize the database. Will return early if initialization is
124   // already PENDING.
125   void InitDatabase();
126
127   // Callback from |db_->Initialize()|.
128   void OnDatabaseInit(bool success);
129
130   // Initiate saving of the provided record. See GetSaveCallback().
131   void SavePerfRecord(ukm::SourceId source_id,
132                       learning::FeatureValue origin,
133                       bool is_top_frame,
134                       mojom::PredictionFeatures features,
135                       mojom::PredictionTargets targets,
136                       uint64_t player_id,
137                       base::OnceClosure save_done_cb);
138
139   // Internal callback for database queries made from GetPerfInfo() (mojo API).
140   // Assesses performance from database stats and passes results to
141   // |got_info_cb|.
142   void OnGotStatsForRequest(
143       const VideoDecodeStatsDB::VideoDescKey& video_key,
144       GetPerfInfoCallback got_info_cb,
145       bool database_success,
146       std::unique_ptr<VideoDecodeStatsDB::DecodeStatsEntry> stats);
147
148   // Internal callback for database queries made from SavePerfRecord(). Compares
149   // past performance to this latest record as means of "grading" the accuracy
150   // of the GetPerfInfo() API. Comparison is recorded via UKM. Then saves the
151   // |new_*| performance stats to the database.
152   void OnGotStatsForSave(
153       ukm::SourceId source_id,
154       bool is_top_frame,
155       uint64_t player_id,
156       const VideoDecodeStatsDB::VideoDescKey& video_key,
157       const VideoDecodeStatsDB::DecodeStatsEntry& new_stats,
158       base::OnceClosure save_done_cb,
159       bool success,
160       std::unique_ptr<VideoDecodeStatsDB::DecodeStatsEntry> past_stats);
161
162   // Internal callback for saving to database. Will run |save_done_cb| if
163   // nonempty.
164   void OnSaveDone(base::OnceClosure save_done_cb, bool success);
165
166   // Report UKM metrics to grade the claims of the API by evaluating how well
167   // |past_stats| predicts |new_stats|.
168   void ReportUkmMetrics(ukm::SourceId source_id,
169                         bool is_top_frame,
170                         uint64_t player_id,
171                         const VideoDecodeStatsDB::VideoDescKey& video_key,
172                         const VideoDecodeStatsDB::DecodeStatsEntry& new_stats,
173                         VideoDecodeStatsDB::DecodeStatsEntry* past_stats);
174
175   void AssessStats(const VideoDecodeStatsDB::VideoDescKey& key,
176                    const VideoDecodeStatsDB::DecodeStatsEntry* stats,
177                    bool* is_smooth,
178                    bool* is_power_efficient);
179
180   // Internal callback for ClearHistory(). Reinitializes the database and runs
181   // |clear_done_cb|.
182   void OnClearedHistory(base::OnceClosure clear_done_cb);
183
184   // Underlying database for managing/coalescing decode stats. Const to enforce
185   // assignment during construction and never cleared. We hand out references to
186   // the db via GetVideoDecodeStatsDB(), so clearing or reassigning breaks those
187   // dependencies.
188   const std::unique_ptr<VideoDecodeStatsDB> db_;
189
190   // Tracks whether we've received OnDatabaseIniti() callback. All database
191   // operations should be deferred until initialization is complete.
192   InitStatus db_init_status_;
193
194   // Vector of bound public API calls, to be run once DB initialization
195   // completes.
196   std::vector<base::OnceClosure> init_deferred_api_calls_;
197
198   // Maps receivers from several render-processes to this single browser-process
199   // service.
200   mojo::ReceiverSet<mojom::VideoDecodePerfHistory> receivers_;
201
202   // Optional helper for local learning.
203   std::unique_ptr<LearningHelper> learning_helper_;
204
205   // Optional callback to create a FeatureProvider for |learning_helper_|.
206   learning::FeatureProviderFactoryCB feature_factory_cb_;
207
208   // Ensures all access to class members come on the same sequence.
209   SEQUENCE_CHECKER(sequence_checker_);
210
211   base::WeakPtrFactory<VideoDecodePerfHistory> weak_ptr_factory_{this};
212 };
213
214 }  // namespace media
215
216 #endif  // MEDIA_MOJO_SERVICES_VIDEO_DECODE_PERF_HISTORY_H_