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