OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/audio/linux/alsa_input.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/scoped_ptr.h" |
| 11 #include "base/time.h" |
| 12 #include "media/audio/linux/alsa_util.h" |
| 13 #include "media/audio/linux/alsa_wrapper.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 const int kNumPacketsInRingBuffer = 3; |
| 18 |
| 19 // If a read failed with no audio data, try again after this duration. |
| 20 const int kNoAudioReadAgainTimeoutMs = 20; |
| 21 |
| 22 const char kDefaultDevice1[] = "default"; |
| 23 const char kDefaultDevice2[] = "plug:default"; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 const char* AlsaPcmInputStream::kAutoSelectDevice = ""; |
| 28 |
| 29 AlsaPcmInputStream::AlsaPcmInputStream(const std::string& device_name, |
| 30 const AudioParameters& params, |
| 31 int samples_per_packet, |
| 32 AlsaWrapper* wrapper) |
| 33 : device_name_(device_name), |
| 34 params_(params), |
| 35 samples_per_packet_(samples_per_packet), |
| 36 bytes_per_packet_(samples_per_packet_ * |
| 37 (params.channels * params.bits_per_sample) / 8), |
| 38 wrapper_(wrapper), |
| 39 packet_duration_ms_( |
| 40 (samples_per_packet_ * base::Time::kMillisecondsPerSecond) / |
| 41 params.sample_rate), |
| 42 callback_(NULL), |
| 43 device_handle_(NULL), |
| 44 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { |
| 45 } |
| 46 |
| 47 bool AlsaPcmInputStream::Open() { |
| 48 if (device_handle_) |
| 49 return false; // Already open. |
| 50 |
| 51 snd_pcm_format_t pcm_format = alsa_util::BitsToFormat( |
| 52 params_.bits_per_sample); |
| 53 if (pcm_format == SND_PCM_FORMAT_UNKNOWN) { |
| 54 LOG(WARNING) << "Unsupported bits per sample: " |
| 55 << params_.bits_per_sample; |
| 56 return false; |
| 57 } |
| 58 |
| 59 int latency_us = packet_duration_ms_ * kNumPacketsInRingBuffer * |
| 60 base::Time::kMicrosecondsPerMillisecond; |
| 61 if (device_name_ == kAutoSelectDevice) { |
| 62 device_handle_ = alsa_util::OpenCaptureDevice(wrapper_, kDefaultDevice1, |
| 63 params_.channels, |
| 64 params_.sample_rate, |
| 65 pcm_format, latency_us); |
| 66 if (!device_handle_) { |
| 67 device_handle_ = alsa_util::OpenCaptureDevice(wrapper_, kDefaultDevice2, |
| 68 params_.channels, |
| 69 params_.sample_rate, |
| 70 pcm_format, latency_us); |
| 71 } |
| 72 } else { |
| 73 device_handle_ = alsa_util::OpenCaptureDevice(wrapper_, |
| 74 device_name_.c_str(), |
| 75 params_.channels, |
| 76 params_.sample_rate, |
| 77 pcm_format, latency_us); |
| 78 } |
| 79 |
| 80 if (device_handle_) |
| 81 audio_packet_.reset(new uint8[bytes_per_packet_]); |
| 82 |
| 83 return device_handle_ != NULL; |
| 84 } |
| 85 |
| 86 void AlsaPcmInputStream::Start(AudioInputCallback* callback) { |
| 87 DCHECK(!callback_ && callback); |
| 88 callback_ = callback; |
| 89 int error = wrapper_->PcmPrepare(device_handle_); |
| 90 if (error < 0) { |
| 91 HandleError("PcmPrepare", error); |
| 92 } else { |
| 93 error = wrapper_->PcmStart(device_handle_); |
| 94 if (error < 0) |
| 95 HandleError("PcmStart", error); |
| 96 } |
| 97 |
| 98 if (error < 0) { |
| 99 callback_ = NULL; |
| 100 } else { |
| 101 // We start reading data a little later than when the packet might have got |
| 102 // filled, to accommodate some delays in the audio driver. This could |
| 103 // also give us a smooth read sequence going forward. |
| 104 int64 delay_ms = packet_duration_ms_ + kNoAudioReadAgainTimeoutMs; |
| 105 next_read_time_ = base::Time::Now() + base::TimeDelta::FromMilliseconds( |
| 106 delay_ms); |
| 107 MessageLoop::current()->PostDelayedTask( |
| 108 FROM_HERE, |
| 109 task_factory_.NewRunnableMethod(&AlsaPcmInputStream::ReadAudio), |
| 110 delay_ms); |
| 111 } |
| 112 } |
| 113 |
| 114 bool AlsaPcmInputStream::Recover(int original_error) { |
| 115 int error = wrapper_->PcmRecover(device_handle_, original_error, 1); |
| 116 if (error < 0) { |
| 117 // Docs say snd_pcm_recover returns the original error if it is not one |
| 118 // of the recoverable ones, so this log message will probably contain the |
| 119 // same error twice. |
| 120 LOG(WARNING) << "Unable to recover from \"" |
| 121 << wrapper_->StrError(original_error) << "\": " |
| 122 << wrapper_->StrError(error); |
| 123 return false; |
| 124 } |
| 125 |
| 126 if (original_error == -EPIPE) { // Buffer underrun/overrun. |
| 127 // For capture streams we have to repeat the explicit start() to get |
| 128 // data flowing again. |
| 129 error = wrapper_->PcmStart(device_handle_); |
| 130 if (error < 0) { |
| 131 HandleError("PcmStart", error); |
| 132 return false; |
| 133 } |
| 134 } |
| 135 |
| 136 return true; |
| 137 } |
| 138 |
| 139 void AlsaPcmInputStream::ReadAudio() { |
| 140 DCHECK(callback_); |
| 141 |
| 142 snd_pcm_sframes_t frames = wrapper_->PcmAvailUpdate(device_handle_); |
| 143 if (frames < 0) { // Potentially recoverable error? |
| 144 LOG(WARNING) << "PcmAvailUpdate(): " << wrapper_->StrError(frames); |
| 145 Recover(frames); |
| 146 } |
| 147 |
| 148 if (frames < samples_per_packet_) { |
| 149 // Not enough data yet or error happened. In both cases wait for a very |
| 150 // small duration before checking again. |
| 151 MessageLoop::current()->PostDelayedTask( |
| 152 FROM_HERE, |
| 153 task_factory_.NewRunnableMethod(&AlsaPcmInputStream::ReadAudio), |
| 154 kNoAudioReadAgainTimeoutMs); |
| 155 return; |
| 156 } |
| 157 |
| 158 int num_packets = frames / samples_per_packet_; |
| 159 while (num_packets--) { |
| 160 int frames_read = wrapper_->PcmReadi(device_handle_, audio_packet_.get(), |
| 161 samples_per_packet_); |
| 162 if (frames_read == samples_per_packet_) { |
| 163 callback_->OnData(this, audio_packet_.get(), bytes_per_packet_); |
| 164 } else { |
| 165 LOG(WARNING) << "PcmReadi returning less than expected frames: " |
| 166 << frames_read << " vs. " << samples_per_packet_ |
| 167 << ". Dropping this packet."; |
| 168 } |
| 169 } |
| 170 |
| 171 next_read_time_ += base::TimeDelta::FromMilliseconds(packet_duration_ms_); |
| 172 int64 delay_ms = (next_read_time_ - base::Time::Now()).InMilliseconds(); |
| 173 if (delay_ms < 0) { |
| 174 LOG(WARNING) << "Audio read callback behind schedule by " |
| 175 << (packet_duration_ms_ - delay_ms) << " (ms)."; |
| 176 delay_ms = 0; |
| 177 } |
| 178 |
| 179 MessageLoop::current()->PostDelayedTask( |
| 180 FROM_HERE, |
| 181 task_factory_.NewRunnableMethod(&AlsaPcmInputStream::ReadAudio), |
| 182 delay_ms); |
| 183 } |
| 184 |
| 185 void AlsaPcmInputStream::Stop() { |
| 186 if (!device_handle_ || !callback_) |
| 187 return; |
| 188 |
| 189 task_factory_.RevokeAll(); // Cancel the next scheduled read. |
| 190 int error = wrapper_->PcmDrop(device_handle_); |
| 191 if (error < 0) |
| 192 HandleError("PcmDrop", error); |
| 193 } |
| 194 |
| 195 void AlsaPcmInputStream::Close() { |
| 196 // Check in case we were already closed or not initialized yet. |
| 197 if (!device_handle_ || !callback_) |
| 198 return; |
| 199 |
| 200 task_factory_.RevokeAll(); // Cancel the next scheduled read. |
| 201 int error = alsa_util::CloseDevice(wrapper_, device_handle_); |
| 202 if (error < 0) |
| 203 HandleError("PcmClose", error); |
| 204 |
| 205 audio_packet_.reset(); |
| 206 device_handle_ = NULL; |
| 207 callback_->OnClose(this); |
| 208 } |
| 209 |
| 210 void AlsaPcmInputStream::HandleError(const char* method, int error) { |
| 211 LOG(WARNING) << method << ": " << wrapper_->StrError(error); |
| 212 callback_->OnError(this, error); |
| 213 } |
| 214 |
OLD | NEW |