blob: a8cc733437f7c0e566c7162631c038bb10cb6fae [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) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700541 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700542 adev->primary_output->devices);
543 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700544 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);
683 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800684 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;
Eric Laurentc8400632013-02-14 19:04:54 -0800689 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
Eric Laurentc4aef752013-09-12 17:45:53 -0700936 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
937 adev->visualizer_stop_output != NULL)
938 adev->visualizer_stop_output(out->handle);
939
Eric Laurent150dbfe2013-02-27 14:31:02 -0800940 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700941 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700942
943 /* 2. Disable the rx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700944 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800945
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800946 list_remove(&uc_info->list);
947 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800948
Eric Laurent07eeafd2013-10-06 12:52:49 -0700949 /* Must be called after removing the usecase from list */
950 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
951 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
952
Eric Laurent994a6932013-07-17 11:51:42 -0700953 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800954 return ret;
955}
956
957int start_output_stream(struct stream_out *out)
958{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800959 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800960 struct audio_usecase *uc_info;
961 struct audio_device *adev = out->dev;
962
Eric Laurent994a6932013-07-17 11:51:42 -0700963 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700964 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -0700965 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800966 if (out->pcm_device_id < 0) {
967 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
968 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800969 ret = -EINVAL;
970 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800971 }
972
973 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
974 uc_info->id = out->usecase;
975 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800976 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700977 uc_info->devices = out->devices;
978 uc_info->in_snd_device = SND_DEVICE_NONE;
979 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800980
Eric Laurent07eeafd2013-10-06 12:52:49 -0700981 /* This must be called before adding this usecase to the list */
982 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
983 check_and_set_hdmi_channels(adev, out->config.channels);
984
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800985 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800986
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700987 select_devices(adev, out->usecase);
988
Andy Hung31aca912014-03-20 17:14:59 -0700989 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700990 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700991 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700992 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -0700993 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700994 if (out->pcm && !pcm_is_ready(out->pcm)) {
995 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
996 pcm_close(out->pcm);
997 out->pcm = NULL;
998 ret = -EIO;
999 goto error_open;
1000 }
1001 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001002 out->pcm = NULL;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001003 out->compr = compress_open(adev->snd_card, out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001004 COMPRESS_IN, &out->compr_config);
1005 if (out->compr && !is_compress_ready(out->compr)) {
1006 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1007 compress_close(out->compr);
1008 out->compr = NULL;
1009 ret = -EIO;
1010 goto error_open;
1011 }
1012 if (out->offload_callback)
1013 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001014
1015 if (adev->visualizer_start_output != NULL)
1016 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001017 }
Eric Laurent994a6932013-07-17 11:51:42 -07001018 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001019 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001020error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001021 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001022error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001023 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001024}
1025
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026static int check_input_parameters(uint32_t sample_rate,
1027 audio_format_t format,
1028 int channel_count)
1029{
1030 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1031
1032 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1033
1034 switch (sample_rate) {
1035 case 8000:
1036 case 11025:
1037 case 12000:
1038 case 16000:
1039 case 22050:
1040 case 24000:
1041 case 32000:
1042 case 44100:
1043 case 48000:
1044 break;
1045 default:
1046 return -EINVAL;
1047 }
1048
1049 return 0;
1050}
1051
1052static size_t get_input_buffer_size(uint32_t sample_rate,
1053 audio_format_t format,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001054 int channel_count,
1055 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001056{
1057 size_t size = 0;
1058
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001059 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1060 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001061
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001062 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001063 if (is_low_latency)
Glenn Kasten4f993392014-05-14 07:30:48 -07001064 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001065 /* ToDo: should use frame_size computed based on the format and
1066 channel_count here. */
1067 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001068
Glenn Kasten4f993392014-05-14 07:30:48 -07001069 /* make sure the size is multiple of 32 bytes
1070 * At 48 kHz mono 16-bit PCM:
1071 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1072 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1073 */
1074 size += 0x1f;
1075 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001076
1077 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001078}
1079
1080static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1081{
1082 struct stream_out *out = (struct stream_out *)stream;
1083
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001084 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001085}
1086
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001087static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001088{
1089 return -ENOSYS;
1090}
1091
1092static size_t out_get_buffer_size(const struct audio_stream *stream)
1093{
1094 struct stream_out *out = (struct stream_out *)stream;
1095
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001096 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1097 return out->compr_config.fragment_size;
1098 }
1099
Eric Laurentfdf296a2014-07-03 16:41:51 -07001100 return out->config.period_size *
1101 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001102}
1103
1104static uint32_t out_get_channels(const struct audio_stream *stream)
1105{
1106 struct stream_out *out = (struct stream_out *)stream;
1107
1108 return out->channel_mask;
1109}
1110
1111static audio_format_t out_get_format(const struct audio_stream *stream)
1112{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001113 struct stream_out *out = (struct stream_out *)stream;
1114
1115 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001116}
1117
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001118static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001119{
1120 return -ENOSYS;
1121}
1122
1123static int out_standby(struct audio_stream *stream)
1124{
1125 struct stream_out *out = (struct stream_out *)stream;
1126 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001127
Eric Laurent994a6932013-07-17 11:51:42 -07001128 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001129 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001130
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001131 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001132 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001133 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001134 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001135 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1136 if (out->pcm) {
1137 pcm_close(out->pcm);
1138 out->pcm = NULL;
1139 }
1140 } else {
1141 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001142 out->gapless_mdata.encoder_delay = 0;
1143 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001144 if (out->compr != NULL) {
1145 compress_close(out->compr);
1146 out->compr = NULL;
1147 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001148 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001149 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001150 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001151 }
1152 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001153 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001154 return 0;
1155}
1156
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001157static int out_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001158{
1159 return 0;
1160}
1161
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001162static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1163{
1164 int ret = 0;
1165 char value[32];
1166 struct compr_gapless_mdata tmp_mdata;
1167
1168 if (!out || !parms) {
1169 return -EINVAL;
1170 }
1171
1172 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1173 if (ret >= 0) {
1174 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1175 } else {
1176 return -EINVAL;
1177 }
1178
1179 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1180 if (ret >= 0) {
1181 tmp_mdata.encoder_padding = atoi(value);
1182 } else {
1183 return -EINVAL;
1184 }
1185
1186 out->gapless_mdata = tmp_mdata;
1187 out->send_new_metadata = 1;
1188 ALOGV("%s new encoder delay %u and padding %u", __func__,
1189 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1190
1191 return 0;
1192}
1193
1194
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001195static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1196{
1197 struct stream_out *out = (struct stream_out *)stream;
1198 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001199 struct audio_usecase *usecase;
1200 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201 struct str_parms *parms;
1202 char value[32];
1203 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001204 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001205 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001206
sangwoobc677242013-08-08 16:53:43 +09001207 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001208 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001209 parms = str_parms_create_str(kvpairs);
1210 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1211 if (ret >= 0) {
1212 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001213 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001214 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001215
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001216 /*
1217 * When HDMI cable is unplugged the music playback is paused and
1218 * the policy manager sends routing=0. But the audioflinger
1219 * continues to write data until standby time (3sec).
1220 * As the HDMI core is turned off, the write gets blocked.
1221 * Avoid this by routing audio to speaker until standby.
1222 */
1223 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1224 val == AUDIO_DEVICE_NONE) {
1225 val = AUDIO_DEVICE_OUT_SPEAKER;
1226 }
1227
1228 /*
1229 * select_devices() call below switches all the usecases on the same
1230 * backend to the new device. Refer to check_usecases_codec_backend() in
1231 * the select_devices(). But how do we undo this?
1232 *
1233 * For example, music playback is active on headset (deep-buffer usecase)
1234 * and if we go to ringtones and select a ringtone, low-latency usecase
1235 * will be started on headset+speaker. As we can't enable headset+speaker
1236 * and headset devices at the same time, select_devices() switches the music
1237 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1238 * So when the ringtone playback is completed, how do we undo the same?
1239 *
1240 * We are relying on the out_set_parameters() call on deep-buffer output,
1241 * once the ringtone playback is ended.
1242 * NOTE: We should not check if the current devices are same as new devices.
1243 * Because select_devices() must be called to switch back the music
1244 * playback to headset.
1245 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001246 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001247 out->devices = val;
1248
1249 if (!out->standby)
1250 select_devices(adev, out->usecase);
1251
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001252 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1253 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001254 (out == adev->primary_output)) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001255 ret = voice_start_call(adev);
1256 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1257 voice_is_in_call(adev) &&
1258 (out == adev->primary_output)) {
1259 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001260 }
1261 }
1262
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001263 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1264 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001265 (out == adev->primary_output)) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001266 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001267 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001268
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001269 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001270 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001271 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001272
1273 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1274 parse_compress_metadata(out, parms);
1275 }
1276
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001278 ALOGV("%s: exit: code(%d)", __func__, status);
1279 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001280}
1281
1282static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1283{
1284 struct stream_out *out = (struct stream_out *)stream;
1285 struct str_parms *query = str_parms_create_str(keys);
1286 char *str;
1287 char value[256];
1288 struct str_parms *reply = str_parms_create();
1289 size_t i, j;
1290 int ret;
1291 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001292 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001293 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1294 if (ret >= 0) {
1295 value[0] = '\0';
1296 i = 0;
1297 while (out->supported_channel_masks[i] != 0) {
1298 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1299 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1300 if (!first) {
1301 strcat(value, "|");
1302 }
1303 strcat(value, out_channels_name_to_enum_table[j].name);
1304 first = false;
1305 break;
1306 }
1307 }
1308 i++;
1309 }
1310 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1311 str = str_parms_to_str(reply);
1312 } else {
1313 str = strdup(keys);
1314 }
1315 str_parms_destroy(query);
1316 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001317 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001318 return str;
1319}
1320
1321static uint32_t out_get_latency(const struct audio_stream_out *stream)
1322{
1323 struct stream_out *out = (struct stream_out *)stream;
1324
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001325 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1326 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1327
1328 return (out->config.period_count * out->config.period_size * 1000) /
1329 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001330}
1331
1332static int out_set_volume(struct audio_stream_out *stream, float left,
1333 float right)
1334{
Eric Laurenta9024de2013-04-04 09:19:12 -07001335 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001336 int volume[2];
1337
Eric Laurenta9024de2013-04-04 09:19:12 -07001338 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1339 /* only take left channel into account: the API is for stereo anyway */
1340 out->muted = (left == 0.0f);
1341 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001342 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1343 const char *mixer_ctl_name = "Compress Playback Volume";
1344 struct audio_device *adev = out->dev;
1345 struct mixer_ctl *ctl;
1346
1347 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1348 if (!ctl) {
Haynes Mathew George20bcfa82014-06-25 13:37:03 -07001349 /* try with the control based on device id */
1350 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1351 PCM_PLAYBACK);
1352 char ctl_name[128] = {0};
1353 snprintf(ctl_name, sizeof(ctl_name),
1354 "Compress Playback %d Volume", pcm_device_id);
1355 ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name);
1356 if (!ctl) {
1357 ALOGE("%s: Could not get volume ctl mixer cmd", __func__);
1358 return -EINVAL;
1359 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001360 }
1361 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1362 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1363 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1364 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001365 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001366
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001367 return -ENOSYS;
1368}
1369
1370static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1371 size_t bytes)
1372{
1373 struct stream_out *out = (struct stream_out *)stream;
1374 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001375 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001376
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001377 pthread_mutex_lock(&out->lock);
1378 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001379 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001380 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001381 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001382 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001383 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001384 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001385 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001386 goto exit;
1387 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001388 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001389
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001390 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001391 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1392 if (out->send_new_metadata) {
1393 ALOGVV("send new gapless metadata");
1394 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1395 out->send_new_metadata = 0;
1396 }
1397
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001398 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001399 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001400 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001401 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1402 }
1403 if (!out->playback_started) {
1404 compress_start(out->compr);
1405 out->playback_started = 1;
1406 out->offload_state = OFFLOAD_STATE_PLAYING;
1407 }
1408 pthread_mutex_unlock(&out->lock);
1409 return ret;
1410 } else {
1411 if (out->pcm) {
1412 if (out->muted)
1413 memset((void *)buffer, 0, bytes);
1414 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1415 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001416 if (ret == 0)
1417 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001418 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001419 }
1420
1421exit:
1422 pthread_mutex_unlock(&out->lock);
1423
1424 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001425 if (out->pcm)
1426 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001427 out_standby(&out->stream.common);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001428 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001429 out_get_sample_rate(&out->stream.common));
1430 }
1431 return bytes;
1432}
1433
1434static int out_get_render_position(const struct audio_stream_out *stream,
1435 uint32_t *dsp_frames)
1436{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001437 struct stream_out *out = (struct stream_out *)stream;
1438 *dsp_frames = 0;
1439 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1440 pthread_mutex_lock(&out->lock);
1441 if (out->compr != NULL) {
1442 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1443 &out->sample_rate);
1444 ALOGVV("%s rendered frames %d sample_rate %d",
1445 __func__, *dsp_frames, out->sample_rate);
1446 }
1447 pthread_mutex_unlock(&out->lock);
1448 return 0;
1449 } else
1450 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001451}
1452
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001453static int out_add_audio_effect(const struct audio_stream *stream __unused,
1454 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001455{
1456 return 0;
1457}
1458
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001459static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1460 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001461{
1462 return 0;
1463}
1464
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001465static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1466 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001467{
1468 return -EINVAL;
1469}
1470
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001471static int out_get_presentation_position(const struct audio_stream_out *stream,
1472 uint64_t *frames, struct timespec *timestamp)
1473{
1474 struct stream_out *out = (struct stream_out *)stream;
1475 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001476 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001477
1478 pthread_mutex_lock(&out->lock);
1479
Eric Laurent949a0892013-09-20 09:20:13 -07001480 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1481 if (out->compr != NULL) {
1482 compress_get_tstamp(out->compr, &dsp_frames,
1483 &out->sample_rate);
1484 ALOGVV("%s rendered frames %ld sample_rate %d",
1485 __func__, dsp_frames, out->sample_rate);
1486 *frames = dsp_frames;
1487 ret = 0;
1488 /* this is the best we can do */
1489 clock_gettime(CLOCK_MONOTONIC, timestamp);
1490 }
1491 } else {
1492 if (out->pcm) {
1493 size_t avail;
1494 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1495 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001496 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001497 // This adjustment accounts for buffering after app processor.
1498 // It is based on estimated DSP latency per use case, rather than exact.
1499 signed_frames -=
1500 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1501
Eric Laurent949a0892013-09-20 09:20:13 -07001502 // It would be unusual for this value to be negative, but check just in case ...
1503 if (signed_frames >= 0) {
1504 *frames = signed_frames;
1505 ret = 0;
1506 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001507 }
1508 }
1509 }
1510
1511 pthread_mutex_unlock(&out->lock);
1512
1513 return ret;
1514}
1515
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001516static int out_set_callback(struct audio_stream_out *stream,
1517 stream_callback_t callback, void *cookie)
1518{
1519 struct stream_out *out = (struct stream_out *)stream;
1520
1521 ALOGV("%s", __func__);
1522 pthread_mutex_lock(&out->lock);
1523 out->offload_callback = callback;
1524 out->offload_cookie = cookie;
1525 pthread_mutex_unlock(&out->lock);
1526 return 0;
1527}
1528
1529static int out_pause(struct audio_stream_out* stream)
1530{
1531 struct stream_out *out = (struct stream_out *)stream;
1532 int status = -ENOSYS;
1533 ALOGV("%s", __func__);
1534 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1535 pthread_mutex_lock(&out->lock);
1536 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1537 status = compress_pause(out->compr);
1538 out->offload_state = OFFLOAD_STATE_PAUSED;
1539 }
1540 pthread_mutex_unlock(&out->lock);
1541 }
1542 return status;
1543}
1544
1545static int out_resume(struct audio_stream_out* stream)
1546{
1547 struct stream_out *out = (struct stream_out *)stream;
1548 int status = -ENOSYS;
1549 ALOGV("%s", __func__);
1550 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1551 status = 0;
1552 pthread_mutex_lock(&out->lock);
1553 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1554 status = compress_resume(out->compr);
1555 out->offload_state = OFFLOAD_STATE_PLAYING;
1556 }
1557 pthread_mutex_unlock(&out->lock);
1558 }
1559 return status;
1560}
1561
1562static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1563{
1564 struct stream_out *out = (struct stream_out *)stream;
1565 int status = -ENOSYS;
1566 ALOGV("%s", __func__);
1567 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1568 pthread_mutex_lock(&out->lock);
1569 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1570 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1571 else
1572 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1573 pthread_mutex_unlock(&out->lock);
1574 }
1575 return status;
1576}
1577
1578static int out_flush(struct audio_stream_out* stream)
1579{
1580 struct stream_out *out = (struct stream_out *)stream;
1581 ALOGV("%s", __func__);
1582 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1583 pthread_mutex_lock(&out->lock);
1584 stop_compressed_output_l(out);
1585 pthread_mutex_unlock(&out->lock);
1586 return 0;
1587 }
1588 return -ENOSYS;
1589}
1590
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001591/** audio_stream_in implementation **/
1592static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1593{
1594 struct stream_in *in = (struct stream_in *)stream;
1595
1596 return in->config.rate;
1597}
1598
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001599static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001600{
1601 return -ENOSYS;
1602}
1603
1604static size_t in_get_buffer_size(const struct audio_stream *stream)
1605{
1606 struct stream_in *in = (struct stream_in *)stream;
1607
Eric Laurentfdf296a2014-07-03 16:41:51 -07001608 return in->config.period_size *
1609 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001610}
1611
1612static uint32_t in_get_channels(const struct audio_stream *stream)
1613{
1614 struct stream_in *in = (struct stream_in *)stream;
1615
1616 return in->channel_mask;
1617}
1618
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001619static audio_format_t in_get_format(const struct audio_stream *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001620{
1621 return AUDIO_FORMAT_PCM_16_BIT;
1622}
1623
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001624static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001625{
1626 return -ENOSYS;
1627}
1628
1629static int in_standby(struct audio_stream *stream)
1630{
1631 struct stream_in *in = (struct stream_in *)stream;
1632 struct audio_device *adev = in->dev;
1633 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001634 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001635 pthread_mutex_lock(&in->lock);
1636 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001637 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001638 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001639 if (in->pcm) {
1640 pcm_close(in->pcm);
1641 in->pcm = NULL;
1642 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001643 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001644 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001645 }
1646 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001647 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001648 return status;
1649}
1650
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001651static int in_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001652{
1653 return 0;
1654}
1655
1656static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1657{
1658 struct stream_in *in = (struct stream_in *)stream;
1659 struct audio_device *adev = in->dev;
1660 struct str_parms *parms;
1661 char *str;
1662 char value[32];
1663 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07001664 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001665
Eric Laurent994a6932013-07-17 11:51:42 -07001666 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001667 parms = str_parms_create_str(kvpairs);
1668
1669 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1670
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001671 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001672 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001673 if (ret >= 0) {
1674 val = atoi(value);
1675 /* no audio source uses val == 0 */
1676 if ((in->source != val) && (val != 0)) {
1677 in->source = val;
1678 }
1679 }
1680
1681 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07001682
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001683 if (ret >= 0) {
1684 val = atoi(value);
1685 if ((in->device != val) && (val != 0)) {
1686 in->device = val;
1687 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001688 if (!in->standby)
Eric Laurent03f09432014-03-25 18:09:11 -07001689 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001690 }
1691 }
1692
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001693 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001694 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001695
1696 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001697 ALOGV("%s: exit: status(%d)", __func__, status);
1698 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001699}
1700
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001701static char* in_get_parameters(const struct audio_stream *stream __unused,
1702 const char *keys __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001703{
1704 return strdup("");
1705}
1706
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001707static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001708{
1709 return 0;
1710}
1711
1712static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1713 size_t bytes)
1714{
1715 struct stream_in *in = (struct stream_in *)stream;
1716 struct audio_device *adev = in->dev;
1717 int i, ret = -1;
1718
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001719 pthread_mutex_lock(&in->lock);
1720 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001721 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001722 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001723 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001724 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001725 goto exit;
1726 }
1727 in->standby = 0;
1728 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001729
1730 if (in->pcm) {
1731 ret = pcm_read(in->pcm, buffer, bytes);
1732 }
1733
1734 /*
1735 * Instead of writing zeroes here, we could trust the hardware
1736 * to always provide zeroes when muted.
1737 */
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001738 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001739 memset(buffer, 0, bytes);
1740
1741exit:
1742 pthread_mutex_unlock(&in->lock);
1743
1744 if (ret != 0) {
1745 in_standby(&in->stream.common);
1746 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001747 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001748 in_get_sample_rate(&in->stream.common));
1749 }
1750 return bytes;
1751}
1752
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001753static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001754{
1755 return 0;
1756}
1757
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001758static int add_remove_audio_effect(const struct audio_stream *stream,
1759 effect_handle_t effect,
1760 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001761{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001762 struct stream_in *in = (struct stream_in *)stream;
1763 int status = 0;
1764 effect_descriptor_t desc;
1765
1766 status = (*effect)->get_descriptor(effect, &desc);
1767 if (status != 0)
1768 return status;
1769
1770 pthread_mutex_lock(&in->lock);
1771 pthread_mutex_lock(&in->dev->lock);
1772 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1773 in->enable_aec != enable &&
1774 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1775 in->enable_aec = enable;
1776 if (!in->standby)
1777 select_devices(in->dev, in->usecase);
1778 }
1779 pthread_mutex_unlock(&in->dev->lock);
1780 pthread_mutex_unlock(&in->lock);
1781
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001782 return 0;
1783}
1784
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001785static int in_add_audio_effect(const struct audio_stream *stream,
1786 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001787{
Eric Laurent994a6932013-07-17 11:51:42 -07001788 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001789 return add_remove_audio_effect(stream, effect, true);
1790}
1791
1792static int in_remove_audio_effect(const struct audio_stream *stream,
1793 effect_handle_t effect)
1794{
Eric Laurent994a6932013-07-17 11:51:42 -07001795 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001796 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001797}
1798
1799static int adev_open_output_stream(struct audio_hw_device *dev,
1800 audio_io_handle_t handle,
1801 audio_devices_t devices,
1802 audio_output_flags_t flags,
1803 struct audio_config *config,
1804 struct audio_stream_out **stream_out)
1805{
1806 struct audio_device *adev = (struct audio_device *)dev;
1807 struct stream_out *out;
1808 int i, ret;
1809
Eric Laurent994a6932013-07-17 11:51:42 -07001810 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001811 __func__, config->sample_rate, config->channel_mask, devices, flags);
1812 *stream_out = NULL;
1813 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1814
1815 if (devices == AUDIO_DEVICE_NONE)
1816 devices = AUDIO_DEVICE_OUT_SPEAKER;
1817
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001818 out->flags = flags;
1819 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001820 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001821 out->format = config->format;
1822 out->sample_rate = config->sample_rate;
1823 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1824 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001825 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001826
1827 /* Init use case and pcm_config */
1828 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07001829 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001830 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001831 pthread_mutex_lock(&adev->lock);
1832 ret = read_hdmi_channel_masks(out);
1833 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001834 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001835 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001836
1837 if (config->sample_rate == 0)
1838 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1839 if (config->channel_mask == 0)
1840 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1841
1842 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001843 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001844 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1845 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001846 out->config.rate = config->sample_rate;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07001847 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001848 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001849 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1850 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1851 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1852 ALOGE("%s: Unsupported Offload information", __func__);
1853 ret = -EINVAL;
1854 goto error_open;
1855 }
1856 if (!is_supported_format(config->offload_info.format)) {
1857 ALOGE("%s: Unsupported audio format", __func__);
1858 ret = -EINVAL;
1859 goto error_open;
1860 }
1861
1862 out->compr_config.codec = (struct snd_codec *)
1863 calloc(1, sizeof(struct snd_codec));
1864
1865 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1866 if (config->offload_info.channel_mask)
1867 out->channel_mask = config->offload_info.channel_mask;
1868 else if (config->channel_mask)
1869 out->channel_mask = config->channel_mask;
1870 out->format = config->offload_info.format;
1871 out->sample_rate = config->offload_info.sample_rate;
1872
1873 out->stream.set_callback = out_set_callback;
1874 out->stream.pause = out_pause;
1875 out->stream.resume = out_resume;
1876 out->stream.drain = out_drain;
1877 out->stream.flush = out_flush;
1878
1879 out->compr_config.codec->id =
1880 get_snd_codec_id(config->offload_info.format);
1881 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1882 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1883 out->compr_config.codec->sample_rate =
1884 compress_get_alsa_rate(config->offload_info.sample_rate);
1885 out->compr_config.codec->bit_rate =
1886 config->offload_info.bit_rate;
1887 out->compr_config.codec->ch_in =
Eric Laurent0de8d1f2014-07-01 20:34:45 -07001888 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001889 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1890
1891 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1892 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001893
1894 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001895 create_offload_callback_thread(out);
1896 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1897 __func__, config->offload_info.version,
1898 config->offload_info.bit_rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001899 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07001900 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1901 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1902 out->config = pcm_config_deep_buffer;
1903 } else {
1904 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1905 out->config = pcm_config_low_latency;
1906 }
1907 if (config->format != audio_format_from_pcm_format(out->config.format)) {
1908 if (k_enable_extended_precision
1909 && pcm_params_format_test(adev->use_case_table[out->usecase],
1910 pcm_format_from_audio_format(config->format))) {
1911 out->config.format = pcm_format_from_audio_format(config->format);
1912 /* out->format already set to config->format */
1913 } else {
1914 /* deny the externally proposed config format
1915 * and use the one specified in audio_hw layer configuration.
1916 * Note: out->format is returned by out->stream.common.get_format()
1917 * and is used to set config->format in the code several lines below.
1918 */
1919 out->format = audio_format_from_pcm_format(out->config.format);
1920 }
1921 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001922 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001923 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07001924 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
1925 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001926
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001927 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1928 if(adev->primary_output == NULL)
1929 adev->primary_output = out;
1930 else {
1931 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001932 ret = -EEXIST;
1933 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001934 }
1935 }
1936
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001937 /* Check if this usecase is already existing */
1938 pthread_mutex_lock(&adev->lock);
1939 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1940 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001941 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001942 ret = -EEXIST;
1943 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001944 }
1945 pthread_mutex_unlock(&adev->lock);
1946
1947 out->stream.common.get_sample_rate = out_get_sample_rate;
1948 out->stream.common.set_sample_rate = out_set_sample_rate;
1949 out->stream.common.get_buffer_size = out_get_buffer_size;
1950 out->stream.common.get_channels = out_get_channels;
1951 out->stream.common.get_format = out_get_format;
1952 out->stream.common.set_format = out_set_format;
1953 out->stream.common.standby = out_standby;
1954 out->stream.common.dump = out_dump;
1955 out->stream.common.set_parameters = out_set_parameters;
1956 out->stream.common.get_parameters = out_get_parameters;
1957 out->stream.common.add_audio_effect = out_add_audio_effect;
1958 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1959 out->stream.get_latency = out_get_latency;
1960 out->stream.set_volume = out_set_volume;
1961 out->stream.write = out_write;
1962 out->stream.get_render_position = out_get_render_position;
1963 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001964 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001965
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001966 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001967 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001968 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001969
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001970 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
1971 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
1972
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001973 config->format = out->stream.common.get_format(&out->stream.common);
1974 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1975 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1976
1977 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001978 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001979 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001980
1981error_open:
1982 free(out);
1983 *stream_out = NULL;
1984 ALOGD("%s: exit: ret %d", __func__, ret);
1985 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001986}
1987
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001988static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001989 struct audio_stream_out *stream)
1990{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001991 struct stream_out *out = (struct stream_out *)stream;
1992 struct audio_device *adev = out->dev;
1993
Eric Laurent994a6932013-07-17 11:51:42 -07001994 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001995 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001996 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1997 destroy_offload_callback_thread(out);
1998
1999 if (out->compr_config.codec != NULL)
2000 free(out->compr_config.codec);
2001 }
2002 pthread_cond_destroy(&out->cond);
2003 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002004 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002005 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002006}
2007
2008static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2009{
2010 struct audio_device *adev = (struct audio_device *)dev;
2011 struct str_parms *parms;
2012 char *str;
2013 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002014 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002015 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002016 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002017
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002018 ALOGD("%s: enter: %s", __func__, kvpairs);
2019
2020 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002021
2022 parms = str_parms_create_str(kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002023 status = voice_set_parameters(adev, parms);
2024 if (status != 0) {
2025 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002026 }
2027
2028 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2029 if (ret >= 0) {
2030 /* When set to false, HAL should disable EC and NS
2031 * But it is currently not supported.
2032 */
2033 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2034 adev->bluetooth_nrec = true;
2035 else
2036 adev->bluetooth_nrec = false;
2037 }
2038
2039 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2040 if (ret >= 0) {
2041 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2042 adev->screen_off = false;
2043 else
2044 adev->screen_off = true;
2045 }
2046
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002047 ret = str_parms_get_int(parms, "rotation", &val);
2048 if (ret >= 0) {
2049 bool reverse_speakers = false;
2050 switch(val) {
2051 // FIXME: note that the code below assumes that the speakers are in the correct placement
2052 // relative to the user when the device is rotated 90deg from its default rotation. This
2053 // assumption is device-specific, not platform-specific like this code.
2054 case 270:
2055 reverse_speakers = true;
2056 break;
2057 case 0:
2058 case 90:
2059 case 180:
2060 break;
2061 default:
2062 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002063 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002064 }
Eric Laurent03f09432014-03-25 18:09:11 -07002065 if (status == 0) {
Eric Laurent03f09432014-03-25 18:09:11 -07002066 if (adev->speaker_lr_swap != reverse_speakers) {
2067 adev->speaker_lr_swap = reverse_speakers;
2068 // only update the selected device if there is active pcm playback
2069 struct audio_usecase *usecase;
2070 struct listnode *node;
2071 list_for_each(node, &adev->usecase_list) {
2072 usecase = node_to_item(node, struct audio_usecase, list);
2073 if (usecase->type == PCM_PLAYBACK) {
2074 select_devices(adev, usecase->id);
2075 break;
2076 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002077 }
2078 }
2079 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002080 }
2081
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002082 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2083 if (ret >= 0) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002084 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002085 }
2086
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08002087 audio_extn_hfp_set_parameters(adev, parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002088done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002089 str_parms_destroy(parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002090 pthread_mutex_unlock(&adev->lock);
Eric Laurent03f09432014-03-25 18:09:11 -07002091 ALOGV("%s: exit with code(%d)", __func__, status);
2092 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002093}
2094
2095static char* adev_get_parameters(const struct audio_hw_device *dev,
2096 const char *keys)
2097{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002098 struct audio_device *adev = (struct audio_device *)dev;
2099 struct str_parms *reply = str_parms_create();
2100 struct str_parms *query = str_parms_create_str(keys);
2101 char *str;
2102
2103 pthread_mutex_lock(&adev->lock);
2104
2105 voice_get_parameters(adev, query, reply);
2106 str = str_parms_to_str(reply);
2107 str_parms_destroy(query);
2108 str_parms_destroy(reply);
2109
2110 pthread_mutex_unlock(&adev->lock);
2111 ALOGV("%s: exit: returns - %s", __func__, str);
2112 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002113}
2114
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002115static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002116{
2117 return 0;
2118}
2119
Haynes Mathew George5191a852013-09-11 14:19:36 -07002120static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2121{
2122 int ret;
2123 struct audio_device *adev = (struct audio_device *)dev;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002124
Haynes Mathew George5191a852013-09-11 14:19:36 -07002125 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002126 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002127 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002128
Haynes Mathew George5191a852013-09-11 14:19:36 -07002129 return ret;
2130}
2131
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002132static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002133{
2134 return -ENOSYS;
2135}
2136
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002137static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2138 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002139{
2140 return -ENOSYS;
2141}
2142
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002143static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __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_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002149{
2150 return -ENOSYS;
2151}
2152
2153static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2154{
2155 struct audio_device *adev = (struct audio_device *)dev;
2156
2157 pthread_mutex_lock(&adev->lock);
2158 if (adev->mode != mode) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002159 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002160 adev->mode = mode;
2161 }
2162 pthread_mutex_unlock(&adev->lock);
2163 return 0;
2164}
2165
2166static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2167{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002168 int ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002169 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002170
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002171 ALOGD("%s: state %d\n", __func__, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002172 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002173 ret = voice_set_mic_mute(adev, state);
Eric Laurenta609e8e2014-06-18 02:15:17 +00002174 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002175
2176 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002177}
2178
2179static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2180{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002181 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002182 return 0;
2183}
2184
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002185static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002186 const struct audio_config *config)
2187{
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002188 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002189
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002190 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
2191 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002192}
2193
2194static int adev_open_input_stream(struct audio_hw_device *dev,
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002195 audio_io_handle_t handle __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002196 audio_devices_t devices,
2197 struct audio_config *config,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002198 struct audio_stream_in **stream_in,
2199 audio_input_flags_t flags)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002200{
2201 struct audio_device *adev = (struct audio_device *)dev;
2202 struct stream_in *in;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002203 int ret = 0, buffer_size, frame_size;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002204 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002205
Eric Laurent994a6932013-07-17 11:51:42 -07002206 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002207 *stream_in = NULL;
2208 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2209 return -EINVAL;
2210
2211 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2212
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002213 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2214
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002215 in->stream.common.get_sample_rate = in_get_sample_rate;
2216 in->stream.common.set_sample_rate = in_set_sample_rate;
2217 in->stream.common.get_buffer_size = in_get_buffer_size;
2218 in->stream.common.get_channels = in_get_channels;
2219 in->stream.common.get_format = in_get_format;
2220 in->stream.common.set_format = in_set_format;
2221 in->stream.common.standby = in_standby;
2222 in->stream.common.dump = in_dump;
2223 in->stream.common.set_parameters = in_set_parameters;
2224 in->stream.common.get_parameters = in_get_parameters;
2225 in->stream.common.add_audio_effect = in_add_audio_effect;
2226 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2227 in->stream.set_gain = in_set_gain;
2228 in->stream.read = in_read;
2229 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2230
2231 in->device = devices;
2232 in->source = AUDIO_SOURCE_DEFAULT;
2233 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002234 in->standby = 1;
2235 in->channel_mask = config->channel_mask;
2236
2237 /* Update config params with the requested sample rate and channels */
2238 in->usecase = USECASE_AUDIO_RECORD;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002239 bool is_low_latency = false;
2240 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
2241 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
2242 is_low_latency = true;
Glenn Kasten4f993392014-05-14 07:30:48 -07002243#if LOW_LATENCY_CAPTURE_USE_CASE
Glenn Kasten4f993392014-05-14 07:30:48 -07002244 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
2245#endif
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002246 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002247 in->config = pcm_config_audio_capture;
2248 in->config.channels = channel_count;
2249 in->config.rate = config->sample_rate;
2250
Eric Laurentfdf296a2014-07-03 16:41:51 -07002251 frame_size = audio_stream_in_frame_size(&in->stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002252 buffer_size = get_input_buffer_size(config->sample_rate,
2253 config->format,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002254 channel_count,
2255 is_low_latency);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002256 in->config.period_size = buffer_size / frame_size;
2257
2258 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002259 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002260 return 0;
2261
2262err_open:
2263 free(in);
2264 *stream_in = NULL;
2265 return ret;
2266}
2267
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002268static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002269 struct audio_stream_in *stream)
2270{
Eric Laurent994a6932013-07-17 11:51:42 -07002271 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002272
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002273 in_standby(&stream->common);
2274 free(stream);
2275
2276 return;
2277}
2278
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002279static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002280{
2281 return 0;
2282}
2283
Andy Hung31aca912014-03-20 17:14:59 -07002284/* verifies input and output devices and their capabilities.
2285 *
2286 * This verification is required when enabling extended bit-depth or
2287 * sampling rates, as not all qcom products support it.
2288 *
2289 * Suitable for calling only on initialization such as adev_open().
2290 * It fills the audio_device use_case_table[] array.
2291 *
2292 * Has a side-effect that it needs to configure audio routing / devices
2293 * in order to power up the devices and read the device parameters.
2294 * It does not acquire any hw device lock. Should restore the devices
2295 * back to "normal state" upon completion.
2296 */
2297static int adev_verify_devices(struct audio_device *adev)
2298{
2299 /* enumeration is a bit difficult because one really wants to pull
2300 * the use_case, device id, etc from the hidden pcm_device_table[].
2301 * In this case there are the following use cases and device ids.
2302 *
2303 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
2304 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
2305 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
2306 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
2307 * [USECASE_AUDIO_RECORD] = {0, 0},
2308 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
2309 * [USECASE_VOICE_CALL] = {2, 2},
2310 *
2311 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
2312 * USECASE_VOICE_CALL omitted, but possible for either input or output.
2313 */
2314
2315 /* should be the usecases enabled in adev_open_input_stream() */
2316 static const int test_in_usecases[] = {
2317 USECASE_AUDIO_RECORD,
2318 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
2319 };
2320 /* should be the usecases enabled in adev_open_output_stream()*/
2321 static const int test_out_usecases[] = {
2322 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
2323 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
2324 };
2325 static const usecase_type_t usecase_type_by_dir[] = {
2326 PCM_PLAYBACK,
2327 PCM_CAPTURE,
2328 };
2329 static const unsigned flags_by_dir[] = {
2330 PCM_OUT,
2331 PCM_IN,
2332 };
2333
2334 size_t i;
2335 unsigned dir;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002336 const unsigned card_id = adev->snd_card;
Andy Hung31aca912014-03-20 17:14:59 -07002337 char info[512]; /* for possible debug info */
2338
2339 for (dir = 0; dir < 2; ++dir) {
2340 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
2341 const unsigned flags_dir = flags_by_dir[dir];
2342 const size_t testsize =
2343 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
2344 const int *testcases =
2345 dir ? test_in_usecases : test_out_usecases;
2346 const audio_devices_t audio_device =
2347 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
2348
2349 for (i = 0; i < testsize; ++i) {
2350 const audio_usecase_t audio_usecase = testcases[i];
2351 int device_id;
2352 snd_device_t snd_device;
2353 struct pcm_params **pparams;
2354 struct stream_out out;
2355 struct stream_in in;
2356 struct audio_usecase uc_info;
2357 int retval;
2358
2359 pparams = &adev->use_case_table[audio_usecase];
2360 pcm_params_free(*pparams); /* can accept null input */
2361 *pparams = NULL;
2362
2363 /* find the device ID for the use case (signed, for error) */
2364 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
2365 if (device_id < 0)
2366 continue;
2367
2368 /* prepare structures for device probing */
2369 memset(&uc_info, 0, sizeof(uc_info));
2370 uc_info.id = audio_usecase;
2371 uc_info.type = usecase_type;
2372 if (dir) {
2373 adev->active_input = &in;
2374 memset(&in, 0, sizeof(in));
2375 in.device = audio_device;
2376 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
2377 uc_info.stream.in = &in;
2378 } else {
2379 adev->active_input = NULL;
2380 }
2381 memset(&out, 0, sizeof(out));
2382 out.devices = audio_device; /* only field needed in select_devices */
2383 uc_info.stream.out = &out;
2384 uc_info.devices = audio_device;
2385 uc_info.in_snd_device = SND_DEVICE_NONE;
2386 uc_info.out_snd_device = SND_DEVICE_NONE;
2387 list_add_tail(&adev->usecase_list, &uc_info.list);
2388
2389 /* select device - similar to start_(in/out)put_stream() */
2390 retval = select_devices(adev, audio_usecase);
2391 if (retval >= 0) {
2392 *pparams = pcm_params_get(card_id, device_id, flags_dir);
2393#if LOG_NDEBUG == 0
2394 if (*pparams) {
2395 ALOGV("%s: (%s) card %d device %d", __func__,
2396 dir ? "input" : "output", card_id, device_id);
2397 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
2398 ALOGV(info); /* print parameters */
2399 } else {
2400 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
2401 }
2402#endif
2403 }
2404
2405 /* deselect device - similar to stop_(in/out)put_stream() */
2406 /* 1. Get and set stream specific mixer controls */
Glenn Kastene7137302014-04-28 15:12:18 -07002407 retval = disable_audio_route(adev, &uc_info);
Andy Hung31aca912014-03-20 17:14:59 -07002408 /* 2. Disable the rx device */
2409 retval = disable_snd_device(adev,
Glenn Kastene7137302014-04-28 15:12:18 -07002410 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
Andy Hung31aca912014-03-20 17:14:59 -07002411 list_remove(&uc_info.list);
2412 }
2413 }
2414 adev->active_input = NULL; /* restore adev state */
2415 return 0;
2416}
2417
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002418static int adev_close(hw_device_t *device)
2419{
Andy Hung31aca912014-03-20 17:14:59 -07002420 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002421 struct audio_device *adev = (struct audio_device *)device;
2422 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002423 free(adev->snd_dev_ref_cnt);
2424 platform_deinit(adev->platform);
Andy Hung31aca912014-03-20 17:14:59 -07002425 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
2426 pcm_params_free(adev->use_case_table[i]);
2427 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002428 free(device);
2429 return 0;
2430}
2431
Glenn Kasten4f993392014-05-14 07:30:48 -07002432/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
2433 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
2434 * just that it _might_ work.
2435 */
2436static int period_size_is_plausible_for_low_latency(int period_size)
2437{
2438 switch (period_size) {
2439 case 160:
2440 case 240:
2441 case 320:
2442 case 480:
2443 return 1;
2444 default:
2445 return 0;
2446 }
2447}
2448
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002449static int adev_open(const hw_module_t *module, const char *name,
2450 hw_device_t **device)
2451{
2452 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002453 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002454
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002455 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002456 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2457
2458 adev = calloc(1, sizeof(struct audio_device));
2459
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002460 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2461
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002462 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2463 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2464 adev->device.common.module = (struct hw_module_t *)module;
2465 adev->device.common.close = adev_close;
2466
2467 adev->device.init_check = adev_init_check;
2468 adev->device.set_voice_volume = adev_set_voice_volume;
2469 adev->device.set_master_volume = adev_set_master_volume;
2470 adev->device.get_master_volume = adev_get_master_volume;
2471 adev->device.set_master_mute = adev_set_master_mute;
2472 adev->device.get_master_mute = adev_get_master_mute;
2473 adev->device.set_mode = adev_set_mode;
2474 adev->device.set_mic_mute = adev_set_mic_mute;
2475 adev->device.get_mic_mute = adev_get_mic_mute;
2476 adev->device.set_parameters = adev_set_parameters;
2477 adev->device.get_parameters = adev_get_parameters;
2478 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2479 adev->device.open_output_stream = adev_open_output_stream;
2480 adev->device.close_output_stream = adev_close_output_stream;
2481 adev->device.open_input_stream = adev_open_input_stream;
2482 adev->device.close_input_stream = adev_close_input_stream;
2483 adev->device.dump = adev_dump;
2484
2485 /* Set the default route before the PCM stream is opened */
2486 pthread_mutex_lock(&adev->lock);
2487 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002488 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002489 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002490 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002491 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002492 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002493 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002494 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002495 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002496 pthread_mutex_unlock(&adev->lock);
2497
2498 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002499 adev->platform = platform_init(adev);
2500 if (!adev->platform) {
2501 free(adev->snd_dev_ref_cnt);
2502 free(adev);
2503 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2504 *device = NULL;
2505 return -EINVAL;
2506 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002507
2508 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2509 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2510 if (adev->visualizer_lib == NULL) {
2511 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2512 } else {
2513 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2514 adev->visualizer_start_output =
2515 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2516 "visualizer_hal_start_output");
2517 adev->visualizer_stop_output =
2518 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2519 "visualizer_hal_stop_output");
2520 }
2521 }
2522
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002523 adev->bt_wb_speech_enabled = false;
2524
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002525 *device = &adev->device.common;
Andy Hung31aca912014-03-20 17:14:59 -07002526 if (k_enable_extended_precision)
2527 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002528
Glenn Kasten4f993392014-05-14 07:30:48 -07002529 char value[PROPERTY_VALUE_MAX];
2530 int trial;
2531 if (property_get("audio_hal.period_size", value, NULL) > 0) {
2532 trial = atoi(value);
2533 if (period_size_is_plausible_for_low_latency(trial)) {
2534 pcm_config_low_latency.period_size = trial;
2535 pcm_config_low_latency.start_threshold = trial / 4;
2536 pcm_config_low_latency.avail_min = trial / 4;
2537 configured_low_latency_capture_period_size = trial;
2538 }
2539 }
2540 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
2541 trial = atoi(value);
2542 if (period_size_is_plausible_for_low_latency(trial)) {
2543 configured_low_latency_capture_period_size = trial;
2544 }
2545 }
2546
Eric Laurent994a6932013-07-17 11:51:42 -07002547 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002548 return 0;
2549}
2550
2551static struct hw_module_methods_t hal_module_methods = {
2552 .open = adev_open,
2553};
2554
2555struct audio_module HAL_MODULE_INFO_SYM = {
2556 .common = {
2557 .tag = HARDWARE_MODULE_TAG,
2558 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2559 .hal_api_version = HARDWARE_HAL_API_VERSION,
2560 .id = AUDIO_HARDWARE_MODULE_ID,
2561 .name = "QCOM Audio HAL",
2562 .author = "Code Aurora Forum",
2563 .methods = &hal_module_methods,
2564 },
2565};