summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/breakout_box/pushable_media_stream_audio_source_test.cc
blob: 61212ef8a910738127db6c3958113de465eafedc (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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2021 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.

#include "third_party/blink/renderer/modules/breakout_box/pushable_media_stream_audio_source.h"

#include "base/run_loop.h"
#include "media/base/bind_to_current_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom-blink.h"
#include "third_party/blink/public/platform/modules/mediastream/web_media_stream_audio_sink.h"
#include "third_party/blink/public/web/web_heap.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_audio_track.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_component.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/wtf/cross_thread_functional.h"

namespace blink {

namespace {

class FakeMediaStreamAudioSink : public WebMediaStreamAudioSink {
 public:
  FakeMediaStreamAudioSink(
      scoped_refptr<base::SingleThreadTaskRunner> main_thread,
      scoped_refptr<base::SingleThreadTaskRunner> audio_thread)
      : main_task_runner_(std::move(main_thread)),
        audio_task_runner_(std::move(audio_thread)) {}
  ~FakeMediaStreamAudioSink() override = default;

  void SetupNewAudioParameterExpectations(int channels,
                                          int frames,
                                          int sample_rate) {
    expected_channels_ = channels;
    expected_frames_ = frames;
    expected_sample_rate_ = sample_rate;
  }

  void SetDataTimeExpectation(base::TimeTicks time,
                              media::AudioBus* expected_data,
                              base::OnceClosure on_data) {
    DCHECK(!on_data_);

    expected_time_ = time;
    expected_data_ = expected_data;

    on_data_ = std::move(on_data);
  }

  void OnData(const media::AudioBus& data, base::TimeTicks time) override {
    // Make sure the source delivered audio data on the right thread.
    EXPECT_TRUE(audio_task_runner_->BelongsToCurrentThread());

    EXPECT_EQ(time, expected_time_);
    EXPECT_EQ(data.channels(), expected_channels_);
    EXPECT_EQ(data.frames(), expected_frames_);

    if (expected_data_) {
      bool unexpected_data = false;

      for (int ch = 0; ch < data.channels(); ++ch) {
        const float* actual_channel_data = data.channel(ch);
        const float* expected_channel_data = expected_data_->channel(ch);

        for (int i = 0; i < data.frames(); ++i) {
          // If we use ASSERT_EQ here, the test will hang, since |on_data_| will
          // never be called.
          EXPECT_EQ(actual_channel_data[i], expected_channel_data[i]);

          // Force an early exit to prevent log spam from EXPECT_EQ.
          if (actual_channel_data[i] != expected_channel_data[i]) {
            unexpected_data = true;
            break;
          }
        }

        if (unexpected_data)
          break;
      }
    }

    // Call this after all expectations are checked, to prevent test from
    // setting new expectations on the main thread.
    std::move(on_data_).Run();
  }

  void OnSetFormat(const media::AudioParameters& params) override {
    // Make sure the source changed parameters data on the right thread.
    EXPECT_TRUE(audio_task_runner_->BelongsToCurrentThread());

    // Also make sure that the audio thread is different from the main
    // thread (it would be a test error if it wasn't, as it would be
    // impossible for the check above to fail).
    ASSERT_TRUE(!main_task_runner_->BelongsToCurrentThread());

    // This should only be called once per format change.
    EXPECT_FALSE(did_receive_format_change_);

    EXPECT_EQ(params.sample_rate(), expected_sample_rate_);
    EXPECT_EQ(params.channels(), expected_channels_);
    EXPECT_EQ(params.frames_per_buffer(), expected_frames_);

    did_receive_format_change_ = true;
  }

  void ClearDidReceiveFormatChange() { did_receive_format_change_ = false; }

  bool did_receive_format_change() const { return did_receive_format_change_; }

 public:
  int expected_channels_ = 0;
  int expected_frames_ = 0;
  int expected_sample_rate_ = 0;
  media::AudioBus* expected_data_ = nullptr;
  base::TimeTicks expected_time_;

  bool did_receive_format_change_ = false;

  base::OnceClosure on_data_;

  scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
  scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_;
};

}  // namespace

class PushableMediaStreamAudioSourceTest : public testing::Test {
 public:
  PushableMediaStreamAudioSourceTest() {
    // Use the IO thread for testing purposes. This is stricter than an audio
    // sequenced task runner needs to be.
    audio_task_runner_ = Platform::Current()->GetIOTaskRunner();
    main_task_runner_ = Thread::MainThread()->GetTaskRunner();

    pushable_audio_source_ = new PushableMediaStreamAudioSource(
        main_task_runner_, audio_task_runner_);
    stream_source_ = MakeGarbageCollected<MediaStreamSource>(
        "dummy_source_id", MediaStreamSource::kTypeAudio, "dummy_source_name",
        false /* remote */);
    stream_source_->SetPlatformSource(base::WrapUnique(pushable_audio_source_));
    stream_component_ = MakeGarbageCollected<MediaStreamComponent>(
        stream_source_->Id(), stream_source_);
  }

  void TearDown() override {
    stream_source_ = nullptr;
    stream_component_ = nullptr;
    WebHeap::CollectAllGarbageForTesting();
  }

  bool ConnectSourceToTrack() {
    return pushable_audio_source_->ConnectToTrack(stream_component_);
  }

  void SendEmptyBufferAndVerifyParams(FakeMediaStreamAudioSink* fake_sink,
                                      int channels,
                                      int frames,
                                      int sample_rate,
                                      bool expect_format_change) {
    SendDataAndVerifyParams(fake_sink, channels, frames, sample_rate,
                            expect_format_change, nullptr, nullptr);
  }

  void SendDataAndVerifyParams(FakeMediaStreamAudioSink* fake_sink,
                               int channels,
                               int frames,
                               int sample_rate,
                               bool expect_format_change,
                               scoped_refptr<media::AudioBuffer> buffer,
                               media::AudioBus* expected_data) {
    fake_sink->ClearDidReceiveFormatChange();

    if (expect_format_change) {
      fake_sink->SetupNewAudioParameterExpectations(channels, frames,
                                                    sample_rate);
    }

    if (!buffer) {
      base::TimeTicks timestamp = base::TimeTicks::Now();
      buffer = media::AudioBuffer::CreateEmptyBuffer(
          media::GuessChannelLayout(channels), channels, sample_rate, frames,
          timestamp - base::TimeTicks());
    }

    base::RunLoop run_loop;
    fake_sink->SetDataTimeExpectation(base::TimeTicks() + buffer->timestamp(),
                                      expected_data, run_loop.QuitClosure());

    pushable_audio_source_->PushAudioData(std::move(buffer));
    run_loop.Run();

    EXPECT_EQ(fake_sink->did_receive_format_change(), expect_format_change);
  }

 protected:
  ScopedTestingPlatformSupport<IOTaskRunnerTestingPlatformSupport> platform_;

  Persistent<MediaStreamSource> stream_source_;
  Persistent<MediaStreamComponent> stream_component_;

  scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
  scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_;

  PushableMediaStreamAudioSource* pushable_audio_source_;
};

TEST_F(PushableMediaStreamAudioSourceTest, ConnectAndStop) {
  EXPECT_EQ(MediaStreamSource::kReadyStateLive,
            stream_source_->GetReadyState());
  EXPECT_FALSE(pushable_audio_source_->running());

  EXPECT_TRUE(ConnectSourceToTrack());
  EXPECT_EQ(MediaStreamSource::kReadyStateLive,
            stream_source_->GetReadyState());
  EXPECT_TRUE(pushable_audio_source_->running());

  // If the pushable source stops, the MediaStreamSource should stop.
  pushable_audio_source_->StopSource();
  EXPECT_EQ(MediaStreamSource::kReadyStateEnded,
            stream_source_->GetReadyState());
  EXPECT_FALSE(pushable_audio_source_->running());
}

TEST_F(PushableMediaStreamAudioSourceTest, FramesPropagateToSink) {
  EXPECT_TRUE(ConnectSourceToTrack());
  FakeMediaStreamAudioSink fake_sink(main_task_runner_, audio_task_runner_);

  WebMediaStreamAudioSink::AddToAudioTrack(
      &fake_sink, WebMediaStreamTrack(stream_component_.Get()));

  constexpr int kChannels = 1;
  constexpr int kFrames = 256;
  constexpr int kSampleRate = 8000;

  // The first audio data pushed should trigger a call to OnSetFormat().
  SendEmptyBufferAndVerifyParams(&fake_sink, kChannels, kFrames, kSampleRate,
                                 /*expect_format_change=*/true);

  // Using the same audio parameters should not trigger OnSetFormat().
  SendEmptyBufferAndVerifyParams(&fake_sink, kChannels, kFrames, kSampleRate,
                                 /*expect_format_change=*/false);

  // Format changes should trigger OnSetFormat().
  SendEmptyBufferAndVerifyParams(&fake_sink, kChannels * 2, kFrames * 4,
                                 /*sample_rate=*/44100,
                                 /*expect_format_change=*/true);

  WebMediaStreamAudioSink::RemoveFromAudioTrack(
      &fake_sink, WebMediaStreamTrack(stream_component_.Get()));
}

TEST_F(PushableMediaStreamAudioSourceTest, ConvertsFormatInternally) {
  EXPECT_TRUE(ConnectSourceToTrack());
  FakeMediaStreamAudioSink fake_sink(main_task_runner_, audio_task_runner_);

  WebMediaStreamAudioSink::AddToAudioTrack(
      &fake_sink, WebMediaStreamTrack(stream_component_.Get()));

  constexpr media::ChannelLayout kChannelLayout =
      media::ChannelLayout::CHANNEL_LAYOUT_STEREO;
  constexpr int kChannels = 2;
  constexpr int kSampleRate = 8000;
  constexpr int kFrames = 256;
  constexpr base::TimeDelta kDefaultTimeStamp =
      base::TimeDelta::FromMilliseconds(123);

  auto interleaved_buffer = media::AudioBuffer::CreateBuffer(
      media::SampleFormat::kSampleFormatF32, kChannelLayout, kChannels,
      kSampleRate, kFrames);
  interleaved_buffer->set_timestamp(kDefaultTimeStamp);

  // Create interleaved data, with negative values on the second channel.
  float* interleaved_buffer_data =
      reinterpret_cast<float*>(interleaved_buffer->channel_data()[0]);
  for (int i = 0; i < kFrames; ++i) {
    float value = static_cast<float>(i) / kFrames;

    interleaved_buffer_data[0] = value;
    interleaved_buffer_data[1] = -value;
    interleaved_buffer_data += 2;
  }

  // Create reference planar data.
  auto expected_data = media::AudioBus::Create(kChannels, kFrames);
  float* bus_data_ch_0 = expected_data->channel(0);
  float* bus_data_ch_1 = expected_data->channel(1);
  for (int i = 0; i < kFrames; ++i) {
    float value = static_cast<float>(i) / kFrames;
    bus_data_ch_0[i] = value;
    bus_data_ch_1[i] = -value;
  }

  // Sanity check.
  DCHECK(!expected_data->AreFramesZero());

  // Send the data to the pushable source, which should internally convert the
  // interleaved data to planar data before delivering it to sinks.
  SendDataAndVerifyParams(&fake_sink, kChannels, kFrames, kSampleRate,
                          /*expect_format_change=*/true,
                          std::move(interleaved_buffer), expected_data.get());

  auto planar_buffer = media::AudioBuffer::CopyFrom(
      kSampleRate, kDefaultTimeStamp, expected_data.get());

  // The pushable source shouldn't have to convert data internally, and should
  // just wrap it.
  SendDataAndVerifyParams(&fake_sink, kChannels, kFrames, kSampleRate,
                          /*expect_format_change=*/false,
                          std::move(planar_buffer), expected_data.get());

  WebMediaStreamAudioSink::RemoveFromAudioTrack(
      &fake_sink, WebMediaStreamTrack(stream_component_.Get()));
}

}  // namespace blink