File: media_stream_track_impl.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (241 lines) | stat: -rw-r--r-- 10,449 bytes parent folder | download | duplicates (4)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright (C) 2011 Google Inc. All rights reserved.
 * Copyright (C) 2011 Ericsson AB. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_MEDIA_STREAM_TRACK_IMPL_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_MEDIA_STREAM_TRACK_IMPL_H_

#include <memory>

#include "build/build_config.h"
#include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_capture_handle.h"
#include "third_party/blink/renderer/modules/event_target_modules.h"
#include "third_party/blink/renderer/modules/mediastream/media_constraints.h"
#include "third_party/blink/renderer/modules/mediastream/media_stream_track.h"
#include "third_party/blink/renderer/modules/mediastream/speech_recognition_media_stream_audio_sink.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_set.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_descriptor.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_source.h"
#include "third_party/blink/renderer/platform/scheduler/public/frame_scheduler.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

class AudioSourceProvider;
class ImageCapture;
class MediaTrackCapabilities;
class MediaTrackConstraints;
class MediaStream;
class MediaTrackSettings;
class ScriptState;

// Primary implementation of the MediaStreamTrack interface and idl type.
class MODULES_EXPORT MediaStreamTrackImpl : public MediaStreamTrack,
                                            public MediaStreamSource::Observer {
  USING_PRE_FINALIZER(MediaStreamTrackImpl, Dispose);

 public:
  // Create a MediaStreamTrackImpl of the appropriate type for the display
  // surface type.
  static MediaStreamTrack* Create(ExecutionContext* context,
                                  MediaStreamComponent* component,
                                  base::OnceClosure callback);

  MediaStreamTrackImpl(ExecutionContext*, MediaStreamComponent*);
  MediaStreamTrackImpl(ExecutionContext*,
                       MediaStreamComponent*,
                       base::OnceClosure callback);
  MediaStreamTrackImpl(ExecutionContext*,
                       MediaStreamComponent*,
                       MediaStreamSource::ReadyState,
                       base::OnceClosure callback,
                       bool is_clone = false);
  ~MediaStreamTrackImpl() override;

  // MediaStreamTrack
  String kind() const override;
  String id() const override;
  String label() const override;
  bool enabled() const override;
  void setEnabled(bool) override;
  bool muted() const override;
  String ContentHint() const override;
  void SetContentHint(const String&) override;
  V8MediaStreamTrackState readyState() const override;
  MediaStreamTrack* clone(ExecutionContext*) override;
  void stopTrack(ExecutionContext*) override;
  MediaTrackCapabilities* getCapabilities() const override;
  MediaTrackConstraints* getConstraints() const override;
  MediaTrackSettings* getSettings() const override;
  V8UnionMediaStreamTrackAudioStatsOrMediaStreamTrackVideoStats* stats()
      override;
  CaptureHandle* getCaptureHandle() const override;
  ScriptPromise<IDLUndefined> applyConstraints(
      ScriptState*,
      const MediaTrackConstraints*) override;

  // These two functions are called when constraints have been successfully
  // applied.
  // Called from UserMediaRequest when it succeeds. It is not IDL-exposed.
  // SetInitialConstraints() is expected to be called once when capture starts.
  // SetConstraints() is called later, when changing the set of constraints.
  void SetInitialConstraints(const MediaConstraints& constraints) override;
  void SetConstraints(const MediaConstraints& constraints) override;

  DEFINE_ATTRIBUTE_EVENT_LISTENER(mute, kMute)
  DEFINE_ATTRIBUTE_EVENT_LISTENER(unmute, kUnmute)
  DEFINE_ATTRIBUTE_EVENT_LISTENER(ended, kEnded)
  DEFINE_ATTRIBUTE_EVENT_LISTENER(capturehandlechange, kCapturehandlechange)
  DEFINE_ATTRIBUTE_EVENT_LISTENER(configurationchange, kConfigurationchange)

  // Returns the enum value of the ready state.
  MediaStreamSource::ReadyState GetReadyState() override {
    return ready_state_;
  }

  MediaStreamComponent* Component() const override { return component_.Get(); }
  bool Ended() const override;

  void RegisterMediaStream(MediaStream*) override;
  void UnregisterMediaStream(MediaStream*) override;

  void RegisterSink(SpeechRecognitionMediaStreamAudioSink*) override;

  // EventTarget
  const AtomicString& InterfaceName() const override;
  ExecutionContext* GetExecutionContext() const override;
  void AddedEventListener(const AtomicString&,
                          RegisteredEventListener&) override;

  // ScriptWrappable
  bool HasPendingActivity() const final;

  std::unique_ptr<AudioSourceProvider> CreateWebAudioSource(
      int context_sample_rate,
      base::TimeDelta platform_buffer_duration) override;

  MediaStreamTrackPlatform::VideoFrameStats GetVideoFrameStats() const;

  void TransferAudioFrameStatsTo(
      MediaStreamTrackPlatform::AudioFrameStats& destination);

  ImageCapture* GetImageCapture() override { return image_capture_.Get(); }

  std::optional<const MediaStreamDevice> device() const override;

  void BeingTransferred(const base::UnguessableToken& transfer_id) override;
  bool TransferAllowed(String& message) const override;

  void AddObserver(MediaStreamTrack::Observer*) override;

  void Trace(Visitor*) const override;

  std::optional<int> GetZoomLevelForTesting() const { return zoom_level_; }

  bool IsCapturedSurfaceResolutionActive(
      const MediaStreamTrackPlatform::Settings& platform_settings) const;

 protected:
  // Given a partially built MediaStreamTrackImpl, finishes the job of making it
  // into a clone of |this|.
  // Useful for sub-classes, as they need to clone both state from
  // this class as well as of their own class.
  void CloneInternal(MediaStreamTrackImpl*);

 private:
  friend class CanvasCaptureMediaStreamTrack;
  friend class InternalsMediaStream;

  void Dispose();

  // MediaStreamTrack
  void applyConstraints(ScriptPromiseResolver<IDLUndefined>*,
                        const MediaTrackConstraints*) override;

  // MediaStreamSource::Observer
  void SourceChangedState() override;
  void SourceChangedCaptureConfiguration() override;
  void SourceChangedCaptureHandle() override;
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
  void SourceChangedZoomLevel(int) override;
#endif
  void PropagateTrackEnded();

  void PropagateTrackEnabled(bool enabled);

  void SendLogMessage(const WTF::String& message);

  // Ensures that |feature_handle_for_scheduler_| is initialized.
  void EnsureFeatureHandleForScheduler();

  void SetConstraintsInternal(const MediaConstraints& constraints,
                              bool initial_values);

  void setReadyState(MediaStreamSource::ReadyState ready_state);

  // Callback used after getting the current image capture capabilities and
  // settings to dispatch a configurationchange event if they differ from the
  // old ones.
  void MaybeDispatchConfigurationChange(bool has_changed);

  // This handle notifies the scheduler about a live media stream track
  // associated with a frame. The handle should be destroyed when the track
  // is stopped.
  FrameScheduler::SchedulingAffectingFeatureHandle
      feature_handle_for_scheduler_;

  // This handle notifies the scheduler about a live media stream track
  // for the purpose of disabling/enabling BFCache. When there is a live stream
  // track, the page should not be BFCached.
  // TODO(crbug.com/1502395): Currently we intentionally use this handler for
  // BFCache although its behavior is almost the same as the one above. The one
  // above uses the WebRTC feature even though it's not necessarily related to
  // Web RTC. Discuss with those who own the handler and merge the two handlers
  // into one.
  FrameScheduler::SchedulingAffectingFeatureHandle
      feature_handle_for_scheduler_on_live_media_stream_track_;

  MediaStreamSource::ReadyState ready_state_;
  HeapHashSet<Member<MediaStream>> registered_media_streams_;
  HeapHashSet<Member<SpeechRecognitionMediaStreamAudioSink>> registered_sinks_;
  bool is_iterating_registered_media_streams_ = false;
  const Member<MediaStreamComponent> component_;
  Member<ImageCapture> image_capture_;
  WeakMember<ExecutionContext> execution_context_;
  HeapHashSet<WeakMember<MediaStreamTrack::Observer>> observers_;
  bool muted_ = false;
  std::optional<int> zoom_level_;
  MediaConstraints constraints_;
  std::optional<bool> suppress_local_audio_playback_setting_;
  std::optional<bool> restrict_own_audio_setting_;
  Member<V8UnionMediaStreamTrackAudioStatsOrMediaStreamTrackVideoStats> stats_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_MEDIA_STREAM_TRACK_IMPL_H_