OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_unified.h" | 5 #include "media/audio/cras/cras_unified.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/macros.h" | 10 #include "base/macros.h" |
9 #include "media/audio/cras/audio_manager_cras.h" | 11 #include "media/audio/cras/audio_manager_cras.h" |
10 | 12 |
11 namespace media { | 13 namespace media { |
12 | 14 |
13 // Overview of operation: | 15 // Overview of operation: |
14 // 1) An object of CrasUnifiedStream is created by the AudioManager | 16 // 1) An object of CrasUnifiedStream is created by the AudioManager |
15 // factory: audio_man->MakeAudioStream(). | 17 // factory: audio_man->MakeAudioStream(). |
16 // 2) Next some thread will call Open(), at that point a client is created and | 18 // 2) Next some thread will call Open(), at that point a client is created and |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 : client_(NULL), | 56 : client_(NULL), |
55 stream_id_(0), | 57 stream_id_(0), |
56 params_(params), | 58 params_(params), |
57 bytes_per_frame_(0), | 59 bytes_per_frame_(0), |
58 is_playing_(false), | 60 is_playing_(false), |
59 volume_(1.0), | 61 volume_(1.0), |
60 manager_(manager), | 62 manager_(manager), |
61 source_callback_(NULL), | 63 source_callback_(NULL), |
62 stream_direction_(CRAS_STREAM_OUTPUT) { | 64 stream_direction_(CRAS_STREAM_OUTPUT) { |
63 DCHECK(manager_); | 65 DCHECK(manager_); |
64 DCHECK(params_.channels() > 0); | 66 DCHECK_GT(params_.channels(), 0); |
65 | 67 |
66 output_bus_ = AudioBus::Create(params); | 68 output_bus_ = AudioBus::Create(params); |
67 } | 69 } |
68 | 70 |
69 CrasUnifiedStream::~CrasUnifiedStream() { | 71 CrasUnifiedStream::~CrasUnifiedStream() { |
70 DCHECK(!is_playing_); | 72 DCHECK(!is_playing_); |
71 } | 73 } |
72 | 74 |
73 bool CrasUnifiedStream::Open() { | 75 bool CrasUnifiedStream::Open() { |
74 // Sanity check input values. | 76 // Sanity check input values. |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 if (!client_) | 228 if (!client_) |
227 return; | 229 return; |
228 volume_ = static_cast<float>(volume); | 230 volume_ = static_cast<float>(volume); |
229 cras_client_set_stream_volume(client_, stream_id_, volume_); | 231 cras_client_set_stream_volume(client_, stream_id_, volume_); |
230 } | 232 } |
231 | 233 |
232 void CrasUnifiedStream::GetVolume(double* volume) { | 234 void CrasUnifiedStream::GetVolume(double* volume) { |
233 *volume = volume_; | 235 *volume = volume_; |
234 } | 236 } |
235 | 237 |
236 uint32_t CrasUnifiedStream::GetBytesLatency(const struct timespec& latency_ts) { | |
237 uint32_t latency_usec; | |
238 | |
239 // Treat negative latency (if we are too slow to render) as 0. | |
240 if (latency_ts.tv_sec < 0 || latency_ts.tv_nsec < 0) { | |
241 latency_usec = 0; | |
242 } else { | |
243 latency_usec = (latency_ts.tv_sec * base::Time::kMicrosecondsPerSecond) + | |
244 latency_ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond; | |
245 } | |
246 | |
247 double frames_latency = | |
248 latency_usec * params_.sample_rate() / base::Time::kMicrosecondsPerSecond; | |
249 | |
250 return static_cast<unsigned int>(frames_latency * bytes_per_frame_); | |
251 } | |
252 | |
253 // Static callback asking for samples. | 238 // Static callback asking for samples. |
254 int CrasUnifiedStream::UnifiedCallback(cras_client* client, | 239 int CrasUnifiedStream::UnifiedCallback(cras_client* client, |
255 cras_stream_id_t stream_id, | 240 cras_stream_id_t stream_id, |
256 uint8_t* input_samples, | 241 uint8_t* input_samples, |
257 uint8_t* output_samples, | 242 uint8_t* output_samples, |
258 unsigned int frames, | 243 unsigned int frames, |
259 const timespec* input_ts, | 244 const timespec* input_ts, |
260 const timespec* output_ts, | 245 const timespec* output_ts, |
261 void* arg) { | 246 void* arg) { |
262 CrasUnifiedStream* me = static_cast<CrasUnifiedStream*>(arg); | 247 CrasUnifiedStream* me = static_cast<CrasUnifiedStream*>(arg); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 | 283 |
299 uint32_t CrasUnifiedStream::WriteAudio(size_t frames, | 284 uint32_t CrasUnifiedStream::WriteAudio(size_t frames, |
300 uint8_t* buffer, | 285 uint8_t* buffer, |
301 const timespec* sample_ts) { | 286 const timespec* sample_ts) { |
302 DCHECK_EQ(frames, static_cast<size_t>(output_bus_->frames())); | 287 DCHECK_EQ(frames, static_cast<size_t>(output_bus_->frames())); |
303 | 288 |
304 // Determine latency and pass that on to the source. | 289 // Determine latency and pass that on to the source. |
305 timespec latency_ts = {0, 0}; | 290 timespec latency_ts = {0, 0}; |
306 cras_client_calc_playback_latency(sample_ts, &latency_ts); | 291 cras_client_calc_playback_latency(sample_ts, &latency_ts); |
307 | 292 |
| 293 // Treat negative latency (if we are too slow to render) as 0. |
| 294 const base::TimeDelta delay = |
| 295 std::max(base::TimeDelta::FromTimeSpec(latency_ts), base::TimeDelta()); |
| 296 |
308 int frames_filled = source_callback_->OnMoreData( | 297 int frames_filled = source_callback_->OnMoreData( |
309 output_bus_.get(), GetBytesLatency(latency_ts), 0); | 298 delay, base::TimeTicks::Now(), 0, output_bus_.get()); |
310 | 299 |
311 // Note: If this ever changes to output raw float the data must be clipped and | 300 // Note: If this ever changes to output raw float the data must be clipped and |
312 // sanitized since it may come from an untrusted source such as NaCl. | 301 // sanitized since it may come from an untrusted source such as NaCl. |
313 output_bus_->ToInterleaved( | 302 output_bus_->ToInterleaved( |
314 frames_filled, bytes_per_frame_ / params_.channels(), buffer); | 303 frames_filled, bytes_per_frame_ / params_.channels(), buffer); |
315 | 304 |
316 return frames_filled; | 305 return frames_filled; |
317 } | 306 } |
318 | 307 |
319 void CrasUnifiedStream::NotifyStreamError(int err) { | 308 void CrasUnifiedStream::NotifyStreamError(int err) { |
320 // This will remove the stream from the client. | 309 // This will remove the stream from the client. |
321 if (source_callback_) | 310 if (source_callback_) |
322 source_callback_->OnError(this); | 311 source_callback_->OnError(this); |
323 } | 312 } |
324 | 313 |
325 } // namespace media | 314 } // namespace media |
OLD | NEW |