Chromium Code Reviews| 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/android/opensles_output.h" | 5 #include "media/audio/android/opensles_output.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/time/time.h" | |
| 9 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
| 10 #include "media/audio/android/audio_manager_android.h" | 11 #include "media/audio/android/audio_manager_android.h" |
| 11 | 12 |
| 12 #define LOG_ON_FAILURE_AND_RETURN(op, ...) \ | 13 #define LOG_ON_FAILURE_AND_RETURN(op, ...) \ |
| 13 do { \ | 14 do { \ |
| 14 SLresult err = (op); \ | 15 SLresult err = (op); \ |
| 15 if (err != SL_RESULT_SUCCESS) { \ | 16 if (err != SL_RESULT_SUCCESS) { \ |
| 16 DLOG(ERROR) << #op << " failed: " << err; \ | 17 DLOG(ERROR) << #op << " failed: " << err; \ |
| 17 return __VA_ARGS__; \ | 18 return __VA_ARGS__; \ |
| 18 } \ | 19 } \ |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 } | 334 } |
| 334 | 335 |
| 335 void OpenSLESOutputStream::FillBufferQueueNoLock() { | 336 void OpenSLESOutputStream::FillBufferQueueNoLock() { |
| 336 // Ensure that the calling thread has acquired the lock since it is not | 337 // Ensure that the calling thread has acquired the lock since it is not |
| 337 // done in this method. | 338 // done in this method. |
| 338 lock_.AssertAcquired(); | 339 lock_.AssertAcquired(); |
| 339 | 340 |
| 340 // Calculate the position relative to the number of frames written. | 341 // Calculate the position relative to the number of frames written. |
| 341 uint32_t position_in_ms = 0; | 342 uint32_t position_in_ms = 0; |
| 342 SLresult err = (*player_)->GetPosition(player_, &position_in_ms); | 343 SLresult err = (*player_)->GetPosition(player_, &position_in_ms); |
| 343 const int delay = | 344 // Given the position of the playback head, compute the approximate number of |
| 345 // frames that have been queued to the buffer but not yet played out. | |
| 346 // Note that the value returned by GetFramesToTarget() is negative because | |
| 347 // more frames have been added to |delay_calculator_| than have been played | |
| 348 // out and thus the target timestamp is earlier than the current timestamp of | |
| 349 // |delay_calculator_|. | |
| 350 const int delay_frames = | |
| 344 err == SL_RESULT_SUCCESS | 351 err == SL_RESULT_SUCCESS |
| 345 ? -delay_calculator_.GetFramesToTarget( | 352 ? -delay_calculator_.GetFramesToTarget( |
| 346 base::TimeDelta::FromMilliseconds(position_in_ms)) * | 353 base::TimeDelta::FromMilliseconds(position_in_ms)) |
| 347 bytes_per_frame_ | |
| 348 : 0; | 354 : 0; |
| 349 DCHECK_GE(delay, 0); | 355 DCHECK_GE(delay_frames, 0); |
| 356 | |
| 357 // Convert the frame delay to time. | |
| 358 const base::TimeDelta delay = base::TimeDelta::FromMicroseconds( | |
| 359 delay_frames * base::Time::kMicrosecondsPerSecond / | |
|
chcunningham
2016/09/23 20:53:29
int division
jameswest
2016/09/29 00:52:24
Moved to utility function.
| |
| 360 format_.samplesPerSec); | |
| 350 | 361 |
| 351 // Read data from the registered client source. | 362 // Read data from the registered client source. |
| 352 const int frames_filled = callback_->OnMoreData(audio_bus_.get(), delay, 0); | 363 const int frames_filled = |
| 364 callback_->OnMoreData(delay, base::TimeTicks::Now(), 0, audio_bus_.get()); | |
| 353 if (frames_filled <= 0) { | 365 if (frames_filled <= 0) { |
| 354 // Audio source is shutting down, or halted on error. | 366 // Audio source is shutting down, or halted on error. |
| 355 return; | 367 return; |
| 356 } | 368 } |
| 357 | 369 |
| 358 // Note: If the internal representation ever changes from 16-bit PCM to | 370 // Note: If the internal representation ever changes from 16-bit PCM to |
| 359 // raw float, the data must be clipped and sanitized since it may come | 371 // raw float, the data must be clipped and sanitized since it may come |
| 360 // from an untrusted source such as NaCl. | 372 // from an untrusted source such as NaCl. |
| 361 audio_bus_->Scale(muted_ ? 0.0f : volume_); | 373 audio_bus_->Scale(muted_ ? 0.0f : volume_); |
| 362 audio_bus_->ToInterleaved(frames_filled, format_.bitsPerSample / 8, | 374 audio_bus_->ToInterleaved(frames_filled, format_.bitsPerSample / 8, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 } | 406 } |
| 395 } | 407 } |
| 396 | 408 |
| 397 void OpenSLESOutputStream::HandleError(SLresult error) { | 409 void OpenSLESOutputStream::HandleError(SLresult error) { |
| 398 DLOG(ERROR) << "OpenSLES Output error " << error; | 410 DLOG(ERROR) << "OpenSLES Output error " << error; |
| 399 if (callback_) | 411 if (callback_) |
| 400 callback_->OnError(this); | 412 callback_->OnError(this); |
| 401 } | 413 } |
| 402 | 414 |
| 403 } // namespace media | 415 } // namespace media |
| OLD | NEW |