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 |
15 // Overview of operation: | 16 // Overview of operation: |
16 // 1) An object of CrasUnifiedStream is created by the AudioManager | 17 // 1) An object of CrasUnifiedStream is created by the AudioManager |
17 // factory: audio_man->MakeAudioStream(). | 18 // factory: audio_man->MakeAudioStream(). |
18 // 2) Next some thread will call Open(), at that point a client is created and | 19 // 2) Next some thread will call Open(), at that point a client is created and |
19 // configured for the correct format and sample rate. | 20 // configured for the correct format and sample rate. |
20 // 3) Then Start(source) is called and a stream is added to the CRAS client | 21 // 3) Then Start(source) is called and a stream is added to the CRAS client |
(...skipping 24 matching lines...) Expand all Loading... | |
45 // | Remove stream | | 46 // | Remove stream | |
46 // |<----------------------------------| | 47 // |<----------------------------------| |
47 // | | | 48 // | | |
48 // | 49 // |
49 // For Unified streams the Chrome client is notified whenever buffer_frames have | 50 // 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 | 51 // 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 | 52 // before the hardware buffer underruns and fills the buffer with another block |
52 // of audio. | 53 // of audio. |
53 | 54 |
54 CrasUnifiedStream::CrasUnifiedStream(const AudioParameters& params, | 55 CrasUnifiedStream::CrasUnifiedStream(const AudioParameters& params, |
55 AudioManagerCras* manager) | 56 AudioManagerCras* manager, |
57 const std::string& device_id) | |
56 : client_(NULL), | 58 : client_(NULL), |
57 stream_id_(0), | 59 stream_id_(0), |
58 params_(params), | 60 params_(params), |
59 bytes_per_frame_(0), | 61 bytes_per_frame_(0), |
60 is_playing_(false), | 62 is_playing_(false), |
61 volume_(1.0), | 63 volume_(1.0), |
62 manager_(manager), | 64 manager_(manager), |
63 source_callback_(NULL), | 65 source_callback_(NULL), |
64 stream_direction_(CRAS_STREAM_OUTPUT) { | 66 stream_direction_(CRAS_STREAM_OUTPUT), |
67 pin_device_(NO_DEVICE) { | |
65 DCHECK(manager_); | 68 DCHECK(manager_); |
66 DCHECK_GT(params_.channels(), 0); | 69 DCHECK_GT(params_.channels(), 0); |
67 | 70 |
68 output_bus_ = AudioBus::Create(params); | 71 output_bus_ = AudioBus::Create(params); |
72 if (!manager_->IsDefault(device_id, false)) { | |
73 uint64_t cras_node_id; | |
74 base::StringToUint64(device_id, &cras_node_id); | |
75 pin_device_ = dev_index_of(cras_node_id); | |
76 } | |
69 } | 77 } |
70 | 78 |
71 CrasUnifiedStream::~CrasUnifiedStream() { | 79 CrasUnifiedStream::~CrasUnifiedStream() { |
72 DCHECK(!is_playing_); | 80 DCHECK(!is_playing_); |
73 } | 81 } |
74 | 82 |
75 bool CrasUnifiedStream::Open() { | 83 bool CrasUnifiedStream::Open() { |
76 // Sanity check input values. | 84 // Sanity check input values. |
77 if (params_.sample_rate() <= 0) { | 85 if (params_.sample_rate() <= 0) { |
78 LOG(WARNING) << "Unsupported audio frequency."; | 86 LOG(WARNING) << "Unsupported audio frequency."; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 callback->OnError(this); | 197 callback->OnError(this); |
190 cras_audio_format_destroy(audio_format); | 198 cras_audio_format_destroy(audio_format); |
191 return; | 199 return; |
192 } | 200 } |
193 | 201 |
194 // Before starting the stream, save the number of bytes in a frame for use in | 202 // Before starting the stream, save the number of bytes in a frame for use in |
195 // the callback. | 203 // the callback. |
196 bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format); | 204 bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format); |
197 | 205 |
198 // Adding the stream will start the audio callbacks requesting data. | 206 // Adding the stream will start the audio callbacks requesting data. |
199 if (cras_client_add_stream(client_, &stream_id_, stream_params) < 0) { | 207 if (cras_client_add_pinned_stream(client_, pin_device_, &stream_id_, |
200 LOG(WARNING) << "Failed to add the stream"; | 208 stream_params)) { |
209 LOG(WARNING) << "Failed to add the stream."; | |
dgreid
2016/11/30 01:45:08
How does this work when the Chrome OS UI switches
Qiang(Joe) Xu
2016/11/30 02:08:44
The expectation can be satisfied. When GetUserMedi
| |
201 callback->OnError(this); | 210 callback->OnError(this); |
202 cras_audio_format_destroy(audio_format); | 211 cras_audio_format_destroy(audio_format); |
203 cras_client_stream_params_destroy(stream_params); | 212 cras_client_stream_params_destroy(stream_params); |
204 return; | 213 return; |
205 } | 214 } |
206 | 215 |
207 // Set initial volume. | 216 // Set initial volume. |
208 cras_client_set_stream_volume(client_, stream_id_, volume_); | 217 cras_client_set_stream_volume(client_, stream_id_, volume_); |
209 | 218 |
210 // Done with config params. | 219 // Done with config params. |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 return frames_filled; | 314 return frames_filled; |
306 } | 315 } |
307 | 316 |
308 void CrasUnifiedStream::NotifyStreamError(int err) { | 317 void CrasUnifiedStream::NotifyStreamError(int err) { |
309 // This will remove the stream from the client. | 318 // This will remove the stream from the client. |
310 if (source_callback_) | 319 if (source_callback_) |
311 source_callback_->OnError(this); | 320 source_callback_->OnError(this); |
312 } | 321 } |
313 | 322 |
314 } // namespace media | 323 } // namespace media |
OLD | NEW |