| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chromecast/media/audio/cast_audio_output_stream.h" | 5 #include "chromecast/media/audio/cast_audio_output_stream.h" |
| 6 | 6 |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 14 #include "base/synchronization/waitable_event.h" | |
| 15 #include "base/thread_task_runner_handle.h" | |
| 16 #include "chromecast/base/metrics/cast_metrics_test_helper.h" | 8 #include "chromecast/base/metrics/cast_metrics_test_helper.h" |
| 17 #include "chromecast/media/audio/cast_audio_manager.h" | 9 #include "chromecast/media/audio/cast_audio_manager.h" |
| 18 #include "chromecast/media/base/media_message_loop.h" | |
| 19 #include "chromecast/media/cma/backend/media_pipeline_backend_default.h" | |
| 20 #include "chromecast/public/media/cast_decoder_buffer.h" | 10 #include "chromecast/public/media/cast_decoder_buffer.h" |
| 21 #include "chromecast/public/media/decoder_config.h" | |
| 22 #include "chromecast/public/media/decrypt_context.h" | |
| 23 #include "chromecast/public/media/media_pipeline_backend.h" | 11 #include "chromecast/public/media/media_pipeline_backend.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 13 |
| 26 namespace chromecast { | 14 namespace chromecast { |
| 27 namespace media { | 15 namespace media { |
| 28 namespace { | 16 namespace { |
| 29 const char kDefaultDeviceId[] = ""; | 17 const char kDefaultDeviceId[] = ""; |
| 30 | 18 |
| 31 void RunUntilIdle(base::TaskRunner* task_runner) { | |
| 32 base::WaitableEvent completion_event(false, false); | |
| 33 task_runner->PostTask(FROM_HERE, | |
| 34 base::Bind(&base::WaitableEvent::Signal, | |
| 35 base::Unretained(&completion_event))); | |
| 36 completion_event.Wait(); | |
| 37 } | |
| 38 | |
| 39 class FakeAudioDecoder : public MediaPipelineBackend::AudioDecoder { | 19 class FakeAudioDecoder : public MediaPipelineBackend::AudioDecoder { |
| 40 public: | 20 public: |
| 41 enum PipelineStatus { | 21 enum PipelineStatus { |
| 42 PIPELINE_STATUS_OK, | 22 PIPELINE_STATUS_OK, |
| 43 PIPELINE_STATUS_BUSY, | 23 PIPELINE_STATUS_BUSY, |
| 44 PIPELINE_STATUS_ERROR, | 24 PIPELINE_STATUS_ERROR, |
| 45 PIPELINE_STATUS_ASYNC_ERROR, | 25 PIPELINE_STATUS_ASYNC_ERROR, |
| 46 }; | 26 }; |
| 47 | 27 |
| 48 FakeAudioDecoder() | 28 FakeAudioDecoder() |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 class FakeAudioManager : public CastAudioManager { | 167 class FakeAudioManager : public CastAudioManager { |
| 188 public: | 168 public: |
| 189 FakeAudioManager(scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 169 FakeAudioManager(scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 190 : CastAudioManager(task_runner, task_runner, nullptr, nullptr), | 170 : CastAudioManager(task_runner, task_runner, nullptr, nullptr), |
| 191 media_pipeline_backend_(nullptr) {} | 171 media_pipeline_backend_(nullptr) {} |
| 192 ~FakeAudioManager() override {} | 172 ~FakeAudioManager() override {} |
| 193 | 173 |
| 194 // CastAudioManager overrides. | 174 // CastAudioManager overrides. |
| 195 std::unique_ptr<MediaPipelineBackend> CreateMediaPipelineBackend( | 175 std::unique_ptr<MediaPipelineBackend> CreateMediaPipelineBackend( |
| 196 const MediaPipelineDeviceParams& params) override { | 176 const MediaPipelineDeviceParams& params) override { |
| 197 DCHECK(media::MediaMessageLoop::GetTaskRunner()->BelongsToCurrentThread()); | 177 DCHECK(GetTaskRunner()->BelongsToCurrentThread()); |
| 198 DCHECK(!media_pipeline_backend_); | 178 DCHECK(!media_pipeline_backend_); |
| 199 | 179 |
| 200 std::unique_ptr<FakeMediaPipelineBackend> backend( | 180 std::unique_ptr<FakeMediaPipelineBackend> backend( |
| 201 new FakeMediaPipelineBackend()); | 181 new FakeMediaPipelineBackend()); |
| 202 // Cache the backend locally to be used by tests. | 182 // Cache the backend locally to be used by tests. |
| 203 media_pipeline_backend_ = backend.get(); | 183 media_pipeline_backend_ = backend.get(); |
| 204 return std::move(backend); | 184 return std::move(backend); |
| 205 } | 185 } |
| 206 void ReleaseOutputStream(::media::AudioOutputStream* stream) override { | 186 void ReleaseOutputStream(::media::AudioOutputStream* stream) override { |
| 207 DCHECK(media_pipeline_backend_); | 187 DCHECK(media_pipeline_backend_); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 226 : format_(::media::AudioParameters::AUDIO_PCM_LINEAR), | 206 : format_(::media::AudioParameters::AUDIO_PCM_LINEAR), |
| 227 channel_layout_(::media::CHANNEL_LAYOUT_MONO), | 207 channel_layout_(::media::CHANNEL_LAYOUT_MONO), |
| 228 sample_rate_(::media::AudioParameters::kAudioCDSampleRate), | 208 sample_rate_(::media::AudioParameters::kAudioCDSampleRate), |
| 229 bits_per_sample_(16), | 209 bits_per_sample_(16), |
| 230 frames_per_buffer_(256) {} | 210 frames_per_buffer_(256) {} |
| 231 ~CastAudioOutputStreamTest() override {} | 211 ~CastAudioOutputStreamTest() override {} |
| 232 | 212 |
| 233 protected: | 213 protected: |
| 234 void SetUp() override { | 214 void SetUp() override { |
| 235 metrics::InitializeMetricsHelperForTesting(); | 215 metrics::InitializeMetricsHelperForTesting(); |
| 236 | 216 audio_manager_.reset(new FakeAudioManager(message_loop_.task_runner())); |
| 237 audio_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
| 238 backend_task_runner_ = media::MediaMessageLoop::GetTaskRunner(); | |
| 239 audio_manager_.reset(new FakeAudioManager(audio_task_runner_)); | |
| 240 } | 217 } |
| 241 | 218 |
| 242 void TearDown() override { | 219 void TearDown() override { |
| 243 audio_manager_.reset(); | 220 audio_manager_.reset(); |
| 244 } | 221 } |
| 245 | 222 |
| 246 ::media::AudioParameters GetAudioParams() { | 223 ::media::AudioParameters GetAudioParams() { |
| 247 return ::media::AudioParameters(format_, channel_layout_, sample_rate_, | 224 return ::media::AudioParameters(format_, channel_layout_, sample_rate_, |
| 248 bits_per_sample_, frames_per_buffer_); | 225 bits_per_sample_, frames_per_buffer_); |
| 249 } | 226 } |
| 250 | 227 |
| 251 FakeMediaPipelineBackend* GetBackend() { | 228 FakeMediaPipelineBackend* GetBackend() { |
| 252 return audio_manager_->media_pipeline_backend(); | 229 return audio_manager_->media_pipeline_backend(); |
| 253 } | 230 } |
| 254 | 231 |
| 255 FakeAudioDecoder* GetAudio() { | 232 FakeAudioDecoder* GetAudio() { |
| 256 FakeMediaPipelineBackend* backend = GetBackend(); | 233 FakeMediaPipelineBackend* backend = GetBackend(); |
| 257 return (backend ? backend->decoder() : nullptr); | 234 return (backend ? backend->decoder() : nullptr); |
| 258 } | 235 } |
| 259 | 236 |
| 260 // Synchronous utility functions. | |
| 261 ::media::AudioOutputStream* CreateStream() { | 237 ::media::AudioOutputStream* CreateStream() { |
| 262 return audio_manager_->MakeAudioOutputStream(GetAudioParams(), | 238 return audio_manager_->MakeAudioOutputStream(GetAudioParams(), |
| 263 kDefaultDeviceId); | 239 kDefaultDeviceId); |
| 264 } | 240 } |
| 265 bool OpenStream(::media::AudioOutputStream* stream) { | |
| 266 bool success = stream->Open(); | |
| 267 // Drain the backend task runner so that appropriate states are set on | |
| 268 // the backend pipeline devices. | |
| 269 RunUntilIdle(backend_task_runner_.get()); | |
| 270 return success; | |
| 271 } | |
| 272 void CloseStream(::media::AudioOutputStream* stream) { | |
| 273 stream->Close(); | |
| 274 RunUntilIdle(backend_task_runner_.get()); | |
| 275 // Backend task runner may have posted more tasks to the audio task runner. | |
| 276 // So we need to drain it once more. | |
| 277 message_loop_.RunUntilIdle(); | |
| 278 } | |
| 279 void StartStream( | |
| 280 ::media::AudioOutputStream* stream, | |
| 281 ::media::AudioOutputStream::AudioSourceCallback* source_callback) { | |
| 282 stream->Start(source_callback); | |
| 283 // Drain the audio task runner so that tasks posted by | |
| 284 // media::AudioOutputStream::Start are run as well. | |
| 285 message_loop_.RunUntilIdle(); | |
| 286 // Drain the backend task runner so that appropriate states are set on | |
| 287 // the backend pipeline devices. | |
| 288 RunUntilIdle(backend_task_runner_.get()); | |
| 289 // Drain the audio task runner again to run the tasks posted by the | |
| 290 // backend on audio task runner. | |
| 291 message_loop_.RunUntilIdle(); | |
| 292 } | |
| 293 void StopStream(::media::AudioOutputStream* stream) { | |
| 294 stream->Stop(); | |
| 295 // Drain the backend task runner so that appropriate states are set on | |
| 296 // the backend pipeline devices. | |
| 297 RunUntilIdle(backend_task_runner_.get()); | |
| 298 } | |
| 299 void SetStreamVolume(::media::AudioOutputStream* stream, double volume) { | |
| 300 stream->SetVolume(volume); | |
| 301 // Drain the backend task runner so that appropriate states are set on | |
| 302 // the backend pipeline devices. | |
| 303 RunUntilIdle(backend_task_runner_.get()); | |
| 304 } | |
| 305 double GetStreamVolume(::media::AudioOutputStream* stream) { | |
| 306 double volume = 0.0; | |
| 307 stream->GetVolume(&volume); | |
| 308 // No need to drain the backend task runner because getting the volume | |
| 309 // does not involve posting any task to the backend. | |
| 310 return volume; | |
| 311 } | |
| 312 | 241 |
| 313 void RunAudioLoopFor(int frames) { | 242 // Runs the messsage loop for duration equivalent to the given number of |
| 243 // audio |frames|. |
| 244 void RunMessageLoopFor(int frames) { |
| 314 ::media::AudioParameters audio_params = GetAudioParams(); | 245 ::media::AudioParameters audio_params = GetAudioParams(); |
| 315 base::TimeDelta duration = audio_params.GetBufferDuration() * frames; | 246 base::TimeDelta duration = audio_params.GetBufferDuration() * frames; |
| 316 | 247 |
| 317 base::RunLoop run_loop; | 248 base::RunLoop run_loop; |
| 318 message_loop_.task_runner()->PostDelayedTask( | 249 message_loop_.task_runner()->PostDelayedTask( |
| 319 FROM_HERE, run_loop.QuitClosure(), duration); | 250 FROM_HERE, run_loop.QuitClosure(), duration); |
| 320 run_loop.Run(); | 251 run_loop.Run(); |
| 321 } | 252 } |
| 322 | 253 |
| 323 base::MessageLoop message_loop_; | 254 base::MessageLoop message_loop_; |
| 324 std::unique_ptr<FakeAudioManager> audio_manager_; | 255 std::unique_ptr<FakeAudioManager> audio_manager_; |
| 325 scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_; | |
| 326 scoped_refptr<base::SingleThreadTaskRunner> backend_task_runner_; | |
| 327 | |
| 328 // AudioParameters used to create AudioOutputStream. | 256 // AudioParameters used to create AudioOutputStream. |
| 329 // Tests can modify these parameters before calling CreateStream. | 257 // Tests can modify these parameters before calling CreateStream. |
| 330 ::media::AudioParameters::Format format_; | 258 ::media::AudioParameters::Format format_; |
| 331 ::media::ChannelLayout channel_layout_; | 259 ::media::ChannelLayout channel_layout_; |
| 332 int sample_rate_; | 260 int sample_rate_; |
| 333 int bits_per_sample_; | 261 int bits_per_sample_; |
| 334 int frames_per_buffer_; | 262 int frames_per_buffer_; |
| 335 }; | 263 }; |
| 336 | 264 |
| 337 TEST_F(CastAudioOutputStreamTest, Format) { | 265 TEST_F(CastAudioOutputStreamTest, Format) { |
| 338 ::media::AudioParameters::Format format[] = { | 266 ::media::AudioParameters::Format format[] = { |
| 339 //::media::AudioParameters::AUDIO_PCM_LINEAR, | 267 //::media::AudioParameters::AUDIO_PCM_LINEAR, |
| 340 ::media::AudioParameters::AUDIO_PCM_LOW_LATENCY}; | 268 ::media::AudioParameters::AUDIO_PCM_LOW_LATENCY}; |
| 341 for (size_t i = 0; i < arraysize(format); ++i) { | 269 for (size_t i = 0; i < arraysize(format); ++i) { |
| 342 format_ = format[i]; | 270 format_ = format[i]; |
| 343 ::media::AudioOutputStream* stream = CreateStream(); | 271 ::media::AudioOutputStream* stream = CreateStream(); |
| 344 ASSERT_TRUE(stream); | 272 ASSERT_TRUE(stream); |
| 345 EXPECT_TRUE(OpenStream(stream)); | 273 EXPECT_TRUE(stream->Open()); |
| 346 | 274 |
| 347 FakeAudioDecoder* audio_decoder = GetAudio(); | 275 FakeAudioDecoder* audio_decoder = GetAudio(); |
| 348 ASSERT_TRUE(audio_decoder); | 276 ASSERT_TRUE(audio_decoder); |
| 349 const AudioConfig& audio_config = audio_decoder->config(); | 277 const AudioConfig& audio_config = audio_decoder->config(); |
| 350 EXPECT_EQ(kCodecPCM, audio_config.codec); | 278 EXPECT_EQ(kCodecPCM, audio_config.codec); |
| 351 EXPECT_EQ(kSampleFormatS16, audio_config.sample_format); | 279 EXPECT_EQ(kSampleFormatS16, audio_config.sample_format); |
| 352 EXPECT_FALSE(audio_config.encryption_scheme.is_encrypted()); | 280 EXPECT_FALSE(audio_config.encryption_scheme.is_encrypted()); |
| 353 | 281 |
| 354 CloseStream(stream); | 282 stream->Close(); |
| 355 } | 283 } |
| 356 } | 284 } |
| 357 | 285 |
| 358 TEST_F(CastAudioOutputStreamTest, ChannelLayout) { | 286 TEST_F(CastAudioOutputStreamTest, ChannelLayout) { |
| 359 ::media::ChannelLayout layout[] = {::media::CHANNEL_LAYOUT_MONO, | 287 ::media::ChannelLayout layout[] = {::media::CHANNEL_LAYOUT_MONO, |
| 360 ::media::CHANNEL_LAYOUT_STEREO}; | 288 ::media::CHANNEL_LAYOUT_STEREO}; |
| 361 for (size_t i = 0; i < arraysize(layout); ++i) { | 289 for (size_t i = 0; i < arraysize(layout); ++i) { |
| 362 channel_layout_ = layout[i]; | 290 channel_layout_ = layout[i]; |
| 363 ::media::AudioOutputStream* stream = CreateStream(); | 291 ::media::AudioOutputStream* stream = CreateStream(); |
| 364 ASSERT_TRUE(stream); | 292 ASSERT_TRUE(stream); |
| 365 EXPECT_TRUE(OpenStream(stream)); | 293 EXPECT_TRUE(stream->Open()); |
| 366 | 294 |
| 367 FakeAudioDecoder* audio_decoder = GetAudio(); | 295 FakeAudioDecoder* audio_decoder = GetAudio(); |
| 368 ASSERT_TRUE(audio_decoder); | 296 ASSERT_TRUE(audio_decoder); |
| 369 const AudioConfig& audio_config = audio_decoder->config(); | 297 const AudioConfig& audio_config = audio_decoder->config(); |
| 370 EXPECT_EQ(::media::ChannelLayoutToChannelCount(channel_layout_), | 298 EXPECT_EQ(::media::ChannelLayoutToChannelCount(channel_layout_), |
| 371 audio_config.channel_number); | 299 audio_config.channel_number); |
| 372 | 300 |
| 373 CloseStream(stream); | 301 stream->Close(); |
| 374 } | 302 } |
| 375 } | 303 } |
| 376 | 304 |
| 377 TEST_F(CastAudioOutputStreamTest, SampleRate) { | 305 TEST_F(CastAudioOutputStreamTest, SampleRate) { |
| 378 sample_rate_ = ::media::AudioParameters::kAudioCDSampleRate; | 306 sample_rate_ = ::media::AudioParameters::kAudioCDSampleRate; |
| 379 ::media::AudioOutputStream* stream = CreateStream(); | 307 ::media::AudioOutputStream* stream = CreateStream(); |
| 380 ASSERT_TRUE(stream); | 308 ASSERT_TRUE(stream); |
| 381 EXPECT_TRUE(OpenStream(stream)); | 309 EXPECT_TRUE(stream->Open()); |
| 382 | 310 |
| 383 FakeAudioDecoder* audio_decoder = GetAudio(); | 311 FakeAudioDecoder* audio_decoder = GetAudio(); |
| 384 ASSERT_TRUE(audio_decoder); | 312 ASSERT_TRUE(audio_decoder); |
| 385 const AudioConfig& audio_config = audio_decoder->config(); | 313 const AudioConfig& audio_config = audio_decoder->config(); |
| 386 EXPECT_EQ(sample_rate_, audio_config.samples_per_second); | 314 EXPECT_EQ(sample_rate_, audio_config.samples_per_second); |
| 387 | 315 |
| 388 CloseStream(stream); | 316 stream->Close(); |
| 389 } | 317 } |
| 390 | 318 |
| 391 TEST_F(CastAudioOutputStreamTest, BitsPerSample) { | 319 TEST_F(CastAudioOutputStreamTest, BitsPerSample) { |
| 392 bits_per_sample_ = 16; | 320 bits_per_sample_ = 16; |
| 393 ::media::AudioOutputStream* stream = CreateStream(); | 321 ::media::AudioOutputStream* stream = CreateStream(); |
| 394 ASSERT_TRUE(stream); | 322 ASSERT_TRUE(stream); |
| 395 EXPECT_TRUE(OpenStream(stream)); | 323 EXPECT_TRUE(stream->Open()); |
| 396 | 324 |
| 397 FakeAudioDecoder* audio_decoder = GetAudio(); | 325 FakeAudioDecoder* audio_decoder = GetAudio(); |
| 398 ASSERT_TRUE(audio_decoder); | 326 ASSERT_TRUE(audio_decoder); |
| 399 const AudioConfig& audio_config = audio_decoder->config(); | 327 const AudioConfig& audio_config = audio_decoder->config(); |
| 400 EXPECT_EQ(bits_per_sample_ / 8, audio_config.bytes_per_channel); | 328 EXPECT_EQ(bits_per_sample_ / 8, audio_config.bytes_per_channel); |
| 401 | 329 |
| 402 CloseStream(stream); | 330 stream->Close(); |
| 403 } | 331 } |
| 404 | 332 |
| 405 TEST_F(CastAudioOutputStreamTest, DeviceState) { | 333 TEST_F(CastAudioOutputStreamTest, DeviceState) { |
| 406 ::media::AudioOutputStream* stream = CreateStream(); | 334 ::media::AudioOutputStream* stream = CreateStream(); |
| 407 ASSERT_TRUE(stream); | 335 ASSERT_TRUE(stream); |
| 408 EXPECT_FALSE(GetAudio()); | 336 EXPECT_FALSE(GetAudio()); |
| 409 | 337 |
| 410 EXPECT_TRUE(OpenStream(stream)); | 338 EXPECT_TRUE(stream->Open()); |
| 411 FakeAudioDecoder* audio_decoder = GetAudio(); | 339 FakeAudioDecoder* audio_decoder = GetAudio(); |
| 412 ASSERT_TRUE(audio_decoder); | 340 ASSERT_TRUE(audio_decoder); |
| 413 FakeMediaPipelineBackend* backend = GetBackend(); | 341 FakeMediaPipelineBackend* backend = GetBackend(); |
| 414 ASSERT_TRUE(backend); | 342 ASSERT_TRUE(backend); |
| 415 EXPECT_EQ(FakeMediaPipelineBackend::kStateStopped, backend->state()); | 343 EXPECT_EQ(FakeMediaPipelineBackend::kStateStopped, backend->state()); |
| 416 | 344 |
| 417 std::unique_ptr<FakeAudioSourceCallback> source_callback( | 345 std::unique_ptr<FakeAudioSourceCallback> source_callback( |
| 418 new FakeAudioSourceCallback); | 346 new FakeAudioSourceCallback); |
| 419 StartStream(stream, source_callback.get()); | 347 stream->Start(source_callback.get()); |
| 420 EXPECT_EQ(FakeMediaPipelineBackend::kStateRunning, backend->state()); | 348 EXPECT_EQ(FakeMediaPipelineBackend::kStateRunning, backend->state()); |
| 421 | 349 |
| 422 StopStream(stream); | 350 stream->Stop(); |
| 423 EXPECT_EQ(FakeMediaPipelineBackend::kStatePaused, backend->state()); | 351 EXPECT_EQ(FakeMediaPipelineBackend::kStatePaused, backend->state()); |
| 424 | 352 |
| 425 CloseStream(stream); | 353 stream->Close(); |
| 426 EXPECT_FALSE(GetAudio()); | 354 EXPECT_FALSE(GetAudio()); |
| 427 } | 355 } |
| 428 | 356 |
| 429 TEST_F(CastAudioOutputStreamTest, PushFrame) { | 357 TEST_F(CastAudioOutputStreamTest, PushFrame) { |
| 430 ::media::AudioOutputStream* stream = CreateStream(); | 358 ::media::AudioOutputStream* stream = CreateStream(); |
| 431 ASSERT_TRUE(stream); | 359 ASSERT_TRUE(stream); |
| 432 EXPECT_TRUE(OpenStream(stream)); | 360 EXPECT_TRUE(stream->Open()); |
| 433 | 361 |
| 434 FakeAudioDecoder* audio_decoder = GetAudio(); | 362 FakeAudioDecoder* audio_decoder = GetAudio(); |
| 435 ASSERT_TRUE(audio_decoder); | 363 ASSERT_TRUE(audio_decoder); |
| 436 // Verify initial state. | 364 // Verify initial state. |
| 437 EXPECT_EQ(0u, audio_decoder->pushed_buffer_count()); | 365 EXPECT_EQ(0u, audio_decoder->pushed_buffer_count()); |
| 438 EXPECT_FALSE(audio_decoder->last_buffer()); | 366 EXPECT_FALSE(audio_decoder->last_buffer()); |
| 439 | 367 |
| 440 std::unique_ptr<FakeAudioSourceCallback> source_callback( | 368 std::unique_ptr<FakeAudioSourceCallback> source_callback( |
| 441 new FakeAudioSourceCallback); | 369 new FakeAudioSourceCallback); |
| 442 StartStream(stream, source_callback.get()); | 370 stream->Start(source_callback.get()); |
| 443 RunAudioLoopFor(2); | 371 RunMessageLoopFor(2); |
| 444 StopStream(stream); | 372 stream->Stop(); |
| 445 | 373 |
| 446 // Verify that the stream pushed frames to the backend. | 374 // Verify that the stream pushed frames to the backend. |
| 447 EXPECT_LT(0u, audio_decoder->pushed_buffer_count()); | 375 EXPECT_LT(0u, audio_decoder->pushed_buffer_count()); |
| 448 EXPECT_TRUE(audio_decoder->last_buffer()); | 376 EXPECT_TRUE(audio_decoder->last_buffer()); |
| 449 | 377 |
| 450 // Verify decoder buffer. | 378 // Verify decoder buffer. |
| 451 ::media::AudioParameters audio_params = GetAudioParams(); | 379 ::media::AudioParameters audio_params = GetAudioParams(); |
| 452 const size_t expected_frame_size = | 380 const size_t expected_frame_size = |
| 453 static_cast<size_t>(audio_params.GetBytesPerBuffer()); | 381 static_cast<size_t>(audio_params.GetBytesPerBuffer()); |
| 454 const CastDecoderBuffer* buffer = audio_decoder->last_buffer(); | 382 const CastDecoderBuffer* buffer = audio_decoder->last_buffer(); |
| 455 EXPECT_TRUE(buffer->data()); | 383 EXPECT_TRUE(buffer->data()); |
| 456 EXPECT_EQ(expected_frame_size, buffer->data_size()); | 384 EXPECT_EQ(expected_frame_size, buffer->data_size()); |
| 457 EXPECT_FALSE(buffer->decrypt_config()); // Null because of raw audio. | 385 EXPECT_FALSE(buffer->decrypt_config()); // Null because of raw audio. |
| 458 EXPECT_FALSE(buffer->end_of_stream()); | 386 EXPECT_FALSE(buffer->end_of_stream()); |
| 459 | 387 |
| 460 // No error must be reported to source callback. | 388 // No error must be reported to source callback. |
| 461 EXPECT_FALSE(source_callback->error()); | 389 EXPECT_FALSE(source_callback->error()); |
| 462 | 390 |
| 463 CloseStream(stream); | 391 stream->Close(); |
| 464 } | 392 } |
| 465 | 393 |
| 466 TEST_F(CastAudioOutputStreamTest, DeviceBusy) { | 394 TEST_F(CastAudioOutputStreamTest, DeviceBusy) { |
| 467 ::media::AudioOutputStream* stream = CreateStream(); | 395 ::media::AudioOutputStream* stream = CreateStream(); |
| 468 ASSERT_TRUE(stream); | 396 ASSERT_TRUE(stream); |
| 469 EXPECT_TRUE(OpenStream(stream)); | 397 EXPECT_TRUE(stream->Open()); |
| 470 | 398 |
| 471 FakeAudioDecoder* audio_decoder = GetAudio(); | 399 FakeAudioDecoder* audio_decoder = GetAudio(); |
| 472 ASSERT_TRUE(audio_decoder); | 400 ASSERT_TRUE(audio_decoder); |
| 473 audio_decoder->set_pipeline_status(FakeAudioDecoder::PIPELINE_STATUS_BUSY); | 401 audio_decoder->set_pipeline_status(FakeAudioDecoder::PIPELINE_STATUS_BUSY); |
| 474 | 402 |
| 475 std::unique_ptr<FakeAudioSourceCallback> source_callback( | 403 std::unique_ptr<FakeAudioSourceCallback> source_callback( |
| 476 new FakeAudioSourceCallback); | 404 new FakeAudioSourceCallback); |
| 477 StartStream(stream, source_callback.get()); | 405 stream->Start(source_callback.get()); |
| 478 | 406 RunMessageLoopFor(5); |
| 479 // Make sure that one frame was pushed. | 407 // Make sure that one frame was pushed. |
| 480 EXPECT_EQ(1u, audio_decoder->pushed_buffer_count()); | 408 EXPECT_EQ(1u, audio_decoder->pushed_buffer_count()); |
| 481 // No error must be reported to source callback. | 409 // No error must be reported to source callback. |
| 482 EXPECT_FALSE(source_callback->error()); | 410 EXPECT_FALSE(source_callback->error()); |
| 483 | 411 |
| 484 // Sleep for a few frames and verify that more frames were not pushed | 412 // Sleep for a few frames and verify that more frames were not pushed |
| 485 // because the backend device was busy. | 413 // because the backend device was busy. |
| 486 RunAudioLoopFor(5); | 414 RunMessageLoopFor(5); |
| 487 RunUntilIdle(backend_task_runner_.get()); | |
| 488 EXPECT_EQ(1u, audio_decoder->pushed_buffer_count()); | 415 EXPECT_EQ(1u, audio_decoder->pushed_buffer_count()); |
| 489 | 416 |
| 490 // Unblock the pipeline and verify that PushFrame resumes. | 417 // Unblock the pipeline and verify that PushFrame resumes. |
| 491 // (have to post because this directly calls buffer complete) | 418 audio_decoder->set_pipeline_status(FakeAudioDecoder::PIPELINE_STATUS_OK); |
| 492 backend_task_runner_->PostTask( | 419 RunMessageLoopFor(5); |
| 493 FROM_HERE, | |
| 494 base::Bind(&FakeAudioDecoder::set_pipeline_status, | |
| 495 base::Unretained(audio_decoder), | |
| 496 FakeAudioDecoder::PIPELINE_STATUS_OK)); | |
| 497 RunAudioLoopFor(5); | |
| 498 RunUntilIdle(backend_task_runner_.get()); | |
| 499 EXPECT_LT(1u, audio_decoder->pushed_buffer_count()); | 420 EXPECT_LT(1u, audio_decoder->pushed_buffer_count()); |
| 500 EXPECT_FALSE(source_callback->error()); | 421 EXPECT_FALSE(source_callback->error()); |
| 501 | 422 |
| 502 StopStream(stream); | 423 stream->Stop(); |
| 503 CloseStream(stream); | 424 stream->Close(); |
| 504 } | 425 } |
| 505 | 426 |
| 506 TEST_F(CastAudioOutputStreamTest, DeviceError) { | 427 TEST_F(CastAudioOutputStreamTest, DeviceError) { |
| 507 ::media::AudioOutputStream* stream = CreateStream(); | 428 ::media::AudioOutputStream* stream = CreateStream(); |
| 508 ASSERT_TRUE(stream); | 429 ASSERT_TRUE(stream); |
| 509 EXPECT_TRUE(OpenStream(stream)); | 430 EXPECT_TRUE(stream->Open()); |
| 510 | 431 |
| 511 FakeAudioDecoder* audio_decoder = GetAudio(); | 432 FakeAudioDecoder* audio_decoder = GetAudio(); |
| 512 ASSERT_TRUE(audio_decoder); | 433 ASSERT_TRUE(audio_decoder); |
| 513 audio_decoder->set_pipeline_status(FakeAudioDecoder::PIPELINE_STATUS_ERROR); | 434 audio_decoder->set_pipeline_status(FakeAudioDecoder::PIPELINE_STATUS_ERROR); |
| 514 | 435 |
| 515 std::unique_ptr<FakeAudioSourceCallback> source_callback( | 436 std::unique_ptr<FakeAudioSourceCallback> source_callback( |
| 516 new FakeAudioSourceCallback); | 437 new FakeAudioSourceCallback); |
| 517 StartStream(stream, source_callback.get()); | 438 stream->Start(source_callback.get()); |
| 518 RunAudioLoopFor(2); | 439 RunMessageLoopFor(2); |
| 519 | |
| 520 // Make sure that AudioOutputStream attempted to push the initial frame. | 440 // Make sure that AudioOutputStream attempted to push the initial frame. |
| 521 EXPECT_LT(0u, audio_decoder->pushed_buffer_count()); | 441 EXPECT_LT(0u, audio_decoder->pushed_buffer_count()); |
| 522 // AudioOutputStream must report error to source callback. | 442 // AudioOutputStream must report error to source callback. |
| 523 EXPECT_TRUE(source_callback->error()); | 443 EXPECT_TRUE(source_callback->error()); |
| 524 | 444 |
| 525 StopStream(stream); | 445 stream->Stop(); |
| 526 CloseStream(stream); | 446 stream->Close(); |
| 527 } | 447 } |
| 528 | 448 |
| 529 TEST_F(CastAudioOutputStreamTest, DeviceAsyncError) { | 449 TEST_F(CastAudioOutputStreamTest, DeviceAsyncError) { |
| 530 ::media::AudioOutputStream* stream = CreateStream(); | 450 ::media::AudioOutputStream* stream = CreateStream(); |
| 531 ASSERT_TRUE(stream); | 451 ASSERT_TRUE(stream); |
| 532 EXPECT_TRUE(OpenStream(stream)); | 452 EXPECT_TRUE(stream->Open()); |
| 533 | 453 |
| 534 FakeAudioDecoder* audio_decoder = GetAudio(); | 454 FakeAudioDecoder* audio_decoder = GetAudio(); |
| 535 ASSERT_TRUE(audio_decoder); | 455 ASSERT_TRUE(audio_decoder); |
| 536 audio_decoder->set_pipeline_status( | 456 audio_decoder->set_pipeline_status( |
| 537 FakeAudioDecoder::PIPELINE_STATUS_ASYNC_ERROR); | 457 FakeAudioDecoder::PIPELINE_STATUS_ASYNC_ERROR); |
| 538 | 458 |
| 539 std::unique_ptr<FakeAudioSourceCallback> source_callback( | 459 std::unique_ptr<FakeAudioSourceCallback> source_callback( |
| 540 new FakeAudioSourceCallback); | 460 new FakeAudioSourceCallback); |
| 541 StartStream(stream, source_callback.get()); | 461 stream->Start(source_callback.get()); |
| 542 RunAudioLoopFor(5); | 462 RunMessageLoopFor(5); |
| 543 | 463 |
| 544 // Make sure that one frame was pushed. | 464 // Make sure that one frame was pushed. |
| 545 EXPECT_EQ(1u, audio_decoder->pushed_buffer_count()); | 465 EXPECT_EQ(1u, audio_decoder->pushed_buffer_count()); |
| 546 | |
| 547 // Unblock the pipeline and verify that PushFrame resumes. | |
| 548 // (have to post because this directly calls buffer complete) | |
| 549 backend_task_runner_->PostTask( | |
| 550 FROM_HERE, | |
| 551 base::Bind(&FakeAudioDecoder::set_pipeline_status, | |
| 552 base::Unretained(audio_decoder), | |
| 553 FakeAudioDecoder::PIPELINE_STATUS_OK)); | |
| 554 | |
| 555 RunAudioLoopFor(5); | |
| 556 RunUntilIdle(backend_task_runner_.get()); | |
| 557 // AudioOutputStream must report error to source callback. | 466 // AudioOutputStream must report error to source callback. |
| 558 EXPECT_TRUE(source_callback->error()); | 467 EXPECT_TRUE(source_callback->error()); |
| 559 | 468 |
| 560 StopStream(stream); | 469 stream->Stop(); |
| 561 CloseStream(stream); | 470 stream->Close(); |
| 562 } | 471 } |
| 563 | 472 |
| 564 TEST_F(CastAudioOutputStreamTest, Volume) { | 473 TEST_F(CastAudioOutputStreamTest, Volume) { |
| 565 ::media::AudioOutputStream* stream = CreateStream(); | 474 ::media::AudioOutputStream* stream = CreateStream(); |
| 566 ASSERT_TRUE(stream); | 475 ASSERT_TRUE(stream); |
| 567 ASSERT_TRUE(OpenStream(stream)); | 476 ASSERT_TRUE(stream->Open()); |
| 568 FakeAudioDecoder* audio_decoder = GetAudio(); | 477 FakeAudioDecoder* audio_decoder = GetAudio(); |
| 569 ASSERT_TRUE(audio_decoder); | 478 ASSERT_TRUE(audio_decoder); |
| 570 | 479 |
| 571 double volume = GetStreamVolume(stream); | 480 double volume = 0.0; |
| 481 stream->GetVolume(&volume); |
| 572 EXPECT_EQ(1.0, volume); | 482 EXPECT_EQ(1.0, volume); |
| 573 EXPECT_EQ(1.0f, audio_decoder->volume()); | 483 EXPECT_EQ(1.0f, audio_decoder->volume()); |
| 574 | 484 |
| 575 SetStreamVolume(stream, 0.5); | 485 stream->SetVolume(0.5); |
| 576 volume = GetStreamVolume(stream); | 486 stream->GetVolume(&volume); |
| 577 EXPECT_EQ(0.5, volume); | 487 EXPECT_EQ(0.5, volume); |
| 578 EXPECT_EQ(0.5f, audio_decoder->volume()); | 488 EXPECT_EQ(0.5f, audio_decoder->volume()); |
| 579 | 489 |
| 580 CloseStream(stream); | 490 stream->Close(); |
| 581 } | 491 } |
| 582 | 492 |
| 583 TEST_F(CastAudioOutputStreamTest, StartStopStart) { | 493 TEST_F(CastAudioOutputStreamTest, StartStopStart) { |
| 584 ::media::AudioOutputStream* stream = CreateStream(); | 494 ::media::AudioOutputStream* stream = CreateStream(); |
| 585 ASSERT_TRUE(stream); | 495 ASSERT_TRUE(stream); |
| 586 ASSERT_TRUE(OpenStream(stream)); | 496 ASSERT_TRUE(stream->Open()); |
| 587 | 497 |
| 588 std::unique_ptr<FakeAudioSourceCallback> source_callback( | 498 std::unique_ptr<FakeAudioSourceCallback> source_callback( |
| 589 new FakeAudioSourceCallback); | 499 new FakeAudioSourceCallback); |
| 590 stream->Start(source_callback.get()); | 500 stream->Start(source_callback.get()); |
| 591 RunAudioLoopFor(2); | 501 RunMessageLoopFor(2); |
| 592 stream->Stop(); | 502 stream->Stop(); |
| 593 stream->Start(source_callback.get()); | 503 stream->Start(source_callback.get()); |
| 594 RunAudioLoopFor(2); | 504 RunMessageLoopFor(2); |
| 595 RunUntilIdle(backend_task_runner_.get()); | |
| 596 | 505 |
| 597 FakeAudioDecoder* audio_device = GetAudio(); | 506 FakeAudioDecoder* audio_device = GetAudio(); |
| 598 EXPECT_TRUE(audio_device); | 507 EXPECT_TRUE(audio_device); |
| 599 EXPECT_EQ(FakeMediaPipelineBackend::kStateRunning, GetBackend()->state()); | 508 EXPECT_EQ(FakeMediaPipelineBackend::kStateRunning, GetBackend()->state()); |
| 600 | 509 |
| 601 CloseStream(stream); | 510 stream->Close(); |
| 602 } | 511 } |
| 603 | 512 |
| 604 TEST_F(CastAudioOutputStreamTest, CloseWithoutStart) { | 513 TEST_F(CastAudioOutputStreamTest, CloseWithoutStart) { |
| 605 ::media::AudioOutputStream* stream = CreateStream(); | 514 ::media::AudioOutputStream* stream = CreateStream(); |
| 606 ASSERT_TRUE(stream); | 515 ASSERT_TRUE(stream); |
| 607 ASSERT_TRUE(OpenStream(stream)); | 516 ASSERT_TRUE(stream->Open()); |
| 608 CloseStream(stream); | 517 stream->Close(); |
| 609 } | 518 } |
| 610 | 519 |
| 611 } // namespace | 520 } // namespace |
| 612 } // namespace media | 521 } // namespace media |
| 613 } // namespace chromecast | 522 } // namespace chromecast |
| OLD | NEW |