OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/atomicops.h" | 7 #include "base/atomicops.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | |
10 #include "base/synchronization/lock.h" | 9 #include "base/synchronization/lock.h" |
11 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
12 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
13 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
14 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
15 #include "content/public/renderer/media_stream_audio_sink.h" | 14 #include "content/public/renderer/media_stream_audio_sink.h" |
16 #include "content/renderer/media/media_stream_audio_source.h" | 15 #include "content/renderer/media/media_stream_audio_source.h" |
17 #include "content/renderer/media/media_stream_audio_track.h" | 16 #include "content/renderer/media/media_stream_audio_track.h" |
18 #include "media/base/audio_bus.h" | 17 #include "media/base/audio_bus.h" |
19 #include "media/base/audio_parameters.h" | 18 #include "media/base/audio_parameters.h" |
20 #include "media/base/bind_to_current_loop.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "third_party/WebKit/public/platform/WebString.h" | 20 #include "third_party/WebKit/public/platform/WebString.h" |
23 #include "third_party/WebKit/public/web/WebHeap.h" | 21 #include "third_party/WebKit/public/web/WebHeap.h" |
24 | 22 |
25 namespace content { | 23 namespace content { |
26 | 24 |
27 namespace { | 25 namespace { |
28 | 26 |
29 constexpr int kSampleRate = 8000; | 27 constexpr int kSampleRate = 8000; |
30 constexpr int kBufferSize = kSampleRate / 100; | 28 constexpr int kBufferSize = kSampleRate / 100; |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 base::subtle::Atomic32 audio_is_silent_; | 232 base::subtle::Atomic32 audio_is_silent_; |
235 bool was_ended_; | 233 bool was_ended_; |
236 EnableState enable_state_; | 234 EnableState enable_state_; |
237 | 235 |
238 DISALLOW_COPY_AND_ASSIGN(FakeMediaStreamAudioSink); | 236 DISALLOW_COPY_AND_ASSIGN(FakeMediaStreamAudioSink); |
239 }; | 237 }; |
240 | 238 |
241 } // namespace | 239 } // namespace |
242 | 240 |
243 class MediaStreamAudioTest : public ::testing::Test { | 241 class MediaStreamAudioTest : public ::testing::Test { |
244 public: | |
245 void CallbackFunction() { callback_is_called_ = true; } | |
246 | |
247 protected: | 242 protected: |
248 void SetUp() override { | 243 void SetUp() override { |
249 blink_audio_source_.initialize(blink::WebString::fromUTF8("audio_id"), | 244 blink_audio_source_.initialize(blink::WebString::fromUTF8("audio_id"), |
250 blink::WebMediaStreamSource::TypeAudio, | 245 blink::WebMediaStreamSource::TypeAudio, |
251 blink::WebString::fromUTF8("audio_track"), | 246 blink::WebString::fromUTF8("audio_track"), |
252 false /* remote */); | 247 false /* remote */); |
253 blink_audio_track_.initialize(blink_audio_source_.id(), | 248 blink_audio_track_.initialize(blink_audio_source_.id(), |
254 blink_audio_source_); | 249 blink_audio_source_); |
255 } | 250 } |
256 | 251 |
257 void TearDown() override { | 252 void TearDown() override { |
258 blink_audio_track_.reset(); | 253 blink_audio_track_.reset(); |
259 blink_audio_source_.reset(); | 254 blink_audio_source_.reset(); |
260 blink::WebHeap::collectAllGarbageForTesting(); | 255 blink::WebHeap::collectAllGarbageForTesting(); |
261 } | 256 } |
262 | 257 |
263 FakeMediaStreamAudioSource* source() const { | 258 FakeMediaStreamAudioSource* source() const { |
264 return static_cast<FakeMediaStreamAudioSource*>( | 259 return static_cast<FakeMediaStreamAudioSource*>( |
265 MediaStreamAudioSource::From(blink_audio_source_)); | 260 MediaStreamAudioSource::From(blink_audio_source_)); |
266 } | 261 } |
267 | 262 |
268 MediaStreamAudioTrack* track() const { | 263 MediaStreamAudioTrack* track() const { |
269 return MediaStreamAudioTrack::From(blink_audio_track_); | 264 return MediaStreamAudioTrack::From(blink_audio_track_); |
270 } | 265 } |
271 | 266 |
272 blink::WebMediaStreamSource blink_audio_source_; | 267 blink::WebMediaStreamSource blink_audio_source_; |
273 blink::WebMediaStreamTrack blink_audio_track_; | 268 blink::WebMediaStreamTrack blink_audio_track_; |
274 | 269 |
275 base::MessageLoop message_loop_; | 270 base::MessageLoop message_loop_; |
276 | |
277 bool callback_is_called_ = false; | |
278 }; | 271 }; |
279 | 272 |
280 // Tests that a simple source-->track-->sink connection and audio data flow | 273 // Tests that a simple source-->track-->sink connection and audio data flow |
281 // works. | 274 // works. |
282 TEST_F(MediaStreamAudioTest, BasicUsage) { | 275 TEST_F(MediaStreamAudioTest, BasicUsage) { |
283 // Create the source, but it should not be started yet. | 276 // Create the source, but it should not be started yet. |
284 ASSERT_FALSE(source()); | 277 ASSERT_FALSE(source()); |
285 blink_audio_source_.setExtraData(new FakeMediaStreamAudioSource()); | 278 blink_audio_source_.setExtraData(new FakeMediaStreamAudioSource()); |
286 ASSERT_TRUE(source()); | 279 ASSERT_TRUE(source()); |
287 EXPECT_FALSE(source()->was_started()); | 280 EXPECT_FALSE(source()->was_started()); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 // The first track and sink should not have been affected by changing the | 446 // The first track and sink should not have been affected by changing the |
454 // enabled state of the second track and sink. They should still be disabled, | 447 // enabled state of the second track and sink. They should still be disabled, |
455 // with silent audio being consumed at the sink. | 448 // with silent audio being consumed at the sink. |
456 EXPECT_EQ(FakeMediaStreamAudioSink::WAS_DISABLED, sink.enable_state()); | 449 EXPECT_EQ(FakeMediaStreamAudioSink::WAS_DISABLED, sink.enable_state()); |
457 EXPECT_TRUE(sink.is_audio_silent()); | 450 EXPECT_TRUE(sink.is_audio_silent()); |
458 | 451 |
459 MediaStreamAudioTrack::From(another_blink_track)->RemoveSink(&another_sink); | 452 MediaStreamAudioTrack::From(another_blink_track)->RemoveSink(&another_sink); |
460 track()->RemoveSink(&sink); | 453 track()->RemoveSink(&sink); |
461 } | 454 } |
462 | 455 |
463 // Tests that a callback is fired when initialization completes on a track. | |
464 TEST_F(MediaStreamAudioTest, CallbackOnTrackInitialization) { | |
465 // Create a source, connect it to track, and connect the track to a | |
466 // sink. | |
467 blink_audio_source_.setExtraData(new FakeMediaStreamAudioSource()); | |
468 ASSERT_TRUE(source()); | |
469 EXPECT_TRUE(source()->ConnectToTrack(blink_audio_track_)); | |
470 ASSERT_TRUE(track()); | |
471 FakeMediaStreamAudioSink sink; | |
472 ASSERT_TRUE(!sink.params().IsValid()); | |
473 track()->AddSink(&sink); | |
474 // The test callback is not thread-safe, so needs to be called on the | |
475 // current thread, not the thread from which it is triggered. | |
476 track()->SetFormatConfiguredCallback(media::BindToCurrentLoop(base::Bind( | |
477 &MediaStreamAudioTest::CallbackFunction, base::Unretained(this)))); | |
478 EXPECT_FALSE(callback_is_called_); | |
479 // Wait until valid parameters are propagated to the sink, and then confirm | |
480 // the parameters are correct at the track and the sink. | |
481 while (!sink.params().IsValid()) | |
482 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | |
483 // Since the callback is waiting to run on this thread, we have to run | |
484 // an event loop. | |
485 base::RunLoop().RunUntilIdle(); | |
486 EXPECT_TRUE(callback_is_called_); | |
487 track()->RemoveSink(&sink); | |
488 } | |
489 | |
490 } // namespace content | 456 } // namespace content |
OLD | NEW |