| 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/cma/backend/alsa/stream_mixer_alsa_input_impl.h" | 5 #include "chromecast/media/cma/backend/alsa/stream_mixer_alsa_input_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 | 58 |
| 59 const int kMaxSlewTimeUpMs = 15; | 59 const int kMaxSlewTimeUpMs = 15; |
| 60 const int kMaxSlewTimeDownMs = 15; | 60 const int kMaxSlewTimeDownMs = 15; |
| 61 | 61 |
| 62 } // namespace | 62 } // namespace |
| 63 | 63 |
| 64 StreamMixerAlsaInputImpl::StreamMixerAlsaInputImpl( | 64 StreamMixerAlsaInputImpl::StreamMixerAlsaInputImpl( |
| 65 StreamMixerAlsaInput::Delegate* delegate, | 65 StreamMixerAlsaInput::Delegate* delegate, |
| 66 int input_samples_per_second, | 66 int input_samples_per_second, |
| 67 bool primary, | 67 bool primary, |
| 68 std::string name, |
| 68 StreamMixerAlsa* mixer) | 69 StreamMixerAlsa* mixer) |
| 69 : delegate_(delegate), | 70 : delegate_(delegate), |
| 70 input_samples_per_second_(input_samples_per_second), | 71 input_samples_per_second_(input_samples_per_second), |
| 71 primary_(primary), | 72 primary_(primary), |
| 73 name_(name), |
| 72 mixer_(mixer), | 74 mixer_(mixer), |
| 73 mixer_task_runner_(mixer_->task_runner()), | 75 mixer_task_runner_(mixer_->task_runner()), |
| 74 caller_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 76 caller_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 75 resample_ratio_(1.0), | 77 resample_ratio_(1.0), |
| 76 state_(kStateUninitialized), | 78 state_(kStateUninitialized), |
| 77 slew_volume_(kMaxSlewTimeUpMs, kMaxSlewTimeDownMs), | 79 slew_volume_(kMaxSlewTimeUpMs, kMaxSlewTimeDownMs), |
| 78 queued_frames_(0), | 80 queued_frames_(0), |
| 79 queued_frames_including_resampler_(0), | 81 queued_frames_including_resampler_(0), |
| 80 current_buffer_offset_(0), | 82 current_buffer_offset_(0), |
| 81 max_queued_frames_(kMaxInputQueueUs * input_samples_per_second / | 83 max_queued_frames_(kMaxInputQueueUs * input_samples_per_second / |
| 82 base::Time::kMicrosecondsPerSecond), | 84 base::Time::kMicrosecondsPerSecond), |
| 83 fade_frames_remaining_(0), | 85 fade_frames_remaining_(0), |
| 84 fade_out_frames_total_(0), | 86 fade_out_frames_total_(0), |
| 85 zeroed_frames_(0), | 87 zeroed_frames_(0), |
| 86 is_underflowing_(false), | 88 is_underflowing_(false), |
| 87 weak_factory_(this) { | 89 weak_factory_(this) { |
| 88 LOG(INFO) << "Create " << this; | 90 LOG(INFO) << "Create " << name_ << " (" << this << ")"; |
| 89 DCHECK(delegate_); | 91 DCHECK(delegate_); |
| 90 DCHECK(mixer_); | 92 DCHECK(mixer_); |
| 91 weak_this_ = weak_factory_.GetWeakPtr(); | 93 weak_this_ = weak_factory_.GetWeakPtr(); |
| 92 } | 94 } |
| 93 | 95 |
| 94 StreamMixerAlsaInputImpl::~StreamMixerAlsaInputImpl() { | 96 StreamMixerAlsaInputImpl::~StreamMixerAlsaInputImpl() { |
| 95 LOG(INFO) << "Destroy " << this; | 97 LOG(INFO) << "Destroy " << name_ << " (" << this << ")"; |
| 96 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); | 98 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); |
| 97 } | 99 } |
| 98 | 100 |
| 99 int StreamMixerAlsaInputImpl::input_samples_per_second() const { | 101 int StreamMixerAlsaInputImpl::input_samples_per_second() const { |
| 100 return input_samples_per_second_; | 102 return input_samples_per_second_; |
| 101 } | 103 } |
| 102 | 104 |
| 103 bool StreamMixerAlsaInputImpl::primary() const { | 105 bool StreamMixerAlsaInputImpl::primary() const { |
| 104 return primary_; | 106 return primary_; |
| 105 } | 107 } |
| 106 | 108 |
| 109 std::string StreamMixerAlsaInputImpl::name() const { |
| 110 return name_; |
| 111 } |
| 112 |
| 107 bool StreamMixerAlsaInputImpl::IsDeleting() const { | 113 bool StreamMixerAlsaInputImpl::IsDeleting() const { |
| 108 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); | 114 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); |
| 109 return (state_ == kStateFinalFade || state_ == kStateDeleted); | 115 return (state_ == kStateFinalFade || state_ == kStateDeleted); |
| 110 } | 116 } |
| 111 | 117 |
| 112 void StreamMixerAlsaInputImpl::Initialize( | 118 void StreamMixerAlsaInputImpl::Initialize( |
| 113 const MediaPipelineBackendAlsa::RenderingDelay& mixer_rendering_delay) { | 119 const MediaPipelineBackendAlsa::RenderingDelay& mixer_rendering_delay) { |
| 114 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); | 120 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); |
| 115 DCHECK(!IsDeleting()); | 121 DCHECK(!IsDeleting()); |
| 116 if (mixer_->output_samples_per_second() != input_samples_per_second_) { | 122 if (mixer_->output_samples_per_second() != input_samples_per_second_) { |
| (...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 int StreamMixerAlsaInputImpl::NormalFadeFrames() { | 425 int StreamMixerAlsaInputImpl::NormalFadeFrames() { |
| 420 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); | 426 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); |
| 421 int frames = (mixer_->output_samples_per_second() * kFadeMs / | 427 int frames = (mixer_->output_samples_per_second() * kFadeMs / |
| 422 base::Time::kMillisecondsPerSecond) - | 428 base::Time::kMillisecondsPerSecond) - |
| 423 1; | 429 1; |
| 424 return std::max(frames, 0); | 430 return std::max(frames, 0); |
| 425 } | 431 } |
| 426 | 432 |
| 427 void StreamMixerAlsaInputImpl::FadeIn(::media::AudioBus* dest, int frames) { | 433 void StreamMixerAlsaInputImpl::FadeIn(::media::AudioBus* dest, int frames) { |
| 428 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); | 434 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); |
| 429 LOG(INFO) << "Fading in, " << fade_frames_remaining_ << " frames remaining"; | |
| 430 float fade_in_frames = mixer_->output_samples_per_second() * kFadeMs / | 435 float fade_in_frames = mixer_->output_samples_per_second() * kFadeMs / |
| 431 base::Time::kMillisecondsPerSecond; | 436 base::Time::kMillisecondsPerSecond; |
| 432 for (int f = 0; f < frames && fade_frames_remaining_; ++f) { | 437 for (int f = 0; f < frames && fade_frames_remaining_; ++f) { |
| 433 float fade_multiplier = 1.0 - fade_frames_remaining_ / fade_in_frames; | 438 float fade_multiplier = 1.0 - fade_frames_remaining_ / fade_in_frames; |
| 434 for (int c = 0; c < kNumOutputChannels; ++c) | 439 for (int c = 0; c < kNumOutputChannels; ++c) |
| 435 dest->channel(c)[f] *= fade_multiplier; | 440 dest->channel(c)[f] *= fade_multiplier; |
| 436 --fade_frames_remaining_; | 441 --fade_frames_remaining_; |
| 437 } | 442 } |
| 438 } | 443 } |
| 439 | 444 |
| 440 void StreamMixerAlsaInputImpl::FadeOut(::media::AudioBus* dest, int frames) { | 445 void StreamMixerAlsaInputImpl::FadeOut(::media::AudioBus* dest, int frames) { |
| 441 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); | 446 DCHECK(mixer_task_runner_->BelongsToCurrentThread()); |
| 442 LOG(INFO) << "Fading out, " << fade_frames_remaining_ << " frames remaining"; | |
| 443 int f = 0; | 447 int f = 0; |
| 444 for (; f < frames && fade_frames_remaining_; ++f) { | 448 for (; f < frames && fade_frames_remaining_; ++f) { |
| 445 float fade_multiplier = | 449 float fade_multiplier = |
| 446 fade_frames_remaining_ / static_cast<float>(fade_out_frames_total_); | 450 fade_frames_remaining_ / static_cast<float>(fade_out_frames_total_); |
| 447 for (int c = 0; c < kNumOutputChannels; ++c) | 451 for (int c = 0; c < kNumOutputChannels; ++c) |
| 448 dest->channel(c)[f] *= fade_multiplier; | 452 dest->channel(c)[f] *= fade_multiplier; |
| 449 --fade_frames_remaining_; | 453 --fade_frames_remaining_; |
| 450 } | 454 } |
| 451 // Zero remaining frames | 455 // Zero remaining frames |
| 452 for (; f < frames; ++f) { | 456 for (; f < frames; ++f) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 | 512 |
| 509 if (state_ == kStateFadingOut) { | 513 if (state_ == kStateFadingOut) { |
| 510 // Tell the mixer that some more data might be available (since when fading | 514 // Tell the mixer that some more data might be available (since when fading |
| 511 // out, we can drain the queue completely). | 515 // out, we can drain the queue completely). |
| 512 mixer_->OnFramesQueued(); | 516 mixer_->OnFramesQueued(); |
| 513 } | 517 } |
| 514 } | 518 } |
| 515 | 519 |
| 516 void StreamMixerAlsaInputImpl::SetVolumeMultiplier(float multiplier) { | 520 void StreamMixerAlsaInputImpl::SetVolumeMultiplier(float multiplier) { |
| 517 RUN_ON_MIXER_THREAD(SetVolumeMultiplier, multiplier); | 521 RUN_ON_MIXER_THREAD(SetVolumeMultiplier, multiplier); |
| 518 LOG(INFO) << this << ": stream volume = " << multiplier; | 522 LOG(INFO) << name_ << "(" << this << "): stream volume = " << multiplier; |
| 519 DCHECK(!IsDeleting()); | 523 DCHECK(!IsDeleting()); |
| 520 if (multiplier > 1.0f) | 524 if (multiplier > 1.0f) |
| 521 multiplier = 1.0f; | 525 multiplier = 1.0f; |
| 522 if (multiplier < 0.0f) | 526 if (multiplier < 0.0f) |
| 523 multiplier = 0.0f; | 527 multiplier = 0.0f; |
| 524 slew_volume_.SetVolume(multiplier); | 528 slew_volume_.SetVolume(multiplier); |
| 525 } | 529 } |
| 526 | 530 |
| 527 void StreamMixerAlsaInputImpl::VolumeScaleAccumulate(bool repeat_transition, | 531 void StreamMixerAlsaInputImpl::VolumeScaleAccumulate(bool repeat_transition, |
| 528 const float* src, | 532 const float* src, |
| 529 int frames, | 533 int frames, |
| 530 float* dest) { | 534 float* dest) { |
| 531 slew_volume_.ProcessFMAC(repeat_transition, src, frames, dest); | 535 slew_volume_.ProcessFMAC(repeat_transition, src, frames, dest); |
| 532 } | 536 } |
| 533 | 537 |
| 534 } // namespace media | 538 } // namespace media |
| 535 } // namespace chromecast | 539 } // namespace chromecast |
| OLD | NEW |