| 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" | |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 12 #include "media/audio/audio_manager.h" | 11 #include "media/audio/audio_manager.h" |
| 13 #include "media/audio/cras/audio_manager_cras.h" | 12 #include "media/audio/cras/audio_manager_cras.h" |
| 14 | 13 |
| 15 namespace media { | 14 namespace media { |
| 16 | 15 |
| 17 CrasInputStream::CrasInputStream(const AudioParameters& params, | 16 CrasInputStream::CrasInputStream(const AudioParameters& params, |
| 18 AudioManagerCras* manager, | 17 AudioManagerCras* manager, |
| 19 const std::string& device_id) | 18 const std::string& device_id) |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 params_.channels()); | 155 params_.channels()); |
| 157 if (!audio_format) { | 156 if (!audio_format) { |
| 158 DLOG(WARNING) << "Error setting up audio parameters."; | 157 DLOG(WARNING) << "Error setting up audio parameters."; |
| 159 callback_->OnError(this); | 158 callback_->OnError(this); |
| 160 callback_ = NULL; | 159 callback_ = NULL; |
| 161 return; | 160 return; |
| 162 } | 161 } |
| 163 | 162 |
| 164 // Initialize channel layout to all -1 to indicate that none of | 163 // Initialize channel layout to all -1 to indicate that none of |
| 165 // the channels is set in the layout. | 164 // the channels is set in the layout. |
| 166 int8 layout[CRAS_CH_MAX]; | 165 int8_t layout[CRAS_CH_MAX]; |
| 167 for (size_t i = 0; i < arraysize(layout); ++i) | 166 for (size_t i = 0; i < arraysize(layout); ++i) |
| 168 layout[i] = -1; | 167 layout[i] = -1; |
| 169 | 168 |
| 170 // Converts to CRAS defined channels. ChannelOrder will return -1 | 169 // Converts to CRAS defined channels. ChannelOrder will return -1 |
| 171 // for channels that are not present in params_.channel_layout(). | 170 // for channels that are not present in params_.channel_layout(). |
| 172 for (size_t i = 0; i < arraysize(kChannelMap); ++i) { | 171 for (size_t i = 0; i < arraysize(kChannelMap); ++i) { |
| 173 layout[kChannelMap[i]] = ChannelOrder(params_.channel_layout(), | 172 layout[kChannelMap[i]] = ChannelOrder(params_.channel_layout(), |
| 174 static_cast<Channels>(i)); | 173 static_cast<Channels>(i)); |
| 175 } | 174 } |
| 176 if (cras_audio_format_set_channel_layout(audio_format, layout) != 0) { | 175 if (cras_audio_format_set_channel_layout(audio_format, layout) != 0) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 // Removing the stream from the client stops audio. | 232 // Removing the stream from the client stops audio. |
| 234 cras_client_rm_stream(client_, stream_id_); | 233 cras_client_rm_stream(client_, stream_id_); |
| 235 | 234 |
| 236 started_ = false; | 235 started_ = false; |
| 237 callback_ = NULL; | 236 callback_ = NULL; |
| 238 } | 237 } |
| 239 | 238 |
| 240 // Static callback asking for samples. Run on high priority thread. | 239 // Static callback asking for samples. Run on high priority thread. |
| 241 int CrasInputStream::SamplesReady(cras_client* client, | 240 int CrasInputStream::SamplesReady(cras_client* client, |
| 242 cras_stream_id_t stream_id, | 241 cras_stream_id_t stream_id, |
| 243 uint8* samples, | 242 uint8_t* samples, |
| 244 size_t frames, | 243 size_t frames, |
| 245 const timespec* sample_ts, | 244 const timespec* sample_ts, |
| 246 void* arg) { | 245 void* arg) { |
| 247 CrasInputStream* me = static_cast<CrasInputStream*>(arg); | 246 CrasInputStream* me = static_cast<CrasInputStream*>(arg); |
| 248 me->ReadAudio(frames, samples, sample_ts); | 247 me->ReadAudio(frames, samples, sample_ts); |
| 249 return frames; | 248 return frames; |
| 250 } | 249 } |
| 251 | 250 |
| 252 // Static callback for stream errors. | 251 // Static callback for stream errors. |
| 253 int CrasInputStream::StreamError(cras_client* client, | 252 int CrasInputStream::StreamError(cras_client* client, |
| 254 cras_stream_id_t stream_id, | 253 cras_stream_id_t stream_id, |
| 255 int err, | 254 int err, |
| 256 void* arg) { | 255 void* arg) { |
| 257 CrasInputStream* me = static_cast<CrasInputStream*>(arg); | 256 CrasInputStream* me = static_cast<CrasInputStream*>(arg); |
| 258 me->NotifyStreamError(err); | 257 me->NotifyStreamError(err); |
| 259 return 0; | 258 return 0; |
| 260 } | 259 } |
| 261 | 260 |
| 262 void CrasInputStream::ReadAudio(size_t frames, | 261 void CrasInputStream::ReadAudio(size_t frames, |
| 263 uint8* buffer, | 262 uint8_t* buffer, |
| 264 const timespec* sample_ts) { | 263 const timespec* sample_ts) { |
| 265 DCHECK(callback_); | 264 DCHECK(callback_); |
| 266 | 265 |
| 267 timespec latency_ts = {0, 0}; | 266 timespec latency_ts = {0, 0}; |
| 268 | 267 |
| 269 // Determine latency and pass that on to the sink. sample_ts is the wall time | 268 // Determine latency and pass that on to the sink. sample_ts is the wall time |
| 270 // indicating when the first sample in the buffer was captured. Convert that | 269 // indicating when the first sample in the buffer was captured. Convert that |
| 271 // to latency in bytes. | 270 // to latency in bytes. |
| 272 cras_client_calc_capture_latency(sample_ts, &latency_ts); | 271 cras_client_calc_capture_latency(sample_ts, &latency_ts); |
| 273 double latency_usec = | 272 double latency_usec = |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 | 331 |
| 333 double CrasInputStream::GetVolumeRatioFromDecibels(double dB) const { | 332 double CrasInputStream::GetVolumeRatioFromDecibels(double dB) const { |
| 334 return pow(10, dB / 20.0); | 333 return pow(10, dB / 20.0); |
| 335 } | 334 } |
| 336 | 335 |
| 337 double CrasInputStream::GetDecibelsFromVolumeRatio(double volume_ratio) const { | 336 double CrasInputStream::GetDecibelsFromVolumeRatio(double volume_ratio) const { |
| 338 return 20 * log10(volume_ratio); | 337 return 20 * log10(volume_ratio); |
| 339 } | 338 } |
| 340 | 339 |
| 341 } // namespace media | 340 } // namespace media |
| OLD | NEW |