blob: 615c5c15e0e11fbf359791c76547bdf5a6d2230b [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
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>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080051
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070052#include "sound/compress_params.h"
53
54#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
55#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
56/* ToDo: Check and update a proper value in msec */
57#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
58#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
59
Glenn Kasten4f993392014-05-14 07:30:48 -070060static unsigned int configured_low_latency_capture_period_size =
61 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
62
Andy Hung31aca912014-03-20 17:14:59 -070063/* This constant enables extended precision handling.
64 * TODO The flag is off until more testing is done.
65 */
66static const bool k_enable_extended_precision = false;
67
Eric Laurentb23d5282013-05-14 15:27:20 -070068struct pcm_config pcm_config_deep_buffer = {
69 .channels = 2,
70 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
71 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
72 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
73 .format = PCM_FORMAT_S16_LE,
74 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
75 .stop_threshold = INT_MAX,
76 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
77};
78
79struct pcm_config pcm_config_low_latency = {
80 .channels = 2,
81 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
82 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
83 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
84 .format = PCM_FORMAT_S16_LE,
85 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
86 .stop_threshold = INT_MAX,
87 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
88};
89
90struct pcm_config pcm_config_hdmi_multi = {
91 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
92 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
93 .period_size = HDMI_MULTI_PERIOD_SIZE,
94 .period_count = HDMI_MULTI_PERIOD_COUNT,
95 .format = PCM_FORMAT_S16_LE,
96 .start_threshold = 0,
97 .stop_threshold = INT_MAX,
98 .avail_min = 0,
99};
100
101struct pcm_config pcm_config_audio_capture = {
102 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700103 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
104 .format = PCM_FORMAT_S16_LE,
105};
106
107struct pcm_config pcm_config_voice_call = {
108 .channels = 1,
109 .rate = 8000,
110 .period_size = 160,
111 .period_count = 2,
112 .format = PCM_FORMAT_S16_LE,
113};
114
115static const char * const use_case_table[AUDIO_USECASE_MAX] = {
116 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
117 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
118 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
119 [USECASE_AUDIO_RECORD] = "audio-record",
120 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800121 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
122 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Eric Laurentb23d5282013-05-14 15:27:20 -0700123 [USECASE_VOICE_CALL] = "voice-call",
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700124 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
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 Laurent86e17132013-09-12 17:49:30 -0700145 if (format == AUDIO_FORMAT_MP3 ||
146 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700147 return true;
148
149 return false;
150}
151
152static int get_snd_codec_id(audio_format_t format)
153{
154 int id = 0;
155
156 switch (format) {
157 case AUDIO_FORMAT_MP3:
158 id = SND_AUDIOCODEC_MP3;
159 break;
160 case AUDIO_FORMAT_AAC:
161 id = SND_AUDIOCODEC_AAC;
162 break;
163 default:
164 ALOGE("%s: Unsupported audio format", __func__);
165 }
166
167 return id;
168}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800169
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800170int enable_audio_route(struct audio_device *adev,
171 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800172{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700173 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800174 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800175
176 if (usecase == NULL)
177 return -EINVAL;
178
179 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
180
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800181 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700182 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800183 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700184 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800185
186 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700187 platform_add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700188 ALOGV("%s: apply and update mixer path: %s", __func__, mixer_path);
189 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800190
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800191 ALOGV("%s: exit", __func__);
192 return 0;
193}
194
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800195int disable_audio_route(struct audio_device *adev,
196 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800197{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700198 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800199 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800200
201 if (usecase == NULL)
202 return -EINVAL;
203
204 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700205 if (usecase->type == PCM_CAPTURE)
206 snd_device = usecase->in_snd_device;
207 else
208 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800209 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700210 platform_add_backend_name(mixer_path, snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700211 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
212 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800213
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800214 ALOGV("%s: exit", __func__);
215 return 0;
216}
217
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800218int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700219 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800220{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800221 if (snd_device < SND_DEVICE_MIN ||
222 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800223 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800224 return -EINVAL;
225 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700226
227 adev->snd_dev_ref_cnt[snd_device]++;
228 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700229 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700230 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700231 return 0;
232 }
233
Eric Laurentb23d5282013-05-14 15:27:20 -0700234 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700235 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800236 return -EINVAL;
237 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800238
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700239 const char * dev_path = platform_get_snd_device_name(snd_device);
240 ALOGV("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
241 audio_route_apply_and_update_path(adev->audio_route, dev_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700242
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800243 return 0;
244}
245
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800246int disable_snd_device(struct audio_device *adev,
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700247 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800248{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800249 if (snd_device < SND_DEVICE_MIN ||
250 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800251 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800252 return -EINVAL;
253 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700254 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
255 ALOGE("%s: device ref cnt is already 0", __func__);
256 return -EINVAL;
257 }
258 adev->snd_dev_ref_cnt[snd_device]--;
259 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700260 const char * dev_path = platform_get_snd_device_name(snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700261 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700262 snd_device, dev_path);
263 audio_route_reset_and_update_path(adev->audio_route, dev_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700264 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800265 return 0;
266}
267
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700268static void check_usecases_codec_backend(struct audio_device *adev,
269 struct audio_usecase *uc_info,
270 snd_device_t snd_device)
271{
272 struct listnode *node;
273 struct audio_usecase *usecase;
274 bool switch_device[AUDIO_USECASE_MAX];
275 int i, num_uc_to_switch = 0;
276
277 /*
278 * This function is to make sure that all the usecases that are active on
279 * the hardware codec backend are always routed to any one device that is
280 * handled by the hardware codec.
281 * For example, if low-latency and deep-buffer usecases are currently active
282 * on speaker and out_set_parameters(headset) is received on low-latency
283 * output, then we have to make sure deep-buffer is also switched to headset,
284 * because of the limitation that both the devices cannot be enabled
285 * at the same time as they share the same backend.
286 */
287 /* Disable all the usecases on the shared backend other than the
288 specified usecase */
289 for (i = 0; i < AUDIO_USECASE_MAX; i++)
290 switch_device[i] = false;
291
292 list_for_each(node, &adev->usecase_list) {
293 usecase = node_to_item(node, struct audio_usecase, list);
294 if (usecase->type != PCM_CAPTURE &&
295 usecase != uc_info &&
296 usecase->out_snd_device != snd_device &&
297 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
298 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
299 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700300 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700301 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700302 switch_device[usecase->id] = true;
303 num_uc_to_switch++;
304 }
305 }
306
307 if (num_uc_to_switch) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700308 list_for_each(node, &adev->usecase_list) {
309 usecase = node_to_item(node, struct audio_usecase, list);
310 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700311 disable_snd_device(adev, usecase->out_snd_device);
sangwon.jeon866d5ff2013-10-17 21:42:50 +0900312 }
313 }
314
315 list_for_each(node, &adev->usecase_list) {
316 usecase = node_to_item(node, struct audio_usecase, list);
317 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700318 enable_snd_device(adev, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700319 }
320 }
321
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700322 /* Re-route all the usecases on the shared backend other than the
323 specified usecase to new snd devices */
324 list_for_each(node, &adev->usecase_list) {
325 usecase = node_to_item(node, struct audio_usecase, list);
326 /* Update the out_snd_device only before enabling the audio route */
327 if (switch_device[usecase->id] ) {
328 usecase->out_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700329 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700330 }
331 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700332 }
333}
334
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700335static void check_and_route_capture_usecases(struct audio_device *adev,
336 struct audio_usecase *uc_info,
337 snd_device_t snd_device)
338{
339 struct listnode *node;
340 struct audio_usecase *usecase;
341 bool switch_device[AUDIO_USECASE_MAX];
342 int i, num_uc_to_switch = 0;
343
344 /*
345 * This function is to make sure that all the active capture usecases
346 * are always routed to the same input sound device.
347 * For example, if audio-record and voice-call usecases are currently
348 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
349 * is received for voice call then we have to make sure that audio-record
350 * usecase is also switched to earpiece i.e. voice-dmic-ef,
351 * because of the limitation that two devices cannot be enabled
352 * at the same time if they share the same backend.
353 */
354 for (i = 0; i < AUDIO_USECASE_MAX; i++)
355 switch_device[i] = false;
356
357 list_for_each(node, &adev->usecase_list) {
358 usecase = node_to_item(node, struct audio_usecase, list);
359 if (usecase->type != PCM_PLAYBACK &&
360 usecase != uc_info &&
361 usecase->in_snd_device != snd_device) {
362 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
363 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700364 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700365 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700366 switch_device[usecase->id] = true;
367 num_uc_to_switch++;
368 }
369 }
370
371 if (num_uc_to_switch) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700372 list_for_each(node, &adev->usecase_list) {
373 usecase = node_to_item(node, struct audio_usecase, list);
374 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700375 disable_snd_device(adev, usecase->in_snd_device);
376 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700377 }
378 }
379
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700380 /* Re-route all the usecases on the shared backend other than the
381 specified usecase to new snd devices */
382 list_for_each(node, &adev->usecase_list) {
383 usecase = node_to_item(node, struct audio_usecase, list);
384 /* Update the in_snd_device only before enabling the audio route */
385 if (switch_device[usecase->id] ) {
386 usecase->in_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700387 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700388 }
389 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700390 }
391}
392
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800393
394/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700395static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800396{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700397 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700398 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800399
400 switch (channels) {
401 /*
402 * Do not handle stereo output in Multi-channel cases
403 * Stereo case is handled in normal playback path
404 */
405 case 6:
406 ALOGV("%s: HDMI supports 5.1", __func__);
407 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
408 break;
409 case 8:
410 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
411 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
412 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
413 break;
414 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700415 ALOGE("HDMI does not support multi channel playback");
416 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800417 break;
418 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700419 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800420}
421
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800422struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
423 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700424{
425 struct audio_usecase *usecase;
426 struct listnode *node;
427
428 list_for_each(node, &adev->usecase_list) {
429 usecase = node_to_item(node, struct audio_usecase, list);
430 if (usecase->id == uc_id)
431 return usecase;
432 }
433 return NULL;
434}
435
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800436int select_devices(struct audio_device *adev,
437 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800438{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800439 snd_device_t out_snd_device = SND_DEVICE_NONE;
440 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700441 struct audio_usecase *usecase = NULL;
442 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800443 struct audio_usecase *hfp_usecase = NULL;
444 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800445 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700446 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800447
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700448 usecase = get_usecase_from_list(adev, uc_id);
449 if (usecase == NULL) {
450 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
451 return -EINVAL;
452 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800453
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800454 if ((usecase->type == VOICE_CALL) ||
455 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700456 out_snd_device = platform_get_output_snd_device(adev->platform,
457 usecase->stream.out->devices);
458 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700459 usecase->devices = usecase->stream.out->devices;
460 } else {
461 /*
462 * If the voice call is active, use the sound devices of voice call usecase
463 * so that it would not result any device switch. All the usecases will
464 * be switched to new device when select_devices() is called for voice call
465 * usecase. This is to avoid switching devices for voice call when
466 * check_usecases_codec_backend() is called below.
467 */
468 if (adev->in_call) {
469 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
470 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
471 in_snd_device = vc_usecase->in_snd_device;
472 out_snd_device = vc_usecase->out_snd_device;
473 }
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800474 } else if (audio_extn_hfp_is_active(adev)) {
475 hfp_ucid = audio_extn_hfp_get_usecase();
476 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
477 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
478 in_snd_device = hfp_usecase->in_snd_device;
479 out_snd_device = hfp_usecase->out_snd_device;
480 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700481 }
482 if (usecase->type == PCM_PLAYBACK) {
483 usecase->devices = usecase->stream.out->devices;
484 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700485 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700486 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700487 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700488 if (usecase->stream.out == adev->primary_output &&
489 adev->active_input &&
490 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
491 select_devices(adev, adev->active_input->usecase);
492 }
493 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700494 } else if (usecase->type == PCM_CAPTURE) {
495 usecase->devices = usecase->stream.in->device;
496 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700497 if (in_snd_device == SND_DEVICE_NONE) {
498 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
499 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700500 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700501 adev->primary_output->devices);
502 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700503 in_snd_device = platform_get_input_snd_device(adev->platform,
504 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700505 }
506 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700507 }
508 }
509
510 if (out_snd_device == usecase->out_snd_device &&
511 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800512 return 0;
513 }
514
sangwoobc677242013-08-08 16:53:43 +0900515 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700516 out_snd_device, platform_get_snd_device_name(out_snd_device),
517 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800518
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800519 /*
520 * Limitation: While in call, to do a device switch we need to disable
521 * and enable both RX and TX devices though one of them is same as current
522 * device.
523 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700524 if (usecase->type == VOICE_CALL) {
525 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800526 }
527
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700528 /* Disable current sound devices */
529 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700530 disable_audio_route(adev, usecase);
531 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800532 }
533
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700534 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700535 disable_audio_route(adev, usecase);
536 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800537 }
538
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700539 /* Applicable only on the targets that has external modem.
540 * New device information should be sent to modem before enabling
541 * the devices to reduce in-call device switch time.
542 */
543 if (usecase->type == VOICE_CALL)
544 status = platform_switch_voice_call_enable_device_config(adev->platform,
545 out_snd_device,
546 in_snd_device);
547
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700548 /* Enable new sound devices */
549 if (out_snd_device != SND_DEVICE_NONE) {
550 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
551 check_usecases_codec_backend(adev, usecase, out_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700552 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800553 }
554
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700555 if (in_snd_device != SND_DEVICE_NONE) {
556 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700557 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700558 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700559
Eric Laurentb23d5282013-05-14 15:27:20 -0700560 if (usecase->type == VOICE_CALL)
561 status = platform_switch_voice_call_device_post(adev->platform,
562 out_snd_device,
563 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800564
sangwoo170731f2013-06-08 15:36:36 +0900565 usecase->in_snd_device = in_snd_device;
566 usecase->out_snd_device = out_snd_device;
567
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700568 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900569
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700570 /* Applicable only on the targets that has external modem.
571 * Enable device command should be sent to modem only after
572 * enabling voice call mixer controls
573 */
574 if (usecase->type == VOICE_CALL)
575 status = platform_switch_voice_call_usecase_route_post(adev->platform,
576 out_snd_device,
577 in_snd_device);
578
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800579 return status;
580}
581
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800582static int stop_input_stream(struct stream_in *in)
583{
584 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800585 struct audio_usecase *uc_info;
586 struct audio_device *adev = in->dev;
587
Eric Laurentc8400632013-02-14 19:04:54 -0800588 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800589
Eric Laurent994a6932013-07-17 11:51:42 -0700590 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700591 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800592 uc_info = get_usecase_from_list(adev, in->usecase);
593 if (uc_info == NULL) {
594 ALOGE("%s: Could not find the usecase (%d) in the list",
595 __func__, in->usecase);
596 return -EINVAL;
597 }
598
Eric Laurent150dbfe2013-02-27 14:31:02 -0800599 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700600 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700601
602 /* 2. Disable the tx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700603 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800604
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800605 list_remove(&uc_info->list);
606 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800607
Eric Laurent994a6932013-07-17 11:51:42 -0700608 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800609 return ret;
610}
611
612int start_input_stream(struct stream_in *in)
613{
614 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800615 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800616 struct audio_usecase *uc_info;
617 struct audio_device *adev = in->dev;
618
Eric Laurent994a6932013-07-17 11:51:42 -0700619 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700620 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800621 if (in->pcm_device_id < 0) {
622 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
623 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800624 ret = -EINVAL;
625 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800626 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700627
628 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800629 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
630 uc_info->id = in->usecase;
631 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800632 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700633 uc_info->devices = in->device;
634 uc_info->in_snd_device = SND_DEVICE_NONE;
635 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800636
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800637 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700638 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800639
Eric Laurentc8400632013-02-14 19:04:54 -0800640 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
641 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800642 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
643 PCM_IN, &in->config);
644 if (in->pcm && !pcm_is_ready(in->pcm)) {
645 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
646 pcm_close(in->pcm);
647 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800648 ret = -EIO;
649 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800650 }
Eric Laurent994a6932013-07-17 11:51:42 -0700651 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800652 return ret;
653
654error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800655 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800656
657error_config:
658 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700659 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800660
661 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800662}
663
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700664/* must be called with out->lock locked */
665static int send_offload_cmd_l(struct stream_out* out, int command)
666{
667 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
668
669 ALOGVV("%s %d", __func__, command);
670
671 cmd->cmd = command;
672 list_add_tail(&out->offload_cmd_list, &cmd->node);
673 pthread_cond_signal(&out->offload_cond);
674 return 0;
675}
676
677/* must be called iwth out->lock locked */
678static void stop_compressed_output_l(struct stream_out *out)
679{
680 out->offload_state = OFFLOAD_STATE_IDLE;
681 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700682 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700683 if (out->compr != NULL) {
684 compress_stop(out->compr);
685 while (out->offload_thread_blocked) {
686 pthread_cond_wait(&out->cond, &out->lock);
687 }
688 }
689}
690
691static void *offload_thread_loop(void *context)
692{
693 struct stream_out *out = (struct stream_out *) context;
694 struct listnode *item;
695
696 out->offload_state = OFFLOAD_STATE_IDLE;
697 out->playback_started = 0;
698
699 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
700 set_sched_policy(0, SP_FOREGROUND);
701 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
702
703 ALOGV("%s", __func__);
704 pthread_mutex_lock(&out->lock);
705 for (;;) {
706 struct offload_cmd *cmd = NULL;
707 stream_callback_event_t event;
708 bool send_callback = false;
709
710 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
711 __func__, list_empty(&out->offload_cmd_list),
712 out->offload_state);
713 if (list_empty(&out->offload_cmd_list)) {
714 ALOGV("%s SLEEPING", __func__);
715 pthread_cond_wait(&out->offload_cond, &out->lock);
716 ALOGV("%s RUNNING", __func__);
717 continue;
718 }
719
720 item = list_head(&out->offload_cmd_list);
721 cmd = node_to_item(item, struct offload_cmd, node);
722 list_remove(item);
723
724 ALOGVV("%s STATE %d CMD %d out->compr %p",
725 __func__, out->offload_state, cmd->cmd, out->compr);
726
727 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
728 free(cmd);
729 break;
730 }
731
732 if (out->compr == NULL) {
733 ALOGE("%s: Compress handle is NULL", __func__);
734 pthread_cond_signal(&out->cond);
735 continue;
736 }
737 out->offload_thread_blocked = true;
738 pthread_mutex_unlock(&out->lock);
739 send_callback = false;
740 switch(cmd->cmd) {
741 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
742 compress_wait(out->compr, -1);
743 send_callback = true;
744 event = STREAM_CBK_EVENT_WRITE_READY;
745 break;
746 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700747 compress_next_track(out->compr);
748 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700749 send_callback = true;
750 event = STREAM_CBK_EVENT_DRAIN_READY;
751 break;
752 case OFFLOAD_CMD_DRAIN:
753 compress_drain(out->compr);
754 send_callback = true;
755 event = STREAM_CBK_EVENT_DRAIN_READY;
756 break;
757 default:
758 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
759 break;
760 }
761 pthread_mutex_lock(&out->lock);
762 out->offload_thread_blocked = false;
763 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700764 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700765 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700766 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700767 free(cmd);
768 }
769
770 pthread_cond_signal(&out->cond);
771 while (!list_empty(&out->offload_cmd_list)) {
772 item = list_head(&out->offload_cmd_list);
773 list_remove(item);
774 free(node_to_item(item, struct offload_cmd, node));
775 }
776 pthread_mutex_unlock(&out->lock);
777
778 return NULL;
779}
780
781static int create_offload_callback_thread(struct stream_out *out)
782{
783 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
784 list_init(&out->offload_cmd_list);
785 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
786 offload_thread_loop, out);
787 return 0;
788}
789
790static int destroy_offload_callback_thread(struct stream_out *out)
791{
792 pthread_mutex_lock(&out->lock);
793 stop_compressed_output_l(out);
794 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
795
796 pthread_mutex_unlock(&out->lock);
797 pthread_join(out->offload_thread, (void **) NULL);
798 pthread_cond_destroy(&out->offload_cond);
799
800 return 0;
801}
802
Eric Laurent07eeafd2013-10-06 12:52:49 -0700803static bool allow_hdmi_channel_config(struct audio_device *adev)
804{
805 struct listnode *node;
806 struct audio_usecase *usecase;
807 bool ret = true;
808
809 list_for_each(node, &adev->usecase_list) {
810 usecase = node_to_item(node, struct audio_usecase, list);
811 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
812 /*
813 * If voice call is already existing, do not proceed further to avoid
814 * disabling/enabling both RX and TX devices, CSD calls, etc.
815 * Once the voice call done, the HDMI channels can be configured to
816 * max channels of remaining use cases.
817 */
818 if (usecase->id == USECASE_VOICE_CALL) {
819 ALOGD("%s: voice call is active, no change in HDMI channels",
820 __func__);
821 ret = false;
822 break;
823 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
824 ALOGD("%s: multi channel playback is active, "
825 "no change in HDMI channels", __func__);
826 ret = false;
827 break;
828 }
829 }
830 }
831 return ret;
832}
833
834static int check_and_set_hdmi_channels(struct audio_device *adev,
835 unsigned int channels)
836{
837 struct listnode *node;
838 struct audio_usecase *usecase;
839
840 /* Check if change in HDMI channel config is allowed */
841 if (!allow_hdmi_channel_config(adev))
842 return 0;
843
844 if (channels == adev->cur_hdmi_channels) {
845 ALOGD("%s: Requested channels are same as current", __func__);
846 return 0;
847 }
848
849 platform_set_hdmi_channels(adev->platform, channels);
850 adev->cur_hdmi_channels = channels;
851
852 /*
853 * Deroute all the playback streams routed to HDMI so that
854 * the back end is deactivated. Note that backend will not
855 * be deactivated if any one stream is connected to it.
856 */
857 list_for_each(node, &adev->usecase_list) {
858 usecase = node_to_item(node, struct audio_usecase, list);
859 if (usecase->type == PCM_PLAYBACK &&
860 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700861 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -0700862 }
863 }
864
865 /*
866 * Enable all the streams disabled above. Now the HDMI backend
867 * will be activated with new channel configuration
868 */
869 list_for_each(node, &adev->usecase_list) {
870 usecase = node_to_item(node, struct audio_usecase, list);
871 if (usecase->type == PCM_PLAYBACK &&
872 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700873 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -0700874 }
875 }
876
877 return 0;
878}
879
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800880static int stop_output_stream(struct stream_out *out)
881{
882 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800883 struct audio_usecase *uc_info;
884 struct audio_device *adev = out->dev;
885
Eric Laurent994a6932013-07-17 11:51:42 -0700886 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700887 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800888 uc_info = get_usecase_from_list(adev, out->usecase);
889 if (uc_info == NULL) {
890 ALOGE("%s: Could not find the usecase (%d) in the list",
891 __func__, out->usecase);
892 return -EINVAL;
893 }
894
Eric Laurentc4aef752013-09-12 17:45:53 -0700895 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
896 adev->visualizer_stop_output != NULL)
897 adev->visualizer_stop_output(out->handle);
898
Eric Laurent150dbfe2013-02-27 14:31:02 -0800899 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700900 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700901
902 /* 2. Disable the rx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700903 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800904
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800905 list_remove(&uc_info->list);
906 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800907
Eric Laurent07eeafd2013-10-06 12:52:49 -0700908 /* Must be called after removing the usecase from list */
909 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
910 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
911
Eric Laurent994a6932013-07-17 11:51:42 -0700912 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800913 return ret;
914}
915
916int start_output_stream(struct stream_out *out)
917{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800918 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800919 struct audio_usecase *uc_info;
920 struct audio_device *adev = out->dev;
921
Eric Laurent994a6932013-07-17 11:51:42 -0700922 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700923 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -0700924 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800925 if (out->pcm_device_id < 0) {
926 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
927 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800928 ret = -EINVAL;
929 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800930 }
931
932 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
933 uc_info->id = out->usecase;
934 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800935 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700936 uc_info->devices = out->devices;
937 uc_info->in_snd_device = SND_DEVICE_NONE;
938 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800939
Eric Laurent07eeafd2013-10-06 12:52:49 -0700940 /* This must be called before adding this usecase to the list */
941 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
942 check_and_set_hdmi_channels(adev, out->config.channels);
943
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800944 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800945
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700946 select_devices(adev, out->usecase);
947
Andy Hung31aca912014-03-20 17:14:59 -0700948 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
949 __func__, SOUND_CARD, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700950 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
951 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -0700952 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700953 if (out->pcm && !pcm_is_ready(out->pcm)) {
954 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
955 pcm_close(out->pcm);
956 out->pcm = NULL;
957 ret = -EIO;
958 goto error_open;
959 }
960 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800961 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700962 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
963 COMPRESS_IN, &out->compr_config);
964 if (out->compr && !is_compress_ready(out->compr)) {
965 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
966 compress_close(out->compr);
967 out->compr = NULL;
968 ret = -EIO;
969 goto error_open;
970 }
971 if (out->offload_callback)
972 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -0700973
974 if (adev->visualizer_start_output != NULL)
975 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800976 }
Eric Laurent994a6932013-07-17 11:51:42 -0700977 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800978 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700979error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800980 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800981error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800982 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800983}
984
985static int stop_voice_call(struct audio_device *adev)
986{
987 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800988 struct audio_usecase *uc_info;
989
Eric Laurent994a6932013-07-17 11:51:42 -0700990 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800991 adev->in_call = false;
Eric Laurentb23d5282013-05-14 15:27:20 -0700992
993 ret = platform_stop_voice_call(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800994
995 /* 1. Close the PCM devices */
996 if (adev->voice_call_rx) {
997 pcm_close(adev->voice_call_rx);
998 adev->voice_call_rx = NULL;
999 }
1000 if (adev->voice_call_tx) {
1001 pcm_close(adev->voice_call_tx);
1002 adev->voice_call_tx = NULL;
1003 }
1004
1005 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1006 if (uc_info == NULL) {
1007 ALOGE("%s: Could not find the usecase (%d) in the list",
1008 __func__, USECASE_VOICE_CALL);
1009 return -EINVAL;
1010 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001011
1012 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001013 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001014
1015 /* 3. Disable the rx and tx devices */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001016 disable_snd_device(adev, uc_info->out_snd_device);
1017 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001018
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001019 list_remove(&uc_info->list);
1020 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001021
Eric Laurent994a6932013-07-17 11:51:42 -07001022 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001023 return ret;
1024}
1025
1026static int start_voice_call(struct audio_device *adev)
1027{
1028 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001029 struct audio_usecase *uc_info;
1030 int pcm_dev_rx_id, pcm_dev_tx_id;
1031
Eric Laurent994a6932013-07-17 11:51:42 -07001032 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001033
1034 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1035 uc_info->id = USECASE_VOICE_CALL;
1036 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001037 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001038 uc_info->devices = adev->primary_output->devices;
1039 uc_info->in_snd_device = SND_DEVICE_NONE;
1040 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001041
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001042 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001043
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001044 select_devices(adev, USECASE_VOICE_CALL);
1045
Eric Laurentb23d5282013-05-14 15:27:20 -07001046 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
1047 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001048
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001049 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1050 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1051 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001052 ret = -EIO;
1053 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001054 }
1055
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001056 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001057 __func__, SOUND_CARD, pcm_dev_rx_id);
1058 adev->voice_call_rx = pcm_open(SOUND_CARD,
1059 pcm_dev_rx_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001060 PCM_OUT | PCM_MONOTONIC, &pcm_config_voice_call);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001061 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1062 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001063 ret = -EIO;
1064 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001065 }
1066
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001067 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001068 __func__, SOUND_CARD, pcm_dev_tx_id);
1069 adev->voice_call_tx = pcm_open(SOUND_CARD,
1070 pcm_dev_tx_id,
1071 PCM_IN, &pcm_config_voice_call);
1072 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1073 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001074 ret = -EIO;
1075 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001076 }
Haynes Mathew George5191a852013-09-11 14:19:36 -07001077
1078 /* set cached volume */
1079 set_voice_volume_l(adev, adev->voice_volume);
1080
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001081 pcm_start(adev->voice_call_rx);
1082 pcm_start(adev->voice_call_tx);
1083
Eric Laurentb23d5282013-05-14 15:27:20 -07001084 ret = platform_start_voice_call(adev->platform);
1085 if (ret < 0) {
1086 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
1087 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001088 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001089 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001090 return 0;
1091
1092error_start_voice:
1093 stop_voice_call(adev);
1094
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001095 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001096 return ret;
1097}
1098
1099static int check_input_parameters(uint32_t sample_rate,
1100 audio_format_t format,
1101 int channel_count)
1102{
1103 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1104
1105 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1106
1107 switch (sample_rate) {
1108 case 8000:
1109 case 11025:
1110 case 12000:
1111 case 16000:
1112 case 22050:
1113 case 24000:
1114 case 32000:
1115 case 44100:
1116 case 48000:
1117 break;
1118 default:
1119 return -EINVAL;
1120 }
1121
1122 return 0;
1123}
1124
1125static size_t get_input_buffer_size(uint32_t sample_rate,
1126 audio_format_t format,
1127 int channel_count)
1128{
1129 size_t size = 0;
1130
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001131 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1132 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001133
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001134 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Glenn Kasten4f993392014-05-14 07:30:48 -07001135 if (sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE)
1136 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001137 /* ToDo: should use frame_size computed based on the format and
1138 channel_count here. */
1139 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001140
Glenn Kasten4f993392014-05-14 07:30:48 -07001141 /* make sure the size is multiple of 32 bytes
1142 * At 48 kHz mono 16-bit PCM:
1143 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1144 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1145 */
1146 size += 0x1f;
1147 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001148
1149 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001150}
1151
1152static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1153{
1154 struct stream_out *out = (struct stream_out *)stream;
1155
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001156 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001157}
1158
1159static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1160{
1161 return -ENOSYS;
1162}
1163
1164static size_t out_get_buffer_size(const struct audio_stream *stream)
1165{
1166 struct stream_out *out = (struct stream_out *)stream;
1167
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001168 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1169 return out->compr_config.fragment_size;
1170 }
1171
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001172 return out->config.period_size * audio_stream_frame_size(stream);
1173}
1174
1175static uint32_t out_get_channels(const struct audio_stream *stream)
1176{
1177 struct stream_out *out = (struct stream_out *)stream;
1178
1179 return out->channel_mask;
1180}
1181
1182static audio_format_t out_get_format(const struct audio_stream *stream)
1183{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001184 struct stream_out *out = (struct stream_out *)stream;
1185
1186 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001187}
1188
1189static int out_set_format(struct audio_stream *stream, audio_format_t format)
1190{
1191 return -ENOSYS;
1192}
1193
1194static int out_standby(struct audio_stream *stream)
1195{
1196 struct stream_out *out = (struct stream_out *)stream;
1197 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001198
Eric Laurent994a6932013-07-17 11:51:42 -07001199 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001200 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001202 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001203 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001204 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001205 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001206 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1207 if (out->pcm) {
1208 pcm_close(out->pcm);
1209 out->pcm = NULL;
1210 }
1211 } else {
1212 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001213 out->gapless_mdata.encoder_delay = 0;
1214 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001215 if (out->compr != NULL) {
1216 compress_close(out->compr);
1217 out->compr = NULL;
1218 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001219 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001220 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001221 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001222 }
1223 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001224 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001225 return 0;
1226}
1227
1228static int out_dump(const struct audio_stream *stream, int fd)
1229{
1230 return 0;
1231}
1232
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001233static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1234{
1235 int ret = 0;
1236 char value[32];
1237 struct compr_gapless_mdata tmp_mdata;
1238
1239 if (!out || !parms) {
1240 return -EINVAL;
1241 }
1242
1243 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1244 if (ret >= 0) {
1245 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1246 } else {
1247 return -EINVAL;
1248 }
1249
1250 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1251 if (ret >= 0) {
1252 tmp_mdata.encoder_padding = atoi(value);
1253 } else {
1254 return -EINVAL;
1255 }
1256
1257 out->gapless_mdata = tmp_mdata;
1258 out->send_new_metadata = 1;
1259 ALOGV("%s new encoder delay %u and padding %u", __func__,
1260 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1261
1262 return 0;
1263}
1264
1265
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001266static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1267{
1268 struct stream_out *out = (struct stream_out *)stream;
1269 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001270 struct audio_usecase *usecase;
1271 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001272 struct str_parms *parms;
1273 char value[32];
1274 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001275 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001276 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277
sangwoobc677242013-08-08 16:53:43 +09001278 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001279 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001280 parms = str_parms_create_str(kvpairs);
1281 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1282 if (ret >= 0) {
1283 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001284 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001285 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001286
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001287 /*
1288 * When HDMI cable is unplugged the music playback is paused and
1289 * the policy manager sends routing=0. But the audioflinger
1290 * continues to write data until standby time (3sec).
1291 * As the HDMI core is turned off, the write gets blocked.
1292 * Avoid this by routing audio to speaker until standby.
1293 */
1294 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1295 val == AUDIO_DEVICE_NONE) {
1296 val = AUDIO_DEVICE_OUT_SPEAKER;
1297 }
1298
1299 /*
1300 * select_devices() call below switches all the usecases on the same
1301 * backend to the new device. Refer to check_usecases_codec_backend() in
1302 * the select_devices(). But how do we undo this?
1303 *
1304 * For example, music playback is active on headset (deep-buffer usecase)
1305 * and if we go to ringtones and select a ringtone, low-latency usecase
1306 * will be started on headset+speaker. As we can't enable headset+speaker
1307 * and headset devices at the same time, select_devices() switches the music
1308 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1309 * So when the ringtone playback is completed, how do we undo the same?
1310 *
1311 * We are relying on the out_set_parameters() call on deep-buffer output,
1312 * once the ringtone playback is ended.
1313 * NOTE: We should not check if the current devices are same as new devices.
1314 * Because select_devices() must be called to switch back the music
1315 * playback to headset.
1316 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001317 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001318 out->devices = val;
1319
1320 if (!out->standby)
1321 select_devices(adev, out->usecase);
1322
1323 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1324 (out == adev->primary_output)) {
1325 start_voice_call(adev);
1326 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1327 (out == adev->primary_output)) {
1328 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001329 }
1330 }
1331
Ravi Kumar Alamandaed9c56a2013-10-02 09:56:18 -07001332 if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001333 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001334 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001335 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001336
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001338 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001339 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001340
1341 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1342 parse_compress_metadata(out, parms);
1343 }
1344
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001345 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001346 ALOGV("%s: exit: code(%d)", __func__, status);
1347 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001348}
1349
1350static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1351{
1352 struct stream_out *out = (struct stream_out *)stream;
1353 struct str_parms *query = str_parms_create_str(keys);
1354 char *str;
1355 char value[256];
1356 struct str_parms *reply = str_parms_create();
1357 size_t i, j;
1358 int ret;
1359 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001360 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001361 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1362 if (ret >= 0) {
1363 value[0] = '\0';
1364 i = 0;
1365 while (out->supported_channel_masks[i] != 0) {
1366 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1367 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1368 if (!first) {
1369 strcat(value, "|");
1370 }
1371 strcat(value, out_channels_name_to_enum_table[j].name);
1372 first = false;
1373 break;
1374 }
1375 }
1376 i++;
1377 }
1378 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1379 str = str_parms_to_str(reply);
1380 } else {
1381 str = strdup(keys);
1382 }
1383 str_parms_destroy(query);
1384 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001385 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001386 return str;
1387}
1388
1389static uint32_t out_get_latency(const struct audio_stream_out *stream)
1390{
1391 struct stream_out *out = (struct stream_out *)stream;
1392
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001393 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1394 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1395
1396 return (out->config.period_count * out->config.period_size * 1000) /
1397 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001398}
1399
1400static int out_set_volume(struct audio_stream_out *stream, float left,
1401 float right)
1402{
Eric Laurenta9024de2013-04-04 09:19:12 -07001403 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001404 int volume[2];
1405
Eric Laurenta9024de2013-04-04 09:19:12 -07001406 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1407 /* only take left channel into account: the API is for stereo anyway */
1408 out->muted = (left == 0.0f);
1409 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001410 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1411 const char *mixer_ctl_name = "Compress Playback Volume";
1412 struct audio_device *adev = out->dev;
1413 struct mixer_ctl *ctl;
1414
1415 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1416 if (!ctl) {
1417 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1418 __func__, mixer_ctl_name);
1419 return -EINVAL;
1420 }
1421 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1422 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1423 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1424 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001425 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001426
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001427 return -ENOSYS;
1428}
1429
1430static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1431 size_t bytes)
1432{
1433 struct stream_out *out = (struct stream_out *)stream;
1434 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001435 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001436
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001437 pthread_mutex_lock(&out->lock);
1438 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001439 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001440 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001441 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001442 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001443 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001444 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001445 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001446 goto exit;
1447 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001448 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001449
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001450 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001451 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1452 if (out->send_new_metadata) {
1453 ALOGVV("send new gapless metadata");
1454 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1455 out->send_new_metadata = 0;
1456 }
1457
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001458 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001459 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001460 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001461 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1462 }
1463 if (!out->playback_started) {
1464 compress_start(out->compr);
1465 out->playback_started = 1;
1466 out->offload_state = OFFLOAD_STATE_PLAYING;
1467 }
1468 pthread_mutex_unlock(&out->lock);
1469 return ret;
1470 } else {
1471 if (out->pcm) {
1472 if (out->muted)
1473 memset((void *)buffer, 0, bytes);
1474 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1475 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001476 if (ret == 0)
1477 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001478 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001479 }
1480
1481exit:
1482 pthread_mutex_unlock(&out->lock);
1483
1484 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001485 if (out->pcm)
1486 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001487 out_standby(&out->stream.common);
1488 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1489 out_get_sample_rate(&out->stream.common));
1490 }
1491 return bytes;
1492}
1493
1494static int out_get_render_position(const struct audio_stream_out *stream,
1495 uint32_t *dsp_frames)
1496{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001497 struct stream_out *out = (struct stream_out *)stream;
1498 *dsp_frames = 0;
1499 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1500 pthread_mutex_lock(&out->lock);
1501 if (out->compr != NULL) {
1502 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1503 &out->sample_rate);
1504 ALOGVV("%s rendered frames %d sample_rate %d",
1505 __func__, *dsp_frames, out->sample_rate);
1506 }
1507 pthread_mutex_unlock(&out->lock);
1508 return 0;
1509 } else
1510 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001511}
1512
1513static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1514{
1515 return 0;
1516}
1517
1518static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1519{
1520 return 0;
1521}
1522
1523static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1524 int64_t *timestamp)
1525{
1526 return -EINVAL;
1527}
1528
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001529static int out_get_presentation_position(const struct audio_stream_out *stream,
1530 uint64_t *frames, struct timespec *timestamp)
1531{
1532 struct stream_out *out = (struct stream_out *)stream;
1533 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001534 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001535
1536 pthread_mutex_lock(&out->lock);
1537
Eric Laurent949a0892013-09-20 09:20:13 -07001538 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1539 if (out->compr != NULL) {
1540 compress_get_tstamp(out->compr, &dsp_frames,
1541 &out->sample_rate);
1542 ALOGVV("%s rendered frames %ld sample_rate %d",
1543 __func__, dsp_frames, out->sample_rate);
1544 *frames = dsp_frames;
1545 ret = 0;
1546 /* this is the best we can do */
1547 clock_gettime(CLOCK_MONOTONIC, timestamp);
1548 }
1549 } else {
1550 if (out->pcm) {
1551 size_t avail;
1552 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1553 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001554 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001555 // This adjustment accounts for buffering after app processor.
1556 // It is based on estimated DSP latency per use case, rather than exact.
1557 signed_frames -=
1558 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1559
Eric Laurent949a0892013-09-20 09:20:13 -07001560 // It would be unusual for this value to be negative, but check just in case ...
1561 if (signed_frames >= 0) {
1562 *frames = signed_frames;
1563 ret = 0;
1564 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001565 }
1566 }
1567 }
1568
1569 pthread_mutex_unlock(&out->lock);
1570
1571 return ret;
1572}
1573
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001574static int out_set_callback(struct audio_stream_out *stream,
1575 stream_callback_t callback, void *cookie)
1576{
1577 struct stream_out *out = (struct stream_out *)stream;
1578
1579 ALOGV("%s", __func__);
1580 pthread_mutex_lock(&out->lock);
1581 out->offload_callback = callback;
1582 out->offload_cookie = cookie;
1583 pthread_mutex_unlock(&out->lock);
1584 return 0;
1585}
1586
1587static int out_pause(struct audio_stream_out* stream)
1588{
1589 struct stream_out *out = (struct stream_out *)stream;
1590 int status = -ENOSYS;
1591 ALOGV("%s", __func__);
1592 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1593 pthread_mutex_lock(&out->lock);
1594 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1595 status = compress_pause(out->compr);
1596 out->offload_state = OFFLOAD_STATE_PAUSED;
1597 }
1598 pthread_mutex_unlock(&out->lock);
1599 }
1600 return status;
1601}
1602
1603static int out_resume(struct audio_stream_out* stream)
1604{
1605 struct stream_out *out = (struct stream_out *)stream;
1606 int status = -ENOSYS;
1607 ALOGV("%s", __func__);
1608 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1609 status = 0;
1610 pthread_mutex_lock(&out->lock);
1611 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1612 status = compress_resume(out->compr);
1613 out->offload_state = OFFLOAD_STATE_PLAYING;
1614 }
1615 pthread_mutex_unlock(&out->lock);
1616 }
1617 return status;
1618}
1619
1620static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1621{
1622 struct stream_out *out = (struct stream_out *)stream;
1623 int status = -ENOSYS;
1624 ALOGV("%s", __func__);
1625 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1626 pthread_mutex_lock(&out->lock);
1627 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1628 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1629 else
1630 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1631 pthread_mutex_unlock(&out->lock);
1632 }
1633 return status;
1634}
1635
1636static int out_flush(struct audio_stream_out* stream)
1637{
1638 struct stream_out *out = (struct stream_out *)stream;
1639 ALOGV("%s", __func__);
1640 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1641 pthread_mutex_lock(&out->lock);
1642 stop_compressed_output_l(out);
1643 pthread_mutex_unlock(&out->lock);
1644 return 0;
1645 }
1646 return -ENOSYS;
1647}
1648
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001649/** audio_stream_in implementation **/
1650static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1651{
1652 struct stream_in *in = (struct stream_in *)stream;
1653
1654 return in->config.rate;
1655}
1656
1657static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1658{
1659 return -ENOSYS;
1660}
1661
1662static size_t in_get_buffer_size(const struct audio_stream *stream)
1663{
1664 struct stream_in *in = (struct stream_in *)stream;
1665
1666 return in->config.period_size * audio_stream_frame_size(stream);
1667}
1668
1669static uint32_t in_get_channels(const struct audio_stream *stream)
1670{
1671 struct stream_in *in = (struct stream_in *)stream;
1672
1673 return in->channel_mask;
1674}
1675
1676static audio_format_t in_get_format(const struct audio_stream *stream)
1677{
1678 return AUDIO_FORMAT_PCM_16_BIT;
1679}
1680
1681static int in_set_format(struct audio_stream *stream, audio_format_t format)
1682{
1683 return -ENOSYS;
1684}
1685
1686static int in_standby(struct audio_stream *stream)
1687{
1688 struct stream_in *in = (struct stream_in *)stream;
1689 struct audio_device *adev = in->dev;
1690 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001691 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001692 pthread_mutex_lock(&in->lock);
1693 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001694 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001695 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001696 if (in->pcm) {
1697 pcm_close(in->pcm);
1698 in->pcm = NULL;
1699 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001700 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001701 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001702 }
1703 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001704 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001705 return status;
1706}
1707
1708static int in_dump(const struct audio_stream *stream, int fd)
1709{
1710 return 0;
1711}
1712
1713static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1714{
1715 struct stream_in *in = (struct stream_in *)stream;
1716 struct audio_device *adev = in->dev;
1717 struct str_parms *parms;
1718 char *str;
1719 char value[32];
1720 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07001721 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001722
Eric Laurent994a6932013-07-17 11:51:42 -07001723 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001724 parms = str_parms_create_str(kvpairs);
1725
1726 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1727
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001728 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001729 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001730 if (ret >= 0) {
1731 val = atoi(value);
1732 /* no audio source uses val == 0 */
1733 if ((in->source != val) && (val != 0)) {
1734 in->source = val;
1735 }
1736 }
1737
1738 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07001739
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001740 if (ret >= 0) {
1741 val = atoi(value);
1742 if ((in->device != val) && (val != 0)) {
1743 in->device = val;
1744 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001745 if (!in->standby)
Eric Laurent03f09432014-03-25 18:09:11 -07001746 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001747 }
1748 }
1749
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001750 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001751 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001752
1753 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001754 ALOGV("%s: exit: status(%d)", __func__, status);
1755 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001756}
1757
1758static char* in_get_parameters(const struct audio_stream *stream,
1759 const char *keys)
1760{
1761 return strdup("");
1762}
1763
1764static int in_set_gain(struct audio_stream_in *stream, float gain)
1765{
1766 return 0;
1767}
1768
1769static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1770 size_t bytes)
1771{
1772 struct stream_in *in = (struct stream_in *)stream;
1773 struct audio_device *adev = in->dev;
1774 int i, ret = -1;
1775
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001776 pthread_mutex_lock(&in->lock);
1777 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001778 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001779 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001780 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001781 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001782 goto exit;
1783 }
1784 in->standby = 0;
1785 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001786
1787 if (in->pcm) {
1788 ret = pcm_read(in->pcm, buffer, bytes);
1789 }
1790
1791 /*
1792 * Instead of writing zeroes here, we could trust the hardware
1793 * to always provide zeroes when muted.
1794 */
1795 if (ret == 0 && adev->mic_mute)
1796 memset(buffer, 0, bytes);
1797
1798exit:
1799 pthread_mutex_unlock(&in->lock);
1800
1801 if (ret != 0) {
1802 in_standby(&in->stream.common);
1803 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1804 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1805 in_get_sample_rate(&in->stream.common));
1806 }
1807 return bytes;
1808}
1809
1810static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1811{
1812 return 0;
1813}
1814
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001815static int add_remove_audio_effect(const struct audio_stream *stream,
1816 effect_handle_t effect,
1817 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001818{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001819 struct stream_in *in = (struct stream_in *)stream;
1820 int status = 0;
1821 effect_descriptor_t desc;
1822
1823 status = (*effect)->get_descriptor(effect, &desc);
1824 if (status != 0)
1825 return status;
1826
1827 pthread_mutex_lock(&in->lock);
1828 pthread_mutex_lock(&in->dev->lock);
1829 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1830 in->enable_aec != enable &&
1831 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1832 in->enable_aec = enable;
1833 if (!in->standby)
1834 select_devices(in->dev, in->usecase);
1835 }
1836 pthread_mutex_unlock(&in->dev->lock);
1837 pthread_mutex_unlock(&in->lock);
1838
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001839 return 0;
1840}
1841
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001842static int in_add_audio_effect(const struct audio_stream *stream,
1843 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001844{
Eric Laurent994a6932013-07-17 11:51:42 -07001845 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001846 return add_remove_audio_effect(stream, effect, true);
1847}
1848
1849static int in_remove_audio_effect(const struct audio_stream *stream,
1850 effect_handle_t effect)
1851{
Eric Laurent994a6932013-07-17 11:51:42 -07001852 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001853 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001854}
1855
1856static int adev_open_output_stream(struct audio_hw_device *dev,
1857 audio_io_handle_t handle,
1858 audio_devices_t devices,
1859 audio_output_flags_t flags,
1860 struct audio_config *config,
1861 struct audio_stream_out **stream_out)
1862{
1863 struct audio_device *adev = (struct audio_device *)dev;
1864 struct stream_out *out;
1865 int i, ret;
1866
Eric Laurent994a6932013-07-17 11:51:42 -07001867 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001868 __func__, config->sample_rate, config->channel_mask, devices, flags);
1869 *stream_out = NULL;
1870 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1871
1872 if (devices == AUDIO_DEVICE_NONE)
1873 devices = AUDIO_DEVICE_OUT_SPEAKER;
1874
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001875 out->flags = flags;
1876 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001877 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001878 out->format = config->format;
1879 out->sample_rate = config->sample_rate;
1880 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1881 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001882 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001883
1884 /* Init use case and pcm_config */
1885 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07001886 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001887 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001888 pthread_mutex_lock(&adev->lock);
1889 ret = read_hdmi_channel_masks(out);
1890 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001891 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001892 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001893
1894 if (config->sample_rate == 0)
1895 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1896 if (config->channel_mask == 0)
1897 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1898
1899 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001900 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001901 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1902 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001903 out->config.rate = config->sample_rate;
1904 out->config.channels = popcount(out->channel_mask);
1905 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001906 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1907 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1908 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1909 ALOGE("%s: Unsupported Offload information", __func__);
1910 ret = -EINVAL;
1911 goto error_open;
1912 }
1913 if (!is_supported_format(config->offload_info.format)) {
1914 ALOGE("%s: Unsupported audio format", __func__);
1915 ret = -EINVAL;
1916 goto error_open;
1917 }
1918
1919 out->compr_config.codec = (struct snd_codec *)
1920 calloc(1, sizeof(struct snd_codec));
1921
1922 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1923 if (config->offload_info.channel_mask)
1924 out->channel_mask = config->offload_info.channel_mask;
1925 else if (config->channel_mask)
1926 out->channel_mask = config->channel_mask;
1927 out->format = config->offload_info.format;
1928 out->sample_rate = config->offload_info.sample_rate;
1929
1930 out->stream.set_callback = out_set_callback;
1931 out->stream.pause = out_pause;
1932 out->stream.resume = out_resume;
1933 out->stream.drain = out_drain;
1934 out->stream.flush = out_flush;
1935
1936 out->compr_config.codec->id =
1937 get_snd_codec_id(config->offload_info.format);
1938 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1939 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1940 out->compr_config.codec->sample_rate =
1941 compress_get_alsa_rate(config->offload_info.sample_rate);
1942 out->compr_config.codec->bit_rate =
1943 config->offload_info.bit_rate;
1944 out->compr_config.codec->ch_in =
1945 popcount(config->channel_mask);
1946 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1947
1948 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1949 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001950
1951 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001952 create_offload_callback_thread(out);
1953 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1954 __func__, config->offload_info.version,
1955 config->offload_info.bit_rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001956 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07001957 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1958 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1959 out->config = pcm_config_deep_buffer;
1960 } else {
1961 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1962 out->config = pcm_config_low_latency;
1963 }
1964 if (config->format != audio_format_from_pcm_format(out->config.format)) {
1965 if (k_enable_extended_precision
1966 && pcm_params_format_test(adev->use_case_table[out->usecase],
1967 pcm_format_from_audio_format(config->format))) {
1968 out->config.format = pcm_format_from_audio_format(config->format);
1969 /* out->format already set to config->format */
1970 } else {
1971 /* deny the externally proposed config format
1972 * and use the one specified in audio_hw layer configuration.
1973 * Note: out->format is returned by out->stream.common.get_format()
1974 * and is used to set config->format in the code several lines below.
1975 */
1976 out->format = audio_format_from_pcm_format(out->config.format);
1977 }
1978 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001979 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001980 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07001981 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
1982 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001983
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001984 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1985 if(adev->primary_output == NULL)
1986 adev->primary_output = out;
1987 else {
1988 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001989 ret = -EEXIST;
1990 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001991 }
1992 }
1993
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001994 /* Check if this usecase is already existing */
1995 pthread_mutex_lock(&adev->lock);
1996 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1997 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001998 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001999 ret = -EEXIST;
2000 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002001 }
2002 pthread_mutex_unlock(&adev->lock);
2003
2004 out->stream.common.get_sample_rate = out_get_sample_rate;
2005 out->stream.common.set_sample_rate = out_set_sample_rate;
2006 out->stream.common.get_buffer_size = out_get_buffer_size;
2007 out->stream.common.get_channels = out_get_channels;
2008 out->stream.common.get_format = out_get_format;
2009 out->stream.common.set_format = out_set_format;
2010 out->stream.common.standby = out_standby;
2011 out->stream.common.dump = out_dump;
2012 out->stream.common.set_parameters = out_set_parameters;
2013 out->stream.common.get_parameters = out_get_parameters;
2014 out->stream.common.add_audio_effect = out_add_audio_effect;
2015 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2016 out->stream.get_latency = out_get_latency;
2017 out->stream.set_volume = out_set_volume;
2018 out->stream.write = out_write;
2019 out->stream.get_render_position = out_get_render_position;
2020 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002021 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002022
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002023 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002024 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002025 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002026
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002027 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2028 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2029
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002030 config->format = out->stream.common.get_format(&out->stream.common);
2031 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2032 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2033
2034 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002035 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002036 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002037
2038error_open:
2039 free(out);
2040 *stream_out = NULL;
2041 ALOGD("%s: exit: ret %d", __func__, ret);
2042 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002043}
2044
2045static void adev_close_output_stream(struct audio_hw_device *dev,
2046 struct audio_stream_out *stream)
2047{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002048 struct stream_out *out = (struct stream_out *)stream;
2049 struct audio_device *adev = out->dev;
2050
Eric Laurent994a6932013-07-17 11:51:42 -07002051 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002052 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002053 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2054 destroy_offload_callback_thread(out);
2055
2056 if (out->compr_config.codec != NULL)
2057 free(out->compr_config.codec);
2058 }
2059 pthread_cond_destroy(&out->cond);
2060 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002061 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002062 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002063}
2064
2065static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2066{
2067 struct audio_device *adev = (struct audio_device *)dev;
2068 struct str_parms *parms;
2069 char *str;
2070 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002071 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002072 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002073 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002074
Eric Laurent994a6932013-07-17 11:51:42 -07002075 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002076
2077 parms = str_parms_create_str(kvpairs);
2078 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
2079 if (ret >= 0) {
2080 int tty_mode;
2081
2082 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
2083 tty_mode = TTY_MODE_OFF;
2084 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
2085 tty_mode = TTY_MODE_VCO;
2086 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
2087 tty_mode = TTY_MODE_HCO;
2088 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
2089 tty_mode = TTY_MODE_FULL;
2090 else
2091 return -EINVAL;
2092
2093 pthread_mutex_lock(&adev->lock);
2094 if (tty_mode != adev->tty_mode) {
2095 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002096 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
2097 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002098 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002099 }
2100 pthread_mutex_unlock(&adev->lock);
2101 }
2102
2103 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2104 if (ret >= 0) {
2105 /* When set to false, HAL should disable EC and NS
2106 * But it is currently not supported.
2107 */
2108 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2109 adev->bluetooth_nrec = true;
2110 else
2111 adev->bluetooth_nrec = false;
2112 }
2113
2114 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2115 if (ret >= 0) {
2116 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2117 adev->screen_off = false;
2118 else
2119 adev->screen_off = true;
2120 }
2121
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002122 ret = str_parms_get_int(parms, "rotation", &val);
2123 if (ret >= 0) {
2124 bool reverse_speakers = false;
2125 switch(val) {
2126 // FIXME: note that the code below assumes that the speakers are in the correct placement
2127 // relative to the user when the device is rotated 90deg from its default rotation. This
2128 // assumption is device-specific, not platform-specific like this code.
2129 case 270:
2130 reverse_speakers = true;
2131 break;
2132 case 0:
2133 case 90:
2134 case 180:
2135 break;
2136 default:
2137 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002138 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002139 }
Eric Laurent03f09432014-03-25 18:09:11 -07002140 if (status == 0) {
2141 pthread_mutex_lock(&adev->lock);
2142 if (adev->speaker_lr_swap != reverse_speakers) {
2143 adev->speaker_lr_swap = reverse_speakers;
2144 // only update the selected device if there is active pcm playback
2145 struct audio_usecase *usecase;
2146 struct listnode *node;
2147 list_for_each(node, &adev->usecase_list) {
2148 usecase = node_to_item(node, struct audio_usecase, list);
2149 if (usecase->type == PCM_PLAYBACK) {
2150 select_devices(adev, usecase->id);
2151 break;
2152 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002153 }
2154 }
Eric Laurent03f09432014-03-25 18:09:11 -07002155 pthread_mutex_unlock(&adev->lock);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002156 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002157 }
2158
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002159 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2160 if (ret >= 0) {
2161 pthread_mutex_lock(&adev->lock);
2162 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
2163 pthread_mutex_unlock(&adev->lock);
2164 }
2165
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08002166 audio_extn_hfp_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002167 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07002168 ALOGV("%s: exit with code(%d)", __func__, status);
2169 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002170}
2171
2172static char* adev_get_parameters(const struct audio_hw_device *dev,
2173 const char *keys)
2174{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002175 return strdup("");
2176}
2177
2178static int adev_init_check(const struct audio_hw_device *dev)
2179{
2180 return 0;
2181}
2182
Haynes Mathew George5191a852013-09-11 14:19:36 -07002183/* always called with adev lock held */
2184static int set_voice_volume_l(struct audio_device *adev, float volume)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002185{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002186 int vol, err = 0;
2187
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002188 if (adev->mode == AUDIO_MODE_IN_CALL) {
2189 if (volume < 0.0) {
2190 volume = 0.0;
2191 } else if (volume > 1.0) {
2192 volume = 1.0;
2193 }
2194
2195 vol = lrint(volume * 100.0);
2196
2197 // Voice volume levels from android are mapped to driver volume levels as follows.
2198 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
2199 // So adjust the volume to get the correct volume index in driver
2200 vol = 100 - vol;
Eric Laurentb23d5282013-05-14 15:27:20 -07002201
2202 err = platform_set_voice_volume(adev->platform, vol);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002203 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002204 return err;
2205}
2206
Haynes Mathew George5191a852013-09-11 14:19:36 -07002207static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2208{
2209 int ret;
2210 struct audio_device *adev = (struct audio_device *)dev;
2211 pthread_mutex_lock(&adev->lock);
2212 /* cache volume */
2213 adev->voice_volume = volume;
2214 ret = set_voice_volume_l(adev, adev->voice_volume);
2215 pthread_mutex_unlock(&adev->lock);
2216 return ret;
2217}
2218
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002219static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2220{
2221 return -ENOSYS;
2222}
2223
2224static int adev_get_master_volume(struct audio_hw_device *dev,
2225 float *volume)
2226{
2227 return -ENOSYS;
2228}
2229
2230static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2231{
2232 return -ENOSYS;
2233}
2234
2235static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2236{
2237 return -ENOSYS;
2238}
2239
2240static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2241{
2242 struct audio_device *adev = (struct audio_device *)dev;
2243
2244 pthread_mutex_lock(&adev->lock);
2245 if (adev->mode != mode) {
2246 adev->mode = mode;
2247 }
2248 pthread_mutex_unlock(&adev->lock);
2249 return 0;
2250}
2251
2252static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2253{
2254 struct audio_device *adev = (struct audio_device *)dev;
2255 int err = 0;
2256
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002257 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002258 adev->mic_mute = state;
Eric Laurentb23d5282013-05-14 15:27:20 -07002259
2260 err = platform_set_mic_mute(adev->platform, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002261 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002262 return err;
2263}
2264
2265static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2266{
2267 struct audio_device *adev = (struct audio_device *)dev;
2268
2269 *state = adev->mic_mute;
2270
2271 return 0;
2272}
2273
2274static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2275 const struct audio_config *config)
2276{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002277 int channel_count = popcount(config->channel_mask);
2278
2279 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2280}
2281
2282static int adev_open_input_stream(struct audio_hw_device *dev,
2283 audio_io_handle_t handle,
2284 audio_devices_t devices,
2285 struct audio_config *config,
2286 struct audio_stream_in **stream_in)
2287{
2288 struct audio_device *adev = (struct audio_device *)dev;
2289 struct stream_in *in;
2290 int ret, buffer_size, frame_size;
2291 int channel_count = popcount(config->channel_mask);
2292
Eric Laurent994a6932013-07-17 11:51:42 -07002293 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002294 *stream_in = NULL;
2295 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2296 return -EINVAL;
2297
2298 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2299
2300 in->stream.common.get_sample_rate = in_get_sample_rate;
2301 in->stream.common.set_sample_rate = in_set_sample_rate;
2302 in->stream.common.get_buffer_size = in_get_buffer_size;
2303 in->stream.common.get_channels = in_get_channels;
2304 in->stream.common.get_format = in_get_format;
2305 in->stream.common.set_format = in_set_format;
2306 in->stream.common.standby = in_standby;
2307 in->stream.common.dump = in_dump;
2308 in->stream.common.set_parameters = in_set_parameters;
2309 in->stream.common.get_parameters = in_get_parameters;
2310 in->stream.common.add_audio_effect = in_add_audio_effect;
2311 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2312 in->stream.set_gain = in_set_gain;
2313 in->stream.read = in_read;
2314 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2315
2316 in->device = devices;
2317 in->source = AUDIO_SOURCE_DEFAULT;
2318 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002319 in->standby = 1;
2320 in->channel_mask = config->channel_mask;
2321
2322 /* Update config params with the requested sample rate and channels */
2323 in->usecase = USECASE_AUDIO_RECORD;
Glenn Kasten4f993392014-05-14 07:30:48 -07002324#if LOW_LATENCY_CAPTURE_USE_CASE
2325 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE)
2326 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
2327#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002328 in->config = pcm_config_audio_capture;
2329 in->config.channels = channel_count;
2330 in->config.rate = config->sample_rate;
2331
2332 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2333 buffer_size = get_input_buffer_size(config->sample_rate,
2334 config->format,
2335 channel_count);
2336 in->config.period_size = buffer_size / frame_size;
2337
2338 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002339 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002340 return 0;
2341
2342err_open:
2343 free(in);
2344 *stream_in = NULL;
2345 return ret;
2346}
2347
2348static void adev_close_input_stream(struct audio_hw_device *dev,
2349 struct audio_stream_in *stream)
2350{
Eric Laurent994a6932013-07-17 11:51:42 -07002351 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002352
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002353 in_standby(&stream->common);
2354 free(stream);
2355
2356 return;
2357}
2358
2359static int adev_dump(const audio_hw_device_t *device, int fd)
2360{
2361 return 0;
2362}
2363
Andy Hung31aca912014-03-20 17:14:59 -07002364/* verifies input and output devices and their capabilities.
2365 *
2366 * This verification is required when enabling extended bit-depth or
2367 * sampling rates, as not all qcom products support it.
2368 *
2369 * Suitable for calling only on initialization such as adev_open().
2370 * It fills the audio_device use_case_table[] array.
2371 *
2372 * Has a side-effect that it needs to configure audio routing / devices
2373 * in order to power up the devices and read the device parameters.
2374 * It does not acquire any hw device lock. Should restore the devices
2375 * back to "normal state" upon completion.
2376 */
2377static int adev_verify_devices(struct audio_device *adev)
2378{
2379 /* enumeration is a bit difficult because one really wants to pull
2380 * the use_case, device id, etc from the hidden pcm_device_table[].
2381 * In this case there are the following use cases and device ids.
2382 *
2383 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
2384 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
2385 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
2386 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
2387 * [USECASE_AUDIO_RECORD] = {0, 0},
2388 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
2389 * [USECASE_VOICE_CALL] = {2, 2},
2390 *
2391 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
2392 * USECASE_VOICE_CALL omitted, but possible for either input or output.
2393 */
2394
2395 /* should be the usecases enabled in adev_open_input_stream() */
2396 static const int test_in_usecases[] = {
2397 USECASE_AUDIO_RECORD,
2398 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
2399 };
2400 /* should be the usecases enabled in adev_open_output_stream()*/
2401 static const int test_out_usecases[] = {
2402 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
2403 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
2404 };
2405 static const usecase_type_t usecase_type_by_dir[] = {
2406 PCM_PLAYBACK,
2407 PCM_CAPTURE,
2408 };
2409 static const unsigned flags_by_dir[] = {
2410 PCM_OUT,
2411 PCM_IN,
2412 };
2413
2414 size_t i;
2415 unsigned dir;
2416 const unsigned card_id = SOUND_CARD;
2417 char info[512]; /* for possible debug info */
2418
2419 for (dir = 0; dir < 2; ++dir) {
2420 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
2421 const unsigned flags_dir = flags_by_dir[dir];
2422 const size_t testsize =
2423 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
2424 const int *testcases =
2425 dir ? test_in_usecases : test_out_usecases;
2426 const audio_devices_t audio_device =
2427 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
2428
2429 for (i = 0; i < testsize; ++i) {
2430 const audio_usecase_t audio_usecase = testcases[i];
2431 int device_id;
2432 snd_device_t snd_device;
2433 struct pcm_params **pparams;
2434 struct stream_out out;
2435 struct stream_in in;
2436 struct audio_usecase uc_info;
2437 int retval;
2438
2439 pparams = &adev->use_case_table[audio_usecase];
2440 pcm_params_free(*pparams); /* can accept null input */
2441 *pparams = NULL;
2442
2443 /* find the device ID for the use case (signed, for error) */
2444 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
2445 if (device_id < 0)
2446 continue;
2447
2448 /* prepare structures for device probing */
2449 memset(&uc_info, 0, sizeof(uc_info));
2450 uc_info.id = audio_usecase;
2451 uc_info.type = usecase_type;
2452 if (dir) {
2453 adev->active_input = &in;
2454 memset(&in, 0, sizeof(in));
2455 in.device = audio_device;
2456 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
2457 uc_info.stream.in = &in;
2458 } else {
2459 adev->active_input = NULL;
2460 }
2461 memset(&out, 0, sizeof(out));
2462 out.devices = audio_device; /* only field needed in select_devices */
2463 uc_info.stream.out = &out;
2464 uc_info.devices = audio_device;
2465 uc_info.in_snd_device = SND_DEVICE_NONE;
2466 uc_info.out_snd_device = SND_DEVICE_NONE;
2467 list_add_tail(&adev->usecase_list, &uc_info.list);
2468
2469 /* select device - similar to start_(in/out)put_stream() */
2470 retval = select_devices(adev, audio_usecase);
2471 if (retval >= 0) {
2472 *pparams = pcm_params_get(card_id, device_id, flags_dir);
2473#if LOG_NDEBUG == 0
2474 if (*pparams) {
2475 ALOGV("%s: (%s) card %d device %d", __func__,
2476 dir ? "input" : "output", card_id, device_id);
2477 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
2478 ALOGV(info); /* print parameters */
2479 } else {
2480 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
2481 }
2482#endif
2483 }
2484
2485 /* deselect device - similar to stop_(in/out)put_stream() */
2486 /* 1. Get and set stream specific mixer controls */
Glenn Kastene7137302014-04-28 15:12:18 -07002487 retval = disable_audio_route(adev, &uc_info);
Andy Hung31aca912014-03-20 17:14:59 -07002488 /* 2. Disable the rx device */
2489 retval = disable_snd_device(adev,
Glenn Kastene7137302014-04-28 15:12:18 -07002490 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
Andy Hung31aca912014-03-20 17:14:59 -07002491 list_remove(&uc_info.list);
2492 }
2493 }
2494 adev->active_input = NULL; /* restore adev state */
2495 return 0;
2496}
2497
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002498static int adev_close(hw_device_t *device)
2499{
Andy Hung31aca912014-03-20 17:14:59 -07002500 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002501 struct audio_device *adev = (struct audio_device *)device;
2502 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002503 free(adev->snd_dev_ref_cnt);
2504 platform_deinit(adev->platform);
Andy Hung31aca912014-03-20 17:14:59 -07002505 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
2506 pcm_params_free(adev->use_case_table[i]);
2507 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002508 free(device);
2509 return 0;
2510}
2511
Glenn Kasten4f993392014-05-14 07:30:48 -07002512/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
2513 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
2514 * just that it _might_ work.
2515 */
2516static int period_size_is_plausible_for_low_latency(int period_size)
2517{
2518 switch (period_size) {
2519 case 160:
2520 case 240:
2521 case 320:
2522 case 480:
2523 return 1;
2524 default:
2525 return 0;
2526 }
2527}
2528
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002529static int adev_open(const hw_module_t *module, const char *name,
2530 hw_device_t **device)
2531{
2532 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002533 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002534
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002535 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002536 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2537
2538 adev = calloc(1, sizeof(struct audio_device));
2539
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002540 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2541 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2542 adev->device.common.module = (struct hw_module_t *)module;
2543 adev->device.common.close = adev_close;
2544
2545 adev->device.init_check = adev_init_check;
2546 adev->device.set_voice_volume = adev_set_voice_volume;
2547 adev->device.set_master_volume = adev_set_master_volume;
2548 adev->device.get_master_volume = adev_get_master_volume;
2549 adev->device.set_master_mute = adev_set_master_mute;
2550 adev->device.get_master_mute = adev_get_master_mute;
2551 adev->device.set_mode = adev_set_mode;
2552 adev->device.set_mic_mute = adev_set_mic_mute;
2553 adev->device.get_mic_mute = adev_get_mic_mute;
2554 adev->device.set_parameters = adev_set_parameters;
2555 adev->device.get_parameters = adev_get_parameters;
2556 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2557 adev->device.open_output_stream = adev_open_output_stream;
2558 adev->device.close_output_stream = adev_close_output_stream;
2559 adev->device.open_input_stream = adev_open_input_stream;
2560 adev->device.close_input_stream = adev_close_input_stream;
2561 adev->device.dump = adev_dump;
2562
2563 /* Set the default route before the PCM stream is opened */
2564 pthread_mutex_lock(&adev->lock);
2565 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002566 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002567 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002568 adev->voice_call_rx = NULL;
2569 adev->voice_call_tx = NULL;
2570 adev->voice_volume = 1.0f;
2571 adev->tty_mode = TTY_MODE_OFF;
2572 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002573 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002574 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002575 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002576 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002577 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002578 pthread_mutex_unlock(&adev->lock);
2579
2580 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002581 adev->platform = platform_init(adev);
2582 if (!adev->platform) {
2583 free(adev->snd_dev_ref_cnt);
2584 free(adev);
2585 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2586 *device = NULL;
2587 return -EINVAL;
2588 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002589
2590 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2591 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2592 if (adev->visualizer_lib == NULL) {
2593 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2594 } else {
2595 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2596 adev->visualizer_start_output =
2597 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2598 "visualizer_hal_start_output");
2599 adev->visualizer_stop_output =
2600 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2601 "visualizer_hal_stop_output");
2602 }
2603 }
2604
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002605 adev->bt_wb_speech_enabled = false;
2606
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002607 *device = &adev->device.common;
Andy Hung31aca912014-03-20 17:14:59 -07002608 if (k_enable_extended_precision)
2609 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002610
Glenn Kasten4f993392014-05-14 07:30:48 -07002611 char value[PROPERTY_VALUE_MAX];
2612 int trial;
2613 if (property_get("audio_hal.period_size", value, NULL) > 0) {
2614 trial = atoi(value);
2615 if (period_size_is_plausible_for_low_latency(trial)) {
2616 pcm_config_low_latency.period_size = trial;
2617 pcm_config_low_latency.start_threshold = trial / 4;
2618 pcm_config_low_latency.avail_min = trial / 4;
2619 configured_low_latency_capture_period_size = trial;
2620 }
2621 }
2622 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
2623 trial = atoi(value);
2624 if (period_size_is_plausible_for_low_latency(trial)) {
2625 configured_low_latency_capture_period_size = trial;
2626 }
2627 }
2628
Eric Laurent994a6932013-07-17 11:51:42 -07002629 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002630 return 0;
2631}
2632
2633static struct hw_module_methods_t hal_module_methods = {
2634 .open = adev_open,
2635};
2636
2637struct audio_module HAL_MODULE_INFO_SYM = {
2638 .common = {
2639 .tag = HARDWARE_MODULE_TAG,
2640 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2641 .hal_api_version = HARDWARE_HAL_API_VERSION,
2642 .id = AUDIO_HARDWARE_MODULE_ID,
2643 .name = "QCOM Audio HAL",
2644 .author = "Code Aurora Forum",
2645 .methods = &hal_module_methods,
2646 },
2647};