OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "media/renderers/audio_renderer_impl.h" | 5 #include "media/renderers/audio_renderer_impl.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/callback_helpers.h" | 14 #include "base/callback_helpers.h" |
| 15 #include "base/command_line.h" |
15 #include "base/logging.h" | 16 #include "base/logging.h" |
16 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
17 #include "base/time/default_tick_clock.h" | 18 #include "base/time/default_tick_clock.h" |
18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
19 #include "media/base/audio_buffer.h" | 20 #include "media/base/audio_buffer.h" |
20 #include "media/base/audio_buffer_converter.h" | 21 #include "media/base/audio_buffer_converter.h" |
21 #include "media/base/audio_hardware_config.h" | 22 #include "media/base/audio_hardware_config.h" |
22 #include "media/base/audio_splicer.h" | 23 #include "media/base/audio_splicer.h" |
23 #include "media/base/bind_to_current_loop.h" | 24 #include "media/base/bind_to_current_loop.h" |
24 #include "media/base/demuxer_stream.h" | 25 #include "media/base/demuxer_stream.h" |
25 #include "media/base/media_log.h" | 26 #include "media/base/media_log.h" |
| 27 #include "media/base/media_switches.h" |
26 #include "media/base/timestamp_constants.h" | 28 #include "media/base/timestamp_constants.h" |
27 #include "media/filters/audio_clock.h" | 29 #include "media/filters/audio_clock.h" |
28 #include "media/filters/decrypting_demuxer_stream.h" | 30 #include "media/filters/decrypting_demuxer_stream.h" |
29 | 31 |
30 namespace media { | 32 namespace media { |
31 | 33 |
32 AudioRendererImpl::AudioRendererImpl( | 34 AudioRendererImpl::AudioRendererImpl( |
33 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 35 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
34 media::AudioRendererSink* sink, | 36 media::AudioRendererSink* sink, |
35 ScopedVector<AudioDecoder> decoders, | 37 ScopedVector<AudioDecoder> decoders, |
36 const AudioHardwareConfig& hardware_config, | 38 const AudioHardwareConfig& hardware_config, |
37 const scoped_refptr<MediaLog>& media_log) | 39 const scoped_refptr<MediaLog>& media_log) |
38 : task_runner_(task_runner), | 40 : task_runner_(task_runner), |
39 expecting_config_changes_(false), | 41 expecting_config_changes_(false), |
40 sink_(sink), | 42 sink_(sink), |
41 audio_buffer_stream_( | 43 audio_buffer_stream_( |
42 new AudioBufferStream(task_runner, std::move(decoders), media_log)), | 44 new AudioBufferStream(task_runner, std::move(decoders), media_log)), |
43 hardware_config_(hardware_config), | 45 hardware_config_(hardware_config), |
44 media_log_(media_log), | 46 media_log_(media_log), |
45 tick_clock_(new base::DefaultTickClock()), | 47 tick_clock_(new base::DefaultTickClock()), |
46 last_audio_memory_usage_(0), | 48 last_audio_memory_usage_(0), |
| 49 last_decoded_sample_rate_(0), |
47 playback_rate_(0.0), | 50 playback_rate_(0.0), |
48 state_(kUninitialized), | 51 state_(kUninitialized), |
49 buffering_state_(BUFFERING_HAVE_NOTHING), | 52 buffering_state_(BUFFERING_HAVE_NOTHING), |
50 rendering_(false), | 53 rendering_(false), |
51 sink_playing_(false), | 54 sink_playing_(false), |
52 pending_read_(false), | 55 pending_read_(false), |
53 received_end_of_stream_(false), | 56 received_end_of_stream_(false), |
54 rendered_end_of_stream_(false), | 57 rendered_end_of_stream_(false), |
55 weak_factory_(this) { | 58 weak_factory_(this) { |
56 audio_buffer_stream_->set_splice_observer(base::Bind( | 59 audio_buffer_stream_->set_splice_observer(base::Bind( |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 #if defined(OS_CHROMEOS) | 365 #if defined(OS_CHROMEOS) |
363 // On ChromeOS let the OS level resampler handle resampling unless the | 366 // On ChromeOS let the OS level resampler handle resampling unless the |
364 // initial sample rate is too low; this allows support for sample rate | 367 // initial sample rate is too low; this allows support for sample rate |
365 // adaptations where necessary. | 368 // adaptations where necessary. |
366 if (stream->audio_decoder_config().samples_per_second() >= 44100) { | 369 if (stream->audio_decoder_config().samples_per_second() >= 44100) { |
367 sample_rate = stream->audio_decoder_config().samples_per_second(); | 370 sample_rate = stream->audio_decoder_config().samples_per_second(); |
368 preferred_buffer_size = 0; // No preference. | 371 preferred_buffer_size = 0; // No preference. |
369 } | 372 } |
370 #endif | 373 #endif |
371 | 374 |
372 audio_parameters_.Reset( | 375 int stream_channel_count = ChannelLayoutToChannelCount( |
373 hw_params.format(), | 376 stream->audio_decoder_config().channel_layout()); |
374 // Always use the source's channel layout to avoid premature downmixing | 377 |
375 // (http://crbug.com/379288), platform specific issues around channel | 378 bool try_supported_channel_layouts = false; |
376 // layouts (http://crbug.com/266674), and unnecessary upmixing overhead. | 379 #if defined(OS_WIN) |
377 stream->audio_decoder_config().channel_layout(), sample_rate, | 380 try_supported_channel_layouts = |
378 hw_params.bits_per_sample(), | 381 base::CommandLine::ForCurrentProcess()->HasSwitch( |
379 AudioHardwareConfig::GetHighLatencyBufferSize(sample_rate, | 382 switches::kTrySupportedChannelLayouts); |
380 preferred_buffer_size)); | 383 #endif |
| 384 |
| 385 // We don't know how to up-mix for DISCRETE layouts (fancy multichannel |
| 386 // hardware with non-standard speaker arrangement). Instead, pretend the |
| 387 // hardware layout is stereo and let the OS take care of further up-mixing |
| 388 // to the discrete layout (http://crbug.com/266674). Additionally, pretend |
| 389 // hardware is stereo whenever kTrySupportedChannelLayouts is set. This flag |
| 390 // is for savvy users who want stereo content to output in all surround |
| 391 // speakers. Using the actual layout (likely 5.1 or higher) will mean our |
| 392 // mixer will attempt to up-mix stereo source streams to just the left/right |
| 393 // speaker of the 5.1 setup, nulling out the other channels |
| 394 // (http://crbug.com/177872). |
| 395 ChannelLayout hw_channel_layout = |
| 396 hw_params.channel_layout() == CHANNEL_LAYOUT_DISCRETE || |
| 397 try_supported_channel_layouts |
| 398 ? CHANNEL_LAYOUT_STEREO |
| 399 : hw_params.channel_layout(); |
| 400 int hw_channel_count = ChannelLayoutToChannelCount(hw_channel_layout); |
| 401 |
| 402 // The layout we pass to |audio_parameters_| will be used for the lifetime |
| 403 // of this audio renderer, regardless of changes to hardware and/or stream |
| 404 // properties. Below we choose the max of stream layout vs. hardware layout |
| 405 // to leave room for changes to the hardware and/or stream (i.e. avoid |
| 406 // premature down-mixing - http://crbug.com/379288). |
| 407 // If stream_channels < hw_channels: |
| 408 // Taking max means we up-mix to hardware layout. If stream later changes |
| 409 // to have more channels, we aren't locked into down-mixing to the |
| 410 // initial stream layout. |
| 411 // If stream_channels > hw_channels: |
| 412 // We choose to output stream's layout, meaning mixing is a no-op for the |
| 413 // renderer. Browser-side will down-mix to the hardware config. If the |
| 414 // hardware later changes to equal stream channels, browser-side will stop |
| 415 // down-mixing and use the data from all stream channels. |
| 416 ChannelLayout renderer_channel_layout = |
| 417 hw_channel_count > stream_channel_count |
| 418 ? hw_channel_layout |
| 419 : stream->audio_decoder_config().channel_layout(); |
| 420 |
| 421 audio_parameters_.Reset(hw_params.format(), renderer_channel_layout, |
| 422 sample_rate, hw_params.bits_per_sample(), |
| 423 AudioHardwareConfig::GetHighLatencyBufferSize( |
| 424 sample_rate, preferred_buffer_size)); |
381 } | 425 } |
382 | 426 |
383 audio_clock_.reset( | 427 audio_clock_.reset( |
384 new AudioClock(base::TimeDelta(), audio_parameters_.sample_rate())); | 428 new AudioClock(base::TimeDelta(), audio_parameters_.sample_rate())); |
385 | 429 |
386 audio_buffer_stream_->Initialize( | 430 audio_buffer_stream_->Initialize( |
387 stream, base::Bind(&AudioRendererImpl::OnAudioBufferStreamInitialized, | 431 stream, base::Bind(&AudioRendererImpl::OnAudioBufferStreamInitialized, |
388 weak_factory_.GetWeakPtr()), | 432 weak_factory_.GetWeakPtr()), |
389 cdm_context, statistics_cb, waiting_for_decryption_key_cb); | 433 cdm_context, statistics_cb, waiting_for_decryption_key_cb); |
390 } | 434 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 DCHECK_EQ(status, AudioBufferStream::OK); | 509 DCHECK_EQ(status, AudioBufferStream::OK); |
466 DCHECK(buffer.get()); | 510 DCHECK(buffer.get()); |
467 | 511 |
468 if (state_ == kFlushing) { | 512 if (state_ == kFlushing) { |
469 ChangeState_Locked(kFlushed); | 513 ChangeState_Locked(kFlushed); |
470 DoFlush_Locked(); | 514 DoFlush_Locked(); |
471 return; | 515 return; |
472 } | 516 } |
473 | 517 |
474 if (expecting_config_changes_) { | 518 if (expecting_config_changes_) { |
| 519 if (last_decoded_sample_rate_ && |
| 520 buffer->sample_rate() != last_decoded_sample_rate_) { |
| 521 DVLOG(1) << __FUNCTION__ << " Updating audio sample_rate." |
| 522 << " ts:" << buffer->timestamp().InMicroseconds() |
| 523 << " old:" << last_decoded_sample_rate_ |
| 524 << " new:" << buffer->sample_rate(); |
| 525 OnConfigChange(); |
| 526 } |
| 527 last_decoded_sample_rate_ = buffer->sample_rate(); |
| 528 |
475 DCHECK(buffer_converter_); | 529 DCHECK(buffer_converter_); |
476 buffer_converter_->AddInput(buffer); | 530 buffer_converter_->AddInput(buffer); |
477 while (buffer_converter_->HasNextBuffer()) { | 531 while (buffer_converter_->HasNextBuffer()) { |
478 if (!splicer_->AddInput(buffer_converter_->GetNextBuffer())) { | 532 if (!splicer_->AddInput(buffer_converter_->GetNextBuffer())) { |
479 HandleAbortedReadOrDecodeError(AUDIO_RENDERER_ERROR_SPLICE_FAILED); | 533 HandleAbortedReadOrDecodeError(AUDIO_RENDERER_ERROR_SPLICE_FAILED); |
480 return; | 534 return; |
481 } | 535 } |
482 } | 536 } |
483 } else { | 537 } else { |
484 if (!splicer_->AddInput(buffer)) { | 538 if (!splicer_->AddInput(buffer)) { |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
827 << buffering_state; | 881 << buffering_state; |
828 DCHECK_NE(buffering_state_, buffering_state); | 882 DCHECK_NE(buffering_state_, buffering_state); |
829 lock_.AssertAcquired(); | 883 lock_.AssertAcquired(); |
830 buffering_state_ = buffering_state; | 884 buffering_state_ = buffering_state; |
831 | 885 |
832 task_runner_->PostTask(FROM_HERE, | 886 task_runner_->PostTask(FROM_HERE, |
833 base::Bind(buffering_state_cb_, buffering_state_)); | 887 base::Bind(buffering_state_cb_, buffering_state_)); |
834 } | 888 } |
835 | 889 |
836 } // namespace media | 890 } // namespace media |
OLD | NEW |