summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/media_capabilities/media_capabilities.h
blob: 833c89968a706743df197f4f2d5df3b2a0c98c41 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2017 The Chromium Authors. All rights reserved.
// 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_MODULES_MEDIA_CAPABILITIES_MEDIA_CAPABILITIES_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIA_CAPABILITIES_MEDIA_CAPABILITIES_H_

#include "media/base/video_codecs.h"  // for media::VideoCodecProfile
#include "media/learning/mojo/public/cpp/mojo_learning_task_controller.h"
#include "media/learning/mojo/public/mojom/learning_task_controller.mojom-blink.h"
#include "media/mojo/mojom/video_decode_perf_history.mojom-blink.h"
#include "third_party/blink/public/common/privacy_budget/identifiable_token.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_configuration.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/visitor.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
#include "third_party/blink/renderer/platform/supplementable.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"

namespace blink {

class ExceptionState;
class ExecutionContext;
class MediaDecodingConfiguration;
class MediaEncodingConfiguration;
class MediaKeySystemAccess;
class NavigatorBase;
class ScriptPromise;
class ScriptPromiseResolver;
class ScriptState;

class MODULES_EXPORT MediaCapabilities final
    : public ScriptWrappable,
      public Supplement<NavigatorBase> {
  DEFINE_WRAPPERTYPEINFO();

 public:
  static const char kLearningBadWindowThresholdParamName[];
  static const char kLearningNnrThresholdParamName[];

  static const char kSupplementName[];

  // Getter for navigator.mediaCapabilities
  static MediaCapabilities* mediaCapabilities(NavigatorBase&);

  explicit MediaCapabilities(NavigatorBase&);

  void Trace(blink::Visitor* visitor) const override;

  ScriptPromise decodingInfo(ScriptState*,
                             const MediaDecodingConfiguration*,
                             ExceptionState&);
  ScriptPromise encodingInfo(ScriptState*, const MediaEncodingConfiguration*);

 private:
  // Stores pending callback state from and intermediate prediction values while
  // we wait for all predictions to arrive.
  class PendingCallbackState : public GarbageCollected<PendingCallbackState> {
   public:
    PendingCallbackState(ScriptPromiseResolver* resolver,
                         MediaKeySystemAccess* access,
                         const base::TimeTicks& request_time,
                         base::Optional<IdentifiableToken> input_token);
    virtual void Trace(blink::Visitor* visitor) const;

    Member<ScriptPromiseResolver> resolver;
    Member<MediaKeySystemAccess> key_system_access;
    base::Optional<bool> is_bad_window_prediction_smooth;
    base::Optional<bool> is_nnr_prediction_smooth;
    base::Optional<bool> db_is_smooth;
    base::Optional<bool> db_is_power_efficient;
    base::Optional<bool> is_gpu_factories_supported;
    base::TimeTicks request_time;
    base::Optional<IdentifiableToken> input_token;
  };

  // Lazily binds remote LearningTaskControllers for ML smoothness predictions
  // and returns whether binding succeeds. Returns true if it was already bound.
  bool EnsureLearningPredictors(ExecutionContext*);

  // Lazily binds to the VideoDecodePerfHistory service. Returns whether it was
  // successful. Returns true if it was already bound.
  bool EnsurePerfHistoryService(ExecutionContext*);

  ScriptPromise GetEmeSupport(ScriptState*,
                              media::VideoCodec,
                              media::VideoCodecProfile,
                              media::VideoColorSpace,
                              const MediaDecodingConfiguration*,
                              const base::TimeTicks& request_time,
                              ExceptionState&);
  // Gets perf info from VideoDecodePerrHistory DB. Will optionally kick off
  // parallel request to GetPerfInfo_ML() when learning experiment is enabled.
  void GetPerfInfo(media::VideoCodec,
                   media::VideoCodecProfile,
                   media::VideoColorSpace,
                   const MediaDecodingConfiguration*,
                   const base::TimeTicks& request_time,
                   ScriptPromiseResolver*,
                   MediaKeySystemAccess*);

  // Gets ML perf predictions from remote LearingTaskControllers.
  void GetPerfInfo_ML(ExecutionContext* execution_context,
                      int callback_id,
                      media::VideoCodec video_codec,
                      media::VideoCodecProfile video_profile,
                      int width,
                      double framerate);

  // Query media::GpuVideoAcceleratorFactories for support of hardware
  // accelerate decode. Only called when |UseGpuFactoriesForPowerEfficient()|
  // is true.
  void GetGpuFactoriesSupport(int callback_id,
                              media::VideoCodec video_codec,
                              media::VideoCodecProfile video_profile,
                              media::VideoColorSpace,
                              const MediaDecodingConfiguration*);

  // Callback for perf info from the VideoDecodePerfHistory service.
  void OnPerfHistoryInfo(int callback_id,
                         bool is_smooth,
                         bool is_power_efficient);

  // Callback for predictions from |bad_window_predictor_|.
  void OnBadWindowPrediction(
      int callback_id,
      const base::Optional<::media::learning::TargetHistogram>& histogram);

  // Callback for predictions from |nnr_predictor_|.
  void OnNnrPrediction(
      int callback_id,
      const base::Optional<::media::learning::TargetHistogram>& histogram);

  // Callback for GetGpuFactoriesSupport().
  void OnGpuFactoriesSupport(int callback_id, bool is_supported);

  // Resolves the callback with associated |callback_id| and removes it from the
  // |pending_callback_map_|.
  void ResolveCallbackIfReady(int callback_id);

  // Creates a new (incremented) callback ID from |last_callback_id_| for
  // mapping in |pending_cb_map_|.
  int CreateCallbackId();

  HeapMojoRemote<media::mojom::blink::VideoDecodePerfHistory,
                 HeapMojoWrapperMode::kWithoutContextObserver>
      decode_history_service_;

  // Connection to a browser-process LearningTaskController for predicting the
  // number of consecutive "bad" dropped frame windows during a playback. See
  // media::SmoothnessHelper.
  HeapMojoRemote<media::learning::mojom::blink::LearningTaskController,
                 HeapMojoWrapperMode::kWithoutContextObserver>
      bad_window_predictor_;

  // Connects to a browser-process LearningTaskController for predicting the
  // number of consecutive non-network re-buffers (NNRs). See
  // media::SmoothnessHelper.
  HeapMojoRemote<media::learning::mojom::blink::LearningTaskController,
                 HeapMojoWrapperMode::kWithoutContextObserver>
      nnr_predictor_;

  // Holds the last key for callbacks in the map below. Incremented for each
  // usage.
  int last_callback_id_ = 0;

  // Maps a callback ID to state for pending callbacks.
  HeapHashMap<int, Member<PendingCallbackState>> pending_cb_map_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIA_CAPABILITIES_MEDIA_CAPABILITIES_H_