Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / audio / alsa / alsa_output.cc
blob554aa5fbeed1d98944beb812b1d3d06437adf4a8
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // THREAD SAFETY
6 //
7 // AlsaPcmOutputStream object is *not* thread-safe and should only be used
8 // from the audio thread. We DCHECK on this assumption whenever we can.
9 //
10 // SEMANTICS OF Close()
12 // Close() is responsible for cleaning up any resources that were acquired after
13 // a successful Open(). Close() will nullify any scheduled outstanding runnable
14 // methods.
17 // SEMANTICS OF ERROR STATES
19 // The object has two distinct error states: |state_| == kInError
20 // and |stop_stream_|. The |stop_stream_| variable is used to indicate
21 // that the playback_handle should no longer be used either because of a
22 // hardware/low-level event.
24 // When |state_| == kInError, all public API functions will fail with an error
25 // (Start() will call the OnError() function on the callback immediately), or
26 // no-op themselves with the exception of Close(). Even if an error state has
27 // been entered, if Open() has previously returned successfully, Close() must be
28 // called to cleanup the ALSA devices and release resources.
30 // When |stop_stream_| is set, no more commands will be made against the
31 // ALSA device, and playback will effectively stop. From the client's point of
32 // view, it will seem that the device has just clogged and stopped requesting
33 // data.
35 #include "media/audio/alsa/alsa_output.h"
37 #include <algorithm>
39 #include "base/bind.h"
40 #include "base/logging.h"
41 #include "base/stl_util.h"
42 #include "base/trace_event/trace_event.h"
43 #include "media/audio/alsa/alsa_util.h"
44 #include "media/audio/alsa/alsa_wrapper.h"
45 #include "media/audio/alsa/audio_manager_alsa.h"
46 #include "media/base/channel_mixer.h"
47 #include "media/base/data_buffer.h"
48 #include "media/base/seekable_buffer.h"
50 namespace media {
52 // Set to 0 during debugging if you want error messages due to underrun
53 // events or other recoverable errors.
54 #if defined(NDEBUG)
55 static const int kPcmRecoverIsSilent = 1;
56 #else
57 static const int kPcmRecoverIsSilent = 0;
58 #endif
60 // The output channel layout if we set up downmixing for the kDefaultDevice
61 // device.
62 static const ChannelLayout kDefaultOutputChannelLayout = CHANNEL_LAYOUT_STEREO;
64 // While the "default" device may support multi-channel audio, in Alsa, only
65 // the device names surround40, surround41, surround50, etc, have a defined
66 // channel mapping according to Lennart:
68 // http://0pointer.de/blog/projects/guide-to-sound-apis.html
70 // This function makes a best guess at the specific > 2 channel device name
71 // based on the number of channels requested. NULL is returned if no device
72 // can be found to match the channel numbers. In this case, using
73 // kDefaultDevice is probably the best bet.
75 // A five channel source is assumed to be surround50 instead of surround41
76 // (which is also 5 channels).
78 // TODO(ajwong): The source data should have enough info to tell us if we want
79 // surround41 versus surround51, etc., instead of needing us to guess based on
80 // channel number. Fix API to pass that data down.
81 static const char* GuessSpecificDeviceName(uint32 channels) {
82 switch (channels) {
83 case 8:
84 return "surround71";
86 case 7:
87 return "surround70";
89 case 6:
90 return "surround51";
92 case 5:
93 return "surround50";
95 case 4:
96 return "surround40";
98 default:
99 return NULL;
103 std::ostream& operator<<(std::ostream& os,
104 AlsaPcmOutputStream::InternalState state) {
105 switch (state) {
106 case AlsaPcmOutputStream::kInError:
107 os << "kInError";
108 break;
109 case AlsaPcmOutputStream::kCreated:
110 os << "kCreated";
111 break;
112 case AlsaPcmOutputStream::kIsOpened:
113 os << "kIsOpened";
114 break;
115 case AlsaPcmOutputStream::kIsPlaying:
116 os << "kIsPlaying";
117 break;
118 case AlsaPcmOutputStream::kIsStopped:
119 os << "kIsStopped";
120 break;
121 case AlsaPcmOutputStream::kIsClosed:
122 os << "kIsClosed";
123 break;
125 return os;
128 const char AlsaPcmOutputStream::kDefaultDevice[] = "default";
129 const char AlsaPcmOutputStream::kAutoSelectDevice[] = "";
130 const char AlsaPcmOutputStream::kPlugPrefix[] = "plug:";
132 // We use 40ms as our minimum required latency. If it is needed, we may be able
133 // to get it down to 20ms.
134 const uint32 AlsaPcmOutputStream::kMinLatencyMicros = 40 * 1000;
136 AlsaPcmOutputStream::AlsaPcmOutputStream(const std::string& device_name,
137 const AudioParameters& params,
138 AlsaWrapper* wrapper,
139 AudioManagerBase* manager)
140 : requested_device_name_(device_name),
141 pcm_format_(alsa_util::BitsToFormat(params.bits_per_sample())),
142 channels_(params.channels()),
143 channel_layout_(params.channel_layout()),
144 sample_rate_(params.sample_rate()),
145 bytes_per_sample_(params.bits_per_sample() / 8),
146 bytes_per_frame_(params.GetBytesPerFrame()),
147 packet_size_(params.GetBytesPerBuffer()),
148 latency_(std::max(
149 base::TimeDelta::FromMicroseconds(kMinLatencyMicros),
150 FramesToTimeDelta(params.frames_per_buffer() * 2, sample_rate_))),
151 bytes_per_output_frame_(bytes_per_frame_),
152 alsa_buffer_frames_(0),
153 stop_stream_(false),
154 wrapper_(wrapper),
155 manager_(manager),
156 message_loop_(base::MessageLoop::current()),
157 playback_handle_(NULL),
158 frames_per_packet_(packet_size_ / bytes_per_frame_),
159 state_(kCreated),
160 volume_(1.0f),
161 source_callback_(NULL),
162 audio_bus_(AudioBus::Create(params)),
163 weak_factory_(this) {
164 DCHECK(manager_->GetTaskRunner()->BelongsToCurrentThread());
165 DCHECK_EQ(audio_bus_->frames() * bytes_per_frame_, packet_size_);
167 // Sanity check input values.
168 if (!params.IsValid()) {
169 LOG(WARNING) << "Unsupported audio parameters.";
170 TransitionTo(kInError);
173 if (pcm_format_ == SND_PCM_FORMAT_UNKNOWN) {
174 LOG(WARNING) << "Unsupported bits per sample: " << params.bits_per_sample();
175 TransitionTo(kInError);
179 AlsaPcmOutputStream::~AlsaPcmOutputStream() {
180 InternalState current_state = state();
181 DCHECK(current_state == kCreated ||
182 current_state == kIsClosed ||
183 current_state == kInError);
184 DCHECK(!playback_handle_);
187 bool AlsaPcmOutputStream::Open() {
188 DCHECK(IsOnAudioThread());
190 if (state() == kInError)
191 return false;
193 if (!CanTransitionTo(kIsOpened)) {
194 NOTREACHED() << "Invalid state: " << state();
195 return false;
198 // We do not need to check if the transition was successful because
199 // CanTransitionTo() was checked above, and it is assumed that this
200 // object's public API is only called on one thread so the state cannot
201 // transition out from under us.
202 TransitionTo(kIsOpened);
204 // Try to open the device.
205 if (requested_device_name_ == kAutoSelectDevice) {
206 playback_handle_ = AutoSelectDevice(latency_.InMicroseconds());
207 if (playback_handle_)
208 DVLOG(1) << "Auto-selected device: " << device_name_;
209 } else {
210 device_name_ = requested_device_name_;
211 playback_handle_ = alsa_util::OpenPlaybackDevice(
212 wrapper_, device_name_.c_str(), channels_, sample_rate_,
213 pcm_format_, latency_.InMicroseconds());
216 // Finish initializing the stream if the device was opened successfully.
217 if (playback_handle_ == NULL) {
218 stop_stream_ = true;
219 TransitionTo(kInError);
220 return false;
222 bytes_per_output_frame_ =
223 channel_mixer_ ? mixed_audio_bus_->channels() * bytes_per_sample_
224 : bytes_per_frame_;
225 uint32 output_packet_size = frames_per_packet_ * bytes_per_output_frame_;
226 buffer_.reset(new media::SeekableBuffer(0, output_packet_size));
228 // Get alsa buffer size.
229 snd_pcm_uframes_t buffer_size;
230 snd_pcm_uframes_t period_size;
231 int error =
232 wrapper_->PcmGetParams(playback_handle_, &buffer_size, &period_size);
233 if (error < 0) {
234 LOG(ERROR) << "Failed to get playback buffer size from ALSA: "
235 << wrapper_->StrError(error);
236 // Buffer size is at least twice of packet size.
237 alsa_buffer_frames_ = frames_per_packet_ * 2;
238 } else {
239 alsa_buffer_frames_ = buffer_size;
242 return true;
245 void AlsaPcmOutputStream::Close() {
246 DCHECK(IsOnAudioThread());
248 if (state() != kIsClosed)
249 TransitionTo(kIsClosed);
251 // Shutdown the audio device.
252 if (playback_handle_) {
253 if (alsa_util::CloseDevice(wrapper_, playback_handle_) < 0) {
254 LOG(WARNING) << "Unable to close audio device. Leaking handle.";
256 playback_handle_ = NULL;
258 // Release the buffer.
259 buffer_.reset();
261 // Signal anything that might already be scheduled to stop.
262 stop_stream_ = true; // Not necessary in production, but unit tests
263 // uses the flag to verify that stream was closed.
266 weak_factory_.InvalidateWeakPtrs();
268 // Signal to the manager that we're closed and can be removed.
269 // Should be last call in the method as it deletes "this".
270 manager_->ReleaseOutputStream(this);
273 void AlsaPcmOutputStream::Start(AudioSourceCallback* callback) {
274 DCHECK(IsOnAudioThread());
276 CHECK(callback);
278 if (stop_stream_)
279 return;
281 // Only post the task if we can enter the playing state.
282 if (TransitionTo(kIsPlaying) != kIsPlaying)
283 return;
285 // Before starting, the buffer might have audio from previous user of this
286 // device.
287 buffer_->Clear();
289 // When starting again, drop all packets in the device and prepare it again
290 // in case we are restarting from a pause state and need to flush old data.
291 int error = wrapper_->PcmDrop(playback_handle_);
292 if (error < 0 && error != -EAGAIN) {
293 LOG(ERROR) << "Failure clearing playback device ("
294 << wrapper_->PcmName(playback_handle_) << "): "
295 << wrapper_->StrError(error);
296 stop_stream_ = true;
297 return;
300 error = wrapper_->PcmPrepare(playback_handle_);
301 if (error < 0 && error != -EAGAIN) {
302 LOG(ERROR) << "Failure preparing stream ("
303 << wrapper_->PcmName(playback_handle_) << "): "
304 << wrapper_->StrError(error);
305 stop_stream_ = true;
306 return;
309 // Ensure the first buffer is silence to avoid startup glitches.
310 int buffer_size = GetAvailableFrames() * bytes_per_output_frame_;
311 scoped_refptr<DataBuffer> silent_packet = new DataBuffer(buffer_size);
312 silent_packet->set_data_size(buffer_size);
313 memset(silent_packet->writable_data(), 0, silent_packet->data_size());
314 buffer_->Append(silent_packet);
315 WritePacket();
317 // Start the callback chain.
318 set_source_callback(callback);
319 WriteTask();
322 void AlsaPcmOutputStream::Stop() {
323 DCHECK(IsOnAudioThread());
325 // Reset the callback, so that it is not called anymore.
326 set_source_callback(NULL);
327 weak_factory_.InvalidateWeakPtrs();
329 TransitionTo(kIsStopped);
332 void AlsaPcmOutputStream::SetVolume(double volume) {
333 DCHECK(IsOnAudioThread());
335 volume_ = static_cast<float>(volume);
338 void AlsaPcmOutputStream::GetVolume(double* volume) {
339 DCHECK(IsOnAudioThread());
341 *volume = volume_;
344 void AlsaPcmOutputStream::BufferPacket(bool* source_exhausted) {
345 DCHECK(IsOnAudioThread());
347 // If stopped, simulate a 0-length packet.
348 if (stop_stream_) {
349 buffer_->Clear();
350 *source_exhausted = true;
351 return;
354 *source_exhausted = false;
356 // Request more data only when we run out of data in the buffer, because
357 // WritePacket() consumes only the current chunk of data.
358 if (!buffer_->forward_bytes()) {
359 // Before making a request to source for data we need to determine the
360 // delay (in bytes) for the requested data to be played.
361 const uint32 hardware_delay = GetCurrentDelay() * bytes_per_frame_;
363 scoped_refptr<media::DataBuffer> packet =
364 new media::DataBuffer(packet_size_);
365 int frames_filled = RunDataCallback(
366 audio_bus_.get(), hardware_delay);
368 size_t packet_size = frames_filled * bytes_per_frame_;
369 DCHECK_LE(packet_size, packet_size_);
371 // TODO(dalecurtis): Channel downmixing, upmixing, should be done in mixer;
372 // volume adjust should use SSE optimized vector_fmul() prior to interleave.
373 AudioBus* output_bus = audio_bus_.get();
374 ChannelLayout output_channel_layout = channel_layout_;
375 if (channel_mixer_) {
376 output_bus = mixed_audio_bus_.get();
377 channel_mixer_->Transform(audio_bus_.get(), output_bus);
378 output_channel_layout = kDefaultOutputChannelLayout;
379 // Adjust packet size for downmix.
380 packet_size = packet_size / bytes_per_frame_ * bytes_per_output_frame_;
383 // Reorder channels for 5.0, 5.1, and 7.1 to match ALSA's channel order,
384 // which has front center at channel index 4 and LFE at channel index 5.
385 // See http://ffmpeg.org/pipermail/ffmpeg-cvslog/2011-June/038454.html.
386 switch (output_channel_layout) {
387 case media::CHANNEL_LAYOUT_5_0:
388 case media::CHANNEL_LAYOUT_5_0_BACK:
389 output_bus->SwapChannels(2, 3);
390 output_bus->SwapChannels(3, 4);
391 break;
392 case media::CHANNEL_LAYOUT_5_1:
393 case media::CHANNEL_LAYOUT_5_1_BACK:
394 case media::CHANNEL_LAYOUT_7_1:
395 output_bus->SwapChannels(2, 4);
396 output_bus->SwapChannels(3, 5);
397 break;
398 default:
399 break;
402 // Note: If this ever changes to output raw float the data must be clipped
403 // and sanitized since it may come from an untrusted source such as NaCl.
404 output_bus->Scale(volume_);
405 output_bus->ToInterleaved(
406 frames_filled, bytes_per_sample_, packet->writable_data());
408 if (packet_size > 0) {
409 packet->set_data_size(packet_size);
410 // Add the packet to the buffer.
411 buffer_->Append(packet);
412 } else {
413 *source_exhausted = true;
418 void AlsaPcmOutputStream::WritePacket() {
419 DCHECK(IsOnAudioThread());
421 // If the device is in error, just eat the bytes.
422 if (stop_stream_) {
423 buffer_->Clear();
424 return;
427 if (state() != kIsPlaying)
428 return;
430 CHECK_EQ(buffer_->forward_bytes() % bytes_per_output_frame_, 0u);
432 const uint8* buffer_data;
433 int buffer_size;
434 if (buffer_->GetCurrentChunk(&buffer_data, &buffer_size)) {
435 snd_pcm_sframes_t frames = std::min(
436 static_cast<snd_pcm_sframes_t>(buffer_size / bytes_per_output_frame_),
437 GetAvailableFrames());
439 if (!frames)
440 return;
442 snd_pcm_sframes_t frames_written =
443 wrapper_->PcmWritei(playback_handle_, buffer_data, frames);
444 if (frames_written < 0) {
445 // Attempt once to immediately recover from EINTR,
446 // EPIPE (overrun/underrun), ESTRPIPE (stream suspended). WritePacket
447 // will eventually be called again, so eventual recovery will happen if
448 // muliple retries are required.
449 frames_written = wrapper_->PcmRecover(playback_handle_,
450 frames_written,
451 kPcmRecoverIsSilent);
452 if (frames_written < 0) {
453 if (frames_written != -EAGAIN) {
454 LOG(ERROR) << "Failed to write to pcm device: "
455 << wrapper_->StrError(frames_written);
456 RunErrorCallback(frames_written);
457 stop_stream_ = true;
460 } else {
461 DCHECK_EQ(frames_written, frames);
463 // Seek forward in the buffer after we've written some data to ALSA.
464 buffer_->Seek(frames_written * bytes_per_output_frame_);
466 } else {
467 // If nothing left to write and playback hasn't started yet, start it now.
468 // This ensures that shorter sounds will still play.
469 if (playback_handle_ &&
470 (wrapper_->PcmState(playback_handle_) == SND_PCM_STATE_PREPARED) &&
471 GetCurrentDelay() > 0) {
472 wrapper_->PcmStart(playback_handle_);
477 void AlsaPcmOutputStream::WriteTask() {
478 DCHECK(IsOnAudioThread());
480 if (stop_stream_)
481 return;
483 if (state() == kIsStopped)
484 return;
486 bool source_exhausted;
487 BufferPacket(&source_exhausted);
488 WritePacket();
490 ScheduleNextWrite(source_exhausted);
493 void AlsaPcmOutputStream::ScheduleNextWrite(bool source_exhausted) {
494 DCHECK(IsOnAudioThread());
496 if (stop_stream_ || state() != kIsPlaying)
497 return;
499 const uint32 kTargetFramesAvailable = alsa_buffer_frames_ / 2;
500 uint32 available_frames = GetAvailableFrames();
502 base::TimeDelta next_fill_time;
503 if (buffer_->forward_bytes() && available_frames) {
504 // If we've got data available and ALSA has room, deliver it immediately.
505 next_fill_time = base::TimeDelta();
506 } else if (buffer_->forward_bytes()) {
507 // If we've got data available and no room, poll until room is available.
508 // Polling in this manner allows us to ensure a more consistent callback
509 // schedule. In testing this yields a variance of +/- 5ms versus the non-
510 // polling strategy which is around +/- 30ms and bimodal.
511 next_fill_time = base::TimeDelta::FromMilliseconds(5);
512 } else if (available_frames < kTargetFramesAvailable) {
513 // Schedule the next write for the moment when the available buffer of the
514 // sound card hits |kTargetFramesAvailable|.
515 next_fill_time = FramesToTimeDelta(
516 kTargetFramesAvailable - available_frames, sample_rate_);
517 } else if (!source_exhausted) {
518 // The sound card has |kTargetFramesAvailable| or more frames available.
519 // Invoke the next write immediately to avoid underrun.
520 next_fill_time = base::TimeDelta();
521 } else {
522 // The sound card has frames available, but our source is exhausted, so
523 // avoid busy looping by delaying a bit.
524 next_fill_time = base::TimeDelta::FromMilliseconds(10);
527 message_loop_->PostDelayedTask(FROM_HERE, base::Bind(
528 &AlsaPcmOutputStream::WriteTask, weak_factory_.GetWeakPtr()),
529 next_fill_time);
532 // static
533 base::TimeDelta AlsaPcmOutputStream::FramesToTimeDelta(int frames,
534 double sample_rate) {
535 return base::TimeDelta::FromMicroseconds(
536 frames * base::Time::kMicrosecondsPerSecond / sample_rate);
539 std::string AlsaPcmOutputStream::FindDeviceForChannels(uint32 channels) {
540 // Constants specified by the ALSA API for device hints.
541 static const int kGetAllDevices = -1;
542 static const char kPcmInterfaceName[] = "pcm";
543 static const char kIoHintName[] = "IOID";
544 static const char kNameHintName[] = "NAME";
546 const char* wanted_device = GuessSpecificDeviceName(channels);
547 if (!wanted_device)
548 return std::string();
550 std::string guessed_device;
551 void** hints = NULL;
552 int error = wrapper_->DeviceNameHint(kGetAllDevices,
553 kPcmInterfaceName,
554 &hints);
555 if (error == 0) {
556 // NOTE: Do not early return from inside this if statement. The
557 // hints above need to be freed.
558 for (void** hint_iter = hints; *hint_iter != NULL; hint_iter++) {
559 // Only examine devices that are output capable.. Valid values are
560 // "Input", "Output", and NULL which means both input and output.
561 scoped_ptr<char, base::FreeDeleter> io(
562 wrapper_->DeviceNameGetHint(*hint_iter, kIoHintName));
563 if (io != NULL && strcmp(io.get(), "Input") == 0)
564 continue;
566 // Attempt to select the closest device for number of channels.
567 scoped_ptr<char, base::FreeDeleter> name(
568 wrapper_->DeviceNameGetHint(*hint_iter, kNameHintName));
569 if (strncmp(wanted_device, name.get(), strlen(wanted_device)) == 0) {
570 guessed_device = name.get();
571 break;
575 // Destroy the hint now that we're done with it.
576 wrapper_->DeviceNameFreeHint(hints);
577 hints = NULL;
578 } else {
579 LOG(ERROR) << "Unable to get hints for devices: "
580 << wrapper_->StrError(error);
583 return guessed_device;
586 snd_pcm_sframes_t AlsaPcmOutputStream::GetCurrentDelay() {
587 snd_pcm_sframes_t delay = -1;
588 // Don't query ALSA's delay if we have underrun since it'll be jammed at some
589 // non-zero value and potentially even negative!
591 // Also, if we're in the prepared state, don't query because that seems to
592 // cause an I/O error when we do query the delay.
593 snd_pcm_state_t pcm_state = wrapper_->PcmState(playback_handle_);
594 if (pcm_state != SND_PCM_STATE_XRUN &&
595 pcm_state != SND_PCM_STATE_PREPARED) {
596 int error = wrapper_->PcmDelay(playback_handle_, &delay);
597 if (error < 0) {
598 // Assume a delay of zero and attempt to recover the device.
599 delay = -1;
600 error = wrapper_->PcmRecover(playback_handle_,
601 error,
602 kPcmRecoverIsSilent);
603 if (error < 0) {
604 LOG(ERROR) << "Failed querying delay: " << wrapper_->StrError(error);
609 // snd_pcm_delay() sometimes returns crazy values. In this case return delay
610 // of data we know currently is in ALSA's buffer. Note: When the underlying
611 // driver is PulseAudio based, certain configuration settings (e.g., tsched=1)
612 // will generate much larger delay values than |alsa_buffer_frames_|, so only
613 // clip if delay is truly crazy (> 10x expected).
614 if (delay < 0 ||
615 static_cast<snd_pcm_uframes_t>(delay) > alsa_buffer_frames_ * 10) {
616 delay = alsa_buffer_frames_ - GetAvailableFrames();
619 if (delay < 0) {
620 delay = 0;
623 return delay;
626 snd_pcm_sframes_t AlsaPcmOutputStream::GetAvailableFrames() {
627 DCHECK(IsOnAudioThread());
629 if (stop_stream_)
630 return 0;
632 // Find the number of frames queued in the sound device.
633 snd_pcm_sframes_t available_frames =
634 wrapper_->PcmAvailUpdate(playback_handle_);
635 if (available_frames < 0) {
636 available_frames = wrapper_->PcmRecover(playback_handle_,
637 available_frames,
638 kPcmRecoverIsSilent);
640 if (available_frames < 0) {
641 LOG(ERROR) << "Failed querying available frames. Assuming 0: "
642 << wrapper_->StrError(available_frames);
643 return 0;
645 if (static_cast<uint32>(available_frames) > alsa_buffer_frames_ * 2) {
646 LOG(ERROR) << "ALSA returned " << available_frames << " of "
647 << alsa_buffer_frames_ << " frames available.";
648 return alsa_buffer_frames_;
651 return available_frames;
654 snd_pcm_t* AlsaPcmOutputStream::AutoSelectDevice(unsigned int latency) {
655 // For auto-selection:
656 // 1) Attempt to open a device that best matches the number of channels
657 // requested.
658 // 2) If that fails, attempt the "plug:" version of it in case ALSA can
659 // remap and do some software conversion to make it work.
660 // 3) If that fails, attempt the "plug:" version of the guessed name in
661 // case ALSA can remap and do some software conversion to make it work.
662 // 4) Fallback to kDefaultDevice.
663 // 5) If that fails too, try the "plug:" version of kDefaultDevice.
664 // 6) Give up.
665 snd_pcm_t* handle = NULL;
666 device_name_ = FindDeviceForChannels(channels_);
668 // Step 1.
669 if (!device_name_.empty()) {
670 if ((handle = alsa_util::OpenPlaybackDevice(wrapper_, device_name_.c_str(),
671 channels_, sample_rate_,
672 pcm_format_,
673 latency)) != NULL) {
674 return handle;
677 // Step 2.
678 device_name_ = kPlugPrefix + device_name_;
679 if ((handle = alsa_util::OpenPlaybackDevice(wrapper_, device_name_.c_str(),
680 channels_, sample_rate_,
681 pcm_format_,
682 latency)) != NULL) {
683 return handle;
686 // Step 3.
687 device_name_ = GuessSpecificDeviceName(channels_);
688 if (!device_name_.empty()) {
689 device_name_ = kPlugPrefix + device_name_;
690 if ((handle = alsa_util::OpenPlaybackDevice(
691 wrapper_, device_name_.c_str(), channels_, sample_rate_,
692 pcm_format_, latency)) != NULL) {
693 return handle;
698 // For the kDefaultDevice device, we can only reliably depend on 2-channel
699 // output to have the correct ordering according to Lennart. For the channel
700 // formats that we know how to downmix from (3 channel to 8 channel), setup
701 // downmixing.
702 uint32 default_channels = channels_;
703 if (default_channels > 2) {
704 channel_mixer_.reset(
705 new ChannelMixer(channel_layout_, kDefaultOutputChannelLayout));
706 default_channels = 2;
707 mixed_audio_bus_ = AudioBus::Create(
708 default_channels, audio_bus_->frames());
711 // Step 4.
712 device_name_ = kDefaultDevice;
713 if ((handle = alsa_util::OpenPlaybackDevice(
714 wrapper_, device_name_.c_str(), default_channels, sample_rate_,
715 pcm_format_, latency)) != NULL) {
716 return handle;
719 // Step 5.
720 device_name_ = kPlugPrefix + device_name_;
721 if ((handle = alsa_util::OpenPlaybackDevice(
722 wrapper_, device_name_.c_str(), default_channels, sample_rate_,
723 pcm_format_, latency)) != NULL) {
724 return handle;
727 // Unable to open any device.
728 device_name_.clear();
729 return NULL;
732 bool AlsaPcmOutputStream::CanTransitionTo(InternalState to) {
733 switch (state_) {
734 case kCreated:
735 return to == kIsOpened || to == kIsClosed || to == kInError;
737 case kIsOpened:
738 return to == kIsPlaying || to == kIsStopped ||
739 to == kIsClosed || to == kInError;
741 case kIsPlaying:
742 return to == kIsPlaying || to == kIsStopped ||
743 to == kIsClosed || to == kInError;
745 case kIsStopped:
746 return to == kIsPlaying || to == kIsStopped ||
747 to == kIsClosed || to == kInError;
749 case kInError:
750 return to == kIsClosed || to == kInError;
752 case kIsClosed:
753 default:
754 return false;
758 AlsaPcmOutputStream::InternalState
759 AlsaPcmOutputStream::TransitionTo(InternalState to) {
760 DCHECK(IsOnAudioThread());
762 if (!CanTransitionTo(to)) {
763 NOTREACHED() << "Cannot transition from: " << state_ << " to: " << to;
764 state_ = kInError;
765 } else {
766 state_ = to;
768 return state_;
771 AlsaPcmOutputStream::InternalState AlsaPcmOutputStream::state() {
772 return state_;
775 bool AlsaPcmOutputStream::IsOnAudioThread() const {
776 return message_loop_ && message_loop_ == base::MessageLoop::current();
779 int AlsaPcmOutputStream::RunDataCallback(AudioBus* audio_bus,
780 uint32 total_bytes_delay) {
781 TRACE_EVENT0("audio", "AlsaPcmOutputStream::RunDataCallback");
783 if (source_callback_)
784 return source_callback_->OnMoreData(audio_bus, total_bytes_delay);
786 return 0;
789 void AlsaPcmOutputStream::RunErrorCallback(int code) {
790 if (source_callback_)
791 source_callback_->OnError(this);
794 // Changes the AudioSourceCallback to proxy calls to. Pass in NULL to
795 // release ownership of the currently registered callback.
796 void AlsaPcmOutputStream::set_source_callback(AudioSourceCallback* callback) {
797 DCHECK(IsOnAudioThread());
798 source_callback_ = callback;
801 } // namespace media