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

Unified Diff: media/audio/alsa/alsa_output.cc

Issue 2060833002: Implementation of 'AudioContext.getOutputTimestamp' method (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added implementation for ALSA. Created 4 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: media/audio/alsa/alsa_output.cc
diff --git a/media/audio/alsa/alsa_output.cc b/media/audio/alsa/alsa_output.cc
index c127fb4c2e93a9c6bfd000e41b9717bd181681e1..dbac2a624008499d18812c8aafa37e9a491b2586 100644
--- a/media/audio/alsa/alsa_output.cc
+++ b/media/audio/alsa/alsa_output.cc
@@ -161,6 +161,7 @@ AlsaPcmOutputStream::AlsaPcmOutputStream(const std::string& device_name,
frames_per_packet_(packet_size_ / bytes_per_frame_),
state_(kCreated),
volume_(1.0f),
+ elapsed_written_frames_(0),
source_callback_(NULL),
audio_bus_(AudioBus::Create(params)),
weak_factory_(this) {
@@ -289,6 +290,9 @@ void AlsaPcmOutputStream::Start(AudioSourceCallback* callback) {
// device.
buffer_->Clear();
+ // Invalidate written frames count.
+ elapsed_written_frames_ = 0;
+
// When starting again, drop all packets in the device and prepare it again
// in case we are restarting from a pause state and need to flush old data.
int error = wrapper_->PcmDrop(playback_handle_);
@@ -466,6 +470,8 @@ void AlsaPcmOutputStream::WritePacket() {
// Seek forward in the buffer after we've written some data to ALSA.
buffer_->Seek(frames_written * bytes_per_output_frame_);
}
+ if (frames_written)
+ elapsed_written_frames_ += frames_written;
} else {
// If nothing left to write and playback hasn't started yet, start it now.
// This ensures that shorter sounds will still play.
@@ -783,8 +789,16 @@ int AlsaPcmOutputStream::RunDataCallback(AudioBus* audio_bus,
uint32_t total_bytes_delay) {
TRACE_EVENT0("audio", "AlsaPcmOutputStream::RunDataCallback");
+ AudioTimestamp output_timestamp;
+ // This is quite a raugh estimation, we need to consider using
+ // snd_pcm_status_get_audio* API when it is accessible.
+ output_timestamp.frames = elapsed_written_frames_ - GetCurrentDelay();
+ output_timestamp.ticks = base::TimeTicks::Now().ToInternalValue();
+ DCHECK(output_timestamp.frames >= 0);
+
if (source_callback_)
- return source_callback_->OnMoreData(audio_bus, total_bytes_delay, 0);
+ return source_callback_->OnMoreData(audio_bus, total_bytes_delay, 0,
+ output_timestamp);
return 0;
}

Powered by Google App Engine
This is Rietveld 408576698