blob: 2c9163f488b0ba3326ab881ad10015880602c363 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002 * Copyright (C) 2013-2014 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_primary"
18/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070019/*#define VERY_VERY_VERBOSE_LOGGING*/
20#ifdef VERY_VERY_VERBOSE_LOGGING
21#define ALOGVV ALOGV
22#else
23#define ALOGVV(a...) do { } while(0)
24#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080025
26#include <errno.h>
27#include <pthread.h>
28#include <stdint.h>
29#include <sys/time.h>
30#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080031#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070032#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070033#include <sys/resource.h>
34#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080035
36#include <cutils/log.h>
37#include <cutils/str_parms.h>
38#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070039#include <cutils/atomic.h>
40#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080041
Eric Laurentb23d5282013-05-14 15:27:20 -070042#include <hardware/audio_effect.h>
Andy Hung31aca912014-03-20 17:14:59 -070043#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070044#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <audio_effects/effect_aec.h>
46#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080047#include "audio_hw.h"
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -080048#include "audio_extn.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070049#include "platform_api.h"
50#include <platform.h>
Vineeta Srivastava4b89e372014-06-19 14:21:42 -070051#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080052
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070053#include "sound/compress_params.h"
54
55#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
56#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
57/* ToDo: Check and update a proper value in msec */
58#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
59#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
60
Glenn Kasten4f993392014-05-14 07:30:48 -070061static unsigned int configured_low_latency_capture_period_size =
62 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
63
Andy Hung31aca912014-03-20 17:14:59 -070064/* This constant enables extended precision handling.
65 * TODO The flag is off until more testing is done.
66 */
67static const bool k_enable_extended_precision = false;
68
Eric Laurentb23d5282013-05-14 15:27:20 -070069struct pcm_config pcm_config_deep_buffer = {
70 .channels = 2,
71 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
72 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
73 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
74 .format = PCM_FORMAT_S16_LE,
75 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
76 .stop_threshold = INT_MAX,
77 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
78};
79
80struct pcm_config pcm_config_low_latency = {
81 .channels = 2,
82 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
83 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
84 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
85 .format = PCM_FORMAT_S16_LE,
86 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
87 .stop_threshold = INT_MAX,
88 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
89};
90
91struct pcm_config pcm_config_hdmi_multi = {
92 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
93 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
94 .period_size = HDMI_MULTI_PERIOD_SIZE,
95 .period_count = HDMI_MULTI_PERIOD_COUNT,
96 .format = PCM_FORMAT_S16_LE,
97 .start_threshold = 0,
98 .stop_threshold = INT_MAX,
99 .avail_min = 0,
100};
101
102struct pcm_config pcm_config_audio_capture = {
103 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700104 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
105 .format = PCM_FORMAT_S16_LE,
106};
107
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700108const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700109 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
110 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
111 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700112 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
113
Eric Laurentb23d5282013-05-14 15:27:20 -0700114 [USECASE_AUDIO_RECORD] = "audio-record",
115 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700116
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800117 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
118 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700119
Eric Laurentb23d5282013-05-14 15:27:20 -0700120 [USECASE_VOICE_CALL] = "voice-call",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700121 [USECASE_VOICE2_CALL] = "voice2-call",
122 [USECASE_VOLTE_CALL] = "volte-call",
123 [USECASE_QCHAT_CALL] = "qchat-call",
124 [USECASE_VOWLAN_CALL] = "vowlan-call",
Eric Laurentb23d5282013-05-14 15:27:20 -0700125};
126
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800127
128#define STRING_TO_ENUM(string) { #string, string }
129
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800130struct string_to_enum {
131 const char *name;
132 uint32_t value;
133};
134
135static const struct string_to_enum out_channels_name_to_enum_table[] = {
136 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
137 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
138 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
139};
140
Haynes Mathew George5191a852013-09-11 14:19:36 -0700141static int set_voice_volume_l(struct audio_device *adev, float volume);
142
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700143static bool is_supported_format(audio_format_t format)
144{
Eric Laurent8251ac82014-07-23 11:00:25 -0700145 switch (format) {
146 case AUDIO_FORMAT_MP3:
147 case AUDIO_FORMAT_AAC_LC:
148 case AUDIO_FORMAT_AAC_HE_V1:
149 case AUDIO_FORMAT_AAC_HE_V2:
150 return true;
151 default:
152 break;
153 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700154 return false;
155}
156
157static int get_snd_codec_id(audio_format_t format)
158{
159 int id = 0;
160
Eric Laurent8251ac82014-07-23 11:00:25 -0700161 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700162 case AUDIO_FORMAT_MP3:
163 id = SND_AUDIOCODEC_MP3;
164 break;
165 case AUDIO_FORMAT_AAC:
166 id = SND_AUDIOCODEC_AAC;
167 break;
168 default:
169 ALOGE("%s: Unsupported audio format", __func__);
170 }
171
172 return id;
173}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800174
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700175int pcm_ioctl(void *pcm, int request, ...)
176{
177 va_list ap;
178 void * arg;
179 int pcm_fd = *(int*)pcm;
180
181 va_start(ap, request);
182 arg = va_arg(ap, void *);
183 va_end(ap);
184
185 return ioctl(pcm_fd, request, arg);
186}
187
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800188int enable_audio_route(struct audio_device *adev,
189 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800190{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700191 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800192 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800193
194 if (usecase == NULL)
195 return -EINVAL;
196
197 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
198
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800199 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700200 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800201 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700202 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800203
204 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500205 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700206 ALOGV("%s: apply and update mixer path: %s", __func__, mixer_path);
207 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800208
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800209 ALOGV("%s: exit", __func__);
210 return 0;
211}
212
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800213int disable_audio_route(struct audio_device *adev,
214 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800215{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700216 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800217 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800218
219 if (usecase == NULL)
220 return -EINVAL;
221
222 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700223 if (usecase->type == PCM_CAPTURE)
224 snd_device = usecase->in_snd_device;
225 else
226 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800227 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500228 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700229 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
230 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800231
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800232 ALOGV("%s: exit", __func__);
233 return 0;
234}
235
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800236int enable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700237 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800238{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800239 if (snd_device < SND_DEVICE_MIN ||
240 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800241 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800242 return -EINVAL;
243 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700244
245 adev->snd_dev_ref_cnt[snd_device]++;
246 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700247 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700248 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249 return 0;
250 }
251
Eric Laurentb23d5282013-05-14 15:27:20 -0700252 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700253 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800254 return -EINVAL;
255 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800256
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700257 const char * dev_path = platform_get_snd_device_name(snd_device);
258 ALOGV("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
259 audio_route_apply_and_update_path(adev->audio_route, dev_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700260
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800261 return 0;
262}
263
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800264int disable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700265 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800266{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800267 if (snd_device < SND_DEVICE_MIN ||
268 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800269 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800270 return -EINVAL;
271 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700272 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
273 ALOGE("%s: device ref cnt is already 0", __func__);
274 return -EINVAL;
275 }
276 adev->snd_dev_ref_cnt[snd_device]--;
277 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700278 const char * dev_path = platform_get_snd_device_name(snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700279 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700280 snd_device, dev_path);
281 audio_route_reset_and_update_path(adev->audio_route, dev_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700282 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800283 return 0;
284}
285
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700286static void check_usecases_codec_backend(struct audio_device *adev,
287 struct audio_usecase *uc_info,
288 snd_device_t snd_device)
289{
290 struct listnode *node;
291 struct audio_usecase *usecase;
292 bool switch_device[AUDIO_USECASE_MAX];
293 int i, num_uc_to_switch = 0;
294
295 /*
296 * This function is to make sure that all the usecases that are active on
297 * the hardware codec backend are always routed to any one device that is
298 * handled by the hardware codec.
299 * For example, if low-latency and deep-buffer usecases are currently active
300 * on speaker and out_set_parameters(headset) is received on low-latency
301 * output, then we have to make sure deep-buffer is also switched to headset,
302 * because of the limitation that both the devices cannot be enabled
303 * at the same time as they share the same backend.
304 */
305 /* Disable all the usecases on the shared backend other than the
306 specified usecase */
307 for (i = 0; i < AUDIO_USECASE_MAX; i++)
308 switch_device[i] = false;
309
310 list_for_each(node, &adev->usecase_list) {
311 usecase = node_to_item(node, struct audio_usecase, list);
312 if (usecase->type != PCM_CAPTURE &&
313 usecase != uc_info &&
314 usecase->out_snd_device != snd_device &&
315 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
316 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
317 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700318 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700319 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700320 switch_device[usecase->id] = true;
321 num_uc_to_switch++;
322 }
323 }
324
325 if (num_uc_to_switch) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700326 list_for_each(node, &adev->usecase_list) {
327 usecase = node_to_item(node, struct audio_usecase, list);
328 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700329 disable_snd_device(adev, usecase->out_snd_device);
sangwon.jeon866d5ff2013-10-17 21:42:50 +0900330 }
331 }
332
333 list_for_each(node, &adev->usecase_list) {
334 usecase = node_to_item(node, struct audio_usecase, list);
335 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700336 enable_snd_device(adev, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700337 }
338 }
339
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700340 /* Re-route all the usecases on the shared backend other than the
341 specified usecase to new snd devices */
342 list_for_each(node, &adev->usecase_list) {
343 usecase = node_to_item(node, struct audio_usecase, list);
344 /* Update the out_snd_device only before enabling the audio route */
345 if (switch_device[usecase->id] ) {
346 usecase->out_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700347 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700348 }
349 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700350 }
351}
352
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700353static void check_and_route_capture_usecases(struct audio_device *adev,
354 struct audio_usecase *uc_info,
355 snd_device_t snd_device)
356{
357 struct listnode *node;
358 struct audio_usecase *usecase;
359 bool switch_device[AUDIO_USECASE_MAX];
360 int i, num_uc_to_switch = 0;
361
362 /*
363 * This function is to make sure that all the active capture usecases
364 * are always routed to the same input sound device.
365 * For example, if audio-record and voice-call usecases are currently
366 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
367 * is received for voice call then we have to make sure that audio-record
368 * usecase is also switched to earpiece i.e. voice-dmic-ef,
369 * because of the limitation that two devices cannot be enabled
370 * at the same time if they share the same backend.
371 */
372 for (i = 0; i < AUDIO_USECASE_MAX; i++)
373 switch_device[i] = false;
374
375 list_for_each(node, &adev->usecase_list) {
376 usecase = node_to_item(node, struct audio_usecase, list);
377 if (usecase->type != PCM_PLAYBACK &&
378 usecase != uc_info &&
379 usecase->in_snd_device != snd_device) {
380 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
381 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700382 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700383 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700384 switch_device[usecase->id] = true;
385 num_uc_to_switch++;
386 }
387 }
388
389 if (num_uc_to_switch) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700390 list_for_each(node, &adev->usecase_list) {
391 usecase = node_to_item(node, struct audio_usecase, list);
392 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700393 disable_snd_device(adev, usecase->in_snd_device);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700394 }
395 }
396
397 list_for_each(node, &adev->usecase_list) {
398 usecase = node_to_item(node, struct audio_usecase, list);
399 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700400 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700401 }
402 }
403
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700404 /* Re-route all the usecases on the shared backend other than the
405 specified usecase to new snd devices */
406 list_for_each(node, &adev->usecase_list) {
407 usecase = node_to_item(node, struct audio_usecase, list);
408 /* Update the in_snd_device only before enabling the audio route */
409 if (switch_device[usecase->id] ) {
410 usecase->in_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700411 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700412 }
413 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700414 }
415}
416
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800417/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700418static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800419{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700420 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700421 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800422
423 switch (channels) {
424 /*
425 * Do not handle stereo output in Multi-channel cases
426 * Stereo case is handled in normal playback path
427 */
428 case 6:
429 ALOGV("%s: HDMI supports 5.1", __func__);
430 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
431 break;
432 case 8:
433 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
434 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
435 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
436 break;
437 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700438 ALOGE("HDMI does not support multi channel playback");
439 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800440 break;
441 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700442 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800443}
444
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700445static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
446{
447 struct audio_usecase *usecase;
448 struct listnode *node;
449
450 list_for_each(node, &adev->usecase_list) {
451 usecase = node_to_item(node, struct audio_usecase, list);
452 if (usecase->type == VOICE_CALL) {
453 ALOGV("%s: usecase id %d", __func__, usecase->id);
454 return usecase->id;
455 }
456 }
457 return USECASE_INVALID;
458}
459
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800460struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
461 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700462{
463 struct audio_usecase *usecase;
464 struct listnode *node;
465
466 list_for_each(node, &adev->usecase_list) {
467 usecase = node_to_item(node, struct audio_usecase, list);
468 if (usecase->id == uc_id)
469 return usecase;
470 }
471 return NULL;
472}
473
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800474int select_devices(struct audio_device *adev,
475 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800476{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800477 snd_device_t out_snd_device = SND_DEVICE_NONE;
478 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700479 struct audio_usecase *usecase = NULL;
480 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800481 struct audio_usecase *hfp_usecase = NULL;
482 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800483 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700484 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800485
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700486 usecase = get_usecase_from_list(adev, uc_id);
487 if (usecase == NULL) {
488 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
489 return -EINVAL;
490 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800491
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800492 if ((usecase->type == VOICE_CALL) ||
493 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700494 out_snd_device = platform_get_output_snd_device(adev->platform,
495 usecase->stream.out->devices);
496 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700497 usecase->devices = usecase->stream.out->devices;
498 } else {
499 /*
500 * If the voice call is active, use the sound devices of voice call usecase
501 * so that it would not result any device switch. All the usecases will
502 * be switched to new device when select_devices() is called for voice call
503 * usecase. This is to avoid switching devices for voice call when
504 * check_usecases_codec_backend() is called below.
505 */
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700506 if (voice_is_in_call(adev)) {
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700507 vc_usecase = get_usecase_from_list(adev,
508 get_voice_usecase_id_from_list(adev));
509 if ((vc_usecase != NULL) &&
510 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
511 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700512 in_snd_device = vc_usecase->in_snd_device;
513 out_snd_device = vc_usecase->out_snd_device;
514 }
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800515 } else if (audio_extn_hfp_is_active(adev)) {
516 hfp_ucid = audio_extn_hfp_get_usecase();
517 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
518 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
519 in_snd_device = hfp_usecase->in_snd_device;
520 out_snd_device = hfp_usecase->out_snd_device;
521 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700522 }
523 if (usecase->type == PCM_PLAYBACK) {
524 usecase->devices = usecase->stream.out->devices;
525 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700526 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700527 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700528 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700529 if (usecase->stream.out == adev->primary_output &&
530 adev->active_input &&
531 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
532 select_devices(adev, adev->active_input->usecase);
533 }
534 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700535 } else if (usecase->type == PCM_CAPTURE) {
536 usecase->devices = usecase->stream.in->device;
537 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700538 if (in_snd_device == SND_DEVICE_NONE) {
539 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
540 adev->primary_output && !adev->primary_output->standby) {
Vineeta Srivastava55a12932014-08-11 11:41:18 -0700541 in_snd_device = platform_get_input_snd_device(adev->platform,
542 adev->primary_output->devices);
543 } else {
544 in_snd_device = platform_get_input_snd_device(adev->platform,
545 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700546 }
547 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700548 }
549 }
550
551 if (out_snd_device == usecase->out_snd_device &&
552 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800553 return 0;
554 }
555
sangwoobc677242013-08-08 16:53:43 +0900556 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700557 out_snd_device, platform_get_snd_device_name(out_snd_device),
558 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800559
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800560 /*
561 * Limitation: While in call, to do a device switch we need to disable
562 * and enable both RX and TX devices though one of them is same as current
563 * device.
564 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700565 if (usecase->type == VOICE_CALL) {
566 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800567 }
568
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700569 /* Disable current sound devices */
570 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700571 disable_audio_route(adev, usecase);
572 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800573 }
574
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700575 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700576 disable_audio_route(adev, usecase);
577 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800578 }
579
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700580 /* Applicable only on the targets that has external modem.
581 * New device information should be sent to modem before enabling
582 * the devices to reduce in-call device switch time.
583 */
584 if (usecase->type == VOICE_CALL)
585 status = platform_switch_voice_call_enable_device_config(adev->platform,
586 out_snd_device,
587 in_snd_device);
588
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700589 /* Enable new sound devices */
590 if (out_snd_device != SND_DEVICE_NONE) {
591 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
592 check_usecases_codec_backend(adev, usecase, out_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700593 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800594 }
595
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700596 if (in_snd_device != SND_DEVICE_NONE) {
597 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700598 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700599 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700600
Eric Laurentb23d5282013-05-14 15:27:20 -0700601 if (usecase->type == VOICE_CALL)
602 status = platform_switch_voice_call_device_post(adev->platform,
603 out_snd_device,
604 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800605
sangwoo170731f2013-06-08 15:36:36 +0900606 usecase->in_snd_device = in_snd_device;
607 usecase->out_snd_device = out_snd_device;
608
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700609 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900610
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700611 /* Applicable only on the targets that has external modem.
612 * Enable device command should be sent to modem only after
613 * enabling voice call mixer controls
614 */
615 if (usecase->type == VOICE_CALL)
616 status = platform_switch_voice_call_usecase_route_post(adev->platform,
617 out_snd_device,
618 in_snd_device);
619
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800620 return status;
621}
622
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800623static int stop_input_stream(struct stream_in *in)
624{
625 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800626 struct audio_usecase *uc_info;
627 struct audio_device *adev = in->dev;
628
Eric Laurentc8400632013-02-14 19:04:54 -0800629 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800630
Eric Laurent994a6932013-07-17 11:51:42 -0700631 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700632 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800633 uc_info = get_usecase_from_list(adev, in->usecase);
634 if (uc_info == NULL) {
635 ALOGE("%s: Could not find the usecase (%d) in the list",
636 __func__, in->usecase);
637 return -EINVAL;
638 }
639
Eric Laurent150dbfe2013-02-27 14:31:02 -0800640 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700641 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700642
643 /* 2. Disable the tx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700644 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800645
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800646 list_remove(&uc_info->list);
647 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800648
Eric Laurent994a6932013-07-17 11:51:42 -0700649 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800650 return ret;
651}
652
653int start_input_stream(struct stream_in *in)
654{
655 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800656 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800657 struct audio_usecase *uc_info;
658 struct audio_device *adev = in->dev;
659
Eric Laurent994a6932013-07-17 11:51:42 -0700660 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700661 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800662 if (in->pcm_device_id < 0) {
663 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
664 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800665 ret = -EINVAL;
666 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800667 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700668
669 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800670 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
671 uc_info->id = in->usecase;
672 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800673 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700674 uc_info->devices = in->device;
675 uc_info->in_snd_device = SND_DEVICE_NONE;
676 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800677
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800678 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700679 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800680
Eric Laurentc8400632013-02-14 19:04:54 -0800681 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700682 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
Vineeta Srivastava55a12932014-08-11 11:41:18 -0700683 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
684 PCM_IN, &in->config);
685 if (in->pcm && !pcm_is_ready(in->pcm)) {
686 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
687 pcm_close(in->pcm);
688 in->pcm = NULL;
689 ret = -EIO;
690 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691 }
Eric Laurent994a6932013-07-17 11:51:42 -0700692 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800693 return ret;
694
695error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800696 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800697
698error_config:
699 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700700 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800701
702 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800703}
704
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700705/* must be called with out->lock locked */
706static int send_offload_cmd_l(struct stream_out* out, int command)
707{
708 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
709
710 ALOGVV("%s %d", __func__, command);
711
712 cmd->cmd = command;
713 list_add_tail(&out->offload_cmd_list, &cmd->node);
714 pthread_cond_signal(&out->offload_cond);
715 return 0;
716}
717
718/* must be called iwth out->lock locked */
719static void stop_compressed_output_l(struct stream_out *out)
720{
721 out->offload_state = OFFLOAD_STATE_IDLE;
722 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700723 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700724 if (out->compr != NULL) {
725 compress_stop(out->compr);
726 while (out->offload_thread_blocked) {
727 pthread_cond_wait(&out->cond, &out->lock);
728 }
729 }
730}
731
732static void *offload_thread_loop(void *context)
733{
734 struct stream_out *out = (struct stream_out *) context;
735 struct listnode *item;
736
737 out->offload_state = OFFLOAD_STATE_IDLE;
738 out->playback_started = 0;
739
740 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
741 set_sched_policy(0, SP_FOREGROUND);
742 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
743
744 ALOGV("%s", __func__);
745 pthread_mutex_lock(&out->lock);
746 for (;;) {
747 struct offload_cmd *cmd = NULL;
748 stream_callback_event_t event;
749 bool send_callback = false;
750
751 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
752 __func__, list_empty(&out->offload_cmd_list),
753 out->offload_state);
754 if (list_empty(&out->offload_cmd_list)) {
755 ALOGV("%s SLEEPING", __func__);
756 pthread_cond_wait(&out->offload_cond, &out->lock);
757 ALOGV("%s RUNNING", __func__);
758 continue;
759 }
760
761 item = list_head(&out->offload_cmd_list);
762 cmd = node_to_item(item, struct offload_cmd, node);
763 list_remove(item);
764
765 ALOGVV("%s STATE %d CMD %d out->compr %p",
766 __func__, out->offload_state, cmd->cmd, out->compr);
767
768 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
769 free(cmd);
770 break;
771 }
772
773 if (out->compr == NULL) {
774 ALOGE("%s: Compress handle is NULL", __func__);
775 pthread_cond_signal(&out->cond);
776 continue;
777 }
778 out->offload_thread_blocked = true;
779 pthread_mutex_unlock(&out->lock);
780 send_callback = false;
781 switch(cmd->cmd) {
782 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
783 compress_wait(out->compr, -1);
784 send_callback = true;
785 event = STREAM_CBK_EVENT_WRITE_READY;
786 break;
787 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700788 compress_next_track(out->compr);
789 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700790 send_callback = true;
791 event = STREAM_CBK_EVENT_DRAIN_READY;
792 break;
793 case OFFLOAD_CMD_DRAIN:
794 compress_drain(out->compr);
795 send_callback = true;
796 event = STREAM_CBK_EVENT_DRAIN_READY;
797 break;
798 default:
799 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
800 break;
801 }
802 pthread_mutex_lock(&out->lock);
803 out->offload_thread_blocked = false;
804 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700805 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700806 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700807 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700808 free(cmd);
809 }
810
811 pthread_cond_signal(&out->cond);
812 while (!list_empty(&out->offload_cmd_list)) {
813 item = list_head(&out->offload_cmd_list);
814 list_remove(item);
815 free(node_to_item(item, struct offload_cmd, node));
816 }
817 pthread_mutex_unlock(&out->lock);
818
819 return NULL;
820}
821
822static int create_offload_callback_thread(struct stream_out *out)
823{
824 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
825 list_init(&out->offload_cmd_list);
826 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
827 offload_thread_loop, out);
828 return 0;
829}
830
831static int destroy_offload_callback_thread(struct stream_out *out)
832{
833 pthread_mutex_lock(&out->lock);
834 stop_compressed_output_l(out);
835 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
836
837 pthread_mutex_unlock(&out->lock);
838 pthread_join(out->offload_thread, (void **) NULL);
839 pthread_cond_destroy(&out->offload_cond);
840
841 return 0;
842}
843
Eric Laurent07eeafd2013-10-06 12:52:49 -0700844static bool allow_hdmi_channel_config(struct audio_device *adev)
845{
846 struct listnode *node;
847 struct audio_usecase *usecase;
848 bool ret = true;
849
850 list_for_each(node, &adev->usecase_list) {
851 usecase = node_to_item(node, struct audio_usecase, list);
852 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
853 /*
854 * If voice call is already existing, do not proceed further to avoid
855 * disabling/enabling both RX and TX devices, CSD calls, etc.
856 * Once the voice call done, the HDMI channels can be configured to
857 * max channels of remaining use cases.
858 */
859 if (usecase->id == USECASE_VOICE_CALL) {
860 ALOGD("%s: voice call is active, no change in HDMI channels",
861 __func__);
862 ret = false;
863 break;
864 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
865 ALOGD("%s: multi channel playback is active, "
866 "no change in HDMI channels", __func__);
867 ret = false;
868 break;
869 }
870 }
871 }
872 return ret;
873}
874
875static int check_and_set_hdmi_channels(struct audio_device *adev,
876 unsigned int channels)
877{
878 struct listnode *node;
879 struct audio_usecase *usecase;
880
881 /* Check if change in HDMI channel config is allowed */
882 if (!allow_hdmi_channel_config(adev))
883 return 0;
884
885 if (channels == adev->cur_hdmi_channels) {
886 ALOGD("%s: Requested channels are same as current", __func__);
887 return 0;
888 }
889
890 platform_set_hdmi_channels(adev->platform, channels);
891 adev->cur_hdmi_channels = channels;
892
893 /*
894 * Deroute all the playback streams routed to HDMI so that
895 * the back end is deactivated. Note that backend will not
896 * be deactivated if any one stream is connected to it.
897 */
898 list_for_each(node, &adev->usecase_list) {
899 usecase = node_to_item(node, struct audio_usecase, list);
900 if (usecase->type == PCM_PLAYBACK &&
901 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700902 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -0700903 }
904 }
905
906 /*
907 * Enable all the streams disabled above. Now the HDMI backend
908 * will be activated with new channel configuration
909 */
910 list_for_each(node, &adev->usecase_list) {
911 usecase = node_to_item(node, struct audio_usecase, list);
912 if (usecase->type == PCM_PLAYBACK &&
913 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700914 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -0700915 }
916 }
917
918 return 0;
919}
920
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800921static int stop_output_stream(struct stream_out *out)
922{
923 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800924 struct audio_usecase *uc_info;
925 struct audio_device *adev = out->dev;
926
Eric Laurent994a6932013-07-17 11:51:42 -0700927 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700928 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800929 uc_info = get_usecase_from_list(adev, out->usecase);
930 if (uc_info == NULL) {
931 ALOGE("%s: Could not find the usecase (%d) in the list",
932 __func__, out->usecase);
933 return -EINVAL;
934 }
935
Haynes Mathew George41f86652014-06-17 14:22:15 -0700936 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
937 if (adev->visualizer_stop_output != NULL)
938 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
939 if (adev->offload_effects_stop_output != NULL)
940 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
941 }
Eric Laurentc4aef752013-09-12 17:45:53 -0700942
Eric Laurent150dbfe2013-02-27 14:31:02 -0800943 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700944 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700945
946 /* 2. Disable the rx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700947 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800948
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800949 list_remove(&uc_info->list);
950 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800951
Eric Laurent07eeafd2013-10-06 12:52:49 -0700952 /* Must be called after removing the usecase from list */
953 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
954 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
955
Eric Laurent994a6932013-07-17 11:51:42 -0700956 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800957 return ret;
958}
959
960int start_output_stream(struct stream_out *out)
961{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800962 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800963 struct audio_usecase *uc_info;
964 struct audio_device *adev = out->dev;
965
Eric Laurent994a6932013-07-17 11:51:42 -0700966 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700967 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -0700968 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800969 if (out->pcm_device_id < 0) {
970 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
971 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800972 ret = -EINVAL;
973 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800974 }
975
976 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
977 uc_info->id = out->usecase;
978 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800979 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700980 uc_info->devices = out->devices;
981 uc_info->in_snd_device = SND_DEVICE_NONE;
982 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800983
Eric Laurent07eeafd2013-10-06 12:52:49 -0700984 /* This must be called before adding this usecase to the list */
985 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
986 check_and_set_hdmi_channels(adev, out->config.channels);
987
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800988 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800989
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700990 select_devices(adev, out->usecase);
991
Andy Hung31aca912014-03-20 17:14:59 -0700992 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700993 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700994 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Vineeta Srivastava55a12932014-08-11 11:41:18 -0700995 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
996 PCM_OUT | PCM_MONOTONIC, &out->config);
997 if (out->pcm && !pcm_is_ready(out->pcm)) {
998 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
999 pcm_close(out->pcm);
1000 out->pcm = NULL;
1001 ret = -EIO;
1002 goto error_open;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001003 }
1004 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001005 out->pcm = NULL;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001006 out->compr = compress_open(adev->snd_card, out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001007 COMPRESS_IN, &out->compr_config);
1008 if (out->compr && !is_compress_ready(out->compr)) {
1009 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1010 compress_close(out->compr);
1011 out->compr = NULL;
1012 ret = -EIO;
1013 goto error_open;
1014 }
1015 if (out->offload_callback)
1016 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001017
1018 if (adev->visualizer_start_output != NULL)
Haynes Mathew George41f86652014-06-17 14:22:15 -07001019 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1020 if (adev->offload_effects_start_output != NULL)
1021 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001022 }
Eric Laurent994a6932013-07-17 11:51:42 -07001023 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001024 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001025error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001027error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001028 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001029}
1030
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031static int check_input_parameters(uint32_t sample_rate,
1032 audio_format_t format,
1033 int channel_count)
1034{
1035 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1036
1037 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1038
1039 switch (sample_rate) {
1040 case 8000:
1041 case 11025:
1042 case 12000:
1043 case 16000:
1044 case 22050:
1045 case 24000:
1046 case 32000:
1047 case 44100:
1048 case 48000:
1049 break;
1050 default:
1051 return -EINVAL;
1052 }
1053
1054 return 0;
1055}
1056
1057static size_t get_input_buffer_size(uint32_t sample_rate,
1058 audio_format_t format,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001059 int channel_count,
1060 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001061{
1062 size_t size = 0;
1063
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001064 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1065 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001066
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001067 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001068 if (is_low_latency)
Glenn Kasten4f993392014-05-14 07:30:48 -07001069 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001070 /* ToDo: should use frame_size computed based on the format and
1071 channel_count here. */
1072 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001073
Glenn Kasten4f993392014-05-14 07:30:48 -07001074 /* make sure the size is multiple of 32 bytes
1075 * At 48 kHz mono 16-bit PCM:
1076 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1077 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1078 */
1079 size += 0x1f;
1080 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001081
1082 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001083}
1084
1085static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1086{
1087 struct stream_out *out = (struct stream_out *)stream;
1088
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001089 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001090}
1091
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001092static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001093{
1094 return -ENOSYS;
1095}
1096
1097static size_t out_get_buffer_size(const struct audio_stream *stream)
1098{
1099 struct stream_out *out = (struct stream_out *)stream;
1100
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001101 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1102 return out->compr_config.fragment_size;
1103 }
Vineeta Srivastava55a12932014-08-11 11:41:18 -07001104
Eric Laurentfdf296a2014-07-03 16:41:51 -07001105 return out->config.period_size *
1106 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001107}
1108
1109static uint32_t out_get_channels(const struct audio_stream *stream)
1110{
1111 struct stream_out *out = (struct stream_out *)stream;
1112
1113 return out->channel_mask;
1114}
1115
1116static audio_format_t out_get_format(const struct audio_stream *stream)
1117{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001118 struct stream_out *out = (struct stream_out *)stream;
1119
1120 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001121}
1122
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001123static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001124{
1125 return -ENOSYS;
1126}
1127
1128static int out_standby(struct audio_stream *stream)
1129{
1130 struct stream_out *out = (struct stream_out *)stream;
1131 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001132
Eric Laurent994a6932013-07-17 11:51:42 -07001133 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001134 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001135
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001136 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001137 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001138 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001139 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001140 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1141 if (out->pcm) {
1142 pcm_close(out->pcm);
1143 out->pcm = NULL;
1144 }
1145 } else {
1146 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001147 out->gapless_mdata.encoder_delay = 0;
1148 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001149 if (out->compr != NULL) {
1150 compress_close(out->compr);
1151 out->compr = NULL;
1152 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001153 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001154 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001155 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001156 }
1157 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001158 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001159 return 0;
1160}
1161
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001162static int out_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001163{
1164 return 0;
1165}
1166
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001167static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1168{
1169 int ret = 0;
1170 char value[32];
1171 struct compr_gapless_mdata tmp_mdata;
1172
1173 if (!out || !parms) {
1174 return -EINVAL;
1175 }
1176
1177 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1178 if (ret >= 0) {
1179 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1180 } else {
1181 return -EINVAL;
1182 }
1183
1184 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1185 if (ret >= 0) {
1186 tmp_mdata.encoder_padding = atoi(value);
1187 } else {
1188 return -EINVAL;
1189 }
1190
1191 out->gapless_mdata = tmp_mdata;
1192 out->send_new_metadata = 1;
1193 ALOGV("%s new encoder delay %u and padding %u", __func__,
1194 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1195
1196 return 0;
1197}
1198
1199
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001200static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1201{
1202 struct stream_out *out = (struct stream_out *)stream;
1203 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001204 struct audio_usecase *usecase;
1205 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001206 struct str_parms *parms;
1207 char value[32];
1208 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001209 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001210 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001211
sangwoobc677242013-08-08 16:53:43 +09001212 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001213 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001214 parms = str_parms_create_str(kvpairs);
1215 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1216 if (ret >= 0) {
1217 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001218 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001219 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001220
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001221 /*
1222 * When HDMI cable is unplugged the music playback is paused and
1223 * the policy manager sends routing=0. But the audioflinger
1224 * continues to write data until standby time (3sec).
1225 * As the HDMI core is turned off, the write gets blocked.
1226 * Avoid this by routing audio to speaker until standby.
1227 */
1228 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1229 val == AUDIO_DEVICE_NONE) {
1230 val = AUDIO_DEVICE_OUT_SPEAKER;
1231 }
1232
1233 /*
1234 * select_devices() call below switches all the usecases on the same
1235 * backend to the new device. Refer to check_usecases_codec_backend() in
1236 * the select_devices(). But how do we undo this?
1237 *
1238 * For example, music playback is active on headset (deep-buffer usecase)
1239 * and if we go to ringtones and select a ringtone, low-latency usecase
1240 * will be started on headset+speaker. As we can't enable headset+speaker
1241 * and headset devices at the same time, select_devices() switches the music
1242 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1243 * So when the ringtone playback is completed, how do we undo the same?
1244 *
1245 * We are relying on the out_set_parameters() call on deep-buffer output,
1246 * once the ringtone playback is ended.
1247 * NOTE: We should not check if the current devices are same as new devices.
1248 * Because select_devices() must be called to switch back the music
1249 * playback to headset.
1250 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001251 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001252 out->devices = val;
1253
1254 if (!out->standby)
1255 select_devices(adev, out->usecase);
1256
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001257 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
Vineeta Srivastava55a12932014-08-11 11:41:18 -07001258 !voice_is_in_call(adev) &&
1259 (out == adev->primary_output)) {
1260 ret = voice_start_call(adev);
1261 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1262 voice_is_in_call(adev) &&
1263 (out == adev->primary_output)) {
1264 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001265 }
1266 }
1267
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001268 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1269 voice_is_in_call(adev) &&
Vineeta Srivastava55a12932014-08-11 11:41:18 -07001270 (out == adev->primary_output)) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001271 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001272 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001274 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001275 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001276 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001277
1278 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1279 parse_compress_metadata(out, parms);
1280 }
1281
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001282 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001283 ALOGV("%s: exit: code(%d)", __func__, status);
1284 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001285}
1286
1287static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1288{
1289 struct stream_out *out = (struct stream_out *)stream;
1290 struct str_parms *query = str_parms_create_str(keys);
1291 char *str;
1292 char value[256];
1293 struct str_parms *reply = str_parms_create();
1294 size_t i, j;
1295 int ret;
1296 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001297 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001298 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1299 if (ret >= 0) {
1300 value[0] = '\0';
1301 i = 0;
1302 while (out->supported_channel_masks[i] != 0) {
1303 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1304 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1305 if (!first) {
1306 strcat(value, "|");
1307 }
1308 strcat(value, out_channels_name_to_enum_table[j].name);
1309 first = false;
1310 break;
1311 }
1312 }
1313 i++;
1314 }
1315 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1316 str = str_parms_to_str(reply);
1317 } else {
1318 str = strdup(keys);
1319 }
1320 str_parms_destroy(query);
1321 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001322 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323 return str;
1324}
1325
1326static uint32_t out_get_latency(const struct audio_stream_out *stream)
1327{
1328 struct stream_out *out = (struct stream_out *)stream;
1329
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001330 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1331 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1332
1333 return (out->config.period_count * out->config.period_size * 1000) /
1334 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001335}
1336
1337static int out_set_volume(struct audio_stream_out *stream, float left,
1338 float right)
1339{
Eric Laurenta9024de2013-04-04 09:19:12 -07001340 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001341 int volume[2];
1342
Eric Laurenta9024de2013-04-04 09:19:12 -07001343 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1344 /* only take left channel into account: the API is for stereo anyway */
1345 out->muted = (left == 0.0f);
1346 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001347 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1348 const char *mixer_ctl_name = "Compress Playback Volume";
1349 struct audio_device *adev = out->dev;
1350 struct mixer_ctl *ctl;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001351 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1352 if (!ctl) {
Haynes Mathew George20bcfa82014-06-25 13:37:03 -07001353 /* try with the control based on device id */
1354 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1355 PCM_PLAYBACK);
1356 char ctl_name[128] = {0};
1357 snprintf(ctl_name, sizeof(ctl_name),
1358 "Compress Playback %d Volume", pcm_device_id);
1359 ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name);
1360 if (!ctl) {
1361 ALOGE("%s: Could not get volume ctl mixer cmd", __func__);
1362 return -EINVAL;
1363 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001364 }
1365 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1366 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1367 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1368 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001369 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001370
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001371 return -ENOSYS;
1372}
1373
1374static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1375 size_t bytes)
1376{
1377 struct stream_out *out = (struct stream_out *)stream;
1378 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001379 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001380
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001381 pthread_mutex_lock(&out->lock);
1382 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001383 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001384 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001385 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001386 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001387 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001388 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001389 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001390 goto exit;
1391 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001392 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001393
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001394 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001395 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1396 if (out->send_new_metadata) {
1397 ALOGVV("send new gapless metadata");
1398 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1399 out->send_new_metadata = 0;
1400 }
1401
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001402 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001403 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001404 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001405 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1406 }
1407 if (!out->playback_started) {
1408 compress_start(out->compr);
1409 out->playback_started = 1;
1410 out->offload_state = OFFLOAD_STATE_PLAYING;
1411 }
1412 pthread_mutex_unlock(&out->lock);
1413 return ret;
1414 } else {
1415 if (out->pcm) {
1416 if (out->muted)
1417 memset((void *)buffer, 0, bytes);
1418 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Vineeta Srivastava55a12932014-08-11 11:41:18 -07001419 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001420 if (ret == 0)
1421 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001422 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001423 }
1424
1425exit:
1426 pthread_mutex_unlock(&out->lock);
1427
1428 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001429 if (out->pcm)
1430 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001431 out_standby(&out->stream.common);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001432 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001433 out_get_sample_rate(&out->stream.common));
1434 }
1435 return bytes;
1436}
1437
1438static int out_get_render_position(const struct audio_stream_out *stream,
1439 uint32_t *dsp_frames)
1440{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001441 struct stream_out *out = (struct stream_out *)stream;
1442 *dsp_frames = 0;
1443 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1444 pthread_mutex_lock(&out->lock);
1445 if (out->compr != NULL) {
1446 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1447 &out->sample_rate);
1448 ALOGVV("%s rendered frames %d sample_rate %d",
1449 __func__, *dsp_frames, out->sample_rate);
1450 }
1451 pthread_mutex_unlock(&out->lock);
1452 return 0;
1453 } else
1454 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001455}
1456
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001457static int out_add_audio_effect(const struct audio_stream *stream __unused,
1458 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001459{
1460 return 0;
1461}
1462
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001463static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1464 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001465{
1466 return 0;
1467}
1468
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001469static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1470 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001471{
1472 return -EINVAL;
1473}
1474
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001475static int out_get_presentation_position(const struct audio_stream_out *stream,
1476 uint64_t *frames, struct timespec *timestamp)
1477{
1478 struct stream_out *out = (struct stream_out *)stream;
1479 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001480 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001481
1482 pthread_mutex_lock(&out->lock);
1483
Eric Laurent949a0892013-09-20 09:20:13 -07001484 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1485 if (out->compr != NULL) {
1486 compress_get_tstamp(out->compr, &dsp_frames,
1487 &out->sample_rate);
1488 ALOGVV("%s rendered frames %ld sample_rate %d",
1489 __func__, dsp_frames, out->sample_rate);
1490 *frames = dsp_frames;
1491 ret = 0;
1492 /* this is the best we can do */
1493 clock_gettime(CLOCK_MONOTONIC, timestamp);
1494 }
1495 } else {
1496 if (out->pcm) {
1497 size_t avail;
1498 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1499 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001500 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001501 // This adjustment accounts for buffering after app processor.
1502 // It is based on estimated DSP latency per use case, rather than exact.
1503 signed_frames -=
1504 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1505
Eric Laurent949a0892013-09-20 09:20:13 -07001506 // It would be unusual for this value to be negative, but check just in case ...
1507 if (signed_frames >= 0) {
1508 *frames = signed_frames;
1509 ret = 0;
1510 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001511 }
1512 }
1513 }
1514
1515 pthread_mutex_unlock(&out->lock);
1516
1517 return ret;
1518}
1519
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001520static int out_set_callback(struct audio_stream_out *stream,
1521 stream_callback_t callback, void *cookie)
1522{
1523 struct stream_out *out = (struct stream_out *)stream;
1524
1525 ALOGV("%s", __func__);
1526 pthread_mutex_lock(&out->lock);
1527 out->offload_callback = callback;
1528 out->offload_cookie = cookie;
1529 pthread_mutex_unlock(&out->lock);
1530 return 0;
1531}
1532
1533static int out_pause(struct audio_stream_out* stream)
1534{
1535 struct stream_out *out = (struct stream_out *)stream;
1536 int status = -ENOSYS;
1537 ALOGV("%s", __func__);
1538 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1539 pthread_mutex_lock(&out->lock);
1540 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1541 status = compress_pause(out->compr);
1542 out->offload_state = OFFLOAD_STATE_PAUSED;
1543 }
1544 pthread_mutex_unlock(&out->lock);
1545 }
1546 return status;
1547}
1548
1549static int out_resume(struct audio_stream_out* stream)
1550{
1551 struct stream_out *out = (struct stream_out *)stream;
1552 int status = -ENOSYS;
1553 ALOGV("%s", __func__);
1554 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1555 status = 0;
1556 pthread_mutex_lock(&out->lock);
1557 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1558 status = compress_resume(out->compr);
1559 out->offload_state = OFFLOAD_STATE_PLAYING;
1560 }
1561 pthread_mutex_unlock(&out->lock);
1562 }
1563 return status;
1564}
1565
1566static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1567{
1568 struct stream_out *out = (struct stream_out *)stream;
1569 int status = -ENOSYS;
1570 ALOGV("%s", __func__);
1571 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1572 pthread_mutex_lock(&out->lock);
1573 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1574 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1575 else
1576 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1577 pthread_mutex_unlock(&out->lock);
1578 }
1579 return status;
1580}
1581
1582static int out_flush(struct audio_stream_out* stream)
1583{
1584 struct stream_out *out = (struct stream_out *)stream;
1585 ALOGV("%s", __func__);
1586 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1587 pthread_mutex_lock(&out->lock);
1588 stop_compressed_output_l(out);
1589 pthread_mutex_unlock(&out->lock);
1590 return 0;
1591 }
1592 return -ENOSYS;
1593}
1594
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001595/** audio_stream_in implementation **/
1596static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1597{
1598 struct stream_in *in = (struct stream_in *)stream;
1599
1600 return in->config.rate;
1601}
1602
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001603static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001604{
1605 return -ENOSYS;
1606}
1607
1608static size_t in_get_buffer_size(const struct audio_stream *stream)
1609{
1610 struct stream_in *in = (struct stream_in *)stream;
1611
Eric Laurentfdf296a2014-07-03 16:41:51 -07001612 return in->config.period_size *
1613 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001614}
1615
1616static uint32_t in_get_channels(const struct audio_stream *stream)
1617{
1618 struct stream_in *in = (struct stream_in *)stream;
1619
1620 return in->channel_mask;
1621}
1622
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001623static audio_format_t in_get_format(const struct audio_stream *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001624{
1625 return AUDIO_FORMAT_PCM_16_BIT;
1626}
1627
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001628static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001629{
1630 return -ENOSYS;
1631}
1632
1633static int in_standby(struct audio_stream *stream)
1634{
1635 struct stream_in *in = (struct stream_in *)stream;
1636 struct audio_device *adev = in->dev;
1637 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001638 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001639 pthread_mutex_lock(&in->lock);
1640 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001641 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001642 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001643 if (in->pcm) {
1644 pcm_close(in->pcm);
1645 in->pcm = NULL;
1646 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001647 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001648 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001649 }
1650 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001651 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001652 return status;
1653}
1654
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001655static int in_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001656{
1657 return 0;
1658}
1659
1660static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1661{
1662 struct stream_in *in = (struct stream_in *)stream;
1663 struct audio_device *adev = in->dev;
1664 struct str_parms *parms;
1665 char *str;
1666 char value[32];
1667 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07001668 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001669
Eric Laurent994a6932013-07-17 11:51:42 -07001670 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001671 parms = str_parms_create_str(kvpairs);
1672
1673 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1674
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001675 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001676 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001677 if (ret >= 0) {
1678 val = atoi(value);
1679 /* no audio source uses val == 0 */
1680 if ((in->source != val) && (val != 0)) {
1681 in->source = val;
1682 }
1683 }
1684
1685 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07001686
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001687 if (ret >= 0) {
1688 val = atoi(value);
Vineeta Srivastava55a12932014-08-11 11:41:18 -07001689 if ((in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001690 in->device = val;
1691 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001692 if (!in->standby)
Eric Laurent03f09432014-03-25 18:09:11 -07001693 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001694 }
1695 }
1696
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001697 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001698 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001699
1700 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001701 ALOGV("%s: exit: status(%d)", __func__, status);
1702 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001703}
1704
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001705static char* in_get_parameters(const struct audio_stream *stream __unused,
1706 const char *keys __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001707{
1708 return strdup("");
1709}
1710
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001711static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001712{
1713 return 0;
1714}
1715
1716static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1717 size_t bytes)
1718{
1719 struct stream_in *in = (struct stream_in *)stream;
1720 struct audio_device *adev = in->dev;
1721 int i, ret = -1;
1722
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001723 pthread_mutex_lock(&in->lock);
1724 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001725 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001726 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001727 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001728 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001729 goto exit;
1730 }
1731 in->standby = 0;
1732 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001733
1734 if (in->pcm) {
Vineeta Srivastava55a12932014-08-11 11:41:18 -07001735 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001736 }
1737
1738 /*
1739 * Instead of writing zeroes here, we could trust the hardware
1740 * to always provide zeroes when muted.
1741 */
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001742 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001743 memset(buffer, 0, bytes);
1744
1745exit:
1746 pthread_mutex_unlock(&in->lock);
1747
1748 if (ret != 0) {
1749 in_standby(&in->stream.common);
1750 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001751 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001752 in_get_sample_rate(&in->stream.common));
1753 }
1754 return bytes;
1755}
1756
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001757static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001758{
1759 return 0;
1760}
1761
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001762static int add_remove_audio_effect(const struct audio_stream *stream,
1763 effect_handle_t effect,
1764 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001765{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001766 struct stream_in *in = (struct stream_in *)stream;
1767 int status = 0;
1768 effect_descriptor_t desc;
1769
1770 status = (*effect)->get_descriptor(effect, &desc);
1771 if (status != 0)
1772 return status;
1773
1774 pthread_mutex_lock(&in->lock);
1775 pthread_mutex_lock(&in->dev->lock);
1776 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1777 in->enable_aec != enable &&
1778 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1779 in->enable_aec = enable;
1780 if (!in->standby)
1781 select_devices(in->dev, in->usecase);
1782 }
Ravi Kumar Alamanda3ad4e1b2014-06-03 00:08:15 -07001783 if (in->enable_ns != enable &&
1784 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1785 in->enable_ns = enable;
1786 if (!in->standby)
1787 select_devices(in->dev, in->usecase);
1788 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001789 pthread_mutex_unlock(&in->dev->lock);
1790 pthread_mutex_unlock(&in->lock);
1791
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001792 return 0;
1793}
1794
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001795static int in_add_audio_effect(const struct audio_stream *stream,
1796 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001797{
Eric Laurent994a6932013-07-17 11:51:42 -07001798 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001799 return add_remove_audio_effect(stream, effect, true);
1800}
1801
1802static int in_remove_audio_effect(const struct audio_stream *stream,
1803 effect_handle_t effect)
1804{
Eric Laurent994a6932013-07-17 11:51:42 -07001805 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001806 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001807}
1808
1809static int adev_open_output_stream(struct audio_hw_device *dev,
1810 audio_io_handle_t handle,
1811 audio_devices_t devices,
1812 audio_output_flags_t flags,
1813 struct audio_config *config,
Eric Laurent91975a62014-07-27 17:15:50 -07001814 struct audio_stream_out **stream_out,
1815 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001816{
1817 struct audio_device *adev = (struct audio_device *)dev;
1818 struct stream_out *out;
1819 int i, ret;
1820
Eric Laurent994a6932013-07-17 11:51:42 -07001821 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001822 __func__, config->sample_rate, config->channel_mask, devices, flags);
1823 *stream_out = NULL;
1824 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1825
1826 if (devices == AUDIO_DEVICE_NONE)
1827 devices = AUDIO_DEVICE_OUT_SPEAKER;
1828
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001829 out->flags = flags;
1830 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001831 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001832 out->format = config->format;
1833 out->sample_rate = config->sample_rate;
1834 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1835 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001836 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001837
1838 /* Init use case and pcm_config */
1839 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07001840 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001841 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001842 pthread_mutex_lock(&adev->lock);
1843 ret = read_hdmi_channel_masks(out);
1844 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001845 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001846 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001847
1848 if (config->sample_rate == 0)
1849 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1850 if (config->channel_mask == 0)
1851 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1852
1853 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001854 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001855 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1856 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001857 out->config.rate = config->sample_rate;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07001858 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001859 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001860 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1861 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1862 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1863 ALOGE("%s: Unsupported Offload information", __func__);
1864 ret = -EINVAL;
1865 goto error_open;
1866 }
1867 if (!is_supported_format(config->offload_info.format)) {
1868 ALOGE("%s: Unsupported audio format", __func__);
1869 ret = -EINVAL;
1870 goto error_open;
1871 }
1872
1873 out->compr_config.codec = (struct snd_codec *)
1874 calloc(1, sizeof(struct snd_codec));
1875
1876 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1877 if (config->offload_info.channel_mask)
1878 out->channel_mask = config->offload_info.channel_mask;
1879 else if (config->channel_mask)
1880 out->channel_mask = config->channel_mask;
1881 out->format = config->offload_info.format;
1882 out->sample_rate = config->offload_info.sample_rate;
1883
1884 out->stream.set_callback = out_set_callback;
1885 out->stream.pause = out_pause;
1886 out->stream.resume = out_resume;
1887 out->stream.drain = out_drain;
1888 out->stream.flush = out_flush;
1889
1890 out->compr_config.codec->id =
1891 get_snd_codec_id(config->offload_info.format);
1892 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1893 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1894 out->compr_config.codec->sample_rate =
1895 compress_get_alsa_rate(config->offload_info.sample_rate);
1896 out->compr_config.codec->bit_rate =
1897 config->offload_info.bit_rate;
1898 out->compr_config.codec->ch_in =
Eric Laurent0de8d1f2014-07-01 20:34:45 -07001899 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001900 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1901
1902 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1903 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001904
1905 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001906 create_offload_callback_thread(out);
1907 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1908 __func__, config->offload_info.version,
1909 config->offload_info.bit_rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001910 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07001911 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1912 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1913 out->config = pcm_config_deep_buffer;
1914 } else {
1915 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1916 out->config = pcm_config_low_latency;
1917 }
1918 if (config->format != audio_format_from_pcm_format(out->config.format)) {
1919 if (k_enable_extended_precision
1920 && pcm_params_format_test(adev->use_case_table[out->usecase],
1921 pcm_format_from_audio_format(config->format))) {
1922 out->config.format = pcm_format_from_audio_format(config->format);
1923 /* out->format already set to config->format */
1924 } else {
1925 /* deny the externally proposed config format
1926 * and use the one specified in audio_hw layer configuration.
1927 * Note: out->format is returned by out->stream.common.get_format()
1928 * and is used to set config->format in the code several lines below.
1929 */
1930 out->format = audio_format_from_pcm_format(out->config.format);
1931 }
1932 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001933 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001934 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07001935 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
1936 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001937
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001938 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Vineeta Srivastava55a12932014-08-11 11:41:18 -07001939 if(adev->primary_output == NULL)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001940 adev->primary_output = out;
1941 else {
1942 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001943 ret = -EEXIST;
1944 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001945 }
1946 }
1947
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001948 /* Check if this usecase is already existing */
1949 pthread_mutex_lock(&adev->lock);
1950 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1951 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001952 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001953 ret = -EEXIST;
1954 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001955 }
1956 pthread_mutex_unlock(&adev->lock);
1957
1958 out->stream.common.get_sample_rate = out_get_sample_rate;
1959 out->stream.common.set_sample_rate = out_set_sample_rate;
1960 out->stream.common.get_buffer_size = out_get_buffer_size;
1961 out->stream.common.get_channels = out_get_channels;
1962 out->stream.common.get_format = out_get_format;
1963 out->stream.common.set_format = out_set_format;
1964 out->stream.common.standby = out_standby;
1965 out->stream.common.dump = out_dump;
1966 out->stream.common.set_parameters = out_set_parameters;
1967 out->stream.common.get_parameters = out_get_parameters;
1968 out->stream.common.add_audio_effect = out_add_audio_effect;
1969 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1970 out->stream.get_latency = out_get_latency;
1971 out->stream.set_volume = out_set_volume;
1972 out->stream.write = out_write;
1973 out->stream.get_render_position = out_get_render_position;
1974 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001975 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001976
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001977 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001978 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001979 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001980
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001981 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
1982 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
1983
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001984 config->format = out->stream.common.get_format(&out->stream.common);
1985 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1986 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1987
1988 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001989 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001990 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001991
1992error_open:
1993 free(out);
1994 *stream_out = NULL;
1995 ALOGD("%s: exit: ret %d", __func__, ret);
1996 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001997}
1998
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001999static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 struct audio_stream_out *stream)
2001{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002002 struct stream_out *out = (struct stream_out *)stream;
2003 struct audio_device *adev = out->dev;
2004
Eric Laurent994a6932013-07-17 11:51:42 -07002005 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002006 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002007 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2008 destroy_offload_callback_thread(out);
2009
2010 if (out->compr_config.codec != NULL)
2011 free(out->compr_config.codec);
2012 }
2013 pthread_cond_destroy(&out->cond);
2014 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002015 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002016 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002017}
2018
2019static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2020{
2021 struct audio_device *adev = (struct audio_device *)dev;
2022 struct str_parms *parms;
2023 char *str;
2024 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002025 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002026 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002027 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002028
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002029 ALOGD("%s: enter: %s", __func__, kvpairs);
2030
2031 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002032
2033 parms = str_parms_create_str(kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002034 status = voice_set_parameters(adev, parms);
2035 if (status != 0) {
2036 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002037 }
2038
2039 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2040 if (ret >= 0) {
2041 /* When set to false, HAL should disable EC and NS
2042 * But it is currently not supported.
2043 */
2044 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2045 adev->bluetooth_nrec = true;
2046 else
2047 adev->bluetooth_nrec = false;
2048 }
2049
2050 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2051 if (ret >= 0) {
2052 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2053 adev->screen_off = false;
2054 else
2055 adev->screen_off = true;
2056 }
2057
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002058 ret = str_parms_get_int(parms, "rotation", &val);
2059 if (ret >= 0) {
2060 bool reverse_speakers = false;
2061 switch(val) {
2062 // FIXME: note that the code below assumes that the speakers are in the correct placement
2063 // relative to the user when the device is rotated 90deg from its default rotation. This
2064 // assumption is device-specific, not platform-specific like this code.
2065 case 270:
2066 reverse_speakers = true;
2067 break;
2068 case 0:
2069 case 90:
2070 case 180:
2071 break;
2072 default:
2073 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002074 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002075 }
Eric Laurent03f09432014-03-25 18:09:11 -07002076 if (status == 0) {
Eric Laurent03f09432014-03-25 18:09:11 -07002077 if (adev->speaker_lr_swap != reverse_speakers) {
2078 adev->speaker_lr_swap = reverse_speakers;
2079 // only update the selected device if there is active pcm playback
2080 struct audio_usecase *usecase;
2081 struct listnode *node;
2082 list_for_each(node, &adev->usecase_list) {
2083 usecase = node_to_item(node, struct audio_usecase, list);
2084 if (usecase->type == PCM_PLAYBACK) {
2085 select_devices(adev, usecase->id);
2086 break;
2087 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002088 }
2089 }
2090 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002091 }
2092
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002093 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2094 if (ret >= 0) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002095 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002096 }
2097
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08002098 audio_extn_hfp_set_parameters(adev, parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002099done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002100 str_parms_destroy(parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002101 pthread_mutex_unlock(&adev->lock);
Eric Laurent03f09432014-03-25 18:09:11 -07002102 ALOGV("%s: exit with code(%d)", __func__, status);
2103 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002104}
2105
2106static char* adev_get_parameters(const struct audio_hw_device *dev,
2107 const char *keys)
2108{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002109 struct audio_device *adev = (struct audio_device *)dev;
2110 struct str_parms *reply = str_parms_create();
2111 struct str_parms *query = str_parms_create_str(keys);
2112 char *str;
2113
2114 pthread_mutex_lock(&adev->lock);
2115
2116 voice_get_parameters(adev, query, reply);
2117 str = str_parms_to_str(reply);
2118 str_parms_destroy(query);
2119 str_parms_destroy(reply);
2120
2121 pthread_mutex_unlock(&adev->lock);
2122 ALOGV("%s: exit: returns - %s", __func__, str);
2123 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002124}
2125
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002126static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002127{
2128 return 0;
2129}
2130
Haynes Mathew George5191a852013-09-11 14:19:36 -07002131static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2132{
2133 int ret;
2134 struct audio_device *adev = (struct audio_device *)dev;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002135
Haynes Mathew George5191a852013-09-11 14:19:36 -07002136 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002137 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002138 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002139
Haynes Mathew George5191a852013-09-11 14:19:36 -07002140 return ret;
2141}
2142
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002143static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002144{
2145 return -ENOSYS;
2146}
2147
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002148static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2149 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002150{
2151 return -ENOSYS;
2152}
2153
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002154static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002155{
2156 return -ENOSYS;
2157}
2158
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002159static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002160{
2161 return -ENOSYS;
2162}
2163
2164static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2165{
2166 struct audio_device *adev = (struct audio_device *)dev;
2167
2168 pthread_mutex_lock(&adev->lock);
2169 if (adev->mode != mode) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002170 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002171 adev->mode = mode;
2172 }
2173 pthread_mutex_unlock(&adev->lock);
2174 return 0;
2175}
2176
2177static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2178{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002179 int ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002180 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002181
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002182 ALOGD("%s: state %d\n", __func__, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002183 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002184 ret = voice_set_mic_mute(adev, state);
Eric Laurenta609e8e2014-06-18 02:15:17 +00002185 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002186
2187 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002188}
2189
2190static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2191{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002192 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002193 return 0;
2194}
2195
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002196static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002197 const struct audio_config *config)
2198{
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002199 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002200
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002201 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
2202 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002203}
2204
2205static int adev_open_input_stream(struct audio_hw_device *dev,
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002206 audio_io_handle_t handle __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002207 audio_devices_t devices,
2208 struct audio_config *config,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002209 struct audio_stream_in **stream_in,
Eric Laurent91975a62014-07-27 17:15:50 -07002210 audio_input_flags_t flags,
2211 const char *address __unused,
2212 audio_source_t source __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002213{
2214 struct audio_device *adev = (struct audio_device *)dev;
2215 struct stream_in *in;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002216 int ret = 0, buffer_size, frame_size;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002217 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002218
Eric Laurent994a6932013-07-17 11:51:42 -07002219 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002220 *stream_in = NULL;
2221 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2222 return -EINVAL;
2223
2224 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2225
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002226 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2227
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002228 in->stream.common.get_sample_rate = in_get_sample_rate;
2229 in->stream.common.set_sample_rate = in_set_sample_rate;
2230 in->stream.common.get_buffer_size = in_get_buffer_size;
2231 in->stream.common.get_channels = in_get_channels;
2232 in->stream.common.get_format = in_get_format;
2233 in->stream.common.set_format = in_set_format;
2234 in->stream.common.standby = in_standby;
2235 in->stream.common.dump = in_dump;
2236 in->stream.common.set_parameters = in_set_parameters;
2237 in->stream.common.get_parameters = in_get_parameters;
2238 in->stream.common.add_audio_effect = in_add_audio_effect;
2239 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2240 in->stream.set_gain = in_set_gain;
2241 in->stream.read = in_read;
2242 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2243
2244 in->device = devices;
2245 in->source = AUDIO_SOURCE_DEFAULT;
2246 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002247 in->standby = 1;
2248 in->channel_mask = config->channel_mask;
2249
2250 /* Update config params with the requested sample rate and channels */
Vineeta Srivastava55a12932014-08-11 11:41:18 -07002251 in->usecase = USECASE_AUDIO_RECORD;
2252 bool is_low_latency = false;
2253 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
2254 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
2255 is_low_latency = true;
Glenn Kasten4f993392014-05-14 07:30:48 -07002256#if LOW_LATENCY_CAPTURE_USE_CASE
Vineeta Srivastava55a12932014-08-11 11:41:18 -07002257 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Glenn Kasten4f993392014-05-14 07:30:48 -07002258#endif
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002259 }
Vineeta Srivastava55a12932014-08-11 11:41:18 -07002260 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002261 in->config.channels = channel_count;
2262 in->config.rate = config->sample_rate;
2263
Vineeta Srivastava55a12932014-08-11 11:41:18 -07002264 frame_size = audio_stream_in_frame_size(&in->stream);
2265 buffer_size = get_input_buffer_size(config->sample_rate,
2266 config->format,
2267 channel_count,
2268 is_low_latency);
2269 in->config.period_size = buffer_size / frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002270
2271 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002272 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002273 return 0;
2274
2275err_open:
2276 free(in);
2277 *stream_in = NULL;
2278 return ret;
2279}
2280
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002281static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002282 struct audio_stream_in *stream)
2283{
Eric Laurent994a6932013-07-17 11:51:42 -07002284 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002285
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002286 in_standby(&stream->common);
2287 free(stream);
2288
2289 return;
2290}
2291
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002292static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002293{
2294 return 0;
2295}
2296
Andy Hung31aca912014-03-20 17:14:59 -07002297/* verifies input and output devices and their capabilities.
2298 *
2299 * This verification is required when enabling extended bit-depth or
2300 * sampling rates, as not all qcom products support it.
2301 *
2302 * Suitable for calling only on initialization such as adev_open().
2303 * It fills the audio_device use_case_table[] array.
2304 *
2305 * Has a side-effect that it needs to configure audio routing / devices
2306 * in order to power up the devices and read the device parameters.
2307 * It does not acquire any hw device lock. Should restore the devices
2308 * back to "normal state" upon completion.
2309 */
2310static int adev_verify_devices(struct audio_device *adev)
2311{
2312 /* enumeration is a bit difficult because one really wants to pull
2313 * the use_case, device id, etc from the hidden pcm_device_table[].
2314 * In this case there are the following use cases and device ids.
2315 *
2316 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
2317 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
2318 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
2319 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
2320 * [USECASE_AUDIO_RECORD] = {0, 0},
2321 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
2322 * [USECASE_VOICE_CALL] = {2, 2},
2323 *
2324 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
2325 * USECASE_VOICE_CALL omitted, but possible for either input or output.
2326 */
2327
2328 /* should be the usecases enabled in adev_open_input_stream() */
2329 static const int test_in_usecases[] = {
2330 USECASE_AUDIO_RECORD,
2331 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
2332 };
2333 /* should be the usecases enabled in adev_open_output_stream()*/
2334 static const int test_out_usecases[] = {
2335 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
2336 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
2337 };
2338 static const usecase_type_t usecase_type_by_dir[] = {
2339 PCM_PLAYBACK,
2340 PCM_CAPTURE,
2341 };
2342 static const unsigned flags_by_dir[] = {
2343 PCM_OUT,
2344 PCM_IN,
2345 };
2346
2347 size_t i;
2348 unsigned dir;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002349 const unsigned card_id = adev->snd_card;
Andy Hung31aca912014-03-20 17:14:59 -07002350 char info[512]; /* for possible debug info */
2351
2352 for (dir = 0; dir < 2; ++dir) {
2353 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
2354 const unsigned flags_dir = flags_by_dir[dir];
2355 const size_t testsize =
2356 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
2357 const int *testcases =
2358 dir ? test_in_usecases : test_out_usecases;
2359 const audio_devices_t audio_device =
2360 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
2361
2362 for (i = 0; i < testsize; ++i) {
2363 const audio_usecase_t audio_usecase = testcases[i];
2364 int device_id;
2365 snd_device_t snd_device;
2366 struct pcm_params **pparams;
2367 struct stream_out out;
2368 struct stream_in in;
2369 struct audio_usecase uc_info;
2370 int retval;
2371
2372 pparams = &adev->use_case_table[audio_usecase];
2373 pcm_params_free(*pparams); /* can accept null input */
2374 *pparams = NULL;
2375
2376 /* find the device ID for the use case (signed, for error) */
2377 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
2378 if (device_id < 0)
2379 continue;
2380
2381 /* prepare structures for device probing */
2382 memset(&uc_info, 0, sizeof(uc_info));
2383 uc_info.id = audio_usecase;
2384 uc_info.type = usecase_type;
2385 if (dir) {
2386 adev->active_input = &in;
2387 memset(&in, 0, sizeof(in));
2388 in.device = audio_device;
2389 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
2390 uc_info.stream.in = &in;
2391 } else {
2392 adev->active_input = NULL;
2393 }
2394 memset(&out, 0, sizeof(out));
2395 out.devices = audio_device; /* only field needed in select_devices */
2396 uc_info.stream.out = &out;
2397 uc_info.devices = audio_device;
2398 uc_info.in_snd_device = SND_DEVICE_NONE;
2399 uc_info.out_snd_device = SND_DEVICE_NONE;
2400 list_add_tail(&adev->usecase_list, &uc_info.list);
2401
2402 /* select device - similar to start_(in/out)put_stream() */
2403 retval = select_devices(adev, audio_usecase);
2404 if (retval >= 0) {
2405 *pparams = pcm_params_get(card_id, device_id, flags_dir);
2406#if LOG_NDEBUG == 0
2407 if (*pparams) {
2408 ALOGV("%s: (%s) card %d device %d", __func__,
2409 dir ? "input" : "output", card_id, device_id);
2410 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
2411 ALOGV(info); /* print parameters */
2412 } else {
2413 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
2414 }
2415#endif
2416 }
2417
2418 /* deselect device - similar to stop_(in/out)put_stream() */
2419 /* 1. Get and set stream specific mixer controls */
Glenn Kastene7137302014-04-28 15:12:18 -07002420 retval = disable_audio_route(adev, &uc_info);
Andy Hung31aca912014-03-20 17:14:59 -07002421 /* 2. Disable the rx device */
2422 retval = disable_snd_device(adev,
Glenn Kastene7137302014-04-28 15:12:18 -07002423 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
Andy Hung31aca912014-03-20 17:14:59 -07002424 list_remove(&uc_info.list);
2425 }
2426 }
2427 adev->active_input = NULL; /* restore adev state */
2428 return 0;
2429}
2430
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002431static int adev_close(hw_device_t *device)
2432{
Andy Hung31aca912014-03-20 17:14:59 -07002433 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002434 struct audio_device *adev = (struct audio_device *)device;
2435 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002436 free(adev->snd_dev_ref_cnt);
2437 platform_deinit(adev->platform);
Andy Hung31aca912014-03-20 17:14:59 -07002438 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
2439 pcm_params_free(adev->use_case_table[i]);
2440 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002441 free(device);
2442 return 0;
2443}
2444
Glenn Kasten4f993392014-05-14 07:30:48 -07002445/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
2446 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
2447 * just that it _might_ work.
2448 */
2449static int period_size_is_plausible_for_low_latency(int period_size)
2450{
2451 switch (period_size) {
2452 case 160:
2453 case 240:
2454 case 320:
2455 case 480:
2456 return 1;
2457 default:
2458 return 0;
2459 }
2460}
2461
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002462static int adev_open(const hw_module_t *module, const char *name,
2463 hw_device_t **device)
2464{
2465 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002466 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002467
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002468 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002469 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2470
2471 adev = calloc(1, sizeof(struct audio_device));
2472
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002473 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2474
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002475 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2476 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2477 adev->device.common.module = (struct hw_module_t *)module;
2478 adev->device.common.close = adev_close;
2479
2480 adev->device.init_check = adev_init_check;
2481 adev->device.set_voice_volume = adev_set_voice_volume;
2482 adev->device.set_master_volume = adev_set_master_volume;
2483 adev->device.get_master_volume = adev_get_master_volume;
2484 adev->device.set_master_mute = adev_set_master_mute;
2485 adev->device.get_master_mute = adev_get_master_mute;
2486 adev->device.set_mode = adev_set_mode;
2487 adev->device.set_mic_mute = adev_set_mic_mute;
2488 adev->device.get_mic_mute = adev_get_mic_mute;
2489 adev->device.set_parameters = adev_set_parameters;
2490 adev->device.get_parameters = adev_get_parameters;
2491 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2492 adev->device.open_output_stream = adev_open_output_stream;
2493 adev->device.close_output_stream = adev_close_output_stream;
2494 adev->device.open_input_stream = adev_open_input_stream;
2495 adev->device.close_input_stream = adev_close_input_stream;
2496 adev->device.dump = adev_dump;
2497
2498 /* Set the default route before the PCM stream is opened */
2499 pthread_mutex_lock(&adev->lock);
2500 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002501 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002502 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002503 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002504 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002505 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002506 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002507 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002508 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002509 pthread_mutex_unlock(&adev->lock);
2510
2511 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002512 adev->platform = platform_init(adev);
2513 if (!adev->platform) {
2514 free(adev->snd_dev_ref_cnt);
2515 free(adev);
2516 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2517 *device = NULL;
2518 return -EINVAL;
2519 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002520
2521 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2522 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2523 if (adev->visualizer_lib == NULL) {
2524 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2525 } else {
2526 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2527 adev->visualizer_start_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002528 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002529 "visualizer_hal_start_output");
2530 adev->visualizer_stop_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002531 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002532 "visualizer_hal_stop_output");
2533 }
2534 }
2535
Haynes Mathew George41f86652014-06-17 14:22:15 -07002536 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2537 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2538 if (adev->offload_effects_lib == NULL) {
2539 ALOGE("%s: DLOPEN failed for %s", __func__,
2540 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2541 } else {
2542 ALOGV("%s: DLOPEN successful for %s", __func__,
2543 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2544 adev->offload_effects_start_output =
2545 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2546 "offload_effects_bundle_hal_start_output");
2547 adev->offload_effects_stop_output =
2548 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2549 "offload_effects_bundle_hal_stop_output");
2550 }
2551 }
2552
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002553 adev->bt_wb_speech_enabled = false;
2554
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002555 *device = &adev->device.common;
Andy Hung31aca912014-03-20 17:14:59 -07002556 if (k_enable_extended_precision)
2557 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002558
Glenn Kasten4f993392014-05-14 07:30:48 -07002559 char value[PROPERTY_VALUE_MAX];
2560 int trial;
2561 if (property_get("audio_hal.period_size", value, NULL) > 0) {
2562 trial = atoi(value);
2563 if (period_size_is_plausible_for_low_latency(trial)) {
2564 pcm_config_low_latency.period_size = trial;
2565 pcm_config_low_latency.start_threshold = trial / 4;
2566 pcm_config_low_latency.avail_min = trial / 4;
2567 configured_low_latency_capture_period_size = trial;
2568 }
2569 }
2570 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
2571 trial = atoi(value);
2572 if (period_size_is_plausible_for_low_latency(trial)) {
2573 configured_low_latency_capture_period_size = trial;
2574 }
2575 }
2576
Eric Laurent994a6932013-07-17 11:51:42 -07002577 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002578 return 0;
2579}
2580
2581static struct hw_module_methods_t hal_module_methods = {
2582 .open = adev_open,
2583};
2584
2585struct audio_module HAL_MODULE_INFO_SYM = {
2586 .common = {
2587 .tag = HARDWARE_MODULE_TAG,
2588 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2589 .hal_api_version = HARDWARE_HAL_API_VERSION,
2590 .id = AUDIO_HARDWARE_MODULE_ID,
2591 .name = "QCOM Audio HAL",
2592 .author = "Code Aurora Forum",
2593 .methods = &hal_module_methods,
2594 },
2595};