1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/audio/cras/cras_unified.h"
#include "base/logging.h"
#include "media/audio/cras/audio_manager_cras.h"
namespace media {
// Overview of operation:
// 1) An object of CrasUnifiedStream is created by the AudioManager
// factory: audio_man->MakeAudioStream().
// 2) Next some thread will call Open(), at that point a client is created and
// configured for the correct format and sample rate.
// 3) Then Start(source) is called and a stream is added to the CRAS client
// which will create its own thread that periodically calls the source for more
// data as buffers are being consumed.
// 4) When finished Stop() is called, which is handled by stopping the stream.
// 5) Finally Close() is called. It cleans up and notifies the audio manager,
// which likely will destroy this object.
//
// Simplified data flow for output only streams:
//
// +-------------+ +------------------+
// | CRAS Server | | Chrome Client |
// +------+------+ Add Stream +---------+--------+
// |<----------------------------------|
// | |
// | Near out of samples, request more |
// |---------------------------------->|
// | | UnifiedCallback()
// | | WriteAudio()
// | |
// | buffer_frames written to shm |
// |<----------------------------------|
// | |
// ... Repeats for each block. ...
// | |
// | |
// | Remove stream |
// |<----------------------------------|
// | |
//
// For Unified streams the Chrome client is notified whenever buffer_frames have
// been captured. For Output streams the client is notified a few milliseconds
// before the hardware buffer underruns and fills the buffer with another block
// of audio.
CrasUnifiedStream::CrasUnifiedStream(const AudioParameters& params,
AudioManagerCras* manager)
: client_(NULL),
stream_id_(0),
params_(params),
bytes_per_frame_(0),
is_playing_(false),
volume_(1.0),
manager_(manager),
source_callback_(NULL),
stream_direction_(CRAS_STREAM_OUTPUT) {
DCHECK(manager_);
DCHECK(params_.channels() > 0);
output_bus_ = AudioBus::Create(params);
}
CrasUnifiedStream::~CrasUnifiedStream() {
DCHECK(!is_playing_);
}
bool CrasUnifiedStream::Open() {
// Sanity check input values.
if (params_.sample_rate() <= 0) {
LOG(WARNING) << "Unsupported audio frequency.";
return false;
}
if (AudioManagerCras::BitsToFormat(params_.bits_per_sample()) ==
SND_PCM_FORMAT_UNKNOWN) {
LOG(WARNING) << "Unsupported pcm format";
return false;
}
// Create the client and connect to the CRAS server.
if (cras_client_create(&client_)) {
LOG(WARNING) << "Couldn't create CRAS client.\n";
client_ = NULL;
return false;
}
if (cras_client_connect(client_)) {
LOG(WARNING) << "Couldn't connect CRAS client.\n";
cras_client_destroy(client_);
client_ = NULL;
return false;
}
// Then start running the client.
if (cras_client_run_thread(client_)) {
LOG(WARNING) << "Couldn't run CRAS client.\n";
cras_client_destroy(client_);
client_ = NULL;
return false;
}
return true;
}
void CrasUnifiedStream::Close() {
if (client_) {
cras_client_stop(client_);
cras_client_destroy(client_);
client_ = NULL;
}
// Signal to the manager that we're closed and can be removed.
// Should be last call in the method as it deletes "this".
manager_->ReleaseOutputStream(this);
}
void CrasUnifiedStream::Start(AudioSourceCallback* callback) {
CHECK(callback);
// Channel map to CRAS_CHANNEL, values in the same order of
// corresponding source in Chromium defined Channels.
static const int kChannelMap[] = {
CRAS_CH_FL,
CRAS_CH_FR,
CRAS_CH_FC,
CRAS_CH_LFE,
CRAS_CH_RL,
CRAS_CH_RR,
CRAS_CH_FLC,
CRAS_CH_FRC,
CRAS_CH_RC,
CRAS_CH_SL,
CRAS_CH_SR
};
source_callback_ = callback;
// Only start if we can enter the playing state.
if (is_playing_)
return;
// Prepare |audio_format| and |stream_params| for the stream we
// will create.
cras_audio_format* audio_format = cras_audio_format_create(
AudioManagerCras::BitsToFormat(params_.bits_per_sample()),
params_.sample_rate(),
params_.channels());
if (!audio_format) {
LOG(WARNING) << "Error setting up audio parameters.";
callback->OnError(this);
return;
}
// Initialize channel layout to all -1 to indicate that none of
// the channels is set in the layout.
int8 layout[CRAS_CH_MAX] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
// Converts to CRAS defined channels. ChannelOrder will return -1
// for channels that does not present in params_.channel_layout().
for (size_t i = 0; i < arraysize(kChannelMap); ++i)
layout[kChannelMap[i]] = ChannelOrder(params_.channel_layout(),
static_cast<Channels>(i));
if (cras_audio_format_set_channel_layout(audio_format, layout)) {
LOG(WARNING) << "Error setting channel layout.";
callback->OnError(this);
return;
}
cras_stream_params* stream_params = cras_client_unified_params_create(
stream_direction_,
params_.frames_per_buffer(),
CRAS_STREAM_TYPE_DEFAULT,
0,
this,
CrasUnifiedStream::UnifiedCallback,
CrasUnifiedStream::StreamError,
audio_format);
if (!stream_params) {
LOG(WARNING) << "Error setting up stream parameters.";
callback->OnError(this);
cras_audio_format_destroy(audio_format);
return;
}
// Before starting the stream, save the number of bytes in a frame for use in
// the callback.
bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format);
// Adding the stream will start the audio callbacks requesting data.
if (cras_client_add_stream(client_, &stream_id_, stream_params) < 0) {
LOG(WARNING) << "Failed to add the stream";
callback->OnError(this);
cras_audio_format_destroy(audio_format);
cras_client_stream_params_destroy(stream_params);
return;
}
// Set initial volume.
cras_client_set_stream_volume(client_, stream_id_, volume_);
// Done with config params.
cras_audio_format_destroy(audio_format);
cras_client_stream_params_destroy(stream_params);
is_playing_ = true;
}
void CrasUnifiedStream::Stop() {
if (!client_)
return;
// Removing the stream from the client stops audio.
cras_client_rm_stream(client_, stream_id_);
is_playing_ = false;
}
void CrasUnifiedStream::SetVolume(double volume) {
if (!client_)
return;
volume_ = static_cast<float>(volume);
cras_client_set_stream_volume(client_, stream_id_, volume_);
}
void CrasUnifiedStream::GetVolume(double* volume) {
*volume = volume_;
}
uint32 CrasUnifiedStream::GetBytesLatency(
const struct timespec& latency_ts) {
uint32 latency_usec;
// Treat negative latency (if we are too slow to render) as 0.
if (latency_ts.tv_sec < 0 || latency_ts.tv_nsec < 0) {
latency_usec = 0;
} else {
latency_usec = (latency_ts.tv_sec * base::Time::kMicrosecondsPerSecond) +
latency_ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond;
}
double frames_latency =
latency_usec * params_.sample_rate() / base::Time::kMicrosecondsPerSecond;
return static_cast<unsigned int>(frames_latency * bytes_per_frame_);
}
// Static callback asking for samples.
int CrasUnifiedStream::UnifiedCallback(cras_client* client,
cras_stream_id_t stream_id,
uint8* input_samples,
uint8* output_samples,
unsigned int frames,
const timespec* input_ts,
const timespec* output_ts,
void* arg) {
CrasUnifiedStream* me = static_cast<CrasUnifiedStream*>(arg);
return me->DispatchCallback(frames,
input_samples,
output_samples,
input_ts,
output_ts);
}
// Static callback for stream errors.
int CrasUnifiedStream::StreamError(cras_client* client,
cras_stream_id_t stream_id,
int err,
void* arg) {
CrasUnifiedStream* me = static_cast<CrasUnifiedStream*>(arg);
me->NotifyStreamError(err);
return 0;
}
// Calls the appropriate rendering function for this type of stream.
uint32 CrasUnifiedStream::DispatchCallback(size_t frames,
uint8* input_samples,
uint8* output_samples,
const timespec* input_ts,
const timespec* output_ts) {
switch (stream_direction_) {
case CRAS_STREAM_OUTPUT:
return WriteAudio(frames, output_samples, output_ts);
case CRAS_STREAM_INPUT:
NOTREACHED() << "CrasUnifiedStream doesn't support input streams.";
return 0;
default:
break;
}
return 0;
}
uint32 CrasUnifiedStream::WriteAudio(size_t frames,
uint8* buffer,
const timespec* sample_ts) {
DCHECK_EQ(frames, static_cast<size_t>(output_bus_->frames()));
// Determine latency and pass that on to the source.
timespec latency_ts = {0, 0};
cras_client_calc_playback_latency(sample_ts, &latency_ts);
int frames_filled = source_callback_->OnMoreData(
output_bus_.get(), AudioBuffersState(0, GetBytesLatency(latency_ts)));
// Note: If this ever changes to output raw float the data must be clipped and
// sanitized since it may come from an untrusted source such as NaCl.
output_bus_->ToInterleaved(
frames_filled, bytes_per_frame_ / params_.channels(), buffer);
return frames_filled;
}
void CrasUnifiedStream::NotifyStreamError(int err) {
// This will remove the stream from the client.
if (source_callback_)
source_callback_->OnError(this);
}
} // namespace media
|