File: power_status_helper.h

package info (click to toggle)
chromium 139.0.7258.154-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,129,716 kB
  • sloc: cpp: 35,100,894; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,921; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,152; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (148 lines) | stat: -rw-r--r-- 5,347 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_MEDIA_POWER_STATUS_HELPER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_MEDIA_POWER_STATUS_HELPER_H_

#include <optional>

#include "base/functional/callback.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "media/base/video_codecs.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/mojom/battery_monitor.mojom-blink.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "ui/gfx/geometry/size.h"

namespace media {
struct PipelineMetadata;
}

namespace blink {

// Class to monitor for power events during playback and record them to UMA/UKM.
class PLATFORM_EXPORT PowerStatusHelper {
 public:
  using CreateBatteryMonitorCB = base::RepeatingCallback<
      mojo::PendingRemote<device::mojom::blink::BatteryMonitor>()>;

  // Bits used to construct UMA buckets.
  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum /* not class */ Bits {
    // Bit layout is: [msb] xx f F RR CC [lsb]
    // R == resolution
    // C == codec
    // F == frame rate
    // f == full screen
    // x == unused
    // Remember that we can't use more than 6 bits, since we shouldn't go over
    // 100 UMA buckets.

    // Codec bits, values 0x00 to 0x03
    // Named "CodecBits" to prevent a name collision with media::VideoCodec.
    kCodecBitsH264 = (0x00) << 0,
    kCodecBitsVP9Profile0 = (0x01) << 0,
    // Ignore Profile1
    kCodecBitsVP9Profile2 = (0x02) << 0,
    // TODO(liberato): add AV1

    // Resolution bits, values 0x00 to 0x03
    kResolution360p = (0x00) << 2,
    kResolution720p = (0x01) << 2,
    kResolution1080p = (0x02) << 2,

    // Frame rate bits, values 0x00 to 0x01
    kFrameRate30 = (0x00) << 4,
    kFrameRate60 = (0x01) << 4,

    // Fullscreen bits, values 0x00 to 0x01
    kFullScreenNo = (0x00) << 5,
    kFullScreenYes = (0x01) << 5,

    // This is not a valid bit for, you know, testing.
    kNotAValidBitForTesting = (0x01) << 10,
  };

  // If |stats_cb| is not provided, then we'll record to UMA.  It's just for
  // the tests.
  explicit PowerStatusHelper(CreateBatteryMonitorCB create_battery_monitor_cb);
  PowerStatusHelper(const PowerStatusHelper&) = delete;
  PowerStatusHelper& operator=(const PowerStatusHelper&) = delete;
  ~PowerStatusHelper();

  // Notify us about changes to the player.
  void SetIsPlaying(bool is_playing);
  void SetMetadata(const media::PipelineMetadata& metadata);
  void SetIsFullscreen(bool is_fullscreen);
  void SetAverageFrameRate(std::optional<int> average_fps);

  // Handle notifications about the experiment state from the power experiment.
  // manager.  |state| indicates whether our player is eligible to record power
  // experiments readings.
  void UpdatePowerExperimentState(bool state);

 private:
  friend class PowerStatusHelperTest;
  friend class PowerStatusHelperBucketTest;

  // Return the UMA bucket for the given video configuration, or nullopt if we
  // don't want to record it.
  static std::optional<int> BucketFor(bool is_playing,
                                      bool has_video,
                                      media::VideoCodec codec,
                                      media::VideoCodecProfile profile,
                                      gfx::Size natural_size,
                                      bool is_fullscreen,
                                      std::optional<int> average_fps);

  // Recompute everything when playback state or power experiment state changes.
  void OnAnyStateChange();

  // Handle updates about the current battery status.
  void OnBatteryStatus(device::mojom::blink::BatteryStatusPtr battery_status);

  // Start monitoring if we haven't already.  Any outstanding callbacks will be
  // cancelled if monitoring was already in progress.
  void StartMonitoring();
  void StopMonitoring();

  // Register to receive a power update the next time it changes.
  void QueryNextStatus();

  CreateBatteryMonitorCB create_battery_monitor_cb_;

  // Most recent parameters we were given.
  bool is_playing_ = false;
  bool has_video_ = false;
  media::VideoCodec codec_ = media::VideoCodec::kUnknown;
  media::VideoCodecProfile profile_ =
      media::VideoCodecProfile::VIDEO_CODEC_PROFILE_UNKNOWN;
  gfx::Size natural_size_;
  bool is_fullscreen_ = false;
  // For estimating fps.  Can be unset if we don't know.
  std::optional<int> average_fps_;

  // Current UMA bucket, if any.
  std::optional<int> current_bucket_;

  // If set, our previous battery level, from 0-100.
  std::optional<float> battery_level_baseline_;
  // The time at which we last got an update from |battery_monitor_|.
  base::TimeTicks last_update_;

  // Are we currently the player that should be recording power for the power
  // experiment, according to the MediaPowerExperimentManager?
  bool experiment_state_ = false;

  mojo::Remote<device::mojom::blink::BatteryMonitor> battery_monitor_;

  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_MEDIA_POWER_STATUS_HELPER_H_