Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: media/audio/android/opensles_output.cc

Issue 2101303004: Pass delay and timestamp to AudioSourceCallback::OnMoreData. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Changes based on comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
344 err == SL_RESULT_SUCCESS 345 // frames that have been queued to the buffer but not yet played out.
345 ? -delay_calculator_.GetFramesToTarget( 346 // This will be added to the system reference time below to compute the target
346 base::TimeDelta::FromMilliseconds(position_in_ms)) * 347 // playout time to pass to OnMoreData().
347 bytes_per_frame_ 348 // Note that the value returned by GetFramesToTarget() is negative because
348 : 0; 349 // more frames have been added to |delay_calculator_| than have been played
350 // out and thus the target timestamp is earlier than the current timestamp of
351 // |delay_calculator_|.
352 const int delay = err == SL_RESULT_SUCCESS
353 ? -delay_calculator_.GetFramesToTarget(
354 base::TimeDelta::FromMilliseconds(position_in_ms))
355 : 0;
349 DCHECK_GE(delay, 0); 356 DCHECK_GE(delay, 0);
350 357
358 // Calculate when the requested data will be played by converting the delay to
359 // time and adding it to the current time.
360 const base::TimeTicks target_playout_time =
361 base::TimeTicks::Now() +
362 base::TimeDelta::FromMicroseconds(
363 delay * base::Time::kMicrosecondsPerSecond / format_.samplesPerSec);
364
351 // Read data from the registered client source. 365 // Read data from the registered client source.
352 const int frames_filled = callback_->OnMoreData(audio_bus_.get(), delay, 0); 366 const int frames_filled =
367 callback_->OnMoreData(target_playout_time, 0, audio_bus_.get());
353 if (frames_filled <= 0) { 368 if (frames_filled <= 0) {
354 // Audio source is shutting down, or halted on error. 369 // Audio source is shutting down, or halted on error.
355 return; 370 return;
356 } 371 }
357 372
358 // Note: If the internal representation ever changes from 16-bit PCM to 373 // 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 374 // raw float, the data must be clipped and sanitized since it may come
360 // from an untrusted source such as NaCl. 375 // from an untrusted source such as NaCl.
361 audio_bus_->Scale(muted_ ? 0.0f : volume_); 376 audio_bus_->Scale(muted_ ? 0.0f : volume_);
362 audio_bus_->ToInterleaved(frames_filled, format_.bitsPerSample / 8, 377 audio_bus_->ToInterleaved(frames_filled, format_.bitsPerSample / 8,
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 } 409 }
395 } 410 }
396 411
397 void OpenSLESOutputStream::HandleError(SLresult error) { 412 void OpenSLESOutputStream::HandleError(SLresult error) {
398 DLOG(ERROR) << "OpenSLES Output error " << error; 413 DLOG(ERROR) << "OpenSLES Output error " << error;
399 if (callback_) 414 if (callback_)
400 callback_->OnError(this); 415 callback_->OnError(this);
401 } 416 }
402 417
403 } // namespace media 418 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698