1*e01b6f76SAndroid Build Coastguard Worker /*
2*e01b6f76SAndroid Build Coastguard Worker * Copyright (C) 2012 The Android Open Source Project
3*e01b6f76SAndroid Build Coastguard Worker *
4*e01b6f76SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*e01b6f76SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*e01b6f76SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*e01b6f76SAndroid Build Coastguard Worker *
8*e01b6f76SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*e01b6f76SAndroid Build Coastguard Worker *
10*e01b6f76SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*e01b6f76SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*e01b6f76SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e01b6f76SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*e01b6f76SAndroid Build Coastguard Worker * limitations under the License.
15*e01b6f76SAndroid Build Coastguard Worker */
16*e01b6f76SAndroid Build Coastguard Worker
17*e01b6f76SAndroid Build Coastguard Worker #define LOG_TAG "modules.usbaudio.audio_hal"
18*e01b6f76SAndroid Build Coastguard Worker /* #define LOG_NDEBUG 0 */
19*e01b6f76SAndroid Build Coastguard Worker
20*e01b6f76SAndroid Build Coastguard Worker #include <errno.h>
21*e01b6f76SAndroid Build Coastguard Worker #include <inttypes.h>
22*e01b6f76SAndroid Build Coastguard Worker #include <math.h>
23*e01b6f76SAndroid Build Coastguard Worker #include <pthread.h>
24*e01b6f76SAndroid Build Coastguard Worker #include <stdint.h>
25*e01b6f76SAndroid Build Coastguard Worker #include <stdlib.h>
26*e01b6f76SAndroid Build Coastguard Worker #include <string.h>
27*e01b6f76SAndroid Build Coastguard Worker #include <sys/time.h>
28*e01b6f76SAndroid Build Coastguard Worker #include <unistd.h>
29*e01b6f76SAndroid Build Coastguard Worker
30*e01b6f76SAndroid Build Coastguard Worker #include <log/log.h>
31*e01b6f76SAndroid Build Coastguard Worker #include <cutils/list.h>
32*e01b6f76SAndroid Build Coastguard Worker #include <cutils/str_parms.h>
33*e01b6f76SAndroid Build Coastguard Worker #include <cutils/properties.h>
34*e01b6f76SAndroid Build Coastguard Worker
35*e01b6f76SAndroid Build Coastguard Worker #include <hardware/audio.h>
36*e01b6f76SAndroid Build Coastguard Worker #include <hardware/audio_alsaops.h>
37*e01b6f76SAndroid Build Coastguard Worker #include <hardware/hardware.h>
38*e01b6f76SAndroid Build Coastguard Worker
39*e01b6f76SAndroid Build Coastguard Worker #include <system/audio.h>
40*e01b6f76SAndroid Build Coastguard Worker
41*e01b6f76SAndroid Build Coastguard Worker #include <tinyalsa/asoundlib.h>
42*e01b6f76SAndroid Build Coastguard Worker
43*e01b6f76SAndroid Build Coastguard Worker #include <audio_utils/channels.h>
44*e01b6f76SAndroid Build Coastguard Worker
45*e01b6f76SAndroid Build Coastguard Worker #include "alsa_device_profile.h"
46*e01b6f76SAndroid Build Coastguard Worker #include "alsa_device_proxy.h"
47*e01b6f76SAndroid Build Coastguard Worker #include "alsa_logging.h"
48*e01b6f76SAndroid Build Coastguard Worker
49*e01b6f76SAndroid Build Coastguard Worker /* Lock play & record samples rates at or above this threshold */
50*e01b6f76SAndroid Build Coastguard Worker #define RATELOCK_THRESHOLD 96000
51*e01b6f76SAndroid Build Coastguard Worker
52*e01b6f76SAndroid Build Coastguard Worker #define max(a, b) ((a) > (b) ? (a) : (b))
53*e01b6f76SAndroid Build Coastguard Worker #define min(a, b) ((a) < (b) ? (a) : (b))
54*e01b6f76SAndroid Build Coastguard Worker
55*e01b6f76SAndroid Build Coastguard Worker struct audio_device {
56*e01b6f76SAndroid Build Coastguard Worker struct audio_hw_device hw_device;
57*e01b6f76SAndroid Build Coastguard Worker
58*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_t lock; /* see note below on mutex acquisition order */
59*e01b6f76SAndroid Build Coastguard Worker
60*e01b6f76SAndroid Build Coastguard Worker /* output */
61*e01b6f76SAndroid Build Coastguard Worker struct listnode output_stream_list;
62*e01b6f76SAndroid Build Coastguard Worker
63*e01b6f76SAndroid Build Coastguard Worker /* input */
64*e01b6f76SAndroid Build Coastguard Worker struct listnode input_stream_list;
65*e01b6f76SAndroid Build Coastguard Worker
66*e01b6f76SAndroid Build Coastguard Worker /* lock input & output sample rates */
67*e01b6f76SAndroid Build Coastguard Worker /*FIXME - How do we address multiple output streams? */
68*e01b6f76SAndroid Build Coastguard Worker uint32_t device_sample_rate; // this should be a rate that is common to both input & output
69*e01b6f76SAndroid Build Coastguard Worker
70*e01b6f76SAndroid Build Coastguard Worker bool mic_muted;
71*e01b6f76SAndroid Build Coastguard Worker
72*e01b6f76SAndroid Build Coastguard Worker int32_t inputs_open; /* number of input streams currently open. */
73*e01b6f76SAndroid Build Coastguard Worker
74*e01b6f76SAndroid Build Coastguard Worker audio_patch_handle_t next_patch_handle; // Increase 1 when create audio patch
75*e01b6f76SAndroid Build Coastguard Worker };
76*e01b6f76SAndroid Build Coastguard Worker
77*e01b6f76SAndroid Build Coastguard Worker struct stream_lock {
78*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_t lock; /* see note below on mutex acquisition order */
79*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
80*e01b6f76SAndroid Build Coastguard Worker };
81*e01b6f76SAndroid Build Coastguard Worker
82*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info {
83*e01b6f76SAndroid Build Coastguard Worker alsa_device_profile profile; /* The profile of the ALSA device */
84*e01b6f76SAndroid Build Coastguard Worker alsa_device_proxy proxy; /* The state */
85*e01b6f76SAndroid Build Coastguard Worker struct listnode list_node;
86*e01b6f76SAndroid Build Coastguard Worker };
87*e01b6f76SAndroid Build Coastguard Worker
88*e01b6f76SAndroid Build Coastguard Worker struct stream_out {
89*e01b6f76SAndroid Build Coastguard Worker struct audio_stream_out stream;
90*e01b6f76SAndroid Build Coastguard Worker
91*e01b6f76SAndroid Build Coastguard Worker struct stream_lock lock;
92*e01b6f76SAndroid Build Coastguard Worker
93*e01b6f76SAndroid Build Coastguard Worker bool standby;
94*e01b6f76SAndroid Build Coastguard Worker
95*e01b6f76SAndroid Build Coastguard Worker struct audio_device *adev; /* hardware information - only using this for the lock */
96*e01b6f76SAndroid Build Coastguard Worker
97*e01b6f76SAndroid Build Coastguard Worker struct listnode alsa_devices; /* The ALSA devices connected to the stream. */
98*e01b6f76SAndroid Build Coastguard Worker
99*e01b6f76SAndroid Build Coastguard Worker unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
100*e01b6f76SAndroid Build Coastguard Worker * This may differ from the device channel count when
101*e01b6f76SAndroid Build Coastguard Worker * the device is not compatible with AudioFlinger
102*e01b6f76SAndroid Build Coastguard Worker * capabilities, e.g. exposes too many channels or
103*e01b6f76SAndroid Build Coastguard Worker * too few channels. */
104*e01b6f76SAndroid Build Coastguard Worker audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
105*e01b6f76SAndroid Build Coastguard Worker * so the proxy doesn't have a channel_mask, but
106*e01b6f76SAndroid Build Coastguard Worker * audio HALs need to talk about channel masks
107*e01b6f76SAndroid Build Coastguard Worker * so expose the one calculated by
108*e01b6f76SAndroid Build Coastguard Worker * adev_open_output_stream */
109*e01b6f76SAndroid Build Coastguard Worker
110*e01b6f76SAndroid Build Coastguard Worker struct listnode list_node;
111*e01b6f76SAndroid Build Coastguard Worker
112*e01b6f76SAndroid Build Coastguard Worker void * conversion_buffer; /* any conversions are put into here
113*e01b6f76SAndroid Build Coastguard Worker * they could come from here too if
114*e01b6f76SAndroid Build Coastguard Worker * there was a previous conversion */
115*e01b6f76SAndroid Build Coastguard Worker size_t conversion_buffer_size; /* in bytes */
116*e01b6f76SAndroid Build Coastguard Worker
117*e01b6f76SAndroid Build Coastguard Worker struct pcm_config config;
118*e01b6f76SAndroid Build Coastguard Worker
119*e01b6f76SAndroid Build Coastguard Worker audio_io_handle_t handle; // Unique constant for a stream
120*e01b6f76SAndroid Build Coastguard Worker
121*e01b6f76SAndroid Build Coastguard Worker audio_patch_handle_t patch_handle; // Patch handle for this stream
122*e01b6f76SAndroid Build Coastguard Worker
123*e01b6f76SAndroid Build Coastguard Worker bool is_bit_perfect; // True if the stream is open with bit-perfect output flag
124*e01b6f76SAndroid Build Coastguard Worker
125*e01b6f76SAndroid Build Coastguard Worker // Mixer information used for volume handling
126*e01b6f76SAndroid Build Coastguard Worker struct mixer* mixer;
127*e01b6f76SAndroid Build Coastguard Worker struct mixer_ctl* volume_ctl;
128*e01b6f76SAndroid Build Coastguard Worker int volume_ctl_num_values;
129*e01b6f76SAndroid Build Coastguard Worker int max_volume_level;
130*e01b6f76SAndroid Build Coastguard Worker int min_volume_level;
131*e01b6f76SAndroid Build Coastguard Worker };
132*e01b6f76SAndroid Build Coastguard Worker
133*e01b6f76SAndroid Build Coastguard Worker struct stream_in {
134*e01b6f76SAndroid Build Coastguard Worker struct audio_stream_in stream;
135*e01b6f76SAndroid Build Coastguard Worker
136*e01b6f76SAndroid Build Coastguard Worker struct stream_lock lock;
137*e01b6f76SAndroid Build Coastguard Worker
138*e01b6f76SAndroid Build Coastguard Worker bool standby;
139*e01b6f76SAndroid Build Coastguard Worker
140*e01b6f76SAndroid Build Coastguard Worker struct audio_device *adev; /* hardware information - only using this for the lock */
141*e01b6f76SAndroid Build Coastguard Worker
142*e01b6f76SAndroid Build Coastguard Worker struct listnode alsa_devices; /* The ALSA devices connected to the stream. */
143*e01b6f76SAndroid Build Coastguard Worker
144*e01b6f76SAndroid Build Coastguard Worker unsigned hal_channel_count; /* channel count exposed to AudioFlinger.
145*e01b6f76SAndroid Build Coastguard Worker * This may differ from the device channel count when
146*e01b6f76SAndroid Build Coastguard Worker * the device is not compatible with AudioFlinger
147*e01b6f76SAndroid Build Coastguard Worker * capabilities, e.g. exposes too many channels or
148*e01b6f76SAndroid Build Coastguard Worker * too few channels. */
149*e01b6f76SAndroid Build Coastguard Worker audio_channel_mask_t hal_channel_mask; /* USB devices deal in channel counts, not masks
150*e01b6f76SAndroid Build Coastguard Worker * so the proxy doesn't have a channel_mask, but
151*e01b6f76SAndroid Build Coastguard Worker * audio HALs need to talk about channel masks
152*e01b6f76SAndroid Build Coastguard Worker * so expose the one calculated by
153*e01b6f76SAndroid Build Coastguard Worker * adev_open_input_stream */
154*e01b6f76SAndroid Build Coastguard Worker
155*e01b6f76SAndroid Build Coastguard Worker struct listnode list_node;
156*e01b6f76SAndroid Build Coastguard Worker
157*e01b6f76SAndroid Build Coastguard Worker /* We may need to read more data from the device in order to data reduce to 16bit, 4chan */
158*e01b6f76SAndroid Build Coastguard Worker void * conversion_buffer; /* any conversions are put into here
159*e01b6f76SAndroid Build Coastguard Worker * they could come from here too if
160*e01b6f76SAndroid Build Coastguard Worker * there was a previous conversion */
161*e01b6f76SAndroid Build Coastguard Worker size_t conversion_buffer_size; /* in bytes */
162*e01b6f76SAndroid Build Coastguard Worker
163*e01b6f76SAndroid Build Coastguard Worker struct pcm_config config;
164*e01b6f76SAndroid Build Coastguard Worker
165*e01b6f76SAndroid Build Coastguard Worker audio_io_handle_t handle; // Unique identifier for a stream
166*e01b6f76SAndroid Build Coastguard Worker
167*e01b6f76SAndroid Build Coastguard Worker audio_patch_handle_t patch_handle; // Patch handle for this stream
168*e01b6f76SAndroid Build Coastguard Worker };
169*e01b6f76SAndroid Build Coastguard Worker
170*e01b6f76SAndroid Build Coastguard Worker // Map channel count to output channel mask
171*e01b6f76SAndroid Build Coastguard Worker static const audio_channel_mask_t OUT_CHANNEL_MASKS_MAP[FCC_24 + 1] = {
172*e01b6f76SAndroid Build Coastguard Worker [0] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted)
173*e01b6f76SAndroid Build Coastguard Worker // != AUDIO_CHANNEL_INVALID == 0xC0000000u
174*e01b6f76SAndroid Build Coastguard Worker
175*e01b6f76SAndroid Build Coastguard Worker [1] = AUDIO_CHANNEL_OUT_MONO,
176*e01b6f76SAndroid Build Coastguard Worker [2] = AUDIO_CHANNEL_OUT_STEREO,
177*e01b6f76SAndroid Build Coastguard Worker [3] = AUDIO_CHANNEL_OUT_2POINT1,
178*e01b6f76SAndroid Build Coastguard Worker [4] = AUDIO_CHANNEL_OUT_QUAD,
179*e01b6f76SAndroid Build Coastguard Worker [5] = AUDIO_CHANNEL_OUT_PENTA,
180*e01b6f76SAndroid Build Coastguard Worker [6] = AUDIO_CHANNEL_OUT_5POINT1,
181*e01b6f76SAndroid Build Coastguard Worker [7] = AUDIO_CHANNEL_OUT_6POINT1,
182*e01b6f76SAndroid Build Coastguard Worker [8] = AUDIO_CHANNEL_OUT_7POINT1,
183*e01b6f76SAndroid Build Coastguard Worker
184*e01b6f76SAndroid Build Coastguard Worker [9 ... 11] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
185*e01b6f76SAndroid Build Coastguard Worker
186*e01b6f76SAndroid Build Coastguard Worker [12] = AUDIO_CHANNEL_OUT_7POINT1POINT4,
187*e01b6f76SAndroid Build Coastguard Worker
188*e01b6f76SAndroid Build Coastguard Worker [13 ... 23] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
189*e01b6f76SAndroid Build Coastguard Worker
190*e01b6f76SAndroid Build Coastguard Worker [24] = AUDIO_CHANNEL_OUT_22POINT2,
191*e01b6f76SAndroid Build Coastguard Worker };
192*e01b6f76SAndroid Build Coastguard Worker static const int OUT_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(OUT_CHANNEL_MASKS_MAP);
193*e01b6f76SAndroid Build Coastguard Worker
194*e01b6f76SAndroid Build Coastguard Worker // Map channel count to input channel mask
195*e01b6f76SAndroid Build Coastguard Worker static const audio_channel_mask_t IN_CHANNEL_MASKS_MAP[] = {
196*e01b6f76SAndroid Build Coastguard Worker AUDIO_CHANNEL_NONE, /* 0 */
197*e01b6f76SAndroid Build Coastguard Worker AUDIO_CHANNEL_IN_MONO, /* 1 */
198*e01b6f76SAndroid Build Coastguard Worker AUDIO_CHANNEL_IN_STEREO, /* 2 */
199*e01b6f76SAndroid Build Coastguard Worker /* channel counts greater than this are not considered */
200*e01b6f76SAndroid Build Coastguard Worker };
201*e01b6f76SAndroid Build Coastguard Worker static const int IN_CHANNEL_MASKS_SIZE = AUDIO_ARRAY_SIZE(IN_CHANNEL_MASKS_MAP);
202*e01b6f76SAndroid Build Coastguard Worker
203*e01b6f76SAndroid Build Coastguard Worker // Map channel count to index mask
204*e01b6f76SAndroid Build Coastguard Worker static const audio_channel_mask_t CHANNEL_INDEX_MASKS_MAP[FCC_24 + 1] = {
205*e01b6f76SAndroid Build Coastguard Worker [0] = AUDIO_CHANNEL_NONE, // == 0 (so this line is optional and could be omitted).
206*e01b6f76SAndroid Build Coastguard Worker
207*e01b6f76SAndroid Build Coastguard Worker [1] = AUDIO_CHANNEL_INDEX_MASK_1,
208*e01b6f76SAndroid Build Coastguard Worker [2] = AUDIO_CHANNEL_INDEX_MASK_2,
209*e01b6f76SAndroid Build Coastguard Worker [3] = AUDIO_CHANNEL_INDEX_MASK_3,
210*e01b6f76SAndroid Build Coastguard Worker [4] = AUDIO_CHANNEL_INDEX_MASK_4,
211*e01b6f76SAndroid Build Coastguard Worker [5] = AUDIO_CHANNEL_INDEX_MASK_5,
212*e01b6f76SAndroid Build Coastguard Worker [6] = AUDIO_CHANNEL_INDEX_MASK_6,
213*e01b6f76SAndroid Build Coastguard Worker [7] = AUDIO_CHANNEL_INDEX_MASK_7,
214*e01b6f76SAndroid Build Coastguard Worker [8] = AUDIO_CHANNEL_INDEX_MASK_8,
215*e01b6f76SAndroid Build Coastguard Worker
216*e01b6f76SAndroid Build Coastguard Worker [9] = AUDIO_CHANNEL_INDEX_MASK_9,
217*e01b6f76SAndroid Build Coastguard Worker [10] = AUDIO_CHANNEL_INDEX_MASK_10,
218*e01b6f76SAndroid Build Coastguard Worker [11] = AUDIO_CHANNEL_INDEX_MASK_11,
219*e01b6f76SAndroid Build Coastguard Worker [12] = AUDIO_CHANNEL_INDEX_MASK_12,
220*e01b6f76SAndroid Build Coastguard Worker [13] = AUDIO_CHANNEL_INDEX_MASK_13,
221*e01b6f76SAndroid Build Coastguard Worker [14] = AUDIO_CHANNEL_INDEX_MASK_14,
222*e01b6f76SAndroid Build Coastguard Worker [15] = AUDIO_CHANNEL_INDEX_MASK_15,
223*e01b6f76SAndroid Build Coastguard Worker [16] = AUDIO_CHANNEL_INDEX_MASK_16,
224*e01b6f76SAndroid Build Coastguard Worker
225*e01b6f76SAndroid Build Coastguard Worker [17] = AUDIO_CHANNEL_INDEX_MASK_17,
226*e01b6f76SAndroid Build Coastguard Worker [18] = AUDIO_CHANNEL_INDEX_MASK_18,
227*e01b6f76SAndroid Build Coastguard Worker [19] = AUDIO_CHANNEL_INDEX_MASK_19,
228*e01b6f76SAndroid Build Coastguard Worker [20] = AUDIO_CHANNEL_INDEX_MASK_20,
229*e01b6f76SAndroid Build Coastguard Worker [21] = AUDIO_CHANNEL_INDEX_MASK_21,
230*e01b6f76SAndroid Build Coastguard Worker [22] = AUDIO_CHANNEL_INDEX_MASK_22,
231*e01b6f76SAndroid Build Coastguard Worker [23] = AUDIO_CHANNEL_INDEX_MASK_23,
232*e01b6f76SAndroid Build Coastguard Worker [24] = AUDIO_CHANNEL_INDEX_MASK_24,
233*e01b6f76SAndroid Build Coastguard Worker };
234*e01b6f76SAndroid Build Coastguard Worker static const int CHANNEL_INDEX_MASKS_SIZE = AUDIO_ARRAY_SIZE(CHANNEL_INDEX_MASKS_MAP);
235*e01b6f76SAndroid Build Coastguard Worker
236*e01b6f76SAndroid Build Coastguard Worker static const char* ALL_VOLUME_CONTROL_NAMES[] = {
237*e01b6f76SAndroid Build Coastguard Worker "PCM Playback Volume",
238*e01b6f76SAndroid Build Coastguard Worker "Headset Playback Volume",
239*e01b6f76SAndroid Build Coastguard Worker "Headphone Playback Volume",
240*e01b6f76SAndroid Build Coastguard Worker "Master Playback Volume",
241*e01b6f76SAndroid Build Coastguard Worker };
242*e01b6f76SAndroid Build Coastguard Worker static const int VOLUME_CONTROL_NAMES_NUM = AUDIO_ARRAY_SIZE(ALL_VOLUME_CONTROL_NAMES);
243*e01b6f76SAndroid Build Coastguard Worker
244*e01b6f76SAndroid Build Coastguard Worker /*
245*e01b6f76SAndroid Build Coastguard Worker * Locking Helpers
246*e01b6f76SAndroid Build Coastguard Worker */
247*e01b6f76SAndroid Build Coastguard Worker /*
248*e01b6f76SAndroid Build Coastguard Worker * NOTE: when multiple mutexes have to be acquired, always take the
249*e01b6f76SAndroid Build Coastguard Worker * stream_in or stream_out mutex first, followed by the audio_device mutex.
250*e01b6f76SAndroid Build Coastguard Worker * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
251*e01b6f76SAndroid Build Coastguard Worker * higher priority playback or capture thread.
252*e01b6f76SAndroid Build Coastguard Worker */
253*e01b6f76SAndroid Build Coastguard Worker
stream_lock_init(struct stream_lock * lock)254*e01b6f76SAndroid Build Coastguard Worker static void stream_lock_init(struct stream_lock *lock) {
255*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_init(&lock->lock, (const pthread_mutexattr_t *) NULL);
256*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_init(&lock->pre_lock, (const pthread_mutexattr_t *) NULL);
257*e01b6f76SAndroid Build Coastguard Worker }
258*e01b6f76SAndroid Build Coastguard Worker
stream_lock(struct stream_lock * lock)259*e01b6f76SAndroid Build Coastguard Worker static void stream_lock(struct stream_lock *lock) {
260*e01b6f76SAndroid Build Coastguard Worker if (lock == NULL) {
261*e01b6f76SAndroid Build Coastguard Worker return;
262*e01b6f76SAndroid Build Coastguard Worker }
263*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_lock(&lock->pre_lock);
264*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_lock(&lock->lock);
265*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_unlock(&lock->pre_lock);
266*e01b6f76SAndroid Build Coastguard Worker }
267*e01b6f76SAndroid Build Coastguard Worker
stream_unlock(struct stream_lock * lock)268*e01b6f76SAndroid Build Coastguard Worker static void stream_unlock(struct stream_lock *lock) {
269*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_unlock(&lock->lock);
270*e01b6f76SAndroid Build Coastguard Worker }
271*e01b6f76SAndroid Build Coastguard Worker
device_lock(struct audio_device * adev)272*e01b6f76SAndroid Build Coastguard Worker static void device_lock(struct audio_device *adev) {
273*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_lock(&adev->lock);
274*e01b6f76SAndroid Build Coastguard Worker }
275*e01b6f76SAndroid Build Coastguard Worker
device_try_lock(struct audio_device * adev)276*e01b6f76SAndroid Build Coastguard Worker static int device_try_lock(struct audio_device *adev) {
277*e01b6f76SAndroid Build Coastguard Worker return pthread_mutex_trylock(&adev->lock);
278*e01b6f76SAndroid Build Coastguard Worker }
279*e01b6f76SAndroid Build Coastguard Worker
device_unlock(struct audio_device * adev)280*e01b6f76SAndroid Build Coastguard Worker static void device_unlock(struct audio_device *adev) {
281*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_unlock(&adev->lock);
282*e01b6f76SAndroid Build Coastguard Worker }
283*e01b6f76SAndroid Build Coastguard Worker
284*e01b6f76SAndroid Build Coastguard Worker /*
285*e01b6f76SAndroid Build Coastguard Worker * streams list management
286*e01b6f76SAndroid Build Coastguard Worker */
adev_add_stream_to_list(struct audio_device * adev,struct listnode * list,struct listnode * stream_node)287*e01b6f76SAndroid Build Coastguard Worker static void adev_add_stream_to_list(
288*e01b6f76SAndroid Build Coastguard Worker struct audio_device* adev, struct listnode* list, struct listnode* stream_node) {
289*e01b6f76SAndroid Build Coastguard Worker device_lock(adev);
290*e01b6f76SAndroid Build Coastguard Worker
291*e01b6f76SAndroid Build Coastguard Worker list_add_tail(list, stream_node);
292*e01b6f76SAndroid Build Coastguard Worker
293*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
294*e01b6f76SAndroid Build Coastguard Worker }
295*e01b6f76SAndroid Build Coastguard Worker
adev_get_stream_out_by_io_handle_l(struct audio_device * adev,audio_io_handle_t handle)296*e01b6f76SAndroid Build Coastguard Worker static struct stream_out* adev_get_stream_out_by_io_handle_l(
297*e01b6f76SAndroid Build Coastguard Worker struct audio_device* adev, audio_io_handle_t handle) {
298*e01b6f76SAndroid Build Coastguard Worker struct listnode *node;
299*e01b6f76SAndroid Build Coastguard Worker list_for_each (node, &adev->output_stream_list) {
300*e01b6f76SAndroid Build Coastguard Worker struct stream_out *out = node_to_item(node, struct stream_out, list_node);
301*e01b6f76SAndroid Build Coastguard Worker if (out->handle == handle) {
302*e01b6f76SAndroid Build Coastguard Worker return out;
303*e01b6f76SAndroid Build Coastguard Worker }
304*e01b6f76SAndroid Build Coastguard Worker }
305*e01b6f76SAndroid Build Coastguard Worker return NULL;
306*e01b6f76SAndroid Build Coastguard Worker }
307*e01b6f76SAndroid Build Coastguard Worker
adev_get_stream_in_by_io_handle_l(struct audio_device * adev,audio_io_handle_t handle)308*e01b6f76SAndroid Build Coastguard Worker static struct stream_in* adev_get_stream_in_by_io_handle_l(
309*e01b6f76SAndroid Build Coastguard Worker struct audio_device* adev, audio_io_handle_t handle) {
310*e01b6f76SAndroid Build Coastguard Worker struct listnode *node;
311*e01b6f76SAndroid Build Coastguard Worker list_for_each (node, &adev->input_stream_list) {
312*e01b6f76SAndroid Build Coastguard Worker struct stream_in *in = node_to_item(node, struct stream_in, list_node);
313*e01b6f76SAndroid Build Coastguard Worker if (in->handle == handle) {
314*e01b6f76SAndroid Build Coastguard Worker return in;
315*e01b6f76SAndroid Build Coastguard Worker }
316*e01b6f76SAndroid Build Coastguard Worker }
317*e01b6f76SAndroid Build Coastguard Worker return NULL;
318*e01b6f76SAndroid Build Coastguard Worker }
319*e01b6f76SAndroid Build Coastguard Worker
adev_get_stream_out_by_patch_handle_l(struct audio_device * adev,audio_patch_handle_t patch_handle)320*e01b6f76SAndroid Build Coastguard Worker static struct stream_out* adev_get_stream_out_by_patch_handle_l(
321*e01b6f76SAndroid Build Coastguard Worker struct audio_device* adev, audio_patch_handle_t patch_handle) {
322*e01b6f76SAndroid Build Coastguard Worker struct listnode *node;
323*e01b6f76SAndroid Build Coastguard Worker list_for_each (node, &adev->output_stream_list) {
324*e01b6f76SAndroid Build Coastguard Worker struct stream_out *out = node_to_item(node, struct stream_out, list_node);
325*e01b6f76SAndroid Build Coastguard Worker if (out->patch_handle == patch_handle) {
326*e01b6f76SAndroid Build Coastguard Worker return out;
327*e01b6f76SAndroid Build Coastguard Worker }
328*e01b6f76SAndroid Build Coastguard Worker }
329*e01b6f76SAndroid Build Coastguard Worker return NULL;
330*e01b6f76SAndroid Build Coastguard Worker }
331*e01b6f76SAndroid Build Coastguard Worker
adev_get_stream_in_by_patch_handle_l(struct audio_device * adev,audio_patch_handle_t patch_handle)332*e01b6f76SAndroid Build Coastguard Worker static struct stream_in* adev_get_stream_in_by_patch_handle_l(
333*e01b6f76SAndroid Build Coastguard Worker struct audio_device* adev, audio_patch_handle_t patch_handle) {
334*e01b6f76SAndroid Build Coastguard Worker struct listnode *node;
335*e01b6f76SAndroid Build Coastguard Worker list_for_each (node, &adev->input_stream_list) {
336*e01b6f76SAndroid Build Coastguard Worker struct stream_in *in = node_to_item(node, struct stream_in, list_node);
337*e01b6f76SAndroid Build Coastguard Worker if (in->patch_handle == patch_handle) {
338*e01b6f76SAndroid Build Coastguard Worker return in;
339*e01b6f76SAndroid Build Coastguard Worker }
340*e01b6f76SAndroid Build Coastguard Worker }
341*e01b6f76SAndroid Build Coastguard Worker return NULL;
342*e01b6f76SAndroid Build Coastguard Worker }
343*e01b6f76SAndroid Build Coastguard Worker
344*e01b6f76SAndroid Build Coastguard Worker /*
345*e01b6f76SAndroid Build Coastguard Worker * Extract the card and device numbers from the supplied key/value pairs.
346*e01b6f76SAndroid Build Coastguard Worker * kvpairs A null-terminated string containing the key/value pairs or card and device.
347*e01b6f76SAndroid Build Coastguard Worker * i.e. "card=1;device=42"
348*e01b6f76SAndroid Build Coastguard Worker * card A pointer to a variable to receive the parsed-out card number.
349*e01b6f76SAndroid Build Coastguard Worker * device A pointer to a variable to receive the parsed-out device number.
350*e01b6f76SAndroid Build Coastguard Worker * NOTE: The variables pointed to by card and device return -1 (undefined) if the
351*e01b6f76SAndroid Build Coastguard Worker * associated key/value pair is not found in the provided string.
352*e01b6f76SAndroid Build Coastguard Worker * Return true if the kvpairs string contain a card/device spec, false otherwise.
353*e01b6f76SAndroid Build Coastguard Worker */
parse_card_device_params(const char * kvpairs,int * card,int * device)354*e01b6f76SAndroid Build Coastguard Worker static bool parse_card_device_params(const char *kvpairs, int *card, int *device)
355*e01b6f76SAndroid Build Coastguard Worker {
356*e01b6f76SAndroid Build Coastguard Worker struct str_parms * parms = str_parms_create_str(kvpairs);
357*e01b6f76SAndroid Build Coastguard Worker char value[32];
358*e01b6f76SAndroid Build Coastguard Worker int param_val;
359*e01b6f76SAndroid Build Coastguard Worker
360*e01b6f76SAndroid Build Coastguard Worker // initialize to "undefined" state.
361*e01b6f76SAndroid Build Coastguard Worker *card = -1;
362*e01b6f76SAndroid Build Coastguard Worker *device = -1;
363*e01b6f76SAndroid Build Coastguard Worker
364*e01b6f76SAndroid Build Coastguard Worker param_val = str_parms_get_str(parms, "card", value, sizeof(value));
365*e01b6f76SAndroid Build Coastguard Worker if (param_val >= 0) {
366*e01b6f76SAndroid Build Coastguard Worker *card = atoi(value);
367*e01b6f76SAndroid Build Coastguard Worker }
368*e01b6f76SAndroid Build Coastguard Worker
369*e01b6f76SAndroid Build Coastguard Worker param_val = str_parms_get_str(parms, "device", value, sizeof(value));
370*e01b6f76SAndroid Build Coastguard Worker if (param_val >= 0) {
371*e01b6f76SAndroid Build Coastguard Worker *device = atoi(value);
372*e01b6f76SAndroid Build Coastguard Worker }
373*e01b6f76SAndroid Build Coastguard Worker
374*e01b6f76SAndroid Build Coastguard Worker str_parms_destroy(parms);
375*e01b6f76SAndroid Build Coastguard Worker
376*e01b6f76SAndroid Build Coastguard Worker return *card >= 0 && *device >= 0;
377*e01b6f76SAndroid Build Coastguard Worker }
378*e01b6f76SAndroid Build Coastguard Worker
device_get_parameters(const alsa_device_profile * profile,const char * keys)379*e01b6f76SAndroid Build Coastguard Worker static char *device_get_parameters(const alsa_device_profile *profile, const char * keys)
380*e01b6f76SAndroid Build Coastguard Worker {
381*e01b6f76SAndroid Build Coastguard Worker if (profile->card < 0 || profile->device < 0) {
382*e01b6f76SAndroid Build Coastguard Worker return strdup("");
383*e01b6f76SAndroid Build Coastguard Worker }
384*e01b6f76SAndroid Build Coastguard Worker
385*e01b6f76SAndroid Build Coastguard Worker struct str_parms *query = str_parms_create_str(keys);
386*e01b6f76SAndroid Build Coastguard Worker struct str_parms *result = str_parms_create();
387*e01b6f76SAndroid Build Coastguard Worker
388*e01b6f76SAndroid Build Coastguard Worker /* These keys are from hardware/libhardware/include/audio.h */
389*e01b6f76SAndroid Build Coastguard Worker /* supported sample rates */
390*e01b6f76SAndroid Build Coastguard Worker if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)) {
391*e01b6f76SAndroid Build Coastguard Worker char* rates_list = profile_get_sample_rate_strs(profile);
392*e01b6f76SAndroid Build Coastguard Worker str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES,
393*e01b6f76SAndroid Build Coastguard Worker rates_list);
394*e01b6f76SAndroid Build Coastguard Worker free(rates_list);
395*e01b6f76SAndroid Build Coastguard Worker }
396*e01b6f76SAndroid Build Coastguard Worker
397*e01b6f76SAndroid Build Coastguard Worker /* supported channel counts */
398*e01b6f76SAndroid Build Coastguard Worker if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS)) {
399*e01b6f76SAndroid Build Coastguard Worker char* channels_list = profile_get_channel_count_strs(profile);
400*e01b6f76SAndroid Build Coastguard Worker str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_CHANNELS,
401*e01b6f76SAndroid Build Coastguard Worker channels_list);
402*e01b6f76SAndroid Build Coastguard Worker free(channels_list);
403*e01b6f76SAndroid Build Coastguard Worker }
404*e01b6f76SAndroid Build Coastguard Worker
405*e01b6f76SAndroid Build Coastguard Worker /* supported sample formats */
406*e01b6f76SAndroid Build Coastguard Worker if (str_parms_has_key(query, AUDIO_PARAMETER_STREAM_SUP_FORMATS)) {
407*e01b6f76SAndroid Build Coastguard Worker char * format_params = profile_get_format_strs(profile);
408*e01b6f76SAndroid Build Coastguard Worker str_parms_add_str(result, AUDIO_PARAMETER_STREAM_SUP_FORMATS,
409*e01b6f76SAndroid Build Coastguard Worker format_params);
410*e01b6f76SAndroid Build Coastguard Worker free(format_params);
411*e01b6f76SAndroid Build Coastguard Worker }
412*e01b6f76SAndroid Build Coastguard Worker str_parms_destroy(query);
413*e01b6f76SAndroid Build Coastguard Worker
414*e01b6f76SAndroid Build Coastguard Worker char* result_str = str_parms_to_str(result);
415*e01b6f76SAndroid Build Coastguard Worker str_parms_destroy(result);
416*e01b6f76SAndroid Build Coastguard Worker
417*e01b6f76SAndroid Build Coastguard Worker ALOGV("device_get_parameters = %s", result_str);
418*e01b6f76SAndroid Build Coastguard Worker
419*e01b6f76SAndroid Build Coastguard Worker return result_str;
420*e01b6f76SAndroid Build Coastguard Worker }
421*e01b6f76SAndroid Build Coastguard Worker
audio_format_from(enum pcm_format format)422*e01b6f76SAndroid Build Coastguard Worker static audio_format_t audio_format_from(enum pcm_format format)
423*e01b6f76SAndroid Build Coastguard Worker {
424*e01b6f76SAndroid Build Coastguard Worker switch (format) {
425*e01b6f76SAndroid Build Coastguard Worker case PCM_FORMAT_S16_LE:
426*e01b6f76SAndroid Build Coastguard Worker return AUDIO_FORMAT_PCM_16_BIT;
427*e01b6f76SAndroid Build Coastguard Worker case PCM_FORMAT_S32_LE:
428*e01b6f76SAndroid Build Coastguard Worker return AUDIO_FORMAT_PCM_32_BIT;
429*e01b6f76SAndroid Build Coastguard Worker case PCM_FORMAT_S8:
430*e01b6f76SAndroid Build Coastguard Worker return AUDIO_FORMAT_PCM_8_BIT;
431*e01b6f76SAndroid Build Coastguard Worker case PCM_FORMAT_S24_LE:
432*e01b6f76SAndroid Build Coastguard Worker return AUDIO_FORMAT_PCM_8_24_BIT;
433*e01b6f76SAndroid Build Coastguard Worker case PCM_FORMAT_S24_3LE:
434*e01b6f76SAndroid Build Coastguard Worker return AUDIO_FORMAT_PCM_24_BIT_PACKED;
435*e01b6f76SAndroid Build Coastguard Worker default:
436*e01b6f76SAndroid Build Coastguard Worker return AUDIO_FORMAT_INVALID;
437*e01b6f76SAndroid Build Coastguard Worker }
438*e01b6f76SAndroid Build Coastguard Worker }
439*e01b6f76SAndroid Build Coastguard Worker
populate_channel_mask_from_profile(const alsa_device_profile * profile,bool is_output,audio_channel_mask_t channel_masks[])440*e01b6f76SAndroid Build Coastguard Worker static unsigned int populate_channel_mask_from_profile(const alsa_device_profile* profile,
441*e01b6f76SAndroid Build Coastguard Worker bool is_output,
442*e01b6f76SAndroid Build Coastguard Worker audio_channel_mask_t channel_masks[])
443*e01b6f76SAndroid Build Coastguard Worker {
444*e01b6f76SAndroid Build Coastguard Worker unsigned int num_channel_masks = 0;
445*e01b6f76SAndroid Build Coastguard Worker const audio_channel_mask_t* channel_masks_map =
446*e01b6f76SAndroid Build Coastguard Worker is_output ? OUT_CHANNEL_MASKS_MAP : IN_CHANNEL_MASKS_MAP;
447*e01b6f76SAndroid Build Coastguard Worker int channel_masks_size = is_output ? OUT_CHANNEL_MASKS_SIZE : IN_CHANNEL_MASKS_SIZE;
448*e01b6f76SAndroid Build Coastguard Worker if (channel_masks_size > FCC_LIMIT + 1) {
449*e01b6f76SAndroid Build Coastguard Worker channel_masks_size = FCC_LIMIT + 1;
450*e01b6f76SAndroid Build Coastguard Worker }
451*e01b6f76SAndroid Build Coastguard Worker unsigned int channel_count = 0;
452*e01b6f76SAndroid Build Coastguard Worker for (size_t i = 0; i < min(channel_masks_size, AUDIO_PORT_MAX_CHANNEL_MASKS) &&
453*e01b6f76SAndroid Build Coastguard Worker (channel_count = profile->channel_counts[i]) != 0 &&
454*e01b6f76SAndroid Build Coastguard Worker num_channel_masks < AUDIO_PORT_MAX_CHANNEL_MASKS; ++i) {
455*e01b6f76SAndroid Build Coastguard Worker if (channel_count < channel_masks_size &&
456*e01b6f76SAndroid Build Coastguard Worker channel_masks_map[channel_count] != AUDIO_CHANNEL_NONE) {
457*e01b6f76SAndroid Build Coastguard Worker channel_masks[num_channel_masks++] = channel_masks_map[channel_count];
458*e01b6f76SAndroid Build Coastguard Worker if (num_channel_masks >= AUDIO_PORT_MAX_CHANNEL_MASKS) {
459*e01b6f76SAndroid Build Coastguard Worker break;
460*e01b6f76SAndroid Build Coastguard Worker }
461*e01b6f76SAndroid Build Coastguard Worker }
462*e01b6f76SAndroid Build Coastguard Worker if (channel_count < CHANNEL_INDEX_MASKS_SIZE &&
463*e01b6f76SAndroid Build Coastguard Worker CHANNEL_INDEX_MASKS_MAP[channel_count] != AUDIO_CHANNEL_NONE) {
464*e01b6f76SAndroid Build Coastguard Worker channel_masks[num_channel_masks++] = CHANNEL_INDEX_MASKS_MAP[channel_count];
465*e01b6f76SAndroid Build Coastguard Worker }
466*e01b6f76SAndroid Build Coastguard Worker }
467*e01b6f76SAndroid Build Coastguard Worker return num_channel_masks;
468*e01b6f76SAndroid Build Coastguard Worker }
469*e01b6f76SAndroid Build Coastguard Worker
populate_sample_rates_from_profile(const alsa_device_profile * profile,unsigned int sample_rates[])470*e01b6f76SAndroid Build Coastguard Worker static unsigned int populate_sample_rates_from_profile(const alsa_device_profile* profile,
471*e01b6f76SAndroid Build Coastguard Worker unsigned int sample_rates[])
472*e01b6f76SAndroid Build Coastguard Worker {
473*e01b6f76SAndroid Build Coastguard Worker unsigned int num_sample_rates = 0;
474*e01b6f76SAndroid Build Coastguard Worker for (;num_sample_rates < min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES) &&
475*e01b6f76SAndroid Build Coastguard Worker profile->sample_rates[num_sample_rates] != 0; num_sample_rates++) {
476*e01b6f76SAndroid Build Coastguard Worker sample_rates[num_sample_rates] = profile->sample_rates[num_sample_rates];
477*e01b6f76SAndroid Build Coastguard Worker }
478*e01b6f76SAndroid Build Coastguard Worker return num_sample_rates;
479*e01b6f76SAndroid Build Coastguard Worker }
480*e01b6f76SAndroid Build Coastguard Worker
are_all_devices_found(unsigned int num_devices_to_find,const int cards_to_find[],const int devices_to_find[],unsigned int num_devices,const int cards[],const int devices[])481*e01b6f76SAndroid Build Coastguard Worker static bool are_all_devices_found(unsigned int num_devices_to_find,
482*e01b6f76SAndroid Build Coastguard Worker const int cards_to_find[],
483*e01b6f76SAndroid Build Coastguard Worker const int devices_to_find[],
484*e01b6f76SAndroid Build Coastguard Worker unsigned int num_devices,
485*e01b6f76SAndroid Build Coastguard Worker const int cards[],
486*e01b6f76SAndroid Build Coastguard Worker const int devices[]) {
487*e01b6f76SAndroid Build Coastguard Worker for (unsigned int i = 0; i < num_devices_to_find; ++i) {
488*e01b6f76SAndroid Build Coastguard Worker unsigned int j = 0;
489*e01b6f76SAndroid Build Coastguard Worker for (; j < num_devices; ++j) {
490*e01b6f76SAndroid Build Coastguard Worker if (cards_to_find[i] == cards[j] && devices_to_find[i] == devices[j]) {
491*e01b6f76SAndroid Build Coastguard Worker break;
492*e01b6f76SAndroid Build Coastguard Worker }
493*e01b6f76SAndroid Build Coastguard Worker }
494*e01b6f76SAndroid Build Coastguard Worker if (j >= num_devices) {
495*e01b6f76SAndroid Build Coastguard Worker return false;
496*e01b6f76SAndroid Build Coastguard Worker }
497*e01b6f76SAndroid Build Coastguard Worker }
498*e01b6f76SAndroid Build Coastguard Worker return true;
499*e01b6f76SAndroid Build Coastguard Worker }
500*e01b6f76SAndroid Build Coastguard Worker
are_devices_the_same(unsigned int left_num_devices,const int left_cards[],const int left_devices[],unsigned int right_num_devices,const int right_cards[],const int right_devices[])501*e01b6f76SAndroid Build Coastguard Worker static bool are_devices_the_same(unsigned int left_num_devices,
502*e01b6f76SAndroid Build Coastguard Worker const int left_cards[],
503*e01b6f76SAndroid Build Coastguard Worker const int left_devices[],
504*e01b6f76SAndroid Build Coastguard Worker unsigned int right_num_devices,
505*e01b6f76SAndroid Build Coastguard Worker const int right_cards[],
506*e01b6f76SAndroid Build Coastguard Worker const int right_devices[]) {
507*e01b6f76SAndroid Build Coastguard Worker if (left_num_devices != right_num_devices) {
508*e01b6f76SAndroid Build Coastguard Worker return false;
509*e01b6f76SAndroid Build Coastguard Worker }
510*e01b6f76SAndroid Build Coastguard Worker return are_all_devices_found(left_num_devices, left_cards, left_devices,
511*e01b6f76SAndroid Build Coastguard Worker right_num_devices, right_cards, right_devices) &&
512*e01b6f76SAndroid Build Coastguard Worker are_all_devices_found(right_num_devices, right_cards, right_devices,
513*e01b6f76SAndroid Build Coastguard Worker left_num_devices, left_cards, left_devices);
514*e01b6f76SAndroid Build Coastguard Worker }
515*e01b6f76SAndroid Build Coastguard Worker
out_stream_find_mixer_volume_control(struct stream_out * out,int card)516*e01b6f76SAndroid Build Coastguard Worker static void out_stream_find_mixer_volume_control(struct stream_out* out, int card) {
517*e01b6f76SAndroid Build Coastguard Worker out->mixer = mixer_open(card);
518*e01b6f76SAndroid Build Coastguard Worker if (out->mixer == NULL) {
519*e01b6f76SAndroid Build Coastguard Worker ALOGI("%s, no mixer found for card=%d", __func__, card);
520*e01b6f76SAndroid Build Coastguard Worker return;
521*e01b6f76SAndroid Build Coastguard Worker }
522*e01b6f76SAndroid Build Coastguard Worker unsigned int num_ctls = mixer_get_num_ctls(out->mixer);
523*e01b6f76SAndroid Build Coastguard Worker for (int i = 0; i < VOLUME_CONTROL_NAMES_NUM; ++i) {
524*e01b6f76SAndroid Build Coastguard Worker for (unsigned int j = 0; j < num_ctls; ++j) {
525*e01b6f76SAndroid Build Coastguard Worker struct mixer_ctl *ctl = mixer_get_ctl(out->mixer, j);
526*e01b6f76SAndroid Build Coastguard Worker enum mixer_ctl_type ctl_type = mixer_ctl_get_type(ctl);
527*e01b6f76SAndroid Build Coastguard Worker if (strcasestr(mixer_ctl_get_name(ctl), ALL_VOLUME_CONTROL_NAMES[i]) == NULL ||
528*e01b6f76SAndroid Build Coastguard Worker ctl_type != MIXER_CTL_TYPE_INT) {
529*e01b6f76SAndroid Build Coastguard Worker continue;
530*e01b6f76SAndroid Build Coastguard Worker }
531*e01b6f76SAndroid Build Coastguard Worker ALOGD("%s, mixer volume control(%s) found", __func__, ALL_VOLUME_CONTROL_NAMES[i]);
532*e01b6f76SAndroid Build Coastguard Worker out->volume_ctl_num_values = mixer_ctl_get_num_values(ctl);
533*e01b6f76SAndroid Build Coastguard Worker if (out->volume_ctl_num_values <= 0) {
534*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s the num(%d) of volume ctl values is wrong",
535*e01b6f76SAndroid Build Coastguard Worker __func__, out->volume_ctl_num_values);
536*e01b6f76SAndroid Build Coastguard Worker out->volume_ctl_num_values = 0;
537*e01b6f76SAndroid Build Coastguard Worker continue;
538*e01b6f76SAndroid Build Coastguard Worker }
539*e01b6f76SAndroid Build Coastguard Worker out->max_volume_level = mixer_ctl_get_range_max(ctl);
540*e01b6f76SAndroid Build Coastguard Worker out->min_volume_level = mixer_ctl_get_range_min(ctl);
541*e01b6f76SAndroid Build Coastguard Worker if (out->max_volume_level < out->min_volume_level) {
542*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s the max volume level(%d) is less than min volume level(%d)",
543*e01b6f76SAndroid Build Coastguard Worker __func__, out->max_volume_level, out->min_volume_level);
544*e01b6f76SAndroid Build Coastguard Worker out->max_volume_level = 0;
545*e01b6f76SAndroid Build Coastguard Worker out->min_volume_level = 0;
546*e01b6f76SAndroid Build Coastguard Worker continue;
547*e01b6f76SAndroid Build Coastguard Worker }
548*e01b6f76SAndroid Build Coastguard Worker out->volume_ctl = ctl;
549*e01b6f76SAndroid Build Coastguard Worker return;
550*e01b6f76SAndroid Build Coastguard Worker }
551*e01b6f76SAndroid Build Coastguard Worker }
552*e01b6f76SAndroid Build Coastguard Worker ALOGI("%s, no volume control found", __func__);
553*e01b6f76SAndroid Build Coastguard Worker }
554*e01b6f76SAndroid Build Coastguard Worker
555*e01b6f76SAndroid Build Coastguard Worker /*
556*e01b6f76SAndroid Build Coastguard Worker * HAl Functions
557*e01b6f76SAndroid Build Coastguard Worker */
558*e01b6f76SAndroid Build Coastguard Worker /**
559*e01b6f76SAndroid Build Coastguard Worker * NOTE: when multiple mutexes have to be acquired, always respect the
560*e01b6f76SAndroid Build Coastguard Worker * following order: hw device > out stream
561*e01b6f76SAndroid Build Coastguard Worker */
562*e01b6f76SAndroid Build Coastguard Worker
stream_get_first_alsa_device(const struct listnode * alsa_devices)563*e01b6f76SAndroid Build Coastguard Worker static struct alsa_device_info* stream_get_first_alsa_device(const struct listnode *alsa_devices) {
564*e01b6f76SAndroid Build Coastguard Worker if (list_empty(alsa_devices)) {
565*e01b6f76SAndroid Build Coastguard Worker return NULL;
566*e01b6f76SAndroid Build Coastguard Worker }
567*e01b6f76SAndroid Build Coastguard Worker return node_to_item(list_head(alsa_devices), struct alsa_device_info, list_node);
568*e01b6f76SAndroid Build Coastguard Worker }
569*e01b6f76SAndroid Build Coastguard Worker
570*e01b6f76SAndroid Build Coastguard Worker /**
571*e01b6f76SAndroid Build Coastguard Worker * Must be called with holding the stream's lock.
572*e01b6f76SAndroid Build Coastguard Worker */
stream_standby_l(struct listnode * alsa_devices,bool * standby)573*e01b6f76SAndroid Build Coastguard Worker static void stream_standby_l(struct listnode *alsa_devices, bool *standby)
574*e01b6f76SAndroid Build Coastguard Worker {
575*e01b6f76SAndroid Build Coastguard Worker if (!*standby) {
576*e01b6f76SAndroid Build Coastguard Worker struct listnode *node;
577*e01b6f76SAndroid Build Coastguard Worker list_for_each (node, alsa_devices) {
578*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info =
579*e01b6f76SAndroid Build Coastguard Worker node_to_item(node, struct alsa_device_info, list_node);
580*e01b6f76SAndroid Build Coastguard Worker proxy_close(&device_info->proxy);
581*e01b6f76SAndroid Build Coastguard Worker }
582*e01b6f76SAndroid Build Coastguard Worker *standby = true;
583*e01b6f76SAndroid Build Coastguard Worker }
584*e01b6f76SAndroid Build Coastguard Worker }
585*e01b6f76SAndroid Build Coastguard Worker
stream_clear_devices(struct listnode * alsa_devices)586*e01b6f76SAndroid Build Coastguard Worker static void stream_clear_devices(struct listnode *alsa_devices)
587*e01b6f76SAndroid Build Coastguard Worker {
588*e01b6f76SAndroid Build Coastguard Worker struct listnode *node, *temp;
589*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = NULL;
590*e01b6f76SAndroid Build Coastguard Worker list_for_each_safe (node, temp, alsa_devices) {
591*e01b6f76SAndroid Build Coastguard Worker device_info = node_to_item(node, struct alsa_device_info, list_node);
592*e01b6f76SAndroid Build Coastguard Worker if (device_info != NULL) {
593*e01b6f76SAndroid Build Coastguard Worker list_remove(&device_info->list_node);
594*e01b6f76SAndroid Build Coastguard Worker free(device_info);
595*e01b6f76SAndroid Build Coastguard Worker }
596*e01b6f76SAndroid Build Coastguard Worker }
597*e01b6f76SAndroid Build Coastguard Worker }
598*e01b6f76SAndroid Build Coastguard Worker
stream_set_new_devices(struct pcm_config * config,struct listnode * alsa_devices,unsigned int num_devices,const int cards[],const int devices[],int direction,bool is_bit_perfect)599*e01b6f76SAndroid Build Coastguard Worker static int stream_set_new_devices(struct pcm_config *config,
600*e01b6f76SAndroid Build Coastguard Worker struct listnode *alsa_devices,
601*e01b6f76SAndroid Build Coastguard Worker unsigned int num_devices,
602*e01b6f76SAndroid Build Coastguard Worker const int cards[],
603*e01b6f76SAndroid Build Coastguard Worker const int devices[],
604*e01b6f76SAndroid Build Coastguard Worker int direction,
605*e01b6f76SAndroid Build Coastguard Worker bool is_bit_perfect)
606*e01b6f76SAndroid Build Coastguard Worker {
607*e01b6f76SAndroid Build Coastguard Worker int status = 0;
608*e01b6f76SAndroid Build Coastguard Worker stream_clear_devices(alsa_devices);
609*e01b6f76SAndroid Build Coastguard Worker
610*e01b6f76SAndroid Build Coastguard Worker for (unsigned int i = 0; i < num_devices; ++i) {
611*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info =
612*e01b6f76SAndroid Build Coastguard Worker (struct alsa_device_info *) calloc(1, sizeof(struct alsa_device_info));
613*e01b6f76SAndroid Build Coastguard Worker profile_init(&device_info->profile, direction);
614*e01b6f76SAndroid Build Coastguard Worker device_info->profile.card = cards[i];
615*e01b6f76SAndroid Build Coastguard Worker device_info->profile.device = devices[i];
616*e01b6f76SAndroid Build Coastguard Worker status = profile_read_device_info(&device_info->profile) ? 0 : -EINVAL;
617*e01b6f76SAndroid Build Coastguard Worker if (status != 0) {
618*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s failed to read device info card=%d;device=%d",
619*e01b6f76SAndroid Build Coastguard Worker __func__, cards[i], devices[i]);
620*e01b6f76SAndroid Build Coastguard Worker goto exit;
621*e01b6f76SAndroid Build Coastguard Worker }
622*e01b6f76SAndroid Build Coastguard Worker status = proxy_prepare(&device_info->proxy, &device_info->profile, config, is_bit_perfect);
623*e01b6f76SAndroid Build Coastguard Worker if (status != 0) {
624*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s failed to prepare device card=%d;device=%d",
625*e01b6f76SAndroid Build Coastguard Worker __func__, cards[i], devices[i]);
626*e01b6f76SAndroid Build Coastguard Worker goto exit;
627*e01b6f76SAndroid Build Coastguard Worker }
628*e01b6f76SAndroid Build Coastguard Worker list_add_tail(alsa_devices, &device_info->list_node);
629*e01b6f76SAndroid Build Coastguard Worker }
630*e01b6f76SAndroid Build Coastguard Worker
631*e01b6f76SAndroid Build Coastguard Worker exit:
632*e01b6f76SAndroid Build Coastguard Worker if (status != 0) {
633*e01b6f76SAndroid Build Coastguard Worker stream_clear_devices(alsa_devices);
634*e01b6f76SAndroid Build Coastguard Worker }
635*e01b6f76SAndroid Build Coastguard Worker return status;
636*e01b6f76SAndroid Build Coastguard Worker }
637*e01b6f76SAndroid Build Coastguard Worker
stream_dump_alsa_devices(const struct listnode * alsa_devices,int fd)638*e01b6f76SAndroid Build Coastguard Worker static void stream_dump_alsa_devices(const struct listnode *alsa_devices, int fd) {
639*e01b6f76SAndroid Build Coastguard Worker struct listnode *node;
640*e01b6f76SAndroid Build Coastguard Worker size_t i = 0;
641*e01b6f76SAndroid Build Coastguard Worker list_for_each(node, alsa_devices) {
642*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info =
643*e01b6f76SAndroid Build Coastguard Worker node_to_item(node, struct alsa_device_info, list_node);
644*e01b6f76SAndroid Build Coastguard Worker const char* direction = device_info->profile.direction == PCM_OUT ? "Output" : "Input";
645*e01b6f76SAndroid Build Coastguard Worker dprintf(fd, "%s Profile %zu:\n", direction, i);
646*e01b6f76SAndroid Build Coastguard Worker profile_dump(&device_info->profile, fd);
647*e01b6f76SAndroid Build Coastguard Worker
648*e01b6f76SAndroid Build Coastguard Worker dprintf(fd, "%s Proxy %zu:\n", direction, i);
649*e01b6f76SAndroid Build Coastguard Worker proxy_dump(&device_info->proxy, fd);
650*e01b6f76SAndroid Build Coastguard Worker }
651*e01b6f76SAndroid Build Coastguard Worker }
652*e01b6f76SAndroid Build Coastguard Worker
653*e01b6f76SAndroid Build Coastguard Worker /*
654*e01b6f76SAndroid Build Coastguard Worker * OUT functions
655*e01b6f76SAndroid Build Coastguard Worker */
out_get_sample_rate(const struct audio_stream * stream)656*e01b6f76SAndroid Build Coastguard Worker static uint32_t out_get_sample_rate(const struct audio_stream *stream)
657*e01b6f76SAndroid Build Coastguard Worker {
658*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(
659*e01b6f76SAndroid Build Coastguard Worker &((struct stream_out*)stream)->alsa_devices);
660*e01b6f76SAndroid Build Coastguard Worker if (device_info == NULL) {
661*e01b6f76SAndroid Build Coastguard Worker ALOGW("%s device info is null", __func__);
662*e01b6f76SAndroid Build Coastguard Worker return 0;
663*e01b6f76SAndroid Build Coastguard Worker }
664*e01b6f76SAndroid Build Coastguard Worker uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
665*e01b6f76SAndroid Build Coastguard Worker ALOGV("out_get_sample_rate() = %d", rate);
666*e01b6f76SAndroid Build Coastguard Worker return rate;
667*e01b6f76SAndroid Build Coastguard Worker }
668*e01b6f76SAndroid Build Coastguard Worker
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)669*e01b6f76SAndroid Build Coastguard Worker static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
670*e01b6f76SAndroid Build Coastguard Worker {
671*e01b6f76SAndroid Build Coastguard Worker return 0;
672*e01b6f76SAndroid Build Coastguard Worker }
673*e01b6f76SAndroid Build Coastguard Worker
out_get_buffer_size(const struct audio_stream * stream)674*e01b6f76SAndroid Build Coastguard Worker static size_t out_get_buffer_size(const struct audio_stream *stream)
675*e01b6f76SAndroid Build Coastguard Worker {
676*e01b6f76SAndroid Build Coastguard Worker const struct stream_out* out = (const struct stream_out*)stream;
677*e01b6f76SAndroid Build Coastguard Worker const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
678*e01b6f76SAndroid Build Coastguard Worker if (device_info == NULL) {
679*e01b6f76SAndroid Build Coastguard Worker ALOGW("%s device info is null", __func__);
680*e01b6f76SAndroid Build Coastguard Worker return 0;
681*e01b6f76SAndroid Build Coastguard Worker }
682*e01b6f76SAndroid Build Coastguard Worker return proxy_get_period_size(&device_info->proxy) * audio_stream_out_frame_size(&(out->stream));
683*e01b6f76SAndroid Build Coastguard Worker }
684*e01b6f76SAndroid Build Coastguard Worker
out_get_channels(const struct audio_stream * stream)685*e01b6f76SAndroid Build Coastguard Worker static uint32_t out_get_channels(const struct audio_stream *stream)
686*e01b6f76SAndroid Build Coastguard Worker {
687*e01b6f76SAndroid Build Coastguard Worker const struct stream_out *out = (const struct stream_out*)stream;
688*e01b6f76SAndroid Build Coastguard Worker return out->hal_channel_mask;
689*e01b6f76SAndroid Build Coastguard Worker }
690*e01b6f76SAndroid Build Coastguard Worker
out_get_format(const struct audio_stream * stream)691*e01b6f76SAndroid Build Coastguard Worker static audio_format_t out_get_format(const struct audio_stream *stream)
692*e01b6f76SAndroid Build Coastguard Worker {
693*e01b6f76SAndroid Build Coastguard Worker /* Note: The HAL doesn't do any FORMAT conversion at this time. It
694*e01b6f76SAndroid Build Coastguard Worker * Relies on the framework to provide data in the specified format.
695*e01b6f76SAndroid Build Coastguard Worker * This could change in the future.
696*e01b6f76SAndroid Build Coastguard Worker */
697*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(
698*e01b6f76SAndroid Build Coastguard Worker &((struct stream_out*)stream)->alsa_devices);
699*e01b6f76SAndroid Build Coastguard Worker if (device_info == NULL) {
700*e01b6f76SAndroid Build Coastguard Worker ALOGW("%s device info is null", __func__);
701*e01b6f76SAndroid Build Coastguard Worker return AUDIO_FORMAT_DEFAULT;
702*e01b6f76SAndroid Build Coastguard Worker }
703*e01b6f76SAndroid Build Coastguard Worker audio_format_t format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
704*e01b6f76SAndroid Build Coastguard Worker return format;
705*e01b6f76SAndroid Build Coastguard Worker }
706*e01b6f76SAndroid Build Coastguard Worker
out_set_format(struct audio_stream * stream,audio_format_t format)707*e01b6f76SAndroid Build Coastguard Worker static int out_set_format(struct audio_stream *stream, audio_format_t format)
708*e01b6f76SAndroid Build Coastguard Worker {
709*e01b6f76SAndroid Build Coastguard Worker return 0;
710*e01b6f76SAndroid Build Coastguard Worker }
711*e01b6f76SAndroid Build Coastguard Worker
out_standby(struct audio_stream * stream)712*e01b6f76SAndroid Build Coastguard Worker static int out_standby(struct audio_stream *stream)
713*e01b6f76SAndroid Build Coastguard Worker {
714*e01b6f76SAndroid Build Coastguard Worker struct stream_out *out = (struct stream_out *)stream;
715*e01b6f76SAndroid Build Coastguard Worker
716*e01b6f76SAndroid Build Coastguard Worker stream_lock(&out->lock);
717*e01b6f76SAndroid Build Coastguard Worker device_lock(out->adev);
718*e01b6f76SAndroid Build Coastguard Worker stream_standby_l(&out->alsa_devices, &out->standby);
719*e01b6f76SAndroid Build Coastguard Worker device_unlock(out->adev);
720*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&out->lock);
721*e01b6f76SAndroid Build Coastguard Worker return 0;
722*e01b6f76SAndroid Build Coastguard Worker }
723*e01b6f76SAndroid Build Coastguard Worker
out_dump(const struct audio_stream * stream,int fd)724*e01b6f76SAndroid Build Coastguard Worker static int out_dump(const struct audio_stream *stream, int fd) {
725*e01b6f76SAndroid Build Coastguard Worker const struct stream_out* out_stream = (const struct stream_out*) stream;
726*e01b6f76SAndroid Build Coastguard Worker
727*e01b6f76SAndroid Build Coastguard Worker if (out_stream != NULL) {
728*e01b6f76SAndroid Build Coastguard Worker stream_dump_alsa_devices(&out_stream->alsa_devices, fd);
729*e01b6f76SAndroid Build Coastguard Worker }
730*e01b6f76SAndroid Build Coastguard Worker
731*e01b6f76SAndroid Build Coastguard Worker return 0;
732*e01b6f76SAndroid Build Coastguard Worker }
733*e01b6f76SAndroid Build Coastguard Worker
out_set_parameters(struct audio_stream * stream __unused,const char * kvpairs)734*e01b6f76SAndroid Build Coastguard Worker static int out_set_parameters(struct audio_stream *stream __unused, const char *kvpairs)
735*e01b6f76SAndroid Build Coastguard Worker {
736*e01b6f76SAndroid Build Coastguard Worker ALOGV("out_set_parameters() keys:%s", kvpairs);
737*e01b6f76SAndroid Build Coastguard Worker
738*e01b6f76SAndroid Build Coastguard Worker // The set parameters here only matters when the routing devices are changed.
739*e01b6f76SAndroid Build Coastguard Worker // When the device version is not less than 3.0, the framework will use create
740*e01b6f76SAndroid Build Coastguard Worker // audio patch API instead of set parameters to chanage audio routing.
741*e01b6f76SAndroid Build Coastguard Worker return 0;
742*e01b6f76SAndroid Build Coastguard Worker }
743*e01b6f76SAndroid Build Coastguard Worker
out_get_parameters(const struct audio_stream * stream,const char * keys)744*e01b6f76SAndroid Build Coastguard Worker static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
745*e01b6f76SAndroid Build Coastguard Worker {
746*e01b6f76SAndroid Build Coastguard Worker struct stream_out *out = (struct stream_out *)stream;
747*e01b6f76SAndroid Build Coastguard Worker stream_lock(&out->lock);
748*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(&out->alsa_devices);
749*e01b6f76SAndroid Build Coastguard Worker char *params_str = NULL;
750*e01b6f76SAndroid Build Coastguard Worker if (device_info != NULL) {
751*e01b6f76SAndroid Build Coastguard Worker params_str = device_get_parameters(&device_info->profile, keys);
752*e01b6f76SAndroid Build Coastguard Worker }
753*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&out->lock);
754*e01b6f76SAndroid Build Coastguard Worker return params_str;
755*e01b6f76SAndroid Build Coastguard Worker }
756*e01b6f76SAndroid Build Coastguard Worker
out_get_latency(const struct audio_stream_out * stream)757*e01b6f76SAndroid Build Coastguard Worker static uint32_t out_get_latency(const struct audio_stream_out *stream)
758*e01b6f76SAndroid Build Coastguard Worker {
759*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(
760*e01b6f76SAndroid Build Coastguard Worker &((struct stream_out*)stream)->alsa_devices);
761*e01b6f76SAndroid Build Coastguard Worker if (device_info == NULL) {
762*e01b6f76SAndroid Build Coastguard Worker ALOGW("%s device info is null", __func__);
763*e01b6f76SAndroid Build Coastguard Worker return 0;
764*e01b6f76SAndroid Build Coastguard Worker }
765*e01b6f76SAndroid Build Coastguard Worker return proxy_get_latency(&device_info->proxy);
766*e01b6f76SAndroid Build Coastguard Worker }
767*e01b6f76SAndroid Build Coastguard Worker
out_set_volume(struct audio_stream_out * stream,float left,float right)768*e01b6f76SAndroid Build Coastguard Worker static int out_set_volume(struct audio_stream_out *stream, float left, float right)
769*e01b6f76SAndroid Build Coastguard Worker {
770*e01b6f76SAndroid Build Coastguard Worker struct stream_out *out = (struct stream_out *)stream;
771*e01b6f76SAndroid Build Coastguard Worker int result = -ENOSYS;
772*e01b6f76SAndroid Build Coastguard Worker stream_lock(&out->lock);
773*e01b6f76SAndroid Build Coastguard Worker if (out->volume_ctl != NULL) {
774*e01b6f76SAndroid Build Coastguard Worker int left_volume =
775*e01b6f76SAndroid Build Coastguard Worker out->min_volume_level + ceil((out->max_volume_level - out->min_volume_level) * left);
776*e01b6f76SAndroid Build Coastguard Worker int right_volume =
777*e01b6f76SAndroid Build Coastguard Worker out->min_volume_level + ceil((out->max_volume_level - out->min_volume_level) * right);
778*e01b6f76SAndroid Build Coastguard Worker int volumes[out->volume_ctl_num_values];
779*e01b6f76SAndroid Build Coastguard Worker if (out->volume_ctl_num_values == 1) {
780*e01b6f76SAndroid Build Coastguard Worker volumes[0] = left_volume;
781*e01b6f76SAndroid Build Coastguard Worker } else {
782*e01b6f76SAndroid Build Coastguard Worker volumes[0] = left_volume;
783*e01b6f76SAndroid Build Coastguard Worker volumes[1] = right_volume;
784*e01b6f76SAndroid Build Coastguard Worker for (int i = 2; i < out->volume_ctl_num_values; ++i) {
785*e01b6f76SAndroid Build Coastguard Worker volumes[i] = left_volume;
786*e01b6f76SAndroid Build Coastguard Worker }
787*e01b6f76SAndroid Build Coastguard Worker }
788*e01b6f76SAndroid Build Coastguard Worker result = mixer_ctl_set_array(out->volume_ctl, volumes, out->volume_ctl_num_values);
789*e01b6f76SAndroid Build Coastguard Worker if (result != 0) {
790*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s error=%d left=%f right=%f", __func__, result, left, right);
791*e01b6f76SAndroid Build Coastguard Worker }
792*e01b6f76SAndroid Build Coastguard Worker }
793*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&out->lock);
794*e01b6f76SAndroid Build Coastguard Worker return result;
795*e01b6f76SAndroid Build Coastguard Worker }
796*e01b6f76SAndroid Build Coastguard Worker
797*e01b6f76SAndroid Build Coastguard Worker /* must be called with hw device and output stream mutexes locked */
start_output_stream(struct stream_out * out)798*e01b6f76SAndroid Build Coastguard Worker static int start_output_stream(struct stream_out *out)
799*e01b6f76SAndroid Build Coastguard Worker {
800*e01b6f76SAndroid Build Coastguard Worker int status = 0;
801*e01b6f76SAndroid Build Coastguard Worker struct listnode *node;
802*e01b6f76SAndroid Build Coastguard Worker list_for_each(node, &out->alsa_devices) {
803*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info =
804*e01b6f76SAndroid Build Coastguard Worker node_to_item(node, struct alsa_device_info, list_node);
805*e01b6f76SAndroid Build Coastguard Worker ALOGV("start_output_stream(card:%d device:%d)",
806*e01b6f76SAndroid Build Coastguard Worker device_info->profile.card, device_info->profile.device);
807*e01b6f76SAndroid Build Coastguard Worker status = proxy_open(&device_info->proxy);
808*e01b6f76SAndroid Build Coastguard Worker if (status != 0) {
809*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s failed to open device(card: %d device: %d)",
810*e01b6f76SAndroid Build Coastguard Worker __func__, device_info->profile.card, device_info->profile.device);
811*e01b6f76SAndroid Build Coastguard Worker goto exit;
812*e01b6f76SAndroid Build Coastguard Worker } else {
813*e01b6f76SAndroid Build Coastguard Worker out->standby = false;
814*e01b6f76SAndroid Build Coastguard Worker }
815*e01b6f76SAndroid Build Coastguard Worker }
816*e01b6f76SAndroid Build Coastguard Worker
817*e01b6f76SAndroid Build Coastguard Worker exit:
818*e01b6f76SAndroid Build Coastguard Worker if (status != 0) {
819*e01b6f76SAndroid Build Coastguard Worker list_for_each(node, &out->alsa_devices) {
820*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info =
821*e01b6f76SAndroid Build Coastguard Worker node_to_item(node, struct alsa_device_info, list_node);
822*e01b6f76SAndroid Build Coastguard Worker proxy_close(&device_info->proxy);
823*e01b6f76SAndroid Build Coastguard Worker }
824*e01b6f76SAndroid Build Coastguard Worker
825*e01b6f76SAndroid Build Coastguard Worker }
826*e01b6f76SAndroid Build Coastguard Worker return status;
827*e01b6f76SAndroid Build Coastguard Worker }
828*e01b6f76SAndroid Build Coastguard Worker
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)829*e01b6f76SAndroid Build Coastguard Worker static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t bytes)
830*e01b6f76SAndroid Build Coastguard Worker {
831*e01b6f76SAndroid Build Coastguard Worker int ret;
832*e01b6f76SAndroid Build Coastguard Worker struct stream_out *out = (struct stream_out *)stream;
833*e01b6f76SAndroid Build Coastguard Worker
834*e01b6f76SAndroid Build Coastguard Worker stream_lock(&out->lock);
835*e01b6f76SAndroid Build Coastguard Worker if (out->standby) {
836*e01b6f76SAndroid Build Coastguard Worker ret = start_output_stream(out);
837*e01b6f76SAndroid Build Coastguard Worker if (ret != 0) {
838*e01b6f76SAndroid Build Coastguard Worker goto err;
839*e01b6f76SAndroid Build Coastguard Worker }
840*e01b6f76SAndroid Build Coastguard Worker }
841*e01b6f76SAndroid Build Coastguard Worker
842*e01b6f76SAndroid Build Coastguard Worker struct listnode* node;
843*e01b6f76SAndroid Build Coastguard Worker list_for_each(node, &out->alsa_devices) {
844*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info* device_info =
845*e01b6f76SAndroid Build Coastguard Worker node_to_item(node, struct alsa_device_info, list_node);
846*e01b6f76SAndroid Build Coastguard Worker alsa_device_proxy* proxy = &device_info->proxy;
847*e01b6f76SAndroid Build Coastguard Worker const void * write_buff = buffer;
848*e01b6f76SAndroid Build Coastguard Worker int num_write_buff_bytes = bytes;
849*e01b6f76SAndroid Build Coastguard Worker const int num_device_channels = proxy_get_channel_count(proxy); /* what we told alsa */
850*e01b6f76SAndroid Build Coastguard Worker const int num_req_channels = out->hal_channel_count; /* what we told AudioFlinger */
851*e01b6f76SAndroid Build Coastguard Worker if (num_device_channels != num_req_channels) {
852*e01b6f76SAndroid Build Coastguard Worker /* allocate buffer */
853*e01b6f76SAndroid Build Coastguard Worker const size_t required_conversion_buffer_size =
854*e01b6f76SAndroid Build Coastguard Worker bytes * num_device_channels / num_req_channels;
855*e01b6f76SAndroid Build Coastguard Worker if (required_conversion_buffer_size > out->conversion_buffer_size) {
856*e01b6f76SAndroid Build Coastguard Worker out->conversion_buffer_size = required_conversion_buffer_size;
857*e01b6f76SAndroid Build Coastguard Worker out->conversion_buffer = realloc(out->conversion_buffer,
858*e01b6f76SAndroid Build Coastguard Worker out->conversion_buffer_size);
859*e01b6f76SAndroid Build Coastguard Worker }
860*e01b6f76SAndroid Build Coastguard Worker /* convert data */
861*e01b6f76SAndroid Build Coastguard Worker const audio_format_t audio_format = out_get_format(&(out->stream.common));
862*e01b6f76SAndroid Build Coastguard Worker const unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
863*e01b6f76SAndroid Build Coastguard Worker num_write_buff_bytes =
864*e01b6f76SAndroid Build Coastguard Worker adjust_channels(write_buff, num_req_channels,
865*e01b6f76SAndroid Build Coastguard Worker out->conversion_buffer, num_device_channels,
866*e01b6f76SAndroid Build Coastguard Worker sample_size_in_bytes, num_write_buff_bytes);
867*e01b6f76SAndroid Build Coastguard Worker write_buff = out->conversion_buffer;
868*e01b6f76SAndroid Build Coastguard Worker }
869*e01b6f76SAndroid Build Coastguard Worker
870*e01b6f76SAndroid Build Coastguard Worker if (write_buff != NULL && num_write_buff_bytes != 0) {
871*e01b6f76SAndroid Build Coastguard Worker proxy_write(proxy, write_buff, num_write_buff_bytes);
872*e01b6f76SAndroid Build Coastguard Worker }
873*e01b6f76SAndroid Build Coastguard Worker }
874*e01b6f76SAndroid Build Coastguard Worker
875*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&out->lock);
876*e01b6f76SAndroid Build Coastguard Worker
877*e01b6f76SAndroid Build Coastguard Worker return bytes;
878*e01b6f76SAndroid Build Coastguard Worker
879*e01b6f76SAndroid Build Coastguard Worker err:
880*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&out->lock);
881*e01b6f76SAndroid Build Coastguard Worker if (ret != 0) {
882*e01b6f76SAndroid Build Coastguard Worker usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
883*e01b6f76SAndroid Build Coastguard Worker out_get_sample_rate(&stream->common));
884*e01b6f76SAndroid Build Coastguard Worker }
885*e01b6f76SAndroid Build Coastguard Worker
886*e01b6f76SAndroid Build Coastguard Worker return bytes;
887*e01b6f76SAndroid Build Coastguard Worker }
888*e01b6f76SAndroid Build Coastguard Worker
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)889*e01b6f76SAndroid Build Coastguard Worker static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames)
890*e01b6f76SAndroid Build Coastguard Worker {
891*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
892*e01b6f76SAndroid Build Coastguard Worker }
893*e01b6f76SAndroid Build Coastguard Worker
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)894*e01b6f76SAndroid Build Coastguard Worker static int out_get_presentation_position(const struct audio_stream_out *stream,
895*e01b6f76SAndroid Build Coastguard Worker uint64_t *frames, struct timespec *timestamp)
896*e01b6f76SAndroid Build Coastguard Worker {
897*e01b6f76SAndroid Build Coastguard Worker struct stream_out *out = (struct stream_out *)stream; // discard const qualifier
898*e01b6f76SAndroid Build Coastguard Worker stream_lock(&out->lock);
899*e01b6f76SAndroid Build Coastguard Worker
900*e01b6f76SAndroid Build Coastguard Worker const struct alsa_device_info* device_info = stream_get_first_alsa_device(&out->alsa_devices);
901*e01b6f76SAndroid Build Coastguard Worker const int ret = device_info == NULL ? -ENODEV :
902*e01b6f76SAndroid Build Coastguard Worker proxy_get_presentation_position(&device_info->proxy, frames, timestamp);
903*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&out->lock);
904*e01b6f76SAndroid Build Coastguard Worker return ret;
905*e01b6f76SAndroid Build Coastguard Worker }
906*e01b6f76SAndroid Build Coastguard Worker
out_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)907*e01b6f76SAndroid Build Coastguard Worker static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
908*e01b6f76SAndroid Build Coastguard Worker {
909*e01b6f76SAndroid Build Coastguard Worker return 0;
910*e01b6f76SAndroid Build Coastguard Worker }
911*e01b6f76SAndroid Build Coastguard Worker
out_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)912*e01b6f76SAndroid Build Coastguard Worker static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
913*e01b6f76SAndroid Build Coastguard Worker {
914*e01b6f76SAndroid Build Coastguard Worker return 0;
915*e01b6f76SAndroid Build Coastguard Worker }
916*e01b6f76SAndroid Build Coastguard Worker
out_get_next_write_timestamp(const struct audio_stream_out * stream,int64_t * timestamp)917*e01b6f76SAndroid Build Coastguard Worker static int out_get_next_write_timestamp(const struct audio_stream_out *stream, int64_t *timestamp)
918*e01b6f76SAndroid Build Coastguard Worker {
919*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
920*e01b6f76SAndroid Build Coastguard Worker }
921*e01b6f76SAndroid Build Coastguard Worker
adev_open_output_stream(struct audio_hw_device * hw_dev,audio_io_handle_t handle,audio_devices_t devicesSpec __unused,audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out,const char * address)922*e01b6f76SAndroid Build Coastguard Worker static int adev_open_output_stream(struct audio_hw_device *hw_dev,
923*e01b6f76SAndroid Build Coastguard Worker audio_io_handle_t handle,
924*e01b6f76SAndroid Build Coastguard Worker audio_devices_t devicesSpec __unused,
925*e01b6f76SAndroid Build Coastguard Worker audio_output_flags_t flags,
926*e01b6f76SAndroid Build Coastguard Worker struct audio_config *config,
927*e01b6f76SAndroid Build Coastguard Worker struct audio_stream_out **stream_out,
928*e01b6f76SAndroid Build Coastguard Worker const char *address /*__unused*/)
929*e01b6f76SAndroid Build Coastguard Worker {
930*e01b6f76SAndroid Build Coastguard Worker ALOGV("adev_open_output_stream() handle:0x%X, devicesSpec:0x%X, flags:0x%X, addr:%s",
931*e01b6f76SAndroid Build Coastguard Worker handle, devicesSpec, flags, address);
932*e01b6f76SAndroid Build Coastguard Worker
933*e01b6f76SAndroid Build Coastguard Worker const bool is_bit_perfect = ((flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE);
934*e01b6f76SAndroid Build Coastguard Worker if (is_bit_perfect && (config->format == AUDIO_FORMAT_DEFAULT ||
935*e01b6f76SAndroid Build Coastguard Worker config->sample_rate == 0 ||
936*e01b6f76SAndroid Build Coastguard Worker config->channel_mask == AUDIO_CHANNEL_NONE)) {
937*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s request bit perfect playback, config(format=%#x, sample_rate=%u, "
938*e01b6f76SAndroid Build Coastguard Worker "channel_mask=%#x) must be specified", __func__, config->format,
939*e01b6f76SAndroid Build Coastguard Worker config->sample_rate, config->channel_mask);
940*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
941*e01b6f76SAndroid Build Coastguard Worker }
942*e01b6f76SAndroid Build Coastguard Worker
943*e01b6f76SAndroid Build Coastguard Worker struct stream_out *out;
944*e01b6f76SAndroid Build Coastguard Worker
945*e01b6f76SAndroid Build Coastguard Worker out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
946*e01b6f76SAndroid Build Coastguard Worker if (out == NULL) {
947*e01b6f76SAndroid Build Coastguard Worker return -ENOMEM;
948*e01b6f76SAndroid Build Coastguard Worker }
949*e01b6f76SAndroid Build Coastguard Worker
950*e01b6f76SAndroid Build Coastguard Worker /* setup function pointers */
951*e01b6f76SAndroid Build Coastguard Worker out->stream.common.get_sample_rate = out_get_sample_rate;
952*e01b6f76SAndroid Build Coastguard Worker out->stream.common.set_sample_rate = out_set_sample_rate;
953*e01b6f76SAndroid Build Coastguard Worker out->stream.common.get_buffer_size = out_get_buffer_size;
954*e01b6f76SAndroid Build Coastguard Worker out->stream.common.get_channels = out_get_channels;
955*e01b6f76SAndroid Build Coastguard Worker out->stream.common.get_format = out_get_format;
956*e01b6f76SAndroid Build Coastguard Worker out->stream.common.set_format = out_set_format;
957*e01b6f76SAndroid Build Coastguard Worker out->stream.common.standby = out_standby;
958*e01b6f76SAndroid Build Coastguard Worker out->stream.common.dump = out_dump;
959*e01b6f76SAndroid Build Coastguard Worker out->stream.common.set_parameters = out_set_parameters;
960*e01b6f76SAndroid Build Coastguard Worker out->stream.common.get_parameters = out_get_parameters;
961*e01b6f76SAndroid Build Coastguard Worker out->stream.common.add_audio_effect = out_add_audio_effect;
962*e01b6f76SAndroid Build Coastguard Worker out->stream.common.remove_audio_effect = out_remove_audio_effect;
963*e01b6f76SAndroid Build Coastguard Worker out->stream.get_latency = out_get_latency;
964*e01b6f76SAndroid Build Coastguard Worker out->stream.set_volume = out_set_volume;
965*e01b6f76SAndroid Build Coastguard Worker out->stream.write = out_write;
966*e01b6f76SAndroid Build Coastguard Worker out->stream.get_render_position = out_get_render_position;
967*e01b6f76SAndroid Build Coastguard Worker out->stream.get_presentation_position = out_get_presentation_position;
968*e01b6f76SAndroid Build Coastguard Worker out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
969*e01b6f76SAndroid Build Coastguard Worker
970*e01b6f76SAndroid Build Coastguard Worker out->handle = handle;
971*e01b6f76SAndroid Build Coastguard Worker
972*e01b6f76SAndroid Build Coastguard Worker stream_lock_init(&out->lock);
973*e01b6f76SAndroid Build Coastguard Worker
974*e01b6f76SAndroid Build Coastguard Worker out->adev = (struct audio_device *)hw_dev;
975*e01b6f76SAndroid Build Coastguard Worker
976*e01b6f76SAndroid Build Coastguard Worker list_init(&out->alsa_devices);
977*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info =
978*e01b6f76SAndroid Build Coastguard Worker (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
979*e01b6f76SAndroid Build Coastguard Worker profile_init(&device_info->profile, PCM_OUT);
980*e01b6f76SAndroid Build Coastguard Worker
981*e01b6f76SAndroid Build Coastguard Worker // build this to hand to the alsa_device_proxy
982*e01b6f76SAndroid Build Coastguard Worker struct pcm_config proxy_config = {};
983*e01b6f76SAndroid Build Coastguard Worker
984*e01b6f76SAndroid Build Coastguard Worker /* Pull out the card/device pair */
985*e01b6f76SAndroid Build Coastguard Worker parse_card_device_params(address, &device_info->profile.card, &device_info->profile.device);
986*e01b6f76SAndroid Build Coastguard Worker
987*e01b6f76SAndroid Build Coastguard Worker profile_read_device_info(&device_info->profile);
988*e01b6f76SAndroid Build Coastguard Worker
989*e01b6f76SAndroid Build Coastguard Worker int ret = 0;
990*e01b6f76SAndroid Build Coastguard Worker
991*e01b6f76SAndroid Build Coastguard Worker /* Rate */
992*e01b6f76SAndroid Build Coastguard Worker if (config->sample_rate == 0) {
993*e01b6f76SAndroid Build Coastguard Worker proxy_config.rate = profile_get_default_sample_rate(&device_info->profile);
994*e01b6f76SAndroid Build Coastguard Worker } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
995*e01b6f76SAndroid Build Coastguard Worker proxy_config.rate = config->sample_rate;
996*e01b6f76SAndroid Build Coastguard Worker } else {
997*e01b6f76SAndroid Build Coastguard Worker ret = -EINVAL;
998*e01b6f76SAndroid Build Coastguard Worker if (is_bit_perfect) {
999*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s requesting bit-perfect but the sample rate(%u) is not valid",
1000*e01b6f76SAndroid Build Coastguard Worker __func__, config->sample_rate);
1001*e01b6f76SAndroid Build Coastguard Worker return ret;
1002*e01b6f76SAndroid Build Coastguard Worker }
1003*e01b6f76SAndroid Build Coastguard Worker proxy_config.rate = config->sample_rate =
1004*e01b6f76SAndroid Build Coastguard Worker profile_get_default_sample_rate(&device_info->profile);
1005*e01b6f76SAndroid Build Coastguard Worker }
1006*e01b6f76SAndroid Build Coastguard Worker
1007*e01b6f76SAndroid Build Coastguard Worker /* TODO: This is a problem if the input does not support this rate */
1008*e01b6f76SAndroid Build Coastguard Worker device_lock(out->adev);
1009*e01b6f76SAndroid Build Coastguard Worker out->adev->device_sample_rate = config->sample_rate;
1010*e01b6f76SAndroid Build Coastguard Worker device_unlock(out->adev);
1011*e01b6f76SAndroid Build Coastguard Worker
1012*e01b6f76SAndroid Build Coastguard Worker /* Format */
1013*e01b6f76SAndroid Build Coastguard Worker if (config->format == AUDIO_FORMAT_DEFAULT) {
1014*e01b6f76SAndroid Build Coastguard Worker proxy_config.format = profile_get_default_format(&device_info->profile);
1015*e01b6f76SAndroid Build Coastguard Worker config->format = audio_format_from_pcm_format(proxy_config.format);
1016*e01b6f76SAndroid Build Coastguard Worker } else {
1017*e01b6f76SAndroid Build Coastguard Worker enum pcm_format fmt = pcm_format_from_audio_format(config->format);
1018*e01b6f76SAndroid Build Coastguard Worker if (profile_is_format_valid(&device_info->profile, fmt)) {
1019*e01b6f76SAndroid Build Coastguard Worker proxy_config.format = fmt;
1020*e01b6f76SAndroid Build Coastguard Worker } else {
1021*e01b6f76SAndroid Build Coastguard Worker ret = -EINVAL;
1022*e01b6f76SAndroid Build Coastguard Worker if (is_bit_perfect) {
1023*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s request bit-perfect but the format(%#x) is not valid",
1024*e01b6f76SAndroid Build Coastguard Worker __func__, config->format);
1025*e01b6f76SAndroid Build Coastguard Worker return ret;
1026*e01b6f76SAndroid Build Coastguard Worker }
1027*e01b6f76SAndroid Build Coastguard Worker proxy_config.format = profile_get_default_format(&device_info->profile);
1028*e01b6f76SAndroid Build Coastguard Worker config->format = audio_format_from_pcm_format(proxy_config.format);
1029*e01b6f76SAndroid Build Coastguard Worker }
1030*e01b6f76SAndroid Build Coastguard Worker }
1031*e01b6f76SAndroid Build Coastguard Worker
1032*e01b6f76SAndroid Build Coastguard Worker /* Channels */
1033*e01b6f76SAndroid Build Coastguard Worker bool calc_mask = false;
1034*e01b6f76SAndroid Build Coastguard Worker if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1035*e01b6f76SAndroid Build Coastguard Worker /* query case */
1036*e01b6f76SAndroid Build Coastguard Worker out->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
1037*e01b6f76SAndroid Build Coastguard Worker calc_mask = true;
1038*e01b6f76SAndroid Build Coastguard Worker } else {
1039*e01b6f76SAndroid Build Coastguard Worker /* explicit case */
1040*e01b6f76SAndroid Build Coastguard Worker out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
1041*e01b6f76SAndroid Build Coastguard Worker }
1042*e01b6f76SAndroid Build Coastguard Worker
1043*e01b6f76SAndroid Build Coastguard Worker /* The Framework is currently limited to no more than this number of channels */
1044*e01b6f76SAndroid Build Coastguard Worker if (out->hal_channel_count > FCC_LIMIT) {
1045*e01b6f76SAndroid Build Coastguard Worker out->hal_channel_count = FCC_LIMIT;
1046*e01b6f76SAndroid Build Coastguard Worker calc_mask = true;
1047*e01b6f76SAndroid Build Coastguard Worker }
1048*e01b6f76SAndroid Build Coastguard Worker
1049*e01b6f76SAndroid Build Coastguard Worker if (calc_mask) {
1050*e01b6f76SAndroid Build Coastguard Worker /* need to calculate the mask from channel count either because this is the query case
1051*e01b6f76SAndroid Build Coastguard Worker * or the specified mask isn't valid for this device, or is more than the FW can handle */
1052*e01b6f76SAndroid Build Coastguard Worker config->channel_mask = out->hal_channel_count <= FCC_2
1053*e01b6f76SAndroid Build Coastguard Worker /* position mask for mono and stereo*/
1054*e01b6f76SAndroid Build Coastguard Worker ? audio_channel_out_mask_from_count(out->hal_channel_count)
1055*e01b6f76SAndroid Build Coastguard Worker /* otherwise indexed */
1056*e01b6f76SAndroid Build Coastguard Worker : audio_channel_mask_for_index_assignment_from_count(out->hal_channel_count);
1057*e01b6f76SAndroid Build Coastguard Worker }
1058*e01b6f76SAndroid Build Coastguard Worker
1059*e01b6f76SAndroid Build Coastguard Worker out->hal_channel_mask = config->channel_mask;
1060*e01b6f76SAndroid Build Coastguard Worker
1061*e01b6f76SAndroid Build Coastguard Worker // Validate the "logical" channel count against support in the "actual" profile.
1062*e01b6f76SAndroid Build Coastguard Worker // if they differ, choose the "actual" number of channels *closest* to the "logical".
1063*e01b6f76SAndroid Build Coastguard Worker // and store THAT in proxy_config.channels
1064*e01b6f76SAndroid Build Coastguard Worker proxy_config.channels =
1065*e01b6f76SAndroid Build Coastguard Worker profile_get_closest_channel_count(&device_info->profile, out->hal_channel_count);
1066*e01b6f76SAndroid Build Coastguard Worker if (is_bit_perfect && proxy_config.channels != out->hal_channel_count) {
1067*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s request bit-perfect, but channel mask(%#x) cannot find exact match",
1068*e01b6f76SAndroid Build Coastguard Worker __func__, config->channel_mask);
1069*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1070*e01b6f76SAndroid Build Coastguard Worker }
1071*e01b6f76SAndroid Build Coastguard Worker
1072*e01b6f76SAndroid Build Coastguard Worker ret = proxy_prepare(&device_info->proxy, &device_info->profile, &proxy_config, is_bit_perfect);
1073*e01b6f76SAndroid Build Coastguard Worker if (is_bit_perfect && ret != 0) {
1074*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s failed to prepare proxy for bit-perfect playback, err=%d", __func__, ret);
1075*e01b6f76SAndroid Build Coastguard Worker return ret;
1076*e01b6f76SAndroid Build Coastguard Worker }
1077*e01b6f76SAndroid Build Coastguard Worker out->config = proxy_config;
1078*e01b6f76SAndroid Build Coastguard Worker
1079*e01b6f76SAndroid Build Coastguard Worker list_add_tail(&out->alsa_devices, &device_info->list_node);
1080*e01b6f76SAndroid Build Coastguard Worker
1081*e01b6f76SAndroid Build Coastguard Worker if ((flags & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE) {
1082*e01b6f76SAndroid Build Coastguard Worker out_stream_find_mixer_volume_control(out, device_info->profile.card);
1083*e01b6f76SAndroid Build Coastguard Worker }
1084*e01b6f76SAndroid Build Coastguard Worker
1085*e01b6f76SAndroid Build Coastguard Worker /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger
1086*e01b6f76SAndroid Build Coastguard Worker * So clear any errors that may have occurred above.
1087*e01b6f76SAndroid Build Coastguard Worker */
1088*e01b6f76SAndroid Build Coastguard Worker ret = 0;
1089*e01b6f76SAndroid Build Coastguard Worker
1090*e01b6f76SAndroid Build Coastguard Worker out->conversion_buffer = NULL;
1091*e01b6f76SAndroid Build Coastguard Worker out->conversion_buffer_size = 0;
1092*e01b6f76SAndroid Build Coastguard Worker
1093*e01b6f76SAndroid Build Coastguard Worker out->standby = true;
1094*e01b6f76SAndroid Build Coastguard Worker
1095*e01b6f76SAndroid Build Coastguard Worker /* Save the stream for adev_dump() */
1096*e01b6f76SAndroid Build Coastguard Worker adev_add_stream_to_list(out->adev, &out->adev->output_stream_list, &out->list_node);
1097*e01b6f76SAndroid Build Coastguard Worker
1098*e01b6f76SAndroid Build Coastguard Worker *stream_out = &out->stream;
1099*e01b6f76SAndroid Build Coastguard Worker
1100*e01b6f76SAndroid Build Coastguard Worker return ret;
1101*e01b6f76SAndroid Build Coastguard Worker }
1102*e01b6f76SAndroid Build Coastguard Worker
adev_close_output_stream(struct audio_hw_device * hw_dev,struct audio_stream_out * stream)1103*e01b6f76SAndroid Build Coastguard Worker static void adev_close_output_stream(struct audio_hw_device *hw_dev,
1104*e01b6f76SAndroid Build Coastguard Worker struct audio_stream_out *stream)
1105*e01b6f76SAndroid Build Coastguard Worker {
1106*e01b6f76SAndroid Build Coastguard Worker struct stream_out *out = (struct stream_out *)stream;
1107*e01b6f76SAndroid Build Coastguard Worker
1108*e01b6f76SAndroid Build Coastguard Worker stream_lock(&out->lock);
1109*e01b6f76SAndroid Build Coastguard Worker /* Close the pcm device */
1110*e01b6f76SAndroid Build Coastguard Worker stream_standby_l(&out->alsa_devices, &out->standby);
1111*e01b6f76SAndroid Build Coastguard Worker stream_clear_devices(&out->alsa_devices);
1112*e01b6f76SAndroid Build Coastguard Worker
1113*e01b6f76SAndroid Build Coastguard Worker free(out->conversion_buffer);
1114*e01b6f76SAndroid Build Coastguard Worker
1115*e01b6f76SAndroid Build Coastguard Worker out->conversion_buffer = NULL;
1116*e01b6f76SAndroid Build Coastguard Worker out->conversion_buffer_size = 0;
1117*e01b6f76SAndroid Build Coastguard Worker
1118*e01b6f76SAndroid Build Coastguard Worker if (out->volume_ctl != NULL) {
1119*e01b6f76SAndroid Build Coastguard Worker for (int i = 0; i < out->volume_ctl_num_values; ++i) {
1120*e01b6f76SAndroid Build Coastguard Worker mixer_ctl_set_value(out->volume_ctl, i, out->max_volume_level);
1121*e01b6f76SAndroid Build Coastguard Worker }
1122*e01b6f76SAndroid Build Coastguard Worker out->volume_ctl = NULL;
1123*e01b6f76SAndroid Build Coastguard Worker }
1124*e01b6f76SAndroid Build Coastguard Worker if (out->mixer != NULL) {
1125*e01b6f76SAndroid Build Coastguard Worker mixer_close(out->mixer);
1126*e01b6f76SAndroid Build Coastguard Worker out->mixer = NULL;
1127*e01b6f76SAndroid Build Coastguard Worker }
1128*e01b6f76SAndroid Build Coastguard Worker
1129*e01b6f76SAndroid Build Coastguard Worker device_lock(out->adev);
1130*e01b6f76SAndroid Build Coastguard Worker list_remove(&out->list_node);
1131*e01b6f76SAndroid Build Coastguard Worker out->adev->device_sample_rate = 0;
1132*e01b6f76SAndroid Build Coastguard Worker device_unlock(out->adev);
1133*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&out->lock);
1134*e01b6f76SAndroid Build Coastguard Worker
1135*e01b6f76SAndroid Build Coastguard Worker free(stream);
1136*e01b6f76SAndroid Build Coastguard Worker }
1137*e01b6f76SAndroid Build Coastguard Worker
adev_get_input_buffer_size(const struct audio_hw_device * hw_dev,const struct audio_config * config)1138*e01b6f76SAndroid Build Coastguard Worker static size_t adev_get_input_buffer_size(const struct audio_hw_device *hw_dev,
1139*e01b6f76SAndroid Build Coastguard Worker const struct audio_config *config)
1140*e01b6f76SAndroid Build Coastguard Worker {
1141*e01b6f76SAndroid Build Coastguard Worker /* TODO This needs to be calculated based on format/channels/rate */
1142*e01b6f76SAndroid Build Coastguard Worker return 320;
1143*e01b6f76SAndroid Build Coastguard Worker }
1144*e01b6f76SAndroid Build Coastguard Worker
1145*e01b6f76SAndroid Build Coastguard Worker /*
1146*e01b6f76SAndroid Build Coastguard Worker * IN functions
1147*e01b6f76SAndroid Build Coastguard Worker */
in_get_sample_rate(const struct audio_stream * stream)1148*e01b6f76SAndroid Build Coastguard Worker static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1149*e01b6f76SAndroid Build Coastguard Worker {
1150*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(
1151*e01b6f76SAndroid Build Coastguard Worker &((const struct stream_in *)stream)->alsa_devices);
1152*e01b6f76SAndroid Build Coastguard Worker if (device_info == NULL) {
1153*e01b6f76SAndroid Build Coastguard Worker ALOGW("%s device info is null", __func__);
1154*e01b6f76SAndroid Build Coastguard Worker return 0;
1155*e01b6f76SAndroid Build Coastguard Worker }
1156*e01b6f76SAndroid Build Coastguard Worker uint32_t rate = proxy_get_sample_rate(&device_info->proxy);
1157*e01b6f76SAndroid Build Coastguard Worker ALOGV("in_get_sample_rate() = %d", rate);
1158*e01b6f76SAndroid Build Coastguard Worker return rate;
1159*e01b6f76SAndroid Build Coastguard Worker }
1160*e01b6f76SAndroid Build Coastguard Worker
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)1161*e01b6f76SAndroid Build Coastguard Worker static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1162*e01b6f76SAndroid Build Coastguard Worker {
1163*e01b6f76SAndroid Build Coastguard Worker ALOGV("in_set_sample_rate(%d) - NOPE", rate);
1164*e01b6f76SAndroid Build Coastguard Worker return -ENOSYS;
1165*e01b6f76SAndroid Build Coastguard Worker }
1166*e01b6f76SAndroid Build Coastguard Worker
in_get_buffer_size(const struct audio_stream * stream)1167*e01b6f76SAndroid Build Coastguard Worker static size_t in_get_buffer_size(const struct audio_stream *stream)
1168*e01b6f76SAndroid Build Coastguard Worker {
1169*e01b6f76SAndroid Build Coastguard Worker const struct stream_in * in = ((const struct stream_in*)stream);
1170*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1171*e01b6f76SAndroid Build Coastguard Worker if (device_info == NULL) {
1172*e01b6f76SAndroid Build Coastguard Worker ALOGW("%s device info is null", __func__);
1173*e01b6f76SAndroid Build Coastguard Worker return 0;
1174*e01b6f76SAndroid Build Coastguard Worker }
1175*e01b6f76SAndroid Build Coastguard Worker return proxy_get_period_size(&device_info->proxy) * audio_stream_in_frame_size(&(in->stream));
1176*e01b6f76SAndroid Build Coastguard Worker }
1177*e01b6f76SAndroid Build Coastguard Worker
in_get_channels(const struct audio_stream * stream)1178*e01b6f76SAndroid Build Coastguard Worker static uint32_t in_get_channels(const struct audio_stream *stream)
1179*e01b6f76SAndroid Build Coastguard Worker {
1180*e01b6f76SAndroid Build Coastguard Worker const struct stream_in *in = (const struct stream_in*)stream;
1181*e01b6f76SAndroid Build Coastguard Worker return in->hal_channel_mask;
1182*e01b6f76SAndroid Build Coastguard Worker }
1183*e01b6f76SAndroid Build Coastguard Worker
in_get_format(const struct audio_stream * stream)1184*e01b6f76SAndroid Build Coastguard Worker static audio_format_t in_get_format(const struct audio_stream *stream)
1185*e01b6f76SAndroid Build Coastguard Worker {
1186*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(
1187*e01b6f76SAndroid Build Coastguard Worker &((const struct stream_in *)stream)->alsa_devices);
1188*e01b6f76SAndroid Build Coastguard Worker if (device_info == NULL) {
1189*e01b6f76SAndroid Build Coastguard Worker ALOGW("%s device info is null", __func__);
1190*e01b6f76SAndroid Build Coastguard Worker return AUDIO_FORMAT_DEFAULT;
1191*e01b6f76SAndroid Build Coastguard Worker }
1192*e01b6f76SAndroid Build Coastguard Worker alsa_device_proxy *proxy = &device_info->proxy;
1193*e01b6f76SAndroid Build Coastguard Worker audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
1194*e01b6f76SAndroid Build Coastguard Worker return format;
1195*e01b6f76SAndroid Build Coastguard Worker }
1196*e01b6f76SAndroid Build Coastguard Worker
in_set_format(struct audio_stream * stream,audio_format_t format)1197*e01b6f76SAndroid Build Coastguard Worker static int in_set_format(struct audio_stream *stream, audio_format_t format)
1198*e01b6f76SAndroid Build Coastguard Worker {
1199*e01b6f76SAndroid Build Coastguard Worker ALOGV("in_set_format(%d) - NOPE", format);
1200*e01b6f76SAndroid Build Coastguard Worker
1201*e01b6f76SAndroid Build Coastguard Worker return -ENOSYS;
1202*e01b6f76SAndroid Build Coastguard Worker }
1203*e01b6f76SAndroid Build Coastguard Worker
in_standby(struct audio_stream * stream)1204*e01b6f76SAndroid Build Coastguard Worker static int in_standby(struct audio_stream *stream)
1205*e01b6f76SAndroid Build Coastguard Worker {
1206*e01b6f76SAndroid Build Coastguard Worker struct stream_in *in = (struct stream_in *)stream;
1207*e01b6f76SAndroid Build Coastguard Worker
1208*e01b6f76SAndroid Build Coastguard Worker stream_lock(&in->lock);
1209*e01b6f76SAndroid Build Coastguard Worker device_lock(in->adev);
1210*e01b6f76SAndroid Build Coastguard Worker stream_standby_l(&in->alsa_devices, &in->standby);
1211*e01b6f76SAndroid Build Coastguard Worker device_unlock(in->adev);
1212*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&in->lock);
1213*e01b6f76SAndroid Build Coastguard Worker return 0;
1214*e01b6f76SAndroid Build Coastguard Worker }
1215*e01b6f76SAndroid Build Coastguard Worker
in_dump(const struct audio_stream * stream,int fd)1216*e01b6f76SAndroid Build Coastguard Worker static int in_dump(const struct audio_stream *stream, int fd)
1217*e01b6f76SAndroid Build Coastguard Worker {
1218*e01b6f76SAndroid Build Coastguard Worker const struct stream_in* in_stream = (const struct stream_in*)stream;
1219*e01b6f76SAndroid Build Coastguard Worker if (in_stream != NULL) {
1220*e01b6f76SAndroid Build Coastguard Worker stream_dump_alsa_devices(&in_stream->alsa_devices, fd);
1221*e01b6f76SAndroid Build Coastguard Worker }
1222*e01b6f76SAndroid Build Coastguard Worker
1223*e01b6f76SAndroid Build Coastguard Worker return 0;
1224*e01b6f76SAndroid Build Coastguard Worker }
1225*e01b6f76SAndroid Build Coastguard Worker
in_set_parameters(struct audio_stream * stream,const char * kvpairs)1226*e01b6f76SAndroid Build Coastguard Worker static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1227*e01b6f76SAndroid Build Coastguard Worker {
1228*e01b6f76SAndroid Build Coastguard Worker ALOGV("in_set_parameters() keys:%s", kvpairs);
1229*e01b6f76SAndroid Build Coastguard Worker
1230*e01b6f76SAndroid Build Coastguard Worker // The set parameters here only matters when the routing devices are changed.
1231*e01b6f76SAndroid Build Coastguard Worker // When the device version higher than 3.0, the framework will use create_audio_patch
1232*e01b6f76SAndroid Build Coastguard Worker // API instead of set_parameters to change audio routing.
1233*e01b6f76SAndroid Build Coastguard Worker return 0;
1234*e01b6f76SAndroid Build Coastguard Worker }
1235*e01b6f76SAndroid Build Coastguard Worker
in_get_parameters(const struct audio_stream * stream,const char * keys)1236*e01b6f76SAndroid Build Coastguard Worker static char * in_get_parameters(const struct audio_stream *stream, const char *keys)
1237*e01b6f76SAndroid Build Coastguard Worker {
1238*e01b6f76SAndroid Build Coastguard Worker struct stream_in *in = (struct stream_in *)stream;
1239*e01b6f76SAndroid Build Coastguard Worker
1240*e01b6f76SAndroid Build Coastguard Worker stream_lock(&in->lock);
1241*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1242*e01b6f76SAndroid Build Coastguard Worker char *params_str = NULL;
1243*e01b6f76SAndroid Build Coastguard Worker if (device_info != NULL) {
1244*e01b6f76SAndroid Build Coastguard Worker params_str = device_get_parameters(&device_info->profile, keys);
1245*e01b6f76SAndroid Build Coastguard Worker }
1246*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&in->lock);
1247*e01b6f76SAndroid Build Coastguard Worker
1248*e01b6f76SAndroid Build Coastguard Worker return params_str;
1249*e01b6f76SAndroid Build Coastguard Worker }
1250*e01b6f76SAndroid Build Coastguard Worker
in_add_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1251*e01b6f76SAndroid Build Coastguard Worker static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1252*e01b6f76SAndroid Build Coastguard Worker {
1253*e01b6f76SAndroid Build Coastguard Worker return 0;
1254*e01b6f76SAndroid Build Coastguard Worker }
1255*e01b6f76SAndroid Build Coastguard Worker
in_remove_audio_effect(const struct audio_stream * stream,effect_handle_t effect)1256*e01b6f76SAndroid Build Coastguard Worker static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1257*e01b6f76SAndroid Build Coastguard Worker {
1258*e01b6f76SAndroid Build Coastguard Worker return 0;
1259*e01b6f76SAndroid Build Coastguard Worker }
1260*e01b6f76SAndroid Build Coastguard Worker
in_set_gain(struct audio_stream_in * stream,float gain)1261*e01b6f76SAndroid Build Coastguard Worker static int in_set_gain(struct audio_stream_in *stream, float gain)
1262*e01b6f76SAndroid Build Coastguard Worker {
1263*e01b6f76SAndroid Build Coastguard Worker return 0;
1264*e01b6f76SAndroid Build Coastguard Worker }
1265*e01b6f76SAndroid Build Coastguard Worker
1266*e01b6f76SAndroid Build Coastguard Worker /* must be called with hw device and input stream mutexes locked */
start_input_stream(struct stream_in * in)1267*e01b6f76SAndroid Build Coastguard Worker static int start_input_stream(struct stream_in *in)
1268*e01b6f76SAndroid Build Coastguard Worker {
1269*e01b6f76SAndroid Build Coastguard Worker // Only care about the first device as only one input device is allowed.
1270*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1271*e01b6f76SAndroid Build Coastguard Worker if (device_info == NULL) {
1272*e01b6f76SAndroid Build Coastguard Worker return -ENODEV;
1273*e01b6f76SAndroid Build Coastguard Worker }
1274*e01b6f76SAndroid Build Coastguard Worker
1275*e01b6f76SAndroid Build Coastguard Worker ALOGV("start_input_stream(card:%d device:%d)",
1276*e01b6f76SAndroid Build Coastguard Worker device_info->profile.card, device_info->profile.device);
1277*e01b6f76SAndroid Build Coastguard Worker int ret = proxy_open(&device_info->proxy);
1278*e01b6f76SAndroid Build Coastguard Worker if (ret == 0) {
1279*e01b6f76SAndroid Build Coastguard Worker in->standby = false;
1280*e01b6f76SAndroid Build Coastguard Worker }
1281*e01b6f76SAndroid Build Coastguard Worker return ret;
1282*e01b6f76SAndroid Build Coastguard Worker }
1283*e01b6f76SAndroid Build Coastguard Worker
1284*e01b6f76SAndroid Build Coastguard Worker /* TODO mutex stuff here (see out_write) */
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)1285*e01b6f76SAndroid Build Coastguard Worker static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes)
1286*e01b6f76SAndroid Build Coastguard Worker {
1287*e01b6f76SAndroid Build Coastguard Worker size_t num_read_buff_bytes = 0;
1288*e01b6f76SAndroid Build Coastguard Worker void * read_buff = buffer;
1289*e01b6f76SAndroid Build Coastguard Worker void * out_buff = buffer;
1290*e01b6f76SAndroid Build Coastguard Worker int ret = 0;
1291*e01b6f76SAndroid Build Coastguard Worker
1292*e01b6f76SAndroid Build Coastguard Worker struct stream_in * in = (struct stream_in *)stream;
1293*e01b6f76SAndroid Build Coastguard Worker
1294*e01b6f76SAndroid Build Coastguard Worker stream_lock(&in->lock);
1295*e01b6f76SAndroid Build Coastguard Worker if (in->standby) {
1296*e01b6f76SAndroid Build Coastguard Worker ret = start_input_stream(in);
1297*e01b6f76SAndroid Build Coastguard Worker if (ret != 0) {
1298*e01b6f76SAndroid Build Coastguard Worker goto err;
1299*e01b6f76SAndroid Build Coastguard Worker }
1300*e01b6f76SAndroid Build Coastguard Worker }
1301*e01b6f76SAndroid Build Coastguard Worker
1302*e01b6f76SAndroid Build Coastguard Worker // Only care about the first device as only one input device is allowed.
1303*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1304*e01b6f76SAndroid Build Coastguard Worker if (device_info == NULL) {
1305*e01b6f76SAndroid Build Coastguard Worker return 0;
1306*e01b6f76SAndroid Build Coastguard Worker }
1307*e01b6f76SAndroid Build Coastguard Worker
1308*e01b6f76SAndroid Build Coastguard Worker /*
1309*e01b6f76SAndroid Build Coastguard Worker * OK, we need to figure out how much data to read to be able to output the requested
1310*e01b6f76SAndroid Build Coastguard Worker * number of bytes in the HAL format (16-bit, stereo).
1311*e01b6f76SAndroid Build Coastguard Worker */
1312*e01b6f76SAndroid Build Coastguard Worker num_read_buff_bytes = bytes;
1313*e01b6f76SAndroid Build Coastguard Worker int num_device_channels = proxy_get_channel_count(&device_info->proxy); /* what we told Alsa */
1314*e01b6f76SAndroid Build Coastguard Worker int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
1315*e01b6f76SAndroid Build Coastguard Worker
1316*e01b6f76SAndroid Build Coastguard Worker if (num_device_channels != num_req_channels) {
1317*e01b6f76SAndroid Build Coastguard Worker num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
1318*e01b6f76SAndroid Build Coastguard Worker }
1319*e01b6f76SAndroid Build Coastguard Worker
1320*e01b6f76SAndroid Build Coastguard Worker /* Setup/Realloc the conversion buffer (if necessary). */
1321*e01b6f76SAndroid Build Coastguard Worker if (num_read_buff_bytes != bytes) {
1322*e01b6f76SAndroid Build Coastguard Worker if (num_read_buff_bytes > in->conversion_buffer_size) {
1323*e01b6f76SAndroid Build Coastguard Worker /*TODO Remove this when AudioPolicyManger/AudioFlinger support arbitrary formats
1324*e01b6f76SAndroid Build Coastguard Worker (and do these conversions themselves) */
1325*e01b6f76SAndroid Build Coastguard Worker in->conversion_buffer_size = num_read_buff_bytes;
1326*e01b6f76SAndroid Build Coastguard Worker in->conversion_buffer = realloc(in->conversion_buffer, in->conversion_buffer_size);
1327*e01b6f76SAndroid Build Coastguard Worker }
1328*e01b6f76SAndroid Build Coastguard Worker read_buff = in->conversion_buffer;
1329*e01b6f76SAndroid Build Coastguard Worker }
1330*e01b6f76SAndroid Build Coastguard Worker
1331*e01b6f76SAndroid Build Coastguard Worker ret = proxy_read(&device_info->proxy, read_buff, num_read_buff_bytes);
1332*e01b6f76SAndroid Build Coastguard Worker if (ret == 0) {
1333*e01b6f76SAndroid Build Coastguard Worker if (num_device_channels != num_req_channels) {
1334*e01b6f76SAndroid Build Coastguard Worker // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
1335*e01b6f76SAndroid Build Coastguard Worker
1336*e01b6f76SAndroid Build Coastguard Worker out_buff = buffer;
1337*e01b6f76SAndroid Build Coastguard Worker /* Num Channels conversion */
1338*e01b6f76SAndroid Build Coastguard Worker if (num_device_channels != num_req_channels) {
1339*e01b6f76SAndroid Build Coastguard Worker audio_format_t audio_format = in_get_format(&(in->stream.common));
1340*e01b6f76SAndroid Build Coastguard Worker unsigned sample_size_in_bytes = audio_bytes_per_sample(audio_format);
1341*e01b6f76SAndroid Build Coastguard Worker
1342*e01b6f76SAndroid Build Coastguard Worker num_read_buff_bytes =
1343*e01b6f76SAndroid Build Coastguard Worker adjust_channels(read_buff, num_device_channels,
1344*e01b6f76SAndroid Build Coastguard Worker out_buff, num_req_channels,
1345*e01b6f76SAndroid Build Coastguard Worker sample_size_in_bytes, num_read_buff_bytes);
1346*e01b6f76SAndroid Build Coastguard Worker }
1347*e01b6f76SAndroid Build Coastguard Worker }
1348*e01b6f76SAndroid Build Coastguard Worker
1349*e01b6f76SAndroid Build Coastguard Worker /* no need to acquire in->adev->lock to read mic_muted here as we don't change its state */
1350*e01b6f76SAndroid Build Coastguard Worker if (num_read_buff_bytes > 0 && in->adev->mic_muted)
1351*e01b6f76SAndroid Build Coastguard Worker memset(buffer, 0, num_read_buff_bytes);
1352*e01b6f76SAndroid Build Coastguard Worker } else {
1353*e01b6f76SAndroid Build Coastguard Worker num_read_buff_bytes = 0; // reset the value after USB headset is unplugged
1354*e01b6f76SAndroid Build Coastguard Worker }
1355*e01b6f76SAndroid Build Coastguard Worker
1356*e01b6f76SAndroid Build Coastguard Worker err:
1357*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&in->lock);
1358*e01b6f76SAndroid Build Coastguard Worker return num_read_buff_bytes;
1359*e01b6f76SAndroid Build Coastguard Worker }
1360*e01b6f76SAndroid Build Coastguard Worker
in_get_input_frames_lost(struct audio_stream_in * stream)1361*e01b6f76SAndroid Build Coastguard Worker static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1362*e01b6f76SAndroid Build Coastguard Worker {
1363*e01b6f76SAndroid Build Coastguard Worker return 0;
1364*e01b6f76SAndroid Build Coastguard Worker }
1365*e01b6f76SAndroid Build Coastguard Worker
in_get_capture_position(const struct audio_stream_in * stream,int64_t * frames,int64_t * time)1366*e01b6f76SAndroid Build Coastguard Worker static int in_get_capture_position(const struct audio_stream_in *stream,
1367*e01b6f76SAndroid Build Coastguard Worker int64_t *frames, int64_t *time)
1368*e01b6f76SAndroid Build Coastguard Worker {
1369*e01b6f76SAndroid Build Coastguard Worker struct stream_in *in = (struct stream_in *)stream; // discard const qualifier
1370*e01b6f76SAndroid Build Coastguard Worker stream_lock(&in->lock);
1371*e01b6f76SAndroid Build Coastguard Worker
1372*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1373*e01b6f76SAndroid Build Coastguard Worker
1374*e01b6f76SAndroid Build Coastguard Worker const int ret = device_info == NULL ? -ENODEV
1375*e01b6f76SAndroid Build Coastguard Worker : proxy_get_capture_position(&device_info->proxy, frames, time);
1376*e01b6f76SAndroid Build Coastguard Worker
1377*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&in->lock);
1378*e01b6f76SAndroid Build Coastguard Worker return ret;
1379*e01b6f76SAndroid Build Coastguard Worker }
1380*e01b6f76SAndroid Build Coastguard Worker
in_get_active_microphones(const struct audio_stream_in * stream,struct audio_microphone_characteristic_t * mic_array,size_t * mic_count)1381*e01b6f76SAndroid Build Coastguard Worker static int in_get_active_microphones(const struct audio_stream_in *stream,
1382*e01b6f76SAndroid Build Coastguard Worker struct audio_microphone_characteristic_t *mic_array,
1383*e01b6f76SAndroid Build Coastguard Worker size_t *mic_count) {
1384*e01b6f76SAndroid Build Coastguard Worker (void)stream;
1385*e01b6f76SAndroid Build Coastguard Worker (void)mic_array;
1386*e01b6f76SAndroid Build Coastguard Worker (void)mic_count;
1387*e01b6f76SAndroid Build Coastguard Worker
1388*e01b6f76SAndroid Build Coastguard Worker return -ENOSYS;
1389*e01b6f76SAndroid Build Coastguard Worker }
1390*e01b6f76SAndroid Build Coastguard Worker
in_set_microphone_direction(const struct audio_stream_in * stream,audio_microphone_direction_t dir)1391*e01b6f76SAndroid Build Coastguard Worker static int in_set_microphone_direction(const struct audio_stream_in *stream,
1392*e01b6f76SAndroid Build Coastguard Worker audio_microphone_direction_t dir) {
1393*e01b6f76SAndroid Build Coastguard Worker (void)stream;
1394*e01b6f76SAndroid Build Coastguard Worker (void)dir;
1395*e01b6f76SAndroid Build Coastguard Worker ALOGV("---- in_set_microphone_direction()");
1396*e01b6f76SAndroid Build Coastguard Worker return -ENOSYS;
1397*e01b6f76SAndroid Build Coastguard Worker }
1398*e01b6f76SAndroid Build Coastguard Worker
in_set_microphone_field_dimension(const struct audio_stream_in * stream,float zoom)1399*e01b6f76SAndroid Build Coastguard Worker static int in_set_microphone_field_dimension(const struct audio_stream_in *stream, float zoom) {
1400*e01b6f76SAndroid Build Coastguard Worker (void)zoom;
1401*e01b6f76SAndroid Build Coastguard Worker ALOGV("---- in_set_microphone_field_dimension()");
1402*e01b6f76SAndroid Build Coastguard Worker return -ENOSYS;
1403*e01b6f76SAndroid Build Coastguard Worker }
1404*e01b6f76SAndroid Build Coastguard Worker
adev_open_input_stream(struct audio_hw_device * hw_dev,audio_io_handle_t handle,audio_devices_t devicesSpec __unused,struct audio_config * config,struct audio_stream_in ** stream_in,audio_input_flags_t flags __unused,const char * address,audio_source_t source __unused)1405*e01b6f76SAndroid Build Coastguard Worker static int adev_open_input_stream(struct audio_hw_device *hw_dev,
1406*e01b6f76SAndroid Build Coastguard Worker audio_io_handle_t handle,
1407*e01b6f76SAndroid Build Coastguard Worker audio_devices_t devicesSpec __unused,
1408*e01b6f76SAndroid Build Coastguard Worker struct audio_config *config,
1409*e01b6f76SAndroid Build Coastguard Worker struct audio_stream_in **stream_in,
1410*e01b6f76SAndroid Build Coastguard Worker audio_input_flags_t flags __unused,
1411*e01b6f76SAndroid Build Coastguard Worker const char *address,
1412*e01b6f76SAndroid Build Coastguard Worker audio_source_t source __unused)
1413*e01b6f76SAndroid Build Coastguard Worker {
1414*e01b6f76SAndroid Build Coastguard Worker ALOGV("adev_open_input_stream() rate:%" PRIu32 ", chanMask:0x%" PRIX32 ", fmt:%" PRIu8,
1415*e01b6f76SAndroid Build Coastguard Worker config->sample_rate, config->channel_mask, config->format);
1416*e01b6f76SAndroid Build Coastguard Worker
1417*e01b6f76SAndroid Build Coastguard Worker /* Pull out the card/device pair */
1418*e01b6f76SAndroid Build Coastguard Worker int32_t card, device;
1419*e01b6f76SAndroid Build Coastguard Worker if (!parse_card_device_params(address, &card, &device)) {
1420*e01b6f76SAndroid Build Coastguard Worker ALOGW("%s fail - invalid address %s", __func__, address);
1421*e01b6f76SAndroid Build Coastguard Worker *stream_in = NULL;
1422*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1423*e01b6f76SAndroid Build Coastguard Worker }
1424*e01b6f76SAndroid Build Coastguard Worker
1425*e01b6f76SAndroid Build Coastguard Worker struct stream_in * const in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1426*e01b6f76SAndroid Build Coastguard Worker if (in == NULL) {
1427*e01b6f76SAndroid Build Coastguard Worker *stream_in = NULL;
1428*e01b6f76SAndroid Build Coastguard Worker return -ENOMEM;
1429*e01b6f76SAndroid Build Coastguard Worker }
1430*e01b6f76SAndroid Build Coastguard Worker
1431*e01b6f76SAndroid Build Coastguard Worker /* setup function pointers */
1432*e01b6f76SAndroid Build Coastguard Worker in->stream.common.get_sample_rate = in_get_sample_rate;
1433*e01b6f76SAndroid Build Coastguard Worker in->stream.common.set_sample_rate = in_set_sample_rate;
1434*e01b6f76SAndroid Build Coastguard Worker in->stream.common.get_buffer_size = in_get_buffer_size;
1435*e01b6f76SAndroid Build Coastguard Worker in->stream.common.get_channels = in_get_channels;
1436*e01b6f76SAndroid Build Coastguard Worker in->stream.common.get_format = in_get_format;
1437*e01b6f76SAndroid Build Coastguard Worker in->stream.common.set_format = in_set_format;
1438*e01b6f76SAndroid Build Coastguard Worker in->stream.common.standby = in_standby;
1439*e01b6f76SAndroid Build Coastguard Worker in->stream.common.dump = in_dump;
1440*e01b6f76SAndroid Build Coastguard Worker in->stream.common.set_parameters = in_set_parameters;
1441*e01b6f76SAndroid Build Coastguard Worker in->stream.common.get_parameters = in_get_parameters;
1442*e01b6f76SAndroid Build Coastguard Worker in->stream.common.add_audio_effect = in_add_audio_effect;
1443*e01b6f76SAndroid Build Coastguard Worker in->stream.common.remove_audio_effect = in_remove_audio_effect;
1444*e01b6f76SAndroid Build Coastguard Worker
1445*e01b6f76SAndroid Build Coastguard Worker in->stream.set_gain = in_set_gain;
1446*e01b6f76SAndroid Build Coastguard Worker in->stream.read = in_read;
1447*e01b6f76SAndroid Build Coastguard Worker in->stream.get_input_frames_lost = in_get_input_frames_lost;
1448*e01b6f76SAndroid Build Coastguard Worker in->stream.get_capture_position = in_get_capture_position;
1449*e01b6f76SAndroid Build Coastguard Worker
1450*e01b6f76SAndroid Build Coastguard Worker in->stream.get_active_microphones = in_get_active_microphones;
1451*e01b6f76SAndroid Build Coastguard Worker in->stream.set_microphone_direction = in_set_microphone_direction;
1452*e01b6f76SAndroid Build Coastguard Worker in->stream.set_microphone_field_dimension = in_set_microphone_field_dimension;
1453*e01b6f76SAndroid Build Coastguard Worker
1454*e01b6f76SAndroid Build Coastguard Worker in->handle = handle;
1455*e01b6f76SAndroid Build Coastguard Worker
1456*e01b6f76SAndroid Build Coastguard Worker stream_lock_init(&in->lock);
1457*e01b6f76SAndroid Build Coastguard Worker
1458*e01b6f76SAndroid Build Coastguard Worker in->adev = (struct audio_device *)hw_dev;
1459*e01b6f76SAndroid Build Coastguard Worker
1460*e01b6f76SAndroid Build Coastguard Worker list_init(&in->alsa_devices);
1461*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info =
1462*e01b6f76SAndroid Build Coastguard Worker (struct alsa_device_info *)calloc(1, sizeof(struct alsa_device_info));
1463*e01b6f76SAndroid Build Coastguard Worker profile_init(&device_info->profile, PCM_IN);
1464*e01b6f76SAndroid Build Coastguard Worker
1465*e01b6f76SAndroid Build Coastguard Worker memset(&in->config, 0, sizeof(in->config));
1466*e01b6f76SAndroid Build Coastguard Worker
1467*e01b6f76SAndroid Build Coastguard Worker int ret = 0;
1468*e01b6f76SAndroid Build Coastguard Worker device_lock(in->adev);
1469*e01b6f76SAndroid Build Coastguard Worker int num_open_inputs = in->adev->inputs_open;
1470*e01b6f76SAndroid Build Coastguard Worker device_unlock(in->adev);
1471*e01b6f76SAndroid Build Coastguard Worker
1472*e01b6f76SAndroid Build Coastguard Worker /* Check if an input stream is already open */
1473*e01b6f76SAndroid Build Coastguard Worker if (num_open_inputs > 0) {
1474*e01b6f76SAndroid Build Coastguard Worker if (!profile_is_cached_for(&device_info->profile, card, device)) {
1475*e01b6f76SAndroid Build Coastguard Worker ALOGW("%s fail - address card:%d device:%d doesn't match existing profile",
1476*e01b6f76SAndroid Build Coastguard Worker __func__, card, device);
1477*e01b6f76SAndroid Build Coastguard Worker ret = -EINVAL;
1478*e01b6f76SAndroid Build Coastguard Worker }
1479*e01b6f76SAndroid Build Coastguard Worker } else {
1480*e01b6f76SAndroid Build Coastguard Worker /* Read input profile only if necessary */
1481*e01b6f76SAndroid Build Coastguard Worker device_info->profile.card = card;
1482*e01b6f76SAndroid Build Coastguard Worker device_info->profile.device = device;
1483*e01b6f76SAndroid Build Coastguard Worker if (!profile_read_device_info(&device_info->profile)) {
1484*e01b6f76SAndroid Build Coastguard Worker ALOGW("%s fail - cannot read profile", __func__);
1485*e01b6f76SAndroid Build Coastguard Worker ret = -EINVAL;
1486*e01b6f76SAndroid Build Coastguard Worker }
1487*e01b6f76SAndroid Build Coastguard Worker }
1488*e01b6f76SAndroid Build Coastguard Worker if (ret != 0) {
1489*e01b6f76SAndroid Build Coastguard Worker free(in);
1490*e01b6f76SAndroid Build Coastguard Worker *stream_in = NULL;
1491*e01b6f76SAndroid Build Coastguard Worker return ret;
1492*e01b6f76SAndroid Build Coastguard Worker }
1493*e01b6f76SAndroid Build Coastguard Worker
1494*e01b6f76SAndroid Build Coastguard Worker /* Rate */
1495*e01b6f76SAndroid Build Coastguard Worker int request_config_rate = config->sample_rate;
1496*e01b6f76SAndroid Build Coastguard Worker if (config->sample_rate == 0) {
1497*e01b6f76SAndroid Build Coastguard Worker config->sample_rate = profile_get_default_sample_rate(&device_info->profile);
1498*e01b6f76SAndroid Build Coastguard Worker }
1499*e01b6f76SAndroid Build Coastguard Worker
1500*e01b6f76SAndroid Build Coastguard Worker if (in->adev->device_sample_rate != 0 && /* we are playing, so lock the rate if possible */
1501*e01b6f76SAndroid Build Coastguard Worker in->adev->device_sample_rate >= RATELOCK_THRESHOLD) {/* but only for high sample rates */
1502*e01b6f76SAndroid Build Coastguard Worker if (config->sample_rate != in->adev->device_sample_rate) {
1503*e01b6f76SAndroid Build Coastguard Worker unsigned highest_rate = profile_get_highest_sample_rate(&device_info->profile);
1504*e01b6f76SAndroid Build Coastguard Worker if (highest_rate == 0) {
1505*e01b6f76SAndroid Build Coastguard Worker ret = -EINVAL; /* error with device */
1506*e01b6f76SAndroid Build Coastguard Worker } else {
1507*e01b6f76SAndroid Build Coastguard Worker in->config.rate = config->sample_rate =
1508*e01b6f76SAndroid Build Coastguard Worker min(highest_rate, in->adev->device_sample_rate);
1509*e01b6f76SAndroid Build Coastguard Worker if (request_config_rate != 0 && in->config.rate != config->sample_rate) {
1510*e01b6f76SAndroid Build Coastguard Worker /* Changing the requested rate */
1511*e01b6f76SAndroid Build Coastguard Worker ret = -EINVAL;
1512*e01b6f76SAndroid Build Coastguard Worker } else {
1513*e01b6f76SAndroid Build Coastguard Worker /* Everything AOK! */
1514*e01b6f76SAndroid Build Coastguard Worker ret = 0;
1515*e01b6f76SAndroid Build Coastguard Worker }
1516*e01b6f76SAndroid Build Coastguard Worker }
1517*e01b6f76SAndroid Build Coastguard Worker } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
1518*e01b6f76SAndroid Build Coastguard Worker in->config.rate = config->sample_rate;
1519*e01b6f76SAndroid Build Coastguard Worker }
1520*e01b6f76SAndroid Build Coastguard Worker } else if (profile_is_sample_rate_valid(&device_info->profile, config->sample_rate)) {
1521*e01b6f76SAndroid Build Coastguard Worker in->config.rate = config->sample_rate;
1522*e01b6f76SAndroid Build Coastguard Worker } else {
1523*e01b6f76SAndroid Build Coastguard Worker in->config.rate = config->sample_rate =
1524*e01b6f76SAndroid Build Coastguard Worker profile_get_default_sample_rate(&device_info->profile);
1525*e01b6f76SAndroid Build Coastguard Worker ret = -EINVAL;
1526*e01b6f76SAndroid Build Coastguard Worker }
1527*e01b6f76SAndroid Build Coastguard Worker
1528*e01b6f76SAndroid Build Coastguard Worker /* Format */
1529*e01b6f76SAndroid Build Coastguard Worker if (config->format == AUDIO_FORMAT_DEFAULT) {
1530*e01b6f76SAndroid Build Coastguard Worker in->config.format = profile_get_default_format(&device_info->profile);
1531*e01b6f76SAndroid Build Coastguard Worker config->format = audio_format_from_pcm_format(in->config.format);
1532*e01b6f76SAndroid Build Coastguard Worker } else {
1533*e01b6f76SAndroid Build Coastguard Worker enum pcm_format fmt = pcm_format_from_audio_format(config->format);
1534*e01b6f76SAndroid Build Coastguard Worker if (profile_is_format_valid(&device_info->profile, fmt)) {
1535*e01b6f76SAndroid Build Coastguard Worker in->config.format = fmt;
1536*e01b6f76SAndroid Build Coastguard Worker } else {
1537*e01b6f76SAndroid Build Coastguard Worker in->config.format = profile_get_default_format(&device_info->profile);
1538*e01b6f76SAndroid Build Coastguard Worker config->format = audio_format_from_pcm_format(in->config.format);
1539*e01b6f76SAndroid Build Coastguard Worker ret = -EINVAL;
1540*e01b6f76SAndroid Build Coastguard Worker }
1541*e01b6f76SAndroid Build Coastguard Worker }
1542*e01b6f76SAndroid Build Coastguard Worker
1543*e01b6f76SAndroid Build Coastguard Worker /* Channels */
1544*e01b6f76SAndroid Build Coastguard Worker bool calc_mask = false;
1545*e01b6f76SAndroid Build Coastguard Worker if (config->channel_mask == AUDIO_CHANNEL_NONE) {
1546*e01b6f76SAndroid Build Coastguard Worker /* query case */
1547*e01b6f76SAndroid Build Coastguard Worker in->hal_channel_count = profile_get_default_channel_count(&device_info->profile);
1548*e01b6f76SAndroid Build Coastguard Worker calc_mask = true;
1549*e01b6f76SAndroid Build Coastguard Worker } else {
1550*e01b6f76SAndroid Build Coastguard Worker /* explicit case */
1551*e01b6f76SAndroid Build Coastguard Worker in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
1552*e01b6f76SAndroid Build Coastguard Worker }
1553*e01b6f76SAndroid Build Coastguard Worker
1554*e01b6f76SAndroid Build Coastguard Worker /* The Framework is currently limited to no more than this number of channels */
1555*e01b6f76SAndroid Build Coastguard Worker if (in->hal_channel_count > FCC_LIMIT) {
1556*e01b6f76SAndroid Build Coastguard Worker in->hal_channel_count = FCC_LIMIT;
1557*e01b6f76SAndroid Build Coastguard Worker calc_mask = true;
1558*e01b6f76SAndroid Build Coastguard Worker }
1559*e01b6f76SAndroid Build Coastguard Worker
1560*e01b6f76SAndroid Build Coastguard Worker if (calc_mask) {
1561*e01b6f76SAndroid Build Coastguard Worker /* need to calculate the mask from channel count either because this is the query case
1562*e01b6f76SAndroid Build Coastguard Worker * or the specified mask isn't valid for this device, or is more than the FW can handle */
1563*e01b6f76SAndroid Build Coastguard Worker in->hal_channel_mask = in->hal_channel_count <= FCC_2
1564*e01b6f76SAndroid Build Coastguard Worker /* position mask for mono & stereo */
1565*e01b6f76SAndroid Build Coastguard Worker ? audio_channel_in_mask_from_count(in->hal_channel_count)
1566*e01b6f76SAndroid Build Coastguard Worker /* otherwise indexed */
1567*e01b6f76SAndroid Build Coastguard Worker : audio_channel_mask_for_index_assignment_from_count(in->hal_channel_count);
1568*e01b6f76SAndroid Build Coastguard Worker
1569*e01b6f76SAndroid Build Coastguard Worker // if we change the mask...
1570*e01b6f76SAndroid Build Coastguard Worker if (in->hal_channel_mask != config->channel_mask &&
1571*e01b6f76SAndroid Build Coastguard Worker config->channel_mask != AUDIO_CHANNEL_NONE) {
1572*e01b6f76SAndroid Build Coastguard Worker config->channel_mask = in->hal_channel_mask;
1573*e01b6f76SAndroid Build Coastguard Worker ret = -EINVAL;
1574*e01b6f76SAndroid Build Coastguard Worker }
1575*e01b6f76SAndroid Build Coastguard Worker } else {
1576*e01b6f76SAndroid Build Coastguard Worker in->hal_channel_mask = config->channel_mask;
1577*e01b6f76SAndroid Build Coastguard Worker }
1578*e01b6f76SAndroid Build Coastguard Worker
1579*e01b6f76SAndroid Build Coastguard Worker if (ret == 0) {
1580*e01b6f76SAndroid Build Coastguard Worker // Validate the "logical" channel count against support in the "actual" profile.
1581*e01b6f76SAndroid Build Coastguard Worker // if they differ, choose the "actual" number of channels *closest* to the "logical".
1582*e01b6f76SAndroid Build Coastguard Worker // and store THAT in proxy_config.channels
1583*e01b6f76SAndroid Build Coastguard Worker in->config.channels =
1584*e01b6f76SAndroid Build Coastguard Worker profile_get_closest_channel_count(&device_info->profile, in->hal_channel_count);
1585*e01b6f76SAndroid Build Coastguard Worker ret = proxy_prepare(&device_info->proxy, &device_info->profile, &in->config,
1586*e01b6f76SAndroid Build Coastguard Worker false /*require_exact_match*/);
1587*e01b6f76SAndroid Build Coastguard Worker if (ret == 0) {
1588*e01b6f76SAndroid Build Coastguard Worker in->standby = true;
1589*e01b6f76SAndroid Build Coastguard Worker
1590*e01b6f76SAndroid Build Coastguard Worker in->conversion_buffer = NULL;
1591*e01b6f76SAndroid Build Coastguard Worker in->conversion_buffer_size = 0;
1592*e01b6f76SAndroid Build Coastguard Worker
1593*e01b6f76SAndroid Build Coastguard Worker *stream_in = &in->stream;
1594*e01b6f76SAndroid Build Coastguard Worker
1595*e01b6f76SAndroid Build Coastguard Worker /* Save this for adev_dump() */
1596*e01b6f76SAndroid Build Coastguard Worker adev_add_stream_to_list(in->adev, &in->adev->input_stream_list, &in->list_node);
1597*e01b6f76SAndroid Build Coastguard Worker } else {
1598*e01b6f76SAndroid Build Coastguard Worker ALOGW("proxy_prepare error %d", ret);
1599*e01b6f76SAndroid Build Coastguard Worker unsigned channel_count = proxy_get_channel_count(&device_info->proxy);
1600*e01b6f76SAndroid Build Coastguard Worker config->channel_mask = channel_count <= FCC_2
1601*e01b6f76SAndroid Build Coastguard Worker ? audio_channel_in_mask_from_count(channel_count)
1602*e01b6f76SAndroid Build Coastguard Worker : audio_channel_mask_for_index_assignment_from_count(channel_count);
1603*e01b6f76SAndroid Build Coastguard Worker config->format = audio_format_from_pcm_format(proxy_get_format(&device_info->proxy));
1604*e01b6f76SAndroid Build Coastguard Worker config->sample_rate = proxy_get_sample_rate(&device_info->proxy);
1605*e01b6f76SAndroid Build Coastguard Worker }
1606*e01b6f76SAndroid Build Coastguard Worker }
1607*e01b6f76SAndroid Build Coastguard Worker
1608*e01b6f76SAndroid Build Coastguard Worker if (ret != 0) {
1609*e01b6f76SAndroid Build Coastguard Worker // Deallocate this stream on error, because AudioFlinger won't call
1610*e01b6f76SAndroid Build Coastguard Worker // adev_close_input_stream() in this case.
1611*e01b6f76SAndroid Build Coastguard Worker *stream_in = NULL;
1612*e01b6f76SAndroid Build Coastguard Worker free(in);
1613*e01b6f76SAndroid Build Coastguard Worker return ret;
1614*e01b6f76SAndroid Build Coastguard Worker }
1615*e01b6f76SAndroid Build Coastguard Worker
1616*e01b6f76SAndroid Build Coastguard Worker list_add_tail(&in->alsa_devices, &device_info->list_node);
1617*e01b6f76SAndroid Build Coastguard Worker
1618*e01b6f76SAndroid Build Coastguard Worker device_lock(in->adev);
1619*e01b6f76SAndroid Build Coastguard Worker ++in->adev->inputs_open;
1620*e01b6f76SAndroid Build Coastguard Worker device_unlock(in->adev);
1621*e01b6f76SAndroid Build Coastguard Worker
1622*e01b6f76SAndroid Build Coastguard Worker return ret;
1623*e01b6f76SAndroid Build Coastguard Worker }
1624*e01b6f76SAndroid Build Coastguard Worker
adev_close_input_stream(struct audio_hw_device * hw_dev,struct audio_stream_in * stream)1625*e01b6f76SAndroid Build Coastguard Worker static void adev_close_input_stream(struct audio_hw_device *hw_dev,
1626*e01b6f76SAndroid Build Coastguard Worker struct audio_stream_in *stream)
1627*e01b6f76SAndroid Build Coastguard Worker {
1628*e01b6f76SAndroid Build Coastguard Worker struct stream_in *in = (struct stream_in *)stream;
1629*e01b6f76SAndroid Build Coastguard Worker
1630*e01b6f76SAndroid Build Coastguard Worker stream_lock(&in->lock);
1631*e01b6f76SAndroid Build Coastguard Worker device_lock(in->adev);
1632*e01b6f76SAndroid Build Coastguard Worker list_remove(&in->list_node);
1633*e01b6f76SAndroid Build Coastguard Worker --in->adev->inputs_open;
1634*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(&in->alsa_devices);
1635*e01b6f76SAndroid Build Coastguard Worker if (device_info != NULL) {
1636*e01b6f76SAndroid Build Coastguard Worker ALOGV("adev_close_input_stream(c:%d d:%d)",
1637*e01b6f76SAndroid Build Coastguard Worker device_info->profile.card, device_info->profile.device);
1638*e01b6f76SAndroid Build Coastguard Worker }
1639*e01b6f76SAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(in->adev->inputs_open < 0,
1640*e01b6f76SAndroid Build Coastguard Worker "invalid inputs_open: %d", in->adev->inputs_open);
1641*e01b6f76SAndroid Build Coastguard Worker
1642*e01b6f76SAndroid Build Coastguard Worker stream_standby_l(&in->alsa_devices, &in->standby);
1643*e01b6f76SAndroid Build Coastguard Worker
1644*e01b6f76SAndroid Build Coastguard Worker device_unlock(in->adev);
1645*e01b6f76SAndroid Build Coastguard Worker
1646*e01b6f76SAndroid Build Coastguard Worker stream_clear_devices(&in->alsa_devices);
1647*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&in->lock);
1648*e01b6f76SAndroid Build Coastguard Worker
1649*e01b6f76SAndroid Build Coastguard Worker free(in->conversion_buffer);
1650*e01b6f76SAndroid Build Coastguard Worker
1651*e01b6f76SAndroid Build Coastguard Worker free(stream);
1652*e01b6f76SAndroid Build Coastguard Worker }
1653*e01b6f76SAndroid Build Coastguard Worker
1654*e01b6f76SAndroid Build Coastguard Worker /*
1655*e01b6f76SAndroid Build Coastguard Worker * ADEV Functions
1656*e01b6f76SAndroid Build Coastguard Worker */
adev_set_parameters(struct audio_hw_device * hw_dev,const char * kvpairs)1657*e01b6f76SAndroid Build Coastguard Worker static int adev_set_parameters(struct audio_hw_device *hw_dev, const char *kvpairs)
1658*e01b6f76SAndroid Build Coastguard Worker {
1659*e01b6f76SAndroid Build Coastguard Worker return 0;
1660*e01b6f76SAndroid Build Coastguard Worker }
1661*e01b6f76SAndroid Build Coastguard Worker
adev_get_parameters(const struct audio_hw_device * hw_dev,const char * keys)1662*e01b6f76SAndroid Build Coastguard Worker static char * adev_get_parameters(const struct audio_hw_device *hw_dev, const char *keys)
1663*e01b6f76SAndroid Build Coastguard Worker {
1664*e01b6f76SAndroid Build Coastguard Worker return strdup("");
1665*e01b6f76SAndroid Build Coastguard Worker }
1666*e01b6f76SAndroid Build Coastguard Worker
adev_init_check(const struct audio_hw_device * hw_dev)1667*e01b6f76SAndroid Build Coastguard Worker static int adev_init_check(const struct audio_hw_device *hw_dev)
1668*e01b6f76SAndroid Build Coastguard Worker {
1669*e01b6f76SAndroid Build Coastguard Worker return 0;
1670*e01b6f76SAndroid Build Coastguard Worker }
1671*e01b6f76SAndroid Build Coastguard Worker
adev_set_voice_volume(struct audio_hw_device * hw_dev,float volume)1672*e01b6f76SAndroid Build Coastguard Worker static int adev_set_voice_volume(struct audio_hw_device *hw_dev, float volume)
1673*e01b6f76SAndroid Build Coastguard Worker {
1674*e01b6f76SAndroid Build Coastguard Worker return -ENOSYS;
1675*e01b6f76SAndroid Build Coastguard Worker }
1676*e01b6f76SAndroid Build Coastguard Worker
adev_set_master_volume(struct audio_hw_device * hw_dev,float volume)1677*e01b6f76SAndroid Build Coastguard Worker static int adev_set_master_volume(struct audio_hw_device *hw_dev, float volume)
1678*e01b6f76SAndroid Build Coastguard Worker {
1679*e01b6f76SAndroid Build Coastguard Worker return -ENOSYS;
1680*e01b6f76SAndroid Build Coastguard Worker }
1681*e01b6f76SAndroid Build Coastguard Worker
adev_set_mode(struct audio_hw_device * hw_dev,audio_mode_t mode)1682*e01b6f76SAndroid Build Coastguard Worker static int adev_set_mode(struct audio_hw_device *hw_dev, audio_mode_t mode)
1683*e01b6f76SAndroid Build Coastguard Worker {
1684*e01b6f76SAndroid Build Coastguard Worker return 0;
1685*e01b6f76SAndroid Build Coastguard Worker }
1686*e01b6f76SAndroid Build Coastguard Worker
adev_set_mic_mute(struct audio_hw_device * hw_dev,bool state)1687*e01b6f76SAndroid Build Coastguard Worker static int adev_set_mic_mute(struct audio_hw_device *hw_dev, bool state)
1688*e01b6f76SAndroid Build Coastguard Worker {
1689*e01b6f76SAndroid Build Coastguard Worker struct audio_device * adev = (struct audio_device *)hw_dev;
1690*e01b6f76SAndroid Build Coastguard Worker device_lock(adev);
1691*e01b6f76SAndroid Build Coastguard Worker adev->mic_muted = state;
1692*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
1693*e01b6f76SAndroid Build Coastguard Worker return -ENOSYS;
1694*e01b6f76SAndroid Build Coastguard Worker }
1695*e01b6f76SAndroid Build Coastguard Worker
adev_get_mic_mute(const struct audio_hw_device * hw_dev,bool * state)1696*e01b6f76SAndroid Build Coastguard Worker static int adev_get_mic_mute(const struct audio_hw_device *hw_dev, bool *state)
1697*e01b6f76SAndroid Build Coastguard Worker {
1698*e01b6f76SAndroid Build Coastguard Worker return -ENOSYS;
1699*e01b6f76SAndroid Build Coastguard Worker }
1700*e01b6f76SAndroid Build Coastguard Worker
adev_create_audio_patch(struct audio_hw_device * dev,unsigned int num_sources,const struct audio_port_config * sources,unsigned int num_sinks,const struct audio_port_config * sinks,audio_patch_handle_t * handle)1701*e01b6f76SAndroid Build Coastguard Worker static int adev_create_audio_patch(struct audio_hw_device *dev,
1702*e01b6f76SAndroid Build Coastguard Worker unsigned int num_sources,
1703*e01b6f76SAndroid Build Coastguard Worker const struct audio_port_config *sources,
1704*e01b6f76SAndroid Build Coastguard Worker unsigned int num_sinks,
1705*e01b6f76SAndroid Build Coastguard Worker const struct audio_port_config *sinks,
1706*e01b6f76SAndroid Build Coastguard Worker audio_patch_handle_t *handle) {
1707*e01b6f76SAndroid Build Coastguard Worker if (num_sources != 1 || num_sinks == 0 || num_sinks > AUDIO_PATCH_PORTS_MAX) {
1708*e01b6f76SAndroid Build Coastguard Worker // Only accept mix->device and device->mix cases. In that case, the number of sources
1709*e01b6f76SAndroid Build Coastguard Worker // must be 1. The number of sinks must be in the range of (0, AUDIO_PATCH_PORTS_MAX].
1710*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1711*e01b6f76SAndroid Build Coastguard Worker }
1712*e01b6f76SAndroid Build Coastguard Worker
1713*e01b6f76SAndroid Build Coastguard Worker if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1714*e01b6f76SAndroid Build Coastguard Worker // If source is a device, the number of sinks should be 1.
1715*e01b6f76SAndroid Build Coastguard Worker if (num_sinks != 1 || sinks[0].type != AUDIO_PORT_TYPE_MIX) {
1716*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1717*e01b6f76SAndroid Build Coastguard Worker }
1718*e01b6f76SAndroid Build Coastguard Worker } else if (sources[0].type == AUDIO_PORT_TYPE_MIX) {
1719*e01b6f76SAndroid Build Coastguard Worker // If source is a mix, all sinks should be device.
1720*e01b6f76SAndroid Build Coastguard Worker for (unsigned int i = 0; i < num_sinks; i++) {
1721*e01b6f76SAndroid Build Coastguard Worker if (sinks[i].type != AUDIO_PORT_TYPE_DEVICE) {
1722*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s() invalid sink type %#x for mix source", __func__, sinks[i].type);
1723*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1724*e01b6f76SAndroid Build Coastguard Worker }
1725*e01b6f76SAndroid Build Coastguard Worker }
1726*e01b6f76SAndroid Build Coastguard Worker } else {
1727*e01b6f76SAndroid Build Coastguard Worker // All other cases are invalid.
1728*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1729*e01b6f76SAndroid Build Coastguard Worker }
1730*e01b6f76SAndroid Build Coastguard Worker
1731*e01b6f76SAndroid Build Coastguard Worker struct audio_device* adev = (struct audio_device*) dev;
1732*e01b6f76SAndroid Build Coastguard Worker bool generatedPatchHandle = false;
1733*e01b6f76SAndroid Build Coastguard Worker device_lock(adev);
1734*e01b6f76SAndroid Build Coastguard Worker if (*handle == AUDIO_PATCH_HANDLE_NONE) {
1735*e01b6f76SAndroid Build Coastguard Worker *handle = ++adev->next_patch_handle;
1736*e01b6f76SAndroid Build Coastguard Worker generatedPatchHandle = true;
1737*e01b6f76SAndroid Build Coastguard Worker }
1738*e01b6f76SAndroid Build Coastguard Worker
1739*e01b6f76SAndroid Build Coastguard Worker int cards[AUDIO_PATCH_PORTS_MAX];
1740*e01b6f76SAndroid Build Coastguard Worker int devices[AUDIO_PATCH_PORTS_MAX];
1741*e01b6f76SAndroid Build Coastguard Worker const struct audio_port_config *port_configs =
1742*e01b6f76SAndroid Build Coastguard Worker sources[0].type == AUDIO_PORT_TYPE_DEVICE ? sources : sinks;
1743*e01b6f76SAndroid Build Coastguard Worker int num_configs = 0;
1744*e01b6f76SAndroid Build Coastguard Worker audio_io_handle_t io_handle = 0;
1745*e01b6f76SAndroid Build Coastguard Worker bool wasStandby = true;
1746*e01b6f76SAndroid Build Coastguard Worker int direction = PCM_OUT;
1747*e01b6f76SAndroid Build Coastguard Worker audio_patch_handle_t *patch_handle = NULL;
1748*e01b6f76SAndroid Build Coastguard Worker struct listnode *alsa_devices = NULL;
1749*e01b6f76SAndroid Build Coastguard Worker struct stream_lock *lock = NULL;
1750*e01b6f76SAndroid Build Coastguard Worker struct pcm_config *config = NULL;
1751*e01b6f76SAndroid Build Coastguard Worker struct stream_in *in = NULL;
1752*e01b6f76SAndroid Build Coastguard Worker struct stream_out *out = NULL;
1753*e01b6f76SAndroid Build Coastguard Worker bool is_bit_perfect = false;
1754*e01b6f76SAndroid Build Coastguard Worker
1755*e01b6f76SAndroid Build Coastguard Worker unsigned int num_saved_devices = 0;
1756*e01b6f76SAndroid Build Coastguard Worker int saved_cards[AUDIO_PATCH_PORTS_MAX];
1757*e01b6f76SAndroid Build Coastguard Worker int saved_devices[AUDIO_PATCH_PORTS_MAX];
1758*e01b6f76SAndroid Build Coastguard Worker
1759*e01b6f76SAndroid Build Coastguard Worker struct listnode *node;
1760*e01b6f76SAndroid Build Coastguard Worker
1761*e01b6f76SAndroid Build Coastguard Worker // Only handle patches for mix->devices and device->mix case.
1762*e01b6f76SAndroid Build Coastguard Worker if (sources[0].type == AUDIO_PORT_TYPE_DEVICE) {
1763*e01b6f76SAndroid Build Coastguard Worker in = adev_get_stream_in_by_io_handle_l(adev, sinks[0].ext.mix.handle);
1764*e01b6f76SAndroid Build Coastguard Worker if (in == NULL) {
1765*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s()can not find stream with handle(%d)", __func__, sinks[0].ext.mix.handle);
1766*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
1767*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1768*e01b6f76SAndroid Build Coastguard Worker }
1769*e01b6f76SAndroid Build Coastguard Worker
1770*e01b6f76SAndroid Build Coastguard Worker direction = PCM_IN;
1771*e01b6f76SAndroid Build Coastguard Worker wasStandby = in->standby;
1772*e01b6f76SAndroid Build Coastguard Worker io_handle = in->handle;
1773*e01b6f76SAndroid Build Coastguard Worker num_configs = num_sources;
1774*e01b6f76SAndroid Build Coastguard Worker patch_handle = &in->patch_handle;
1775*e01b6f76SAndroid Build Coastguard Worker alsa_devices = &in->alsa_devices;
1776*e01b6f76SAndroid Build Coastguard Worker lock = &in->lock;
1777*e01b6f76SAndroid Build Coastguard Worker config = &in->config;
1778*e01b6f76SAndroid Build Coastguard Worker } else {
1779*e01b6f76SAndroid Build Coastguard Worker out = adev_get_stream_out_by_io_handle_l(adev, sources[0].ext.mix.handle);
1780*e01b6f76SAndroid Build Coastguard Worker if (out == NULL) {
1781*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s()can not find stream with handle(%d)", __func__, sources[0].ext.mix.handle);
1782*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
1783*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1784*e01b6f76SAndroid Build Coastguard Worker }
1785*e01b6f76SAndroid Build Coastguard Worker
1786*e01b6f76SAndroid Build Coastguard Worker direction = PCM_OUT;
1787*e01b6f76SAndroid Build Coastguard Worker wasStandby = out->standby;
1788*e01b6f76SAndroid Build Coastguard Worker io_handle = out->handle;
1789*e01b6f76SAndroid Build Coastguard Worker num_configs = num_sinks;
1790*e01b6f76SAndroid Build Coastguard Worker patch_handle = &out->patch_handle;
1791*e01b6f76SAndroid Build Coastguard Worker alsa_devices = &out->alsa_devices;
1792*e01b6f76SAndroid Build Coastguard Worker lock = &out->lock;
1793*e01b6f76SAndroid Build Coastguard Worker config = &out->config;
1794*e01b6f76SAndroid Build Coastguard Worker is_bit_perfect = out->is_bit_perfect;
1795*e01b6f76SAndroid Build Coastguard Worker }
1796*e01b6f76SAndroid Build Coastguard Worker
1797*e01b6f76SAndroid Build Coastguard Worker // Check if the patch handle match the recorded one if a valid patch handle is passed.
1798*e01b6f76SAndroid Build Coastguard Worker if (!generatedPatchHandle && *patch_handle != *handle) {
1799*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s() the patch handle(%d) does not match recorded one(%d) for stream "
1800*e01b6f76SAndroid Build Coastguard Worker "with handle(%d) when creating audio patch",
1801*e01b6f76SAndroid Build Coastguard Worker __func__, *handle, *patch_handle, io_handle);
1802*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
1803*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1804*e01b6f76SAndroid Build Coastguard Worker }
1805*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
1806*e01b6f76SAndroid Build Coastguard Worker
1807*e01b6f76SAndroid Build Coastguard Worker for (unsigned int i = 0; i < num_configs; ++i) {
1808*e01b6f76SAndroid Build Coastguard Worker if (!parse_card_device_params(port_configs[i].ext.device.address, &cards[i], &devices[i])) {
1809*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s, failed to parse card and device %s",
1810*e01b6f76SAndroid Build Coastguard Worker __func__, port_configs[i].ext.device.address);
1811*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1812*e01b6f76SAndroid Build Coastguard Worker }
1813*e01b6f76SAndroid Build Coastguard Worker }
1814*e01b6f76SAndroid Build Coastguard Worker
1815*e01b6f76SAndroid Build Coastguard Worker stream_lock(lock);
1816*e01b6f76SAndroid Build Coastguard Worker list_for_each (node, alsa_devices) {
1817*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info =
1818*e01b6f76SAndroid Build Coastguard Worker node_to_item(node, struct alsa_device_info, list_node);
1819*e01b6f76SAndroid Build Coastguard Worker saved_cards[num_saved_devices] = device_info->profile.card;
1820*e01b6f76SAndroid Build Coastguard Worker saved_devices[num_saved_devices++] = device_info->profile.device;
1821*e01b6f76SAndroid Build Coastguard Worker }
1822*e01b6f76SAndroid Build Coastguard Worker
1823*e01b6f76SAndroid Build Coastguard Worker if (are_devices_the_same(
1824*e01b6f76SAndroid Build Coastguard Worker num_configs, cards, devices, num_saved_devices, saved_cards, saved_devices)) {
1825*e01b6f76SAndroid Build Coastguard Worker // The new devices are the same as original ones. No need to update.
1826*e01b6f76SAndroid Build Coastguard Worker stream_unlock(lock);
1827*e01b6f76SAndroid Build Coastguard Worker return 0;
1828*e01b6f76SAndroid Build Coastguard Worker }
1829*e01b6f76SAndroid Build Coastguard Worker
1830*e01b6f76SAndroid Build Coastguard Worker device_lock(adev);
1831*e01b6f76SAndroid Build Coastguard Worker stream_standby_l(alsa_devices, out == NULL ? &in->standby : &out->standby);
1832*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
1833*e01b6f76SAndroid Build Coastguard Worker
1834*e01b6f76SAndroid Build Coastguard Worker // Timestamps:
1835*e01b6f76SAndroid Build Coastguard Worker // Audio timestamps assume continuous PCM frame counts which are maintained
1836*e01b6f76SAndroid Build Coastguard Worker // with the device proxy.transferred variable. Technically it would be better
1837*e01b6f76SAndroid Build Coastguard Worker // associated with in or out stream, not the device; here we save and restore
1838*e01b6f76SAndroid Build Coastguard Worker // using the first alsa device as a simplification.
1839*e01b6f76SAndroid Build Coastguard Worker uint64_t saved_transferred_frames = 0;
1840*e01b6f76SAndroid Build Coastguard Worker struct alsa_device_info *device_info = stream_get_first_alsa_device(alsa_devices);
1841*e01b6f76SAndroid Build Coastguard Worker if (device_info != NULL) saved_transferred_frames = device_info->proxy.transferred;
1842*e01b6f76SAndroid Build Coastguard Worker
1843*e01b6f76SAndroid Build Coastguard Worker int ret = stream_set_new_devices(
1844*e01b6f76SAndroid Build Coastguard Worker config, alsa_devices, num_configs, cards, devices, direction, is_bit_perfect);
1845*e01b6f76SAndroid Build Coastguard Worker
1846*e01b6f76SAndroid Build Coastguard Worker if (ret != 0) {
1847*e01b6f76SAndroid Build Coastguard Worker *handle = generatedPatchHandle ? AUDIO_PATCH_HANDLE_NONE : *handle;
1848*e01b6f76SAndroid Build Coastguard Worker stream_set_new_devices(
1849*e01b6f76SAndroid Build Coastguard Worker config, alsa_devices, num_saved_devices, saved_cards, saved_devices, direction,
1850*e01b6f76SAndroid Build Coastguard Worker is_bit_perfect);
1851*e01b6f76SAndroid Build Coastguard Worker } else {
1852*e01b6f76SAndroid Build Coastguard Worker *patch_handle = *handle;
1853*e01b6f76SAndroid Build Coastguard Worker }
1854*e01b6f76SAndroid Build Coastguard Worker
1855*e01b6f76SAndroid Build Coastguard Worker // Timestamps: Restore transferred frames.
1856*e01b6f76SAndroid Build Coastguard Worker if (saved_transferred_frames != 0) {
1857*e01b6f76SAndroid Build Coastguard Worker device_info = stream_get_first_alsa_device(alsa_devices);
1858*e01b6f76SAndroid Build Coastguard Worker if (device_info != NULL) device_info->proxy.transferred = saved_transferred_frames;
1859*e01b6f76SAndroid Build Coastguard Worker }
1860*e01b6f76SAndroid Build Coastguard Worker
1861*e01b6f76SAndroid Build Coastguard Worker if (!wasStandby) {
1862*e01b6f76SAndroid Build Coastguard Worker device_lock(adev);
1863*e01b6f76SAndroid Build Coastguard Worker if (in != NULL) {
1864*e01b6f76SAndroid Build Coastguard Worker ret = start_input_stream(in);
1865*e01b6f76SAndroid Build Coastguard Worker }
1866*e01b6f76SAndroid Build Coastguard Worker if (out != NULL) {
1867*e01b6f76SAndroid Build Coastguard Worker ret = start_output_stream(out);
1868*e01b6f76SAndroid Build Coastguard Worker }
1869*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
1870*e01b6f76SAndroid Build Coastguard Worker }
1871*e01b6f76SAndroid Build Coastguard Worker stream_unlock(lock);
1872*e01b6f76SAndroid Build Coastguard Worker return ret;
1873*e01b6f76SAndroid Build Coastguard Worker }
1874*e01b6f76SAndroid Build Coastguard Worker
adev_release_audio_patch(struct audio_hw_device * dev,audio_patch_handle_t patch_handle)1875*e01b6f76SAndroid Build Coastguard Worker static int adev_release_audio_patch(struct audio_hw_device *dev,
1876*e01b6f76SAndroid Build Coastguard Worker audio_patch_handle_t patch_handle)
1877*e01b6f76SAndroid Build Coastguard Worker {
1878*e01b6f76SAndroid Build Coastguard Worker struct audio_device* adev = (struct audio_device*) dev;
1879*e01b6f76SAndroid Build Coastguard Worker
1880*e01b6f76SAndroid Build Coastguard Worker device_lock(adev);
1881*e01b6f76SAndroid Build Coastguard Worker struct stream_out *out = adev_get_stream_out_by_patch_handle_l(adev, patch_handle);
1882*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
1883*e01b6f76SAndroid Build Coastguard Worker if (out != NULL) {
1884*e01b6f76SAndroid Build Coastguard Worker stream_lock(&out->lock);
1885*e01b6f76SAndroid Build Coastguard Worker device_lock(adev);
1886*e01b6f76SAndroid Build Coastguard Worker stream_standby_l(&out->alsa_devices, &out->standby);
1887*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
1888*e01b6f76SAndroid Build Coastguard Worker out->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1889*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&out->lock);
1890*e01b6f76SAndroid Build Coastguard Worker return 0;
1891*e01b6f76SAndroid Build Coastguard Worker }
1892*e01b6f76SAndroid Build Coastguard Worker
1893*e01b6f76SAndroid Build Coastguard Worker device_lock(adev);
1894*e01b6f76SAndroid Build Coastguard Worker struct stream_in *in = adev_get_stream_in_by_patch_handle_l(adev, patch_handle);
1895*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
1896*e01b6f76SAndroid Build Coastguard Worker if (in != NULL) {
1897*e01b6f76SAndroid Build Coastguard Worker stream_lock(&in->lock);
1898*e01b6f76SAndroid Build Coastguard Worker device_lock(adev);
1899*e01b6f76SAndroid Build Coastguard Worker stream_standby_l(&in->alsa_devices, &in->standby);
1900*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
1901*e01b6f76SAndroid Build Coastguard Worker in->patch_handle = AUDIO_PATCH_HANDLE_NONE;
1902*e01b6f76SAndroid Build Coastguard Worker stream_unlock(&in->lock);
1903*e01b6f76SAndroid Build Coastguard Worker return 0;
1904*e01b6f76SAndroid Build Coastguard Worker }
1905*e01b6f76SAndroid Build Coastguard Worker
1906*e01b6f76SAndroid Build Coastguard Worker ALOGE("%s cannot find stream with patch handle as %d", __func__, patch_handle);
1907*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1908*e01b6f76SAndroid Build Coastguard Worker }
1909*e01b6f76SAndroid Build Coastguard Worker
adev_get_audio_port(struct audio_hw_device * dev,struct audio_port * port)1910*e01b6f76SAndroid Build Coastguard Worker static int adev_get_audio_port(struct audio_hw_device *dev, struct audio_port *port)
1911*e01b6f76SAndroid Build Coastguard Worker {
1912*e01b6f76SAndroid Build Coastguard Worker if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1913*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1914*e01b6f76SAndroid Build Coastguard Worker }
1915*e01b6f76SAndroid Build Coastguard Worker
1916*e01b6f76SAndroid Build Coastguard Worker alsa_device_profile profile;
1917*e01b6f76SAndroid Build Coastguard Worker const bool is_output = audio_is_output_device(port->ext.device.type);
1918*e01b6f76SAndroid Build Coastguard Worker profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1919*e01b6f76SAndroid Build Coastguard Worker if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1920*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1921*e01b6f76SAndroid Build Coastguard Worker }
1922*e01b6f76SAndroid Build Coastguard Worker
1923*e01b6f76SAndroid Build Coastguard Worker if (!profile_read_device_info(&profile)) {
1924*e01b6f76SAndroid Build Coastguard Worker return -ENOENT;
1925*e01b6f76SAndroid Build Coastguard Worker }
1926*e01b6f76SAndroid Build Coastguard Worker
1927*e01b6f76SAndroid Build Coastguard Worker port->num_formats = 0;;
1928*e01b6f76SAndroid Build Coastguard Worker for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_FORMATS) &&
1929*e01b6f76SAndroid Build Coastguard Worker profile.formats[i] != 0; ++i) {
1930*e01b6f76SAndroid Build Coastguard Worker audio_format_t format = audio_format_from(profile.formats[i]);
1931*e01b6f76SAndroid Build Coastguard Worker if (format != AUDIO_FORMAT_INVALID) {
1932*e01b6f76SAndroid Build Coastguard Worker port->formats[port->num_formats++] = format;
1933*e01b6f76SAndroid Build Coastguard Worker }
1934*e01b6f76SAndroid Build Coastguard Worker }
1935*e01b6f76SAndroid Build Coastguard Worker
1936*e01b6f76SAndroid Build Coastguard Worker port->num_sample_rates = populate_sample_rates_from_profile(&profile, port->sample_rates);
1937*e01b6f76SAndroid Build Coastguard Worker port->num_channel_masks = populate_channel_mask_from_profile(
1938*e01b6f76SAndroid Build Coastguard Worker &profile, is_output, port->channel_masks);
1939*e01b6f76SAndroid Build Coastguard Worker
1940*e01b6f76SAndroid Build Coastguard Worker return 0;
1941*e01b6f76SAndroid Build Coastguard Worker }
1942*e01b6f76SAndroid Build Coastguard Worker
adev_get_audio_port_v7(struct audio_hw_device * dev,struct audio_port_v7 * port)1943*e01b6f76SAndroid Build Coastguard Worker static int adev_get_audio_port_v7(struct audio_hw_device *dev, struct audio_port_v7 *port)
1944*e01b6f76SAndroid Build Coastguard Worker {
1945*e01b6f76SAndroid Build Coastguard Worker if (port->type != AUDIO_PORT_TYPE_DEVICE) {
1946*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1947*e01b6f76SAndroid Build Coastguard Worker }
1948*e01b6f76SAndroid Build Coastguard Worker
1949*e01b6f76SAndroid Build Coastguard Worker alsa_device_profile profile;
1950*e01b6f76SAndroid Build Coastguard Worker const bool is_output = audio_is_output_device(port->ext.device.type);
1951*e01b6f76SAndroid Build Coastguard Worker profile_init(&profile, is_output ? PCM_OUT : PCM_IN);
1952*e01b6f76SAndroid Build Coastguard Worker if (!parse_card_device_params(port->ext.device.address, &profile.card, &profile.device)) {
1953*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
1954*e01b6f76SAndroid Build Coastguard Worker }
1955*e01b6f76SAndroid Build Coastguard Worker
1956*e01b6f76SAndroid Build Coastguard Worker if (!profile_read_device_info(&profile)) {
1957*e01b6f76SAndroid Build Coastguard Worker return -ENOENT;
1958*e01b6f76SAndroid Build Coastguard Worker }
1959*e01b6f76SAndroid Build Coastguard Worker
1960*e01b6f76SAndroid Build Coastguard Worker audio_channel_mask_t channel_masks[AUDIO_PORT_MAX_CHANNEL_MASKS];
1961*e01b6f76SAndroid Build Coastguard Worker unsigned int num_channel_masks = populate_channel_mask_from_profile(
1962*e01b6f76SAndroid Build Coastguard Worker &profile, is_output, channel_masks);
1963*e01b6f76SAndroid Build Coastguard Worker unsigned int sample_rates[AUDIO_PORT_MAX_SAMPLING_RATES];
1964*e01b6f76SAndroid Build Coastguard Worker const unsigned int num_sample_rates =
1965*e01b6f76SAndroid Build Coastguard Worker populate_sample_rates_from_profile(&profile, sample_rates);
1966*e01b6f76SAndroid Build Coastguard Worker port->num_audio_profiles = 0;;
1967*e01b6f76SAndroid Build Coastguard Worker for (size_t i = 0; i < min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) &&
1968*e01b6f76SAndroid Build Coastguard Worker profile.formats[i] != 0; ++i) {
1969*e01b6f76SAndroid Build Coastguard Worker audio_format_t format = audio_format_from(profile.formats[i]);
1970*e01b6f76SAndroid Build Coastguard Worker if (format == AUDIO_FORMAT_INVALID) {
1971*e01b6f76SAndroid Build Coastguard Worker continue;
1972*e01b6f76SAndroid Build Coastguard Worker }
1973*e01b6f76SAndroid Build Coastguard Worker const unsigned int j = port->num_audio_profiles++;
1974*e01b6f76SAndroid Build Coastguard Worker port->audio_profiles[j].format = format;
1975*e01b6f76SAndroid Build Coastguard Worker port->audio_profiles[j].num_sample_rates = num_sample_rates;
1976*e01b6f76SAndroid Build Coastguard Worker memcpy(port->audio_profiles[j].sample_rates,
1977*e01b6f76SAndroid Build Coastguard Worker sample_rates,
1978*e01b6f76SAndroid Build Coastguard Worker num_sample_rates * sizeof(unsigned int));
1979*e01b6f76SAndroid Build Coastguard Worker port->audio_profiles[j].num_channel_masks = num_channel_masks;
1980*e01b6f76SAndroid Build Coastguard Worker memcpy(port->audio_profiles[j].channel_masks,
1981*e01b6f76SAndroid Build Coastguard Worker channel_masks,
1982*e01b6f76SAndroid Build Coastguard Worker num_channel_masks* sizeof(audio_channel_mask_t));
1983*e01b6f76SAndroid Build Coastguard Worker }
1984*e01b6f76SAndroid Build Coastguard Worker
1985*e01b6f76SAndroid Build Coastguard Worker return 0;
1986*e01b6f76SAndroid Build Coastguard Worker }
1987*e01b6f76SAndroid Build Coastguard Worker
adev_dump(const struct audio_hw_device * device,int fd)1988*e01b6f76SAndroid Build Coastguard Worker static int adev_dump(const struct audio_hw_device *device, int fd)
1989*e01b6f76SAndroid Build Coastguard Worker {
1990*e01b6f76SAndroid Build Coastguard Worker dprintf(fd, "\nUSB audio module:\n");
1991*e01b6f76SAndroid Build Coastguard Worker
1992*e01b6f76SAndroid Build Coastguard Worker struct audio_device* adev = (struct audio_device*)device;
1993*e01b6f76SAndroid Build Coastguard Worker const int kNumRetries = 3;
1994*e01b6f76SAndroid Build Coastguard Worker const int kSleepTimeMS = 500;
1995*e01b6f76SAndroid Build Coastguard Worker
1996*e01b6f76SAndroid Build Coastguard Worker // use device_try_lock() in case we dumpsys during a deadlock
1997*e01b6f76SAndroid Build Coastguard Worker int retry = kNumRetries;
1998*e01b6f76SAndroid Build Coastguard Worker while (retry > 0 && device_try_lock(adev) != 0) {
1999*e01b6f76SAndroid Build Coastguard Worker sleep(kSleepTimeMS);
2000*e01b6f76SAndroid Build Coastguard Worker retry--;
2001*e01b6f76SAndroid Build Coastguard Worker }
2002*e01b6f76SAndroid Build Coastguard Worker
2003*e01b6f76SAndroid Build Coastguard Worker if (retry > 0) {
2004*e01b6f76SAndroid Build Coastguard Worker if (list_empty(&adev->output_stream_list)) {
2005*e01b6f76SAndroid Build Coastguard Worker dprintf(fd, " No output streams.\n");
2006*e01b6f76SAndroid Build Coastguard Worker } else {
2007*e01b6f76SAndroid Build Coastguard Worker struct listnode* node;
2008*e01b6f76SAndroid Build Coastguard Worker list_for_each(node, &adev->output_stream_list) {
2009*e01b6f76SAndroid Build Coastguard Worker struct audio_stream* stream =
2010*e01b6f76SAndroid Build Coastguard Worker (struct audio_stream *)node_to_item(node, struct stream_out, list_node);
2011*e01b6f76SAndroid Build Coastguard Worker out_dump(stream, fd);
2012*e01b6f76SAndroid Build Coastguard Worker }
2013*e01b6f76SAndroid Build Coastguard Worker }
2014*e01b6f76SAndroid Build Coastguard Worker
2015*e01b6f76SAndroid Build Coastguard Worker if (list_empty(&adev->input_stream_list)) {
2016*e01b6f76SAndroid Build Coastguard Worker dprintf(fd, "\n No input streams.\n");
2017*e01b6f76SAndroid Build Coastguard Worker } else {
2018*e01b6f76SAndroid Build Coastguard Worker struct listnode* node;
2019*e01b6f76SAndroid Build Coastguard Worker list_for_each(node, &adev->input_stream_list) {
2020*e01b6f76SAndroid Build Coastguard Worker struct audio_stream* stream =
2021*e01b6f76SAndroid Build Coastguard Worker (struct audio_stream *)node_to_item(node, struct stream_in, list_node);
2022*e01b6f76SAndroid Build Coastguard Worker in_dump(stream, fd);
2023*e01b6f76SAndroid Build Coastguard Worker }
2024*e01b6f76SAndroid Build Coastguard Worker }
2025*e01b6f76SAndroid Build Coastguard Worker
2026*e01b6f76SAndroid Build Coastguard Worker device_unlock(adev);
2027*e01b6f76SAndroid Build Coastguard Worker } else {
2028*e01b6f76SAndroid Build Coastguard Worker // Couldn't lock
2029*e01b6f76SAndroid Build Coastguard Worker dprintf(fd, " Could not obtain device lock.\n");
2030*e01b6f76SAndroid Build Coastguard Worker }
2031*e01b6f76SAndroid Build Coastguard Worker
2032*e01b6f76SAndroid Build Coastguard Worker return 0;
2033*e01b6f76SAndroid Build Coastguard Worker }
2034*e01b6f76SAndroid Build Coastguard Worker
adev_close(hw_device_t * device)2035*e01b6f76SAndroid Build Coastguard Worker static int adev_close(hw_device_t *device)
2036*e01b6f76SAndroid Build Coastguard Worker {
2037*e01b6f76SAndroid Build Coastguard Worker free(device);
2038*e01b6f76SAndroid Build Coastguard Worker
2039*e01b6f76SAndroid Build Coastguard Worker return 0;
2040*e01b6f76SAndroid Build Coastguard Worker }
2041*e01b6f76SAndroid Build Coastguard Worker
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)2042*e01b6f76SAndroid Build Coastguard Worker static int adev_open(const hw_module_t* module, const char* name, hw_device_t** device)
2043*e01b6f76SAndroid Build Coastguard Worker {
2044*e01b6f76SAndroid Build Coastguard Worker if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
2045*e01b6f76SAndroid Build Coastguard Worker return -EINVAL;
2046*e01b6f76SAndroid Build Coastguard Worker
2047*e01b6f76SAndroid Build Coastguard Worker struct audio_device *adev = calloc(1, sizeof(struct audio_device));
2048*e01b6f76SAndroid Build Coastguard Worker if (!adev)
2049*e01b6f76SAndroid Build Coastguard Worker return -ENOMEM;
2050*e01b6f76SAndroid Build Coastguard Worker
2051*e01b6f76SAndroid Build Coastguard Worker pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2052*e01b6f76SAndroid Build Coastguard Worker
2053*e01b6f76SAndroid Build Coastguard Worker list_init(&adev->output_stream_list);
2054*e01b6f76SAndroid Build Coastguard Worker list_init(&adev->input_stream_list);
2055*e01b6f76SAndroid Build Coastguard Worker
2056*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
2057*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_3_2;
2058*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.common.module = (struct hw_module_t *)module;
2059*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.common.close = adev_close;
2060*e01b6f76SAndroid Build Coastguard Worker
2061*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.init_check = adev_init_check;
2062*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.set_voice_volume = adev_set_voice_volume;
2063*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.set_master_volume = adev_set_master_volume;
2064*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.set_mode = adev_set_mode;
2065*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.set_mic_mute = adev_set_mic_mute;
2066*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.get_mic_mute = adev_get_mic_mute;
2067*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.set_parameters = adev_set_parameters;
2068*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.get_parameters = adev_get_parameters;
2069*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
2070*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.open_output_stream = adev_open_output_stream;
2071*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.close_output_stream = adev_close_output_stream;
2072*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.open_input_stream = adev_open_input_stream;
2073*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.close_input_stream = adev_close_input_stream;
2074*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.create_audio_patch = adev_create_audio_patch;
2075*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.release_audio_patch = adev_release_audio_patch;
2076*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.get_audio_port = adev_get_audio_port;
2077*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.get_audio_port_v7 = adev_get_audio_port_v7;
2078*e01b6f76SAndroid Build Coastguard Worker adev->hw_device.dump = adev_dump;
2079*e01b6f76SAndroid Build Coastguard Worker
2080*e01b6f76SAndroid Build Coastguard Worker *device = &adev->hw_device.common;
2081*e01b6f76SAndroid Build Coastguard Worker
2082*e01b6f76SAndroid Build Coastguard Worker return 0;
2083*e01b6f76SAndroid Build Coastguard Worker }
2084*e01b6f76SAndroid Build Coastguard Worker
2085*e01b6f76SAndroid Build Coastguard Worker static struct hw_module_methods_t hal_module_methods = {
2086*e01b6f76SAndroid Build Coastguard Worker .open = adev_open,
2087*e01b6f76SAndroid Build Coastguard Worker };
2088*e01b6f76SAndroid Build Coastguard Worker
2089*e01b6f76SAndroid Build Coastguard Worker struct audio_module HAL_MODULE_INFO_SYM = {
2090*e01b6f76SAndroid Build Coastguard Worker .common = {
2091*e01b6f76SAndroid Build Coastguard Worker .tag = HARDWARE_MODULE_TAG,
2092*e01b6f76SAndroid Build Coastguard Worker .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2093*e01b6f76SAndroid Build Coastguard Worker .hal_api_version = HARDWARE_HAL_API_VERSION,
2094*e01b6f76SAndroid Build Coastguard Worker .id = AUDIO_HARDWARE_MODULE_ID,
2095*e01b6f76SAndroid Build Coastguard Worker .name = "USB audio HW HAL",
2096*e01b6f76SAndroid Build Coastguard Worker .author = "The Android Open Source Project",
2097*e01b6f76SAndroid Build Coastguard Worker .methods = &hal_module_methods,
2098*e01b6f76SAndroid Build Coastguard Worker },
2099*e01b6f76SAndroid Build Coastguard Worker };
2100