File: media_stream_track_transfer_test.cc

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 (265 lines) | stat: -rw-r--r-- 11,532 bytes parent folder | download | duplicates (6)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/task/single_thread_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_dom_exception.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_stream_track_state.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/modules/mediastream/browser_capture_media_stream_track.h"
#include "third_party/blink/renderer/modules/mediastream/media_stream_track.h"
#include "third_party/blink/renderer/modules/mediastream/mock_media_stream_video_source.h"
#include "third_party/blink/renderer/modules/mediastream/mock_mojo_media_stream_dispatcher_host.h"
#include "third_party/blink/renderer/modules/mediastream/user_media_client.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_audio_source.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_source.h"
#include "third_party/blink/renderer/platform/testing/io_task_runner_testing_platform_support.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"

namespace blink {
namespace {

mojom::blink::MediaDevicesDispatcherHost* UnusedMediaDevicesDispatcherHost() {
  NOTREACHED();
}

void SignalSourceReady(
    WebPlatformMediaStreamSource::ConstraintsOnceCallback source_ready,
    WebPlatformMediaStreamSource* source) {
  std::move(source_ready)
      .Run(source, mojom::blink::MediaStreamRequestResult::OK, "");
}

// UserMediaProcessor with mocked CreateVideoSource and CreateAudioSource.
class MockUserMediaProcessor : public UserMediaProcessor {
 public:
  explicit MockUserMediaProcessor(LocalFrame* frame)
      : UserMediaProcessor(
            frame,
            base::BindRepeating(&UnusedMediaDevicesDispatcherHost),
            scheduler::GetSingleThreadTaskRunnerForTesting()) {}

  // UserMediaProcessor overrides.
  std::unique_ptr<MediaStreamVideoSource> CreateVideoSource(
      const MediaStreamDevice& device,
      WebPlatformMediaStreamSource::SourceStoppedCallback stop_callback)
      override {
    auto source = std::make_unique<MockMediaStreamVideoSource>();
    source->SetDevice(device);
    source->SetStopCallback(std::move(stop_callback));
    return source;
  }

  std::unique_ptr<MediaStreamAudioSource> CreateAudioSource(
      const MediaStreamDevice& device,
      WebPlatformMediaStreamSource::ConstraintsRepeatingCallback source_ready)
      override {
    auto source = std::make_unique<MediaStreamAudioSource>(
        scheduler::GetSingleThreadTaskRunnerForTesting(),
        /*is_local_source=*/true);
    source->SetDevice(device);
    // RunUntilIdle is required for this task to complete.
    scheduler::GetSingleThreadTaskRunnerForTesting()->PostTask(
        FROM_HERE, base::BindOnce(&SignalSourceReady, std::move(source_ready),
                                  source.get()));
    return source;
  }
};

class UserMediaClientUnderTest : public UserMediaClient {
 public:
  UserMediaClientUnderTest(
      LocalFrame* frame,
      UserMediaProcessor* user_media_processor,
      UserMediaProcessor* display_user_media_processor,
      scoped_refptr<base::SingleThreadTaskRunner> task_runner)
      : UserMediaClient(frame,
                        user_media_processor,
                        display_user_media_processor,
                        task_runner) {}
};

// ScopedMockUserMediaClient creates and installs a temporary UserMediaClient in
// |window| when constructed and restores the original UserMediaClient when
// destroyed. Uses a MockMojoMediaStreamDispatcherHost and
// MockUserMediaProcessor.
class ScopedMockUserMediaClient {
 public:
  explicit ScopedMockUserMediaClient(LocalDOMWindow* window)
      : original_(Supplement<LocalDOMWindow>::From<UserMediaClient>(window)) {
    auto* user_media_processor =
        MakeGarbageCollected<MockUserMediaProcessor>(window->GetFrame());
    auto* display_user_media_processor =
        MakeGarbageCollected<MockUserMediaProcessor>(window->GetFrame());
    user_media_processor->set_media_stream_dispatcher_host_for_testing(
        mock_media_stream_dispatcher_host.CreatePendingRemoteAndBind());
    display_user_media_processor->set_media_stream_dispatcher_host_for_testing(
        display_mock_media_stream_dispatcher_host.CreatePendingRemoteAndBind());
    temp_ = MakeGarbageCollected<UserMediaClientUnderTest>(
        window->GetFrame(), user_media_processor, display_user_media_processor,
        scheduler::GetSingleThreadTaskRunnerForTesting());
    Supplement<LocalDOMWindow>::ProvideTo<UserMediaClient>(*window,
                                                           temp_.Get());
  }

