| 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/cras/cras_input.h" | 5 #include "media/audio/cras/cras_input.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 callback_ = callback; | 110 callback_ = callback; |
| 111 | 111 |
| 112 // Prepare |audio_format| and |stream_params| for the stream we | 112 // Prepare |audio_format| and |stream_params| for the stream we |
| 113 // will create. | 113 // will create. |
| 114 cras_audio_format* audio_format = cras_audio_format_create( | 114 cras_audio_format* audio_format = cras_audio_format_create( |
| 115 alsa_util::BitsToFormat(params_.bits_per_sample()), | 115 alsa_util::BitsToFormat(params_.bits_per_sample()), |
| 116 params_.sample_rate(), | 116 params_.sample_rate(), |
| 117 params_.channels()); | 117 params_.channels()); |
| 118 if (!audio_format) { | 118 if (!audio_format) { |
| 119 DLOG(WARNING) << "Error setting up audio parameters."; | 119 DLOG(WARNING) << "Error setting up audio parameters."; |
| 120 callback_->OnError(this, -ENOMEM); | 120 callback_->OnError(this); |
| 121 callback_ = NULL; | 121 callback_ = NULL; |
| 122 return; | 122 return; |
| 123 } | 123 } |
| 124 | 124 |
| 125 unsigned int frames_per_packet = params_.frames_per_buffer(); | 125 unsigned int frames_per_packet = params_.frames_per_buffer(); |
| 126 cras_stream_params* stream_params = cras_client_stream_params_create( | 126 cras_stream_params* stream_params = cras_client_stream_params_create( |
| 127 CRAS_STREAM_INPUT, | 127 CRAS_STREAM_INPUT, |
| 128 frames_per_packet, // Total latency. | 128 frames_per_packet, // Total latency. |
| 129 frames_per_packet, // Call back when this many ready. | 129 frames_per_packet, // Call back when this many ready. |
| 130 frames_per_packet, // Minimum Callback level ignored for capture streams. | 130 frames_per_packet, // Minimum Callback level ignored for capture streams. |
| 131 CRAS_STREAM_TYPE_DEFAULT, | 131 CRAS_STREAM_TYPE_DEFAULT, |
| 132 0, // Unused flags. | 132 0, // Unused flags. |
| 133 this, | 133 this, |
| 134 CrasInputStream::SamplesReady, | 134 CrasInputStream::SamplesReady, |
| 135 CrasInputStream::StreamError, | 135 CrasInputStream::StreamError, |
| 136 audio_format); | 136 audio_format); |
| 137 if (!stream_params) { | 137 if (!stream_params) { |
| 138 DLOG(WARNING) << "Error setting up stream parameters."; | 138 DLOG(WARNING) << "Error setting up stream parameters."; |
| 139 callback_->OnError(this, -ENOMEM); | 139 callback_->OnError(this); |
| 140 callback_ = NULL; | 140 callback_ = NULL; |
| 141 cras_audio_format_destroy(audio_format); | 141 cras_audio_format_destroy(audio_format); |
| 142 return; | 142 return; |
| 143 } | 143 } |
| 144 | 144 |
| 145 // Before starting the stream, save the number of bytes in a frame for use in | 145 // Before starting the stream, save the number of bytes in a frame for use in |
| 146 // the callback. | 146 // the callback. |
| 147 bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format); | 147 bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format); |
| 148 | 148 |
| 149 // Adding the stream will start the audio callbacks. | 149 // Adding the stream will start the audio callbacks. |
| 150 if (cras_client_add_stream(client_, &stream_id_, stream_params) == 0) { | 150 if (cras_client_add_stream(client_, &stream_id_, stream_params) == 0) { |
| 151 audio_manager_->IncreaseActiveInputStreamCount(); | 151 audio_manager_->IncreaseActiveInputStreamCount(); |
| 152 } else { | 152 } else { |
| 153 DLOG(WARNING) << "Failed to add the stream."; | 153 DLOG(WARNING) << "Failed to add the stream."; |
| 154 callback_->OnError(this, -EIO); | 154 callback_->OnError(this); |
| 155 callback_ = NULL; | 155 callback_ = NULL; |
| 156 } | 156 } |
| 157 | 157 |
| 158 // Done with config params. | 158 // Done with config params. |
| 159 cras_audio_format_destroy(audio_format); | 159 cras_audio_format_destroy(audio_format); |
| 160 cras_client_stream_params_destroy(stream_params); | 160 cras_client_stream_params_destroy(stream_params); |
| 161 | 161 |
| 162 started_ = true; | 162 started_ = true; |
| 163 } | 163 } |
| 164 | 164 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 | 225 |
| 226 callback_->OnData(this, | 226 callback_->OnData(this, |
| 227 buffer, | 227 buffer, |
| 228 frames * bytes_per_frame_, | 228 frames * bytes_per_frame_, |
| 229 bytes_latency, | 229 bytes_latency, |
| 230 normalized_volume); | 230 normalized_volume); |
| 231 } | 231 } |
| 232 | 232 |
| 233 void CrasInputStream::NotifyStreamError(int err) { | 233 void CrasInputStream::NotifyStreamError(int err) { |
| 234 if (callback_) | 234 if (callback_) |
| 235 callback_->OnError(this, err); | 235 callback_->OnError(this); |
| 236 } | 236 } |
| 237 | 237 |
| 238 double CrasInputStream::GetMaxVolume() { | 238 double CrasInputStream::GetMaxVolume() { |
| 239 DCHECK(client_); | 239 DCHECK(client_); |
| 240 | 240 |
| 241 // Capture gain is returned as dB * 100 (150 => 1.5dBFS). Convert the dB | 241 // Capture gain is returned as dB * 100 (150 => 1.5dBFS). Convert the dB |
| 242 // value to a ratio before returning. | 242 // value to a ratio before returning. |
| 243 double dB = cras_client_get_system_max_capture_gain(client_) / 100.0; | 243 double dB = cras_client_get_system_max_capture_gain(client_) / 100.0; |
| 244 return GetVolumeRatioFromDecibels(dB); | 244 return GetVolumeRatioFromDecibels(dB); |
| 245 } | 245 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 269 | 269 |
| 270 double CrasInputStream::GetVolumeRatioFromDecibels(double dB) const { | 270 double CrasInputStream::GetVolumeRatioFromDecibels(double dB) const { |
| 271 return pow(10, dB / 20.0); | 271 return pow(10, dB / 20.0); |
| 272 } | 272 } |
| 273 | 273 |
| 274 double CrasInputStream::GetDecibelsFromVolumeRatio(double volume_ratio) const { | 274 double CrasInputStream::GetDecibelsFromVolumeRatio(double volume_ratio) const { |
| 275 return 20 * log10(volume_ratio); | 275 return 20 * log10(volume_ratio); |
| 276 } | 276 } |
| 277 | 277 |
| 278 } // namespace media | 278 } // namespace media |
| OLD | NEW |