forked from dnschneid/crouton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
audio
457 lines (395 loc) · 14.5 KB
/
audio
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/bin/sh -e
# Copyright (c) 2015 The crouton Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
REQUIRES='core'
DESCRIPTION='Support audio playback via Chromium OS'\''s audio system.'
CHROOTBIN='volume'
CHROOTETC='pulseaudio-default.pa'
. "${TARGETSDIR:="$PWD"}/common"
### Append to prepare.sh:
# Fetch CRAS source
CRASBUILDTMP="`mktemp -d crouton-cras.XXXXXX --tmpdir=/tmp`"
addtrap "rm -rf --one-file-system '$CRASBUILDTMP'"
# ADHD_HEAD is normally autodetected, but it can be set manually using a custom
# target sourced before this one (used for automated testing)
# The custom target also needs to set CROS_VER_1
if [ -z "$ADHD_HEAD" ]; then
# Chrome OS version (e.g. 4100.86.0)
CROS_VER="`sed -n 's/^CHROMEOS_RELEASE_VERSION=//p' /var/host/lsb-release`"
# Set CROS_VER_1 to the major version number (e.g. 4100), used later on to
# selectively apply patches.
CROS_VER_1="${CROS_VER%%.*}"
cras_version="`cat /var/host/cras-version 2>/dev/null || true`"
ADHD_HEAD="${cras_version##*-}"
# Make sure ADHD_HEAD looks like a commit id
if [ "${#ADHD_HEAD}" -ne 40 -o "${head%[^0-9a-f]*}" != "$head" ]; then
echo "Empty or invalid cras-version (${cras_version})." 1>&2
ADHD_HEAD=""
fi
fi
# TODO: Remove autodetection code (not needed since build 6276)
if [ -z "$ADHD_HEAD" ]; then
# Fetch list of branches from repository
wget -O "$CRASBUILDTMP/adhd.refs" \
'https://chromium.googlesource.com/chromiumos/third_party/adhd/+refs/heads?format=TEXT'
echo "Detecting CRAS branch (Chromium OS build $CROS_VER)..." 1>&2
ADHD_HEAD="`awk '
BEGIN {
len = split("'"$CROS_VER"'", crosvers, ".")
# Prepare a regex with each additional version component optional
for (i = len; i >= 2; i--)
verregex = "(\\\\." crosvers[i] verregex ")?"
verregex = crosvers[1] verregex "($|\\\\.)"
}
# Release branches get the highest score
$2 ~ "release-(R[^-]*-)?" crosvers[1] "($|\\\\.)" {
print 99, $2
}
# Stabilize branches are scored based on how much of the version they match
match($2, "stabilize-" verregex) {
print RLENGTH, $2
}
' "$CRASBUILDTMP/adhd.refs" | sort -V | tail -n 1 | cut -d' ' -f2`"
# If no branch can be found, fall back on manifests. This should only be
# needed on dev or canary channel, and this cannot be used on stable/beta,
# as additional patches may be added to the git branch.
if [ -z "$ADHD_HEAD" ]; then
echo "No CRAS branch found, falling back on manifests..." 1>&2
MANIFEST_GIT="https://chromium.googlesource.com/chromiumos/manifest-versions/+/master/chrome-LKGM-candidates"
# Get the list of releases (e.g. 35). Only last 2 should be required.
majorlist="`wget -O- "$MANIFEST_GIT/buildspecs" \
| tr '>' '\n' | sed -n 's|^\([0-9]*\)/<.*|\1|p' \
| sort -n -r | head -n 2`"
for major in $majorlist; do
# Get the latest manifest for the current build
manifest="`wget -O- "$MANIFEST_GIT/buildspecs/$major" \
| sed -n 's/.*\('"$CROS_VER_1"'.0.0-rc[0-9]*.xml\).*/\1/p'`"
if [ -n "$manifest" ]; then
# Fetch git commit id
ADHD_HEAD="`wget -O- \
"$MANIFEST_GIT/buildspecs/$major/$manifest?format=TEXT" \
| base64 -d \
| sed -n 's|.*path="src/third_party/adhd".* revision="\([0-9a-f]*\)".*|\1|p'`"
break
fi
done
fi
if [ -z "$ADHD_HEAD" ]; then
echo "Cannot figure out CRAS branch or commit ID." 1>&2
exit 1
fi
fi
echo "Fetching CRAS (branch $ADHD_HEAD)..." 1>&2
# Try to download the CRAS commit id first, and fall back on master if that is
# not found (see crbug.com/417820).
archive="$CRASBUILDTMP/adhd.tar.gz"
log="$CRASBUILDTMP/wget.log"
urlbase="https://chromium.googlesource.com/chromiumos/third_party/adhd/+archive"
( wget -O "$archive" "$urlbase/$ADHD_HEAD.tar.gz" 2>&1 \
|| echo "Error fetching CRAS" ) | tee "$log"
if tail -n 1 "$log" | grep -q "^Error"; then
if grep -q "404 Not Found" "$log" 2>/dev/null; then
echo "Branch not found, falling back on master (audio might not work)..." 1>&2
ADHD_HEAD="master"
wget -O "$archive" "$urlbase/$ADHD_HEAD.tar.gz"
else
# wget already printed an explicit error
exit 1
fi
fi
# Build CRAS ALSA plugin for the given architecture ($1)
# A blank parameter means we are building for the native architecture.
build_cras() {
local cras_arch="$1"
local pkgsuffix=''
local pkgdepextra=''
local archextrapath=''
local pkgconfigpath=''
local archgccflags=''
if [ -n "$cras_arch" ]; then
pkgsuffix=":$cras_arch"
pkgdepextra='gcc-multilib'
archextrapath="/$cras_arch-linux-gnu"
pkgconfigpath="/usr/lib$archextrapath/pkgconfig"
archgccflags='-m32'
# Add foreign architecture, if necessary
if ! dpkg --print-foreign-architectures | grep -q "^$cras_arch$"; then
echo "Adding foreign architecture $cras_arch to dpkg..." 1>&2
dpkg --add-architecture "$cras_arch"
apt-get update || true
fi
fi
# Install CRAS dependencies
install --minimal alsa-utils \
libasound2$pkgsuffix libspeexdsp1$pkgsuffix
install --minimal --asdeps gcc $pkgdepextra libc6-dev$pkgsuffix \
pkg-config libspeexdsp-dev$pkgsuffix
# precise does not allow libasound2-dev and libasound2-dev:i386 to be
# installed simultaneously
if release -le precise && [ -n "$cras_arch" ]; then
install --minimal --asdeps libasound2-dev
# Manually link .so file
libasoundso="/usr/lib$archextrapath/libasound.so"
if [ ! -f "$libasoundso" ]; then
addtrap "rm -f '$libasoundso' 2>/dev/null"
ln -sfT libasound.so.2 "$libasoundso"
fi
ALSALIBDIR="/usr/lib$archextrapath/alsa-lib"
else
install --minimal --asdeps libasound2-dev$pkgsuffix
ALSALIBDIR="`PKG_CONFIG_PATH="$pkgconfigpath" \
pkg-config --variable=libdir alsa`/alsa-lib"
fi
# Start subshell for compilation
(
cd "$CRASBUILDTMP"
# Make sure we start fresh
rm -rf --one-file-system cras
# -m prevents "time stamp is in the future" spam
tar -xmf adhd.tar.gz cras/src
cd cras/src
# Create version file
echo '#define VCSID "crouton-'"$ADHD_HEAD"'"' > common/cras_version.h
install --minimal --asdeps patch
# Fix volume control, see crbug.com/415661
# Applied upstream in build 6283
# FIXME: Remove this when 6283 has trickled down to stable
if [ "$CROS_VER_1" -lt 6283 ]; then
echo "Patching CRAS (volume control)..." 1>&2
patch -p3 <<END
diff --git a/cras/src/alsa_plugin/ctl_cras.c b/cras/src/alsa_plugin/ctl_cras.c
index e9a0c47..e9549d9 100644
--- a/cras/src/alsa_plugin/ctl_cras.c
+++ b/cras/src/alsa_plugin/ctl_cras.c
@@ -161,21 +161,30 @@
long *value)
{
struct ctl_cras *cras = (struct ctl_cras *)ext_ctl->private_data;
+ cras_node_id_t node_id;
+ struct cras_ionode_info node;
switch (key) {
case CTL_CRAS_MIXER_PLAYBACK_SWITCH:
- *value = !cras_client_get_system_muted(cras->client);
+ *value = !cras_client_get_user_muted(cras->client);
break;
case CTL_CRAS_MIXER_PLAYBACK_VOLUME:
- *value = cras_client_get_system_volume(cras->client);
+ node_id = cras_client_get_selected_output(cras->client);
+ if (cras_client_get_node_by_id(cras->client, 0,
+ node_id, &node) < 0)
+ return -EIO;
+ *value = node.volume;
break;
case CTL_CRAS_MIXER_CAPTURE_SWITCH:
*value = !cras_client_get_system_capture_muted(cras->client);
break;
case CTL_CRAS_MIXER_CAPTURE_VOLUME:
- *value = capture_gain_to_index(
- cras->client,
- cras_client_get_system_capture_gain(cras->client));
+ node_id = cras_client_get_selected_input(cras->client);
+ if (cras_client_get_node_by_id(cras->client, 1,
+ node_id, &node) < 0)
+ return -EIO;
+ *value = capture_gain_to_index(cras->client,
+ node.capture_gain);
break;
default:
return -EINVAL;
@@ -189,20 +198,24 @@
long *value)
{
struct ctl_cras *cras = (struct ctl_cras *)ext_ctl->private_data;
+ cras_node_id_t node_id;
switch (key) {
case CTL_CRAS_MIXER_PLAYBACK_SWITCH:
- cras_client_set_system_mute(cras->client, !(*value));
+ cras_client_set_user_mute(cras->client, !(*value));
break;
case CTL_CRAS_MIXER_PLAYBACK_VOLUME:
- cras_client_set_system_volume(cras->client, *value);
+ node_id = cras_client_get_selected_output(cras->client);
+ cras_client_set_node_volume(cras->client, node_id, *value);
break;
case CTL_CRAS_MIXER_CAPTURE_SWITCH:
cras_client_set_system_capture_mute(cras->client, !(*value));
break;
case CTL_CRAS_MIXER_CAPTURE_VOLUME:
- cras_client_set_system_capture_gain(
+ node_id = cras_client_get_selected_input(cras->client);
+ cras_client_set_node_capture_gain(
cras->client,
+ node_id,
capture_index_to_gain(cras->client, *value));
break;
default:
END
cat >>libcras/cras_client.h <<END
int cras_client_get_user_muted(struct cras_client *client);
int cras_client_get_node_by_id(const struct cras_client *client,
int input,
const cras_node_id_t node_id,
struct cras_ionode_info* node_info);
END
cat >>libcras/cras_client.c <<END
int cras_client_get_user_muted(struct cras_client *client)
{
if (!client || !client->server_state)
return 0;
return client->server_state->user_mute;
}
int cras_client_get_node_by_id(const struct cras_client *client,
int input,
const cras_node_id_t node_id,
struct cras_ionode_info* node_info)
{
size_t ndevs, nnodes;
struct cras_iodev_info *devs = NULL;
struct cras_ionode_info *nodes = NULL;
int rc = -EINVAL;
unsigned i;
if (!client || !node_info) {
rc = -EINVAL;
goto quit;
}
devs = (struct cras_iodev_info *)
malloc(CRAS_MAX_IODEVS * sizeof(*devs));
if (!devs) {
rc = -ENOMEM;
goto quit;
}
nodes = (struct cras_ionode_info *)
malloc(CRAS_MAX_IONODES * sizeof(*nodes));
if (!nodes) {
rc = -ENOMEM;
goto quit;
}
ndevs = CRAS_MAX_IODEVS;
nnodes = CRAS_MAX_IONODES;
if (input)
rc = cras_client_get_input_devices(client, devs, nodes,
&ndevs, &nnodes);
else
rc = cras_client_get_output_devices(client, devs, nodes,
&ndevs, &nnodes);
if (rc < 0)
goto quit;
rc = -ENOENT;
for (i = 0; i < nnodes; i++) {
if (node_id == cras_make_node_id(nodes[i].iodev_idx,
nodes[i].ionode_idx)) {
memcpy(node_info, &nodes[i], sizeof(*node_info));
rc = 0;
break;
}
}
quit:
free(devs);
free(nodes);
return rc;
}
END
fi
# Remove SBC dependency
sed -e 's/#include <sbc.*//' -i common/cras_sbc_codec.h
cat > common/cras_sbc_codec.c <<END
#include <stdint.h>
#include <stdlib.h>
#include "cras_audio_codec.h"
struct cras_audio_codec *cras_sbc_codec_create(uint8_t freq,
uint8_t mode, uint8_t subbands, uint8_t alloc,
uint8_t blocks, uint8_t bitpool) {
abort();
}
void cras_sbc_codec_destroy(struct cras_audio_codec *codec) {
abort();
}
END
# Drop SBC constants
sed -e 's/SBC_[A-Z0-9_]*/0/g' -i tests/cras_test_client.c
# Directory to install CRAS library/binaries
CRASLIBDIR="/usr/local$archextrapath/lib"
CRASBINDIR="/usr/local$archextrapath/bin"
echo "Compiling CRAS (${cras_arch:-native})..." 1>&2
# Convert Makefile.am to a shell script, and run it.
{
convert_automake
echo '
buildlib libcras
# Pass -rpath=$CRASLIBDIR to linker, so we do not need to add
# the directory to ldconfig search path (some distributions do
# not include /usr/local/lib in /etc/ld.so.conf).
# We also need to add "-L." as we are not using .la files.
extraflags="-Wl,-rpath='"$CRASLIBDIR"' -L."
buildlib libasound_module_pcm_cras "$extraflags"
buildlib libasound_module_ctl_cras "$extraflags"
buildexe cras_test_client "$extraflags"
'
} | sh -s -e $SETOPTIONS
echo "Installing CRAS..." 1>&2
mkdir -p "$CRASBINDIR/" "$CRASLIBDIR/" "$ALSALIBDIR/"
# Only install libcras.so.X.Y.Z
/usr/bin/install -s libcras.so.*.* "$CRASLIBDIR/"
# Generate symbolic link to libcras.so.X
ldconfig -l "$CRASLIBDIR"/libcras.so.*.*
/usr/bin/install -s libasound_module_pcm_cras.so "$ALSALIBDIR/"
/usr/bin/install -s libasound_module_ctl_cras.so "$ALSALIBDIR/"
/usr/bin/install -s cras_test_client "$CRASBINDIR/"
) # End compilation subshell
}
# On x86_64, the ALSA plugin needs to be compiled for both 32-bit and 64-bit
# to allow audio playback using 32-bit applications.
if [ "$ARCH" = 'amd64' ]; then
build_cras 'i386'
fi
# Build CRAS for native architecture
build_cras
cat > /usr/share/alsa/alsa.conf.d/10-cras.conf <<EOF
pcm.cras {
type cras
hint {
show on
description "Chromium OS Audio Server"
}
}
ctl.cras {
type cras
}
# Default: route all audio through the CRAS plugin.
pcm.!default {
type cras
hint {
show on
description "Default ALSA Output (currently Chromium OS Audio Server)"
}
}
ctl.!default {
type cras
}
EOF
# Configure pulseaudio even if it is not installed (yet).
# We use ~/.config/pulse/default.pa for this purpose, but the main user may
# not have been created yet, so we add a script in /etc/profile.d to link
# ~/.config/pulse/default.pa to /etc/crouton/pulseaudio-default.pa.
paconfigdir='$HOME/.config/pulse'
# Old versions of pulseaudio use ~/.pulse/default.pa
if release -le quantal -le wheezy; then
paconfigdir='$HOME/.pulse'
fi
profiledsh='/etc/profile.d/crouton-pulseaudio-cras.sh'
# Make sure symbolic link is setup on login
echo '#!/bin/sh
defaultpa="'"$paconfigdir"'/default.pa"
# Do not install if user is root, or $HOME does not exist
if [ "`id -u`" -ne 0 -a -d "$HOME" -a ! -e "$defaultpa" ]; then
mkdir -p "'"$paconfigdir"'"
ln -sfT /etc/crouton/pulseaudio-default.pa "$defaultpa"
fi' > "$profiledsh"
chmod 755 "$profiledsh"
TIPS="$TIPS
Audio from the chroot will now be forwarded to CRAS (Chromium OS audio server),
through an ALSA plugin.
Future Chromium OS upgrades may break compatibility with the installed version
of CRAS. Should this happen, simply update your chroot.
"