| 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/audio/linux/alsa_input.h" | 5 #include "media/audio/linux/alsa_input.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 base::TimeDelta next_check_time = base::TimeDelta::FromMilliseconds( | 191 base::TimeDelta next_check_time = base::TimeDelta::FromMilliseconds( |
| 192 buffer_duration_ms_ / 2); | 192 buffer_duration_ms_ / 2); |
| 193 MessageLoop::current()->PostDelayedTask( | 193 MessageLoop::current()->PostDelayedTask( |
| 194 FROM_HERE, | 194 FROM_HERE, |
| 195 base::Bind(&AlsaPcmInputStream::ReadAudio, weak_factory_.GetWeakPtr()), | 195 base::Bind(&AlsaPcmInputStream::ReadAudio, weak_factory_.GetWeakPtr()), |
| 196 next_check_time); | 196 next_check_time); |
| 197 return; | 197 return; |
| 198 } | 198 } |
| 199 | 199 |
| 200 int num_buffers = frames / params_.frames_per_buffer(); | 200 int num_buffers = frames / params_.frames_per_buffer(); |
| 201 int num_buffers_read = num_buffers; | |
| 202 uint32 hardware_delay_bytes = | 201 uint32 hardware_delay_bytes = |
| 203 static_cast<uint32>(GetCurrentDelay() * params_.GetBytesPerFrame()); | 202 static_cast<uint32>(GetCurrentDelay() * params_.GetBytesPerFrame()); |
| 204 double normalized_volume = 0.0; | 203 double normalized_volume = 0.0; |
| 205 | 204 |
| 206 // Update the AGC volume level once every second. Note that, |volume| is | 205 // Update the AGC volume level once every second. Note that, |volume| is |
| 207 // also updated each time SetVolume() is called through IPC by the | 206 // also updated each time SetVolume() is called through IPC by the |
| 208 // render-side AGC. | 207 // render-side AGC. |
| 209 QueryAgcVolume(&normalized_volume); | 208 QueryAgcVolume(&normalized_volume); |
| 210 | 209 |
| 211 while (num_buffers--) { | 210 while (num_buffers--) { |
| 212 int frames_read = wrapper_->PcmReadi(device_handle_, audio_buffer_.get(), | 211 int frames_read = wrapper_->PcmReadi(device_handle_, audio_buffer_.get(), |
| 213 params_.frames_per_buffer()); | 212 params_.frames_per_buffer()); |
| 214 if (frames_read == params_.frames_per_buffer()) { | 213 if (frames_read == params_.frames_per_buffer()) { |
| 215 callback_->OnData(this, audio_buffer_.get(), bytes_per_buffer_, | 214 callback_->OnData(this, audio_buffer_.get(), bytes_per_buffer_, |
| 216 hardware_delay_bytes, normalized_volume); | 215 hardware_delay_bytes, normalized_volume); |
| 217 } else { | 216 } else { |
| 218 LOG(WARNING) << "PcmReadi returning less than expected frames: " | 217 LOG(WARNING) << "PcmReadi returning less than expected frames: " |
| 219 << frames_read << " vs. " << params_.frames_per_buffer() | 218 << frames_read << " vs. " << params_.frames_per_buffer() |
| 220 << ". Dropping this buffer."; | 219 << ". Dropping this buffer."; |
| 221 } | 220 } |
| 222 } | 221 } |
| 223 | 222 |
| 224 next_read_time_ += base::TimeDelta::FromMilliseconds( | 223 next_read_time_ += base::TimeDelta::FromMilliseconds(buffer_duration_ms_); |
| 225 buffer_duration_ms_ * num_buffers_read); | |
| 226 base::TimeDelta delay = next_read_time_ - base::Time::Now(); | 224 base::TimeDelta delay = next_read_time_ - base::Time::Now(); |
| 227 if (delay < base::TimeDelta()) { | 225 if (delay < base::TimeDelta()) { |
| 228 LOG(WARNING) << "Audio read callback behind schedule by " | 226 LOG(WARNING) << "Audio read callback behind schedule by " |
| 229 << (buffer_duration_ms_ - delay.InMilliseconds()) | 227 << (buffer_duration_ms_ - delay.InMilliseconds()) |
| 230 << " (ms)."; | 228 << " (ms)."; |
| 231 // Read callback is behind schedule. Assuming there is data pending in | 229 // Read callback is behind schedule. Assuming there is data pending in |
| 232 // the soundcard, invoke the read callback immediate in order to catch up. | 230 // the soundcard, invoke the read callback immediate in order to catch up. |
| 233 read_callback_behind_schedule_ = true; | 231 read_callback_behind_schedule_ = true; |
| 234 delay = base::TimeDelta(); | 232 delay = base::TimeDelta(); |
| 235 } | 233 } |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 | 336 |
| 339 return static_cast<double>(current_volume); | 337 return static_cast<double>(current_volume); |
| 340 } | 338 } |
| 341 | 339 |
| 342 void AlsaPcmInputStream::HandleError(const char* method, int error) { | 340 void AlsaPcmInputStream::HandleError(const char* method, int error) { |
| 343 LOG(WARNING) << method << ": " << wrapper_->StrError(error); | 341 LOG(WARNING) << method << ": " << wrapper_->StrError(error); |
| 344 callback_->OnError(this, error); | 342 callback_->OnError(this, error); |
| 345 } | 343 } |
| 346 | 344 |
| 347 } // namespace media | 345 } // namespace media |
| OLD | NEW |