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> |
(...skipping 26 matching lines...) Expand all Loading... | |
37 const scoped_refptr<MediaLog>& media_log) | 37 const scoped_refptr<MediaLog>& media_log) |
38 : task_runner_(task_runner), | 38 : task_runner_(task_runner), |
39 expecting_config_changes_(false), | 39 expecting_config_changes_(false), |
40 sink_(sink), | 40 sink_(sink), |
41 audio_buffer_stream_( | 41 audio_buffer_stream_( |
42 new AudioBufferStream(task_runner, std::move(decoders), media_log)), | 42 new AudioBufferStream(task_runner, std::move(decoders), media_log)), |
43 hardware_config_(hardware_config), | 43 hardware_config_(hardware_config), |
44 media_log_(media_log), | 44 media_log_(media_log), |
45 tick_clock_(new base::DefaultTickClock()), | 45 tick_clock_(new base::DefaultTickClock()), |
46 last_audio_memory_usage_(0), | 46 last_audio_memory_usage_(0), |
47 last_decoded_sample_rate_(0), | |
47 playback_rate_(0.0), | 48 playback_rate_(0.0), |
48 state_(kUninitialized), | 49 state_(kUninitialized), |
49 buffering_state_(BUFFERING_HAVE_NOTHING), | 50 buffering_state_(BUFFERING_HAVE_NOTHING), |
50 rendering_(false), | 51 rendering_(false), |
51 sink_playing_(false), | 52 sink_playing_(false), |
52 pending_read_(false), | 53 pending_read_(false), |
53 received_end_of_stream_(false), | 54 received_end_of_stream_(false), |
54 rendered_end_of_stream_(false), | 55 rendered_end_of_stream_(false), |
55 weak_factory_(this) { | 56 weak_factory_(this) { |
56 audio_buffer_stream_->set_splice_observer(base::Bind( | 57 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) | 363 #if defined(OS_CHROMEOS) |
363 // On ChromeOS let the OS level resampler handle resampling unless the | 364 // On ChromeOS let the OS level resampler handle resampling unless the |
364 // initial sample rate is too low; this allows support for sample rate | 365 // initial sample rate is too low; this allows support for sample rate |
365 // adaptations where necessary. | 366 // adaptations where necessary. |
366 if (stream->audio_decoder_config().samples_per_second() >= 44100) { | 367 if (stream->audio_decoder_config().samples_per_second() >= 44100) { |
367 sample_rate = stream->audio_decoder_config().samples_per_second(); | 368 sample_rate = stream->audio_decoder_config().samples_per_second(); |
368 preferred_buffer_size = 0; // No preference. | 369 preferred_buffer_size = 0; // No preference. |
369 } | 370 } |
370 #endif | 371 #endif |
371 | 372 |
372 audio_parameters_.Reset( | 373 int stream_channel_count = ChannelLayoutToChannelCount( |
373 hw_params.format(), | 374 stream->audio_decoder_config().channel_layout()); |
374 // Always use the source's channel layout to avoid premature downmixing | 375 |
375 // (http://crbug.com/379288), platform specific issues around channel | 376 // We don't know how to up-mix for DISCRETE layouts (fancy multichannel |
376 // layouts (http://crbug.com/266674), and unnecessary upmixing overhead. | 377 // hardware with non-standard speaker arrangement). Instead, pretend the |
377 stream->audio_decoder_config().channel_layout(), sample_rate, | 378 // hardware layout is 5.1 and let the OS take care of further up-mixing to |
378 hw_params.bits_per_sample(), | 379 // the discrete layout. (http://crbug.com/266674) |
379 AudioHardwareConfig::GetHighLatencyBufferSize(sample_rate, | 380 ChannelLayout hw_channel_layout = |
380 preferred_buffer_size)); | 381 (hw_params.channel_layout() == CHANNEL_LAYOUT_DISCRETE) |
DaleCurtis
2016/03/25 21:57:00
Remove unnecessary parens. Upon further considerat
chcunningham
2016/03/25 23:52:17
Done.
| |
382 ? CHANNEL_LAYOUT_5_1 | |
383 : hw_params.channel_layout(); | |
384 int hw_channel_count = ChannelLayoutToChannelCount(hw_channel_layout); | |
385 | |
386 // The layout we pass to |audio_parameters_| will be used for the lifetime | |
387 // of this audio renderer, regardless of changes to hardware and/or stream | |
388 // properties. Below we choose the max of stream layout vs. hardware layout | |
389 // to leave room for changes to the hardware and/or stream (i.e. avoid | |
390 // premature down-mixing - http://crbug.com/379288). | |
391 // If stream_channels < hw_channels: | |
392 // Taking max means we up-mix to hardware layout. If stream later changes | |
393 // to have more channels, we aren't locked into down-mixing to the | |
394 // initial stream layout. | |
395 // If stream_channels > hw_channels: | |
396 // We choose to output stream's layout, meaning mixing is a no-op for the | |
397 // renderer. Browser-side will down-mix to the hardware config. If the | |
398 // hardware later changes to equal stream channels, browser-side will stop | |
399 // down-mixing and use the data from all stream channels. | |
400 ChannelLayout renderer_channel_layout = | |
401 (hw_channel_count > stream_channel_count) | |
402 ? hw_channel_layout | |
403 : stream->audio_decoder_config().channel_layout(); | |
404 | |
405 audio_parameters_.Reset(hw_params.format(), renderer_channel_layout, | |
406 sample_rate, hw_params.bits_per_sample(), | |
407 AudioHardwareConfig::GetHighLatencyBufferSize( | |
408 sample_rate, preferred_buffer_size)); | |
381 } | 409 } |
382 | 410 |
383 audio_clock_.reset( | 411 audio_clock_.reset( |
384 new AudioClock(base::TimeDelta(), audio_parameters_.sample_rate())); | 412 new AudioClock(base::TimeDelta(), audio_parameters_.sample_rate())); |
385 | 413 |
386 audio_buffer_stream_->Initialize( | 414 audio_buffer_stream_->Initialize( |
387 stream, base::Bind(&AudioRendererImpl::OnAudioBufferStreamInitialized, | 415 stream, base::Bind(&AudioRendererImpl::OnAudioBufferStreamInitialized, |
388 weak_factory_.GetWeakPtr()), | 416 weak_factory_.GetWeakPtr()), |
389 cdm_context, statistics_cb, waiting_for_decryption_key_cb); | 417 cdm_context, statistics_cb, waiting_for_decryption_key_cb); |
390 } | 418 } |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
465 DCHECK_EQ(status, AudioBufferStream::OK); | 493 DCHECK_EQ(status, AudioBufferStream::OK); |
466 DCHECK(buffer.get()); | 494 DCHECK(buffer.get()); |
467 | 495 |
468 if (state_ == kFlushing) { | 496 if (state_ == kFlushing) { |
469 ChangeState_Locked(kFlushed); | 497 ChangeState_Locked(kFlushed); |
470 DoFlush_Locked(); | 498 DoFlush_Locked(); |
471 return; | 499 return; |
472 } | 500 } |
473 | 501 |
474 if (expecting_config_changes_) { | 502 if (expecting_config_changes_) { |
503 if (buffer->sample_rate() != last_decoded_sample_rate_) { | |
504 DVLOG(1) << __FUNCTION__ << " Updating audio sample_rate." | |
505 << " ts:" << buffer->timestamp().InMicroseconds() | |
506 << " old:" << last_decoded_sample_rate_ | |
507 << " new:" << buffer->sample_rate(); | |
508 OnConfigChange(); | |
509 } | |
510 last_decoded_sample_rate_ = buffer->sample_rate(); | |
511 | |
475 DCHECK(buffer_converter_); | 512 DCHECK(buffer_converter_); |
476 buffer_converter_->AddInput(buffer); | 513 buffer_converter_->AddInput(buffer); |
477 while (buffer_converter_->HasNextBuffer()) { | 514 while (buffer_converter_->HasNextBuffer()) { |
478 if (!splicer_->AddInput(buffer_converter_->GetNextBuffer())) { | 515 if (!splicer_->AddInput(buffer_converter_->GetNextBuffer())) { |
479 HandleAbortedReadOrDecodeError(AUDIO_RENDERER_ERROR_SPLICE_FAILED); | 516 HandleAbortedReadOrDecodeError(AUDIO_RENDERER_ERROR_SPLICE_FAILED); |
480 return; | 517 return; |
481 } | 518 } |
482 } | 519 } |
483 } else { | 520 } else { |
484 if (!splicer_->AddInput(buffer)) { | 521 if (!splicer_->AddInput(buffer)) { |
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
827 << buffering_state; | 864 << buffering_state; |
828 DCHECK_NE(buffering_state_, buffering_state); | 865 DCHECK_NE(buffering_state_, buffering_state); |
829 lock_.AssertAcquired(); | 866 lock_.AssertAcquired(); |
830 buffering_state_ = buffering_state; | 867 buffering_state_ = buffering_state; |
831 | 868 |
832 task_runner_->PostTask(FROM_HERE, | 869 task_runner_->PostTask(FROM_HERE, |
833 base::Bind(buffering_state_cb_, buffering_state_)); | 870 base::Bind(buffering_state_cb_, buffering_state_)); |
834 } | 871 } |
835 | 872 |
836 } // namespace media | 873 } // namespace media |
OLD | NEW |