forked from dnschneid/crouton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio
280 lines (234 loc) · 9.26 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
#!/bin/sh -e
# Copyright (c) 2016 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
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 \
libasound2-plugins$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
# 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
# Find the cras test client
cras_test_client='tools/cras_test_client/cras_test_client.c'
# FIXME: remove when 12334.0.0 (R77) becomes minimum target
if [ ! -f "$cras_test_client" ]; then
cras_test_client='tests/cras_test_client.c'
fi
# Drop SBC constants
sed -e 's/SBC_[A-Z0-9_]*/0/g' -i "$cras_test_client"
# FIXME(crbug.com/864815) Remove when fixed upstream
if [ "$cras_arch" = "i386" ]; then
sed -i -e 's/Dumping AEC info to %s, stream %lu/Dumping AEC info to %s, stream %llu/' \
"$cras_test_client"
fi
sed -i -e 's/cras_stream_id_t stream_id;/cras_stream_id_t stream_id = 0;/' \
"$cras_test_client"
# FIXME(http://crrev.com/c/1759370): Remove when R78 becomes minimum target
sed -i -e 's/\(strncpy.*sizeof(info.name)\));/\1 - 1); info.name[sizeof(info.name) - 1] = '"'"'\\0'"'"';/' \
common/cras_shm.c
sed -i -e 's/^struct \([^_]\)/struct __attribute__((__packed__)) \1/' \
common/cras_messages.h
# 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 '
CFLAGS="$CFLAGS -DCRAS_SOCKET_FILE_DIR=\"/var/run/cras\" -Wno-error"
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
alsaconf='/etc/alsa/conf.d'
if [ ! -d "$alsaconf" ]; then
alsaconf='/usr/share/alsa/alsa.conf.d'
if [ ! -d "$alsaconf" ]; then
echo "Unable to find correct ALSA configuration directory." 1>&2
alsaconf='/tmp'
fi
fi
echo "Writing ALSA config into $alsaconf/10-cras.conf" 1>&2
cat > "$alsaconf/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.
"