| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/cras/cras_unified.h" | 5 #include "media/audio/cras/cras_unified.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "media/audio/cras/audio_manager_cras.h" | 12 #include "media/audio/cras/audio_manager_cras.h" |
| 12 | 13 |
| 13 namespace media { | 14 namespace media { |
| 14 | 15 |
| 16 namespace { |
| 17 |
| 18 int GetDevicePin(AudioManagerCras* manager, const std::string& device_id) { |
| 19 if (!manager->IsDefault(device_id, false)) { |
| 20 uint64_t cras_node_id; |
| 21 base::StringToUint64(device_id, &cras_node_id); |
| 22 return dev_index_of(cras_node_id); |
| 23 } |
| 24 return NO_DEVICE; |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 15 // Overview of operation: | 29 // Overview of operation: |
| 16 // 1) An object of CrasUnifiedStream is created by the AudioManager | 30 // 1) An object of CrasUnifiedStream is created by the AudioManager |
| 17 // factory: audio_man->MakeAudioStream(). | 31 // factory: audio_man->MakeAudioStream(). |
| 18 // 2) Next some thread will call Open(), at that point a client is created and | 32 // 2) Next some thread will call Open(), at that point a client is created and |
| 19 // configured for the correct format and sample rate. | 33 // configured for the correct format and sample rate. |
| 20 // 3) Then Start(source) is called and a stream is added to the CRAS client | 34 // 3) Then Start(source) is called and a stream is added to the CRAS client |
| 21 // which will create its own thread that periodically calls the source for more | 35 // which will create its own thread that periodically calls the source for more |
| 22 // data as buffers are being consumed. | 36 // data as buffers are being consumed. |
| 23 // 4) When finished Stop() is called, which is handled by stopping the stream. | 37 // 4) When finished Stop() is called, which is handled by stopping the stream. |
| 24 // 5) Finally Close() is called. It cleans up and notifies the audio manager, | 38 // 5) Finally Close() is called. It cleans up and notifies the audio manager, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 45 // | Remove stream | | 59 // | Remove stream | |
| 46 // |<----------------------------------| | 60 // |<----------------------------------| |
| 47 // | | | 61 // | | |
| 48 // | 62 // |
| 49 // For Unified streams the Chrome client is notified whenever buffer_frames have | 63 // For Unified streams the Chrome client is notified whenever buffer_frames have |
| 50 // been captured. For Output streams the client is notified a few milliseconds | 64 // been captured. For Output streams the client is notified a few milliseconds |
| 51 // before the hardware buffer underruns and fills the buffer with another block | 65 // before the hardware buffer underruns and fills the buffer with another block |
| 52 // of audio. | 66 // of audio. |
| 53 | 67 |
| 54 CrasUnifiedStream::CrasUnifiedStream(const AudioParameters& params, | 68 CrasUnifiedStream::CrasUnifiedStream(const AudioParameters& params, |
| 55 AudioManagerCras* manager) | 69 AudioManagerCras* manager, |
| 70 const std::string& device_id) |
| 56 : client_(NULL), | 71 : client_(NULL), |
| 57 stream_id_(0), | 72 stream_id_(0), |
| 58 params_(params), | 73 params_(params), |
| 59 bytes_per_frame_(0), | 74 bytes_per_frame_(0), |
| 60 is_playing_(false), | 75 is_playing_(false), |
| 61 volume_(1.0), | 76 volume_(1.0), |
| 62 manager_(manager), | 77 manager_(manager), |
| 63 source_callback_(NULL), | 78 source_callback_(NULL), |
| 64 stream_direction_(CRAS_STREAM_OUTPUT) { | 79 output_bus_(AudioBus::Create(params)), |
| 80 stream_direction_(CRAS_STREAM_OUTPUT), |
| 81 pin_device_(GetDevicePin(manager, device_id)) { |
| 65 DCHECK(manager_); | 82 DCHECK(manager_); |
| 66 DCHECK_GT(params_.channels(), 0); | 83 DCHECK_GT(params_.channels(), 0); |
| 67 | |
| 68 output_bus_ = AudioBus::Create(params); | |
| 69 } | 84 } |
| 70 | 85 |
| 71 CrasUnifiedStream::~CrasUnifiedStream() { | 86 CrasUnifiedStream::~CrasUnifiedStream() { |
| 72 DCHECK(!is_playing_); | 87 DCHECK(!is_playing_); |
| 73 } | 88 } |
| 74 | 89 |
| 75 bool CrasUnifiedStream::Open() { | 90 bool CrasUnifiedStream::Open() { |
| 76 // Sanity check input values. | 91 // Sanity check input values. |
| 77 if (params_.sample_rate() <= 0) { | 92 if (params_.sample_rate() <= 0) { |
| 78 LOG(WARNING) << "Unsupported audio frequency."; | 93 LOG(WARNING) << "Unsupported audio frequency."; |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 callback->OnError(this); | 204 callback->OnError(this); |
| 190 cras_audio_format_destroy(audio_format); | 205 cras_audio_format_destroy(audio_format); |
| 191 return; | 206 return; |
| 192 } | 207 } |
| 193 | 208 |
| 194 // Before starting the stream, save the number of bytes in a frame for use in | 209 // Before starting the stream, save the number of bytes in a frame for use in |
| 195 // the callback. | 210 // the callback. |
| 196 bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format); | 211 bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format); |
| 197 | 212 |
| 198 // Adding the stream will start the audio callbacks requesting data. | 213 // Adding the stream will start the audio callbacks requesting data. |
| 199 if (cras_client_add_stream(client_, &stream_id_, stream_params) < 0) { | 214 if (cras_client_add_pinned_stream(client_, pin_device_, &stream_id_, |
| 200 LOG(WARNING) << "Failed to add the stream"; | 215 stream_params)) { |
| 216 LOG(WARNING) << "Failed to add the stream."; |
| 201 callback->OnError(this); | 217 callback->OnError(this); |
| 202 cras_audio_format_destroy(audio_format); | 218 cras_audio_format_destroy(audio_format); |
| 203 cras_client_stream_params_destroy(stream_params); | 219 cras_client_stream_params_destroy(stream_params); |
| 204 return; | 220 return; |
| 205 } | 221 } |
| 206 | 222 |
| 207 // Set initial volume. | 223 // Set initial volume. |
| 208 cras_client_set_stream_volume(client_, stream_id_, volume_); | 224 cras_client_set_stream_volume(client_, stream_id_, volume_); |
| 209 | 225 |
| 210 // Done with config params. | 226 // Done with config params. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 return frames_filled; | 321 return frames_filled; |
| 306 } | 322 } |
| 307 | 323 |
| 308 void CrasUnifiedStream::NotifyStreamError(int err) { | 324 void CrasUnifiedStream::NotifyStreamError(int err) { |
| 309 // This will remove the stream from the client. | 325 // This will remove the stream from the client. |
| 310 if (source_callback_) | 326 if (source_callback_) |
| 311 source_callback_->OnError(this); | 327 source_callback_->OnError(this); |
| 312 } | 328 } |
| 313 | 329 |
| 314 } // namespace media | 330 } // namespace media |
| OLD | NEW |