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

Side by Side Diff: media/audio/linux/alsa_input.cc

Issue 4661001: Simplified AudioOutputStream interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - Created 10 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « media/audio/linux/alsa_input.h ('k') | media/audio/linux/alsa_output.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/linux/alsa_input.h" 5 #include "media/audio/linux/alsa_input.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/scoped_ptr.h" 10 #include "base/scoped_ptr.h"
(...skipping 10 matching lines...) Expand all
21 21
22 const char kDefaultDevice1[] = "default"; 22 const char kDefaultDevice1[] = "default";
23 const char kDefaultDevice2[] = "plug:default"; 23 const char kDefaultDevice2[] = "plug:default";
24 24
25 } // namespace 25 } // namespace
26 26
27 const char* AlsaPcmInputStream::kAutoSelectDevice = ""; 27 const char* AlsaPcmInputStream::kAutoSelectDevice = "";
28 28
29 AlsaPcmInputStream::AlsaPcmInputStream(const std::string& device_name, 29 AlsaPcmInputStream::AlsaPcmInputStream(const std::string& device_name,
30 const AudioParameters& params, 30 const AudioParameters& params,
31 int samples_per_packet,
32 AlsaWrapper* wrapper) 31 AlsaWrapper* wrapper)
33 : device_name_(device_name), 32 : device_name_(device_name),
34 params_(params), 33 params_(params),
35 samples_per_packet_(samples_per_packet), 34 bytes_per_packet_(params.samples_per_packet *
36 bytes_per_packet_(samples_per_packet_ *
37 (params.channels * params.bits_per_sample) / 8), 35 (params.channels * params.bits_per_sample) / 8),
38 wrapper_(wrapper), 36 wrapper_(wrapper),
39 packet_duration_ms_( 37 packet_duration_ms_(
40 (samples_per_packet_ * base::Time::kMillisecondsPerSecond) / 38 (params.samples_per_packet * base::Time::kMillisecondsPerSecond) /
41 params.sample_rate), 39 params.sample_rate),
42 callback_(NULL), 40 callback_(NULL),
43 device_handle_(NULL), 41 device_handle_(NULL),
44 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { 42 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) {
45 } 43 }
46 44
47 AlsaPcmInputStream::~AlsaPcmInputStream() {} 45 AlsaPcmInputStream::~AlsaPcmInputStream() {}
48 46
49 bool AlsaPcmInputStream::Open() { 47 bool AlsaPcmInputStream::Open() {
50 if (device_handle_) 48 if (device_handle_)
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 138
141 void AlsaPcmInputStream::ReadAudio() { 139 void AlsaPcmInputStream::ReadAudio() {
142 DCHECK(callback_); 140 DCHECK(callback_);
143 141
144 snd_pcm_sframes_t frames = wrapper_->PcmAvailUpdate(device_handle_); 142 snd_pcm_sframes_t frames = wrapper_->PcmAvailUpdate(device_handle_);
145 if (frames < 0) { // Potentially recoverable error? 143 if (frames < 0) { // Potentially recoverable error?
146 LOG(WARNING) << "PcmAvailUpdate(): " << wrapper_->StrError(frames); 144 LOG(WARNING) << "PcmAvailUpdate(): " << wrapper_->StrError(frames);
147 Recover(frames); 145 Recover(frames);
148 } 146 }
149 147
150 if (frames < samples_per_packet_) { 148 if (frames < params_.samples_per_packet) {
151 // Not enough data yet or error happened. In both cases wait for a very 149 // Not enough data yet or error happened. In both cases wait for a very
152 // small duration before checking again. 150 // small duration before checking again.
153 MessageLoop::current()->PostDelayedTask( 151 MessageLoop::current()->PostDelayedTask(
154 FROM_HERE, 152 FROM_HERE,
155 task_factory_.NewRunnableMethod(&AlsaPcmInputStream::ReadAudio), 153 task_factory_.NewRunnableMethod(&AlsaPcmInputStream::ReadAudio),
156 kNoAudioReadAgainTimeoutMs); 154 kNoAudioReadAgainTimeoutMs);
157 return; 155 return;
158 } 156 }
159 157
160 int num_packets = frames / samples_per_packet_; 158 int num_packets = frames / params_.samples_per_packet;
161 while (num_packets--) { 159 while (num_packets--) {
162 int frames_read = wrapper_->PcmReadi(device_handle_, audio_packet_.get(), 160 int frames_read = wrapper_->PcmReadi(device_handle_, audio_packet_.get(),
163 samples_per_packet_); 161 params_.samples_per_packet);
164 if (frames_read == samples_per_packet_) { 162 if (frames_read == params_.samples_per_packet) {
165 callback_->OnData(this, audio_packet_.get(), bytes_per_packet_); 163 callback_->OnData(this, audio_packet_.get(), bytes_per_packet_);
166 } else { 164 } else {
167 LOG(WARNING) << "PcmReadi returning less than expected frames: " 165 LOG(WARNING) << "PcmReadi returning less than expected frames: "
168 << frames_read << " vs. " << samples_per_packet_ 166 << frames_read << " vs. " << params_.samples_per_packet
169 << ". Dropping this packet."; 167 << ". Dropping this packet.";
170 } 168 }
171 } 169 }
172 170
173 next_read_time_ += base::TimeDelta::FromMilliseconds(packet_duration_ms_); 171 next_read_time_ += base::TimeDelta::FromMilliseconds(packet_duration_ms_);
174 int64 delay_ms = (next_read_time_ - base::Time::Now()).InMilliseconds(); 172 int64 delay_ms = (next_read_time_ - base::Time::Now()).InMilliseconds();
175 if (delay_ms < 0) { 173 if (delay_ms < 0) {
176 LOG(WARNING) << "Audio read callback behind schedule by " 174 LOG(WARNING) << "Audio read callback behind schedule by "
177 << (packet_duration_ms_ - delay_ms) << " (ms)."; 175 << (packet_duration_ms_ - delay_ms) << " (ms).";
178 delay_ms = 0; 176 delay_ms = 0;
(...skipping 27 matching lines...) Expand all
206 204
207 audio_packet_.reset(); 205 audio_packet_.reset();
208 device_handle_ = NULL; 206 device_handle_ = NULL;
209 callback_->OnClose(this); 207 callback_->OnClose(this);
210 } 208 }
211 209
212 void AlsaPcmInputStream::HandleError(const char* method, int error) { 210 void AlsaPcmInputStream::HandleError(const char* method, int error) {
213 LOG(WARNING) << method << ": " << wrapper_->StrError(error); 211 LOG(WARNING) << method << ": " << wrapper_->StrError(error);
214 callback_->OnError(this, error); 212 callback_->OnError(this, error);
215 } 213 }
216
OLDNEW
« no previous file with comments | « media/audio/linux/alsa_input.h ('k') | media/audio/linux/alsa_output.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698