  ~ScopedMockUserMediaClient() {
    auto* window = temp_->GetSupplementable();
    if (Supplement<LocalDOMWindow>::From<UserMediaClient>(window) ==
        temp_.Get()) {
      if (original_) {
        Supplement<LocalDOMWindow>::ProvideTo<UserMediaClient>(*window,
                                                               original_.Get());
      } else {
        window->Supplementable<LocalDOMWindow>::RemoveSupplement<
            UserMediaClient>();
      }
    }
  }

  MockMojoMediaStreamDispatcherHost mock_media_stream_dispatcher_host;
  MockMojoMediaStreamDispatcherHost display_mock_media_stream_dispatcher_host;

 private:
  Persistent<UserMediaClient> temp_;
  Persistent<UserMediaClient> original_;
};

MediaStreamTrack::TransferredValues TransferredValuesTabCaptureVideo() {
  // The TransferredValues here match the expectations in
  // V8ScriptValueSerializerForModulesTest.TransferMediaStreamTrack. Please keep
  // them in sync.
  return MediaStreamTrack::TransferredValues{
      .track_impl_subtype =
          BrowserCaptureMediaStreamTrack::GetStaticWrapperTypeInfo(),
      .session_id = base::UnguessableToken::Create(),
      .transfer_id = base::UnguessableToken::Create(),
      .kind = "video",
      .id = "component_id",
      .label = "test_name",
      .enabled = false,
      .muted = true,
      .content_hint = WebMediaStreamTrack::ContentHintType::kVideoMotion,
      .ready_state = MediaStreamSource::kReadyStateLive,
      .sub_capture_target_version = 0};
}

mojom::blink::StreamDevices DevicesTabCaptureVideo(
    base::UnguessableToken session_id) {
  MediaStreamDevice device(mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE,
                           "device_id", "device_name");
  device.set_session_id(session_id);
  device.display_media_info = media::mojom::DisplayMediaInformation::New(
      media::mojom::DisplayCaptureSurfaceType::BROWSER,
      /*logical_surface=*/true, media::mojom::CursorCaptureType::NEVER,
      /*capture_handle=*/nullptr,
      /*zoom_level=*/100);
  return {std::nullopt, device};
}

TEST(MediaStreamTrackTransferTest, TabCaptureVideoFromTransferredStateBasic) {
  test::TaskEnvironment task_environment;
  V8TestingScope scope;
  ScopedTestingPlatformSupport<IOTaskRunnerTestingPlatformSupport> platform;
  ScopedMockUserMediaClient scoped_user_media_client(&scope.GetWindow());

  auto data = TransferredValuesTabCaptureVideo();
#if BUILDFLAG(IS_ANDROID)
  data.track_impl_subtype = MediaStreamTrack::GetStaticWrapperTypeInfo();
  data.sub_capture_target_version = std::nullopt;
#endif
  scoped_user_media_client.display_mock_media_stream_dispatcher_host
      .SetStreamDevices(DevicesTabCaptureVideo(data.session_id));

  auto* new_track =
      MediaStreamTrack::FromTransferredState(scope.GetScriptState(), data);

#if BUILDFLAG(IS_ANDROID)
  EXPECT_EQ(new_track->GetWrapperTypeInfo(),
            MediaStreamTrack::GetStaticWrapperTypeInfo());
#else
  EXPECT_EQ(new_track->GetWrapperTypeInfo(),
            BrowserCaptureMediaStreamTrack::GetStaticWrapperTypeInfo());
#endif
  EXPECT_EQ(new_track->Component()->GetSourceName(), "device_name");
  // TODO(crbug.com/1288839): the ID needs to be set correctly
  // EXPECT_EQ(new_track->id(), "component_id");
  // TODO(crbug.com/1288839): Should this match the device info or the
  // transferred data?
  EXPECT_EQ(new_track->label(), "device_name");
  EXPECT_EQ(new_track->kind(), "video");
  // TODO(crbug.com/1288839): enabled needs to be set correctly
  // EXPECT_EQ(new_track->enabled(), false);
  // TODO(crbug.com/1288839): muted needs to be set correctly
  // EXPECT_EQ(new_track->muted(), true);
  // TODO(crbug.com/1288839): the content hint needs to be set correctly
  // EXPECT_EQ(new_track->ContentHint(), "motion");
  EXPECT_EQ(new_track->readyState(), V8MediaStreamTrackState::Enum::kLive);

  platform->RunUntilIdle();
  ThreadState::Current()->CollectAllGarbageForTesting();
}

// TODO(crbug.com/1288839): implement and test transferred sub-capture-target
// version

TEST(MediaStreamTrackTransferTest, TabCaptureAudioFromTransferredState) {
  test::TaskEnvironment task_environment;
  V8TestingScope scope;

  // The TransferredValues here match the expectations in
  // V8ScriptValueSerializerForModulesTest.TransferAudioMediaStreamTrack. Please
  // keep them in sync.
  MediaStreamTrack::TransferredValues data{
      .track_impl_subtype = MediaStreamTrack::GetStaticWrapperTypeInfo(),
      .session_id = base::UnguessableToken::Create(),
      .transfer_id = base::UnguessableToken::Create(),
      .kind = "audio",
      .id = "component_id",
      .label = "test_name",
      .enabled = true,
      .muted = true,
      .content_hint = WebMediaStreamTrack::ContentHintType::kAudioSpeech,
      .ready_state = MediaStreamSource::kReadyStateLive};

  ScopedMockUserMediaClient scoped_user_media_client(&scope.GetWindow());

  MediaStreamDevice device(mojom::MediaStreamType::DISPLAY_AUDIO_CAPTURE,
                           "device_id", "device_name");
  device.set_session_id(data.session_id);
  device.display_media_info = media::mojom::DisplayMediaInformation::New(
      media::mojom::DisplayCaptureSurfaceType::BROWSER,
      /*logical_surface=*/true, media::mojom::CursorCaptureType::NEVER,
      /*capture_handle=*/nullptr,
      /*zoom_level=*/100);
  scoped_user_media_client.display_mock_media_stream_dispatcher_host
      .SetStreamDevices({std::nullopt, device});

  auto* new_track =
      MediaStreamTrack::FromTransferredState(scope.GetScriptState(), data);

  EXPECT_EQ(new_track->GetWrapperTypeInfo(),
            MediaStreamTrack::GetStaticWrapperTypeInfo());
  EXPECT_EQ(new_track->Component()->GetSourceName(), "device_name");
  // TODO(crbug.com/1288839): the ID needs to be set correctly
  // EXPECT_EQ(new_track->id(), "component_id");
  // TODO(crbug.com/1288839): Should this match the device info or the
  // transferred data?
  EXPECT_EQ(new_track->label(), "device_name");
  EXPECT_EQ(new_track->kind(), "audio");
  EXPECT_EQ(new_track->enabled(), true);
  // TODO(crbug.com/1288839): muted needs to be set correctly
  // EXPECT_EQ(new_track->muted(), true);
  // TODO(crbug.com/1288839): the content hint needs to be set correctly
  // EXPECT_EQ(new_track->ContentHint(), "speech");
  EXPECT_EQ(new_track->readyState(), V8MediaStreamTrackState::Enum::kLive);

  base::RunLoop().RunUntilIdle();
  ThreadState::Current()->CollectAllGarbageForTesting();
}

}  // namespace
}  // namespace blink