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" |
9 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
10 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
11 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
12 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
13 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
14 #include "content/public/renderer/media_stream_audio_sink.h" | 15 #include "content/public/renderer/media_stream_audio_sink.h" |
15 #include "content/renderer/media/media_stream_audio_source.h" | 16 #include "content/renderer/media/media_stream_audio_source.h" |
16 #include "content/renderer/media/media_stream_audio_track.h" | 17 #include "content/renderer/media/media_stream_audio_track.h" |
17 #include "media/base/audio_bus.h" | 18 #include "media/base/audio_bus.h" |
18 #include "media/base/audio_parameters.h" | 19 #include "media/base/audio_parameters.h" |
| 20 #include "media/base/bind_to_current_loop.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "third_party/WebKit/public/platform/WebString.h" | 22 #include "third_party/WebKit/public/platform/WebString.h" |
21 #include "third_party/WebKit/public/web/WebHeap.h" | 23 #include "third_party/WebKit/public/web/WebHeap.h" |
22 | 24 |
23 namespace content { | 25 namespace content { |
24 | 26 |
25 namespace { | 27 namespace { |
26 | 28 |
27 constexpr int kSampleRate = 8000; | 29 constexpr int kSampleRate = 8000; |
28 constexpr int kBufferSize = kSampleRate / 100; | 30 constexpr int kBufferSize = kSampleRate / 100; |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 base::subtle::Atomic32 audio_is_silent_; | 234 base::subtle::Atomic32 audio_is_silent_; |
233 bool was_ended_; | 235 bool was_ended_; |
234 EnableState enable_state_; | 236 EnableState enable_state_; |
235 | 237 |
236 DISALLOW_COPY_AND_ASSIGN(FakeMediaStreamAudioSink); | 238 DISALLOW_COPY_AND_ASSIGN(FakeMediaStreamAudioSink); |
237 }; | 239 }; |
238 | 240 |
239 } // namespace | 241 } // namespace |
240 | 242 |
241 class MediaStreamAudioTest : public ::testing::Test { | 243 class MediaStreamAudioTest : public ::testing::Test { |
| 244 public: |
| 245 void CallbackFunction() { callback_is_called_ = true; } |
| 246 |
242 protected: | 247 protected: |
243 void SetUp() override { | 248 void SetUp() override { |
244 blink_audio_source_.initialize(blink::WebString::fromUTF8("audio_id"), | 249 blink_audio_source_.initialize(blink::WebString::fromUTF8("audio_id"), |
245 blink::WebMediaStreamSource::TypeAudio, | 250 blink::WebMediaStreamSource::TypeAudio, |
246 blink::WebString::fromUTF8("audio_track"), | 251 blink::WebString::fromUTF8("audio_track"), |
247 false /* remote */); | 252 false /* remote */); |
248 blink_audio_track_.initialize(blink_audio_source_.id(), | 253 blink_audio_track_.initialize(blink_audio_source_.id(), |
249 blink_audio_source_); | 254 blink_audio_source_); |
250 } | 255 } |
251 | 256 |
252 void TearDown() override { | 257 void TearDown() override { |
253 blink_audio_track_.reset(); | 258 blink_audio_track_.reset(); |
254 blink_audio_source_.reset(); | 259 blink_audio_source_.reset(); |
255 blink::WebHeap::collectAllGarbageForTesting(); | 260 blink::WebHeap::collectAllGarbageForTesting(); |
256 } | 261 } |
257 | 262 |
258 FakeMediaStreamAudioSource* source() const { | 263 FakeMediaStreamAudioSource* source() const { |
259 return static_cast<FakeMediaStreamAudioSource*>( | 264 return static_cast<FakeMediaStreamAudioSource*>( |
260 MediaStreamAudioSource::From(blink_audio_source_)); | 265 MediaStreamAudioSource::From(blink_audio_source_)); |
261 } | 266 } |
262 | 267 |
263 MediaStreamAudioTrack* track() const { | 268 MediaStreamAudioTrack* track() const { |
264 return MediaStreamAudioTrack::From(blink_audio_track_); | 269 return MediaStreamAudioTrack::From(blink_audio_track_); |
265 } | 270 } |
266 | 271 |
267 blink::WebMediaStreamSource blink_audio_source_; | 272 blink::WebMediaStreamSource blink_audio_source_; |
268 blink::WebMediaStreamTrack blink_audio_track_; | 273 blink::WebMediaStreamTrack blink_audio_track_; |
269 | 274 |
270 base::MessageLoop message_loop_; | 275 base::MessageLoop message_loop_; |
| 276 |
| 277 bool callback_is_called_ = false; |
271 }; | 278 }; |
272 | 279 |
273 // Tests that a simple source-->track-->sink connection and audio data flow | 280 // Tests that a simple source-->track-->sink connection and audio data flow |
274 // works. | 281 // works. |
275 TEST_F(MediaStreamAudioTest, BasicUsage) { | 282 TEST_F(MediaStreamAudioTest, BasicUsage) { |
276 // Create the source, but it should not be started yet. | 283 // Create the source, but it should not be started yet. |
277 ASSERT_FALSE(source()); | 284 ASSERT_FALSE(source()); |
278 blink_audio_source_.setExtraData(new FakeMediaStreamAudioSource()); | 285 blink_audio_source_.setExtraData(new FakeMediaStreamAudioSource()); |
279 ASSERT_TRUE(source()); | 286 ASSERT_TRUE(source()); |
280 EXPECT_FALSE(source()->was_started()); | 287 EXPECT_FALSE(source()->was_started()); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 // The first track and sink should not have been affected by changing the | 453 // The first track and sink should not have been affected by changing the |
447 // enabled state of the second track and sink. They should still be disabled, | 454 // enabled state of the second track and sink. They should still be disabled, |
448 // with silent audio being consumed at the sink. | 455 // with silent audio being consumed at the sink. |
449 EXPECT_EQ(FakeMediaStreamAudioSink::WAS_DISABLED, sink.enable_state()); | 456 EXPECT_EQ(FakeMediaStreamAudioSink::WAS_DISABLED, sink.enable_state()); |
450 EXPECT_TRUE(sink.is_audio_silent()); | 457 EXPECT_TRUE(sink.is_audio_silent()); |
451 | 458 |
452 MediaStreamAudioTrack::From(another_blink_track)->RemoveSink(&another_sink); | 459 MediaStreamAudioTrack::From(another_blink_track)->RemoveSink(&another_sink); |
453 track()->RemoveSink(&sink); | 460 track()->RemoveSink(&sink); |
454 } | 461 } |
455 | 462 |
| 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 |
456 } // namespace content | 490 } // namespace content |
OLD | NEW |