blob: 8fc7764d097acfcf980f287916ed3e60e082854e [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002 * Copyright (C) 2013-2014 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_primary"
18/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070019/*#define VERY_VERY_VERBOSE_LOGGING*/
20#ifdef VERY_VERY_VERBOSE_LOGGING
21#define ALOGVV ALOGV
22#else
23#define ALOGVV(a...) do { } while(0)
24#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080025
26#include <errno.h>
27#include <pthread.h>
28#include <stdint.h>
29#include <sys/time.h>
30#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080031#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070032#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070033#include <sys/resource.h>
34#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080035
36#include <cutils/log.h>
37#include <cutils/str_parms.h>
38#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070039#include <cutils/atomic.h>
40#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080041
Eric Laurentb23d5282013-05-14 15:27:20 -070042#include <hardware/audio_effect.h>
Andy Hung31aca912014-03-20 17:14:59 -070043#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070044#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <audio_effects/effect_aec.h>
46#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080047#include "audio_hw.h"
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -080048#include "audio_extn.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070049#include "platform_api.h"
50#include <platform.h>
Vineeta Srivastava4b89e372014-06-19 14:21:42 -070051#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080052
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070053#include "sound/compress_params.h"
54
Marco Nelissen32093f52015-04-08 15:14:02 -070055#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024)
Marco Nelissen94c33a02015-05-12 09:11:34 -070056// 2 buffers causes problems with high bitrate files
57#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 3
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070058/* ToDo: Check and update a proper value in msec */
59#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
60#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
61
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -070062#define PROXY_OPEN_RETRY_COUNT 100
63#define PROXY_OPEN_WAIT_TIME 20
64
Glenn Kasten4f993392014-05-14 07:30:48 -070065static unsigned int configured_low_latency_capture_period_size =
66 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
67
Andy Hung31aca912014-03-20 17:14:59 -070068/* This constant enables extended precision handling.
69 * TODO The flag is off until more testing is done.
70 */
71static const bool k_enable_extended_precision = false;
72
Eric Laurentb23d5282013-05-14 15:27:20 -070073struct pcm_config pcm_config_deep_buffer = {
74 .channels = 2,
75 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
76 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
77 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
78 .format = PCM_FORMAT_S16_LE,
79 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
80 .stop_threshold = INT_MAX,
81 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
82};
83
84struct pcm_config pcm_config_low_latency = {
85 .channels = 2,
86 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
87 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
88 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
89 .format = PCM_FORMAT_S16_LE,
90 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
91 .stop_threshold = INT_MAX,
92 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
93};
94
95struct pcm_config pcm_config_hdmi_multi = {
96 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
97 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
98 .period_size = HDMI_MULTI_PERIOD_SIZE,
99 .period_count = HDMI_MULTI_PERIOD_COUNT,
100 .format = PCM_FORMAT_S16_LE,
101 .start_threshold = 0,
102 .stop_threshold = INT_MAX,
103 .avail_min = 0,
104};
105
106struct pcm_config pcm_config_audio_capture = {
107 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700108 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
109 .format = PCM_FORMAT_S16_LE,
Eric Laurente2d2d1d2015-07-06 17:54:15 -0700110 .stop_threshold = INT_MAX,
111 .avail_min = 0,
Eric Laurentb23d5282013-05-14 15:27:20 -0700112};
113
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700114#define AFE_PROXY_CHANNEL_COUNT 2
115#define AFE_PROXY_SAMPLING_RATE 48000
116
117#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
118#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
119
120struct pcm_config pcm_config_afe_proxy_playback = {
121 .channels = AFE_PROXY_CHANNEL_COUNT,
122 .rate = AFE_PROXY_SAMPLING_RATE,
123 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
124 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
125 .format = PCM_FORMAT_S16_LE,
126 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
127 .stop_threshold = INT_MAX,
128 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
129};
130
131#define AFE_PROXY_RECORD_PERIOD_SIZE 768
132#define AFE_PROXY_RECORD_PERIOD_COUNT 4
133
134struct pcm_config pcm_config_afe_proxy_record = {
135 .channels = AFE_PROXY_CHANNEL_COUNT,
136 .rate = AFE_PROXY_SAMPLING_RATE,
137 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
138 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
139 .format = PCM_FORMAT_S16_LE,
140 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
141 .stop_threshold = INT_MAX,
142 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
143};
144
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700145const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700146 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
147 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
148 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700149 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Ravi Kumar Alamandaf78a4d92015-04-24 15:18:23 -0700150 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700151
Eric Laurentb23d5282013-05-14 15:27:20 -0700152 [USECASE_AUDIO_RECORD] = "audio-record",
153 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700154
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800155 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
156 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700157
Eric Laurentb23d5282013-05-14 15:27:20 -0700158 [USECASE_VOICE_CALL] = "voice-call",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700159 [USECASE_VOICE2_CALL] = "voice2-call",
160 [USECASE_VOLTE_CALL] = "volte-call",
161 [USECASE_QCHAT_CALL] = "qchat-call",
162 [USECASE_VOWLAN_CALL] = "vowlan-call",
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700163
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700164 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
165 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
166
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700167 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
168 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700169};
170
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800171
172#define STRING_TO_ENUM(string) { #string, string }
173
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800174struct string_to_enum {
175 const char *name;
176 uint32_t value;
177};
178
179static const struct string_to_enum out_channels_name_to_enum_table[] = {
180 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
181 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
182 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
183};
184
Haynes Mathew George5191a852013-09-11 14:19:36 -0700185static int set_voice_volume_l(struct audio_device *adev, float volume);
vivek mehta1a9b7c02015-06-25 11:49:38 -0700186static struct audio_device *adev = NULL;
187static pthread_mutex_t adev_init_lock;
188static unsigned int audio_device_ref_count;
189
190__attribute__ ((visibility ("default")))
191bool audio_hw_send_gain_dep_calibration(int level) {
192 bool ret_val = false;
193 ALOGV("%s: enter ... ", __func__);
194
195 pthread_mutex_lock(&adev_init_lock);
196
197 if (adev != NULL && adev->platform != NULL) {
198 pthread_mutex_lock(&adev->lock);
199 ret_val = platform_send_gain_dep_cal(adev->platform, level);
200 pthread_mutex_unlock(&adev->lock);
201 } else {
202 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
203 }
204
205 pthread_mutex_unlock(&adev_init_lock);
206
207 ALOGV("%s: exit with ret_val %d ", __func__, ret_val);
208 return ret_val;
209}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700210
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700211static bool is_supported_format(audio_format_t format)
212{
Eric Laurent8251ac82014-07-23 11:00:25 -0700213 switch (format) {
214 case AUDIO_FORMAT_MP3:
215 case AUDIO_FORMAT_AAC_LC:
216 case AUDIO_FORMAT_AAC_HE_V1:
217 case AUDIO_FORMAT_AAC_HE_V2:
218 return true;
219 default:
220 break;
221 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700222 return false;
223}
224
225static int get_snd_codec_id(audio_format_t format)
226{
227 int id = 0;
228
Eric Laurent8251ac82014-07-23 11:00:25 -0700229 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700230 case AUDIO_FORMAT_MP3:
231 id = SND_AUDIOCODEC_MP3;
232 break;
233 case AUDIO_FORMAT_AAC:
234 id = SND_AUDIOCODEC_AAC;
235 break;
236 default:
237 ALOGE("%s: Unsupported audio format", __func__);
238 }
239
240 return id;
241}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800242
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800243int enable_audio_route(struct audio_device *adev,
244 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800245{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700246 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800247 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800248
249 if (usecase == NULL)
250 return -EINVAL;
251
252 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
253
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800254 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700255 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800256 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700257 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800258
259 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500260 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700261 ALOGD("%s: apply and update mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700262 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800263
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800264 ALOGV("%s: exit", __func__);
265 return 0;
266}
267
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800268int disable_audio_route(struct audio_device *adev,
269 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800270{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700271 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800272 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800273
274 if (usecase == NULL)
275 return -EINVAL;
276
277 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700278 if (usecase->type == PCM_CAPTURE)
279 snd_device = usecase->in_snd_device;
280 else
281 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800282 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500283 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700284 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700285 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800286
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800287 ALOGV("%s: exit", __func__);
288 return 0;
289}
290
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800291int enable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700292 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800293{
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700294 int i, num_devices = 0;
295 snd_device_t new_snd_devices[2];
296
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800297 if (snd_device < SND_DEVICE_MIN ||
298 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800299 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800300 return -EINVAL;
301 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700302
303 adev->snd_dev_ref_cnt[snd_device]++;
304 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700305 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700306 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700307 return 0;
308 }
309
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700310 /* due to the possibility of calibration overwrite between listen
311 and audio, notify sound trigger hal before audio calibration is sent */
312 audio_extn_sound_trigger_update_device_status(snd_device,
313 ST_EVENT_SND_DEVICE_BUSY);
314
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700315 if (audio_extn_spkr_prot_is_enabled())
316 audio_extn_spkr_prot_calib_cancel(adev);
317
Eric Laurentb23d5282013-05-14 15:27:20 -0700318 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700319 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700320 audio_extn_sound_trigger_update_device_status(snd_device,
321 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800322 return -EINVAL;
323 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800324
zhaoyang yin4211fad2015-06-04 21:13:25 +0800325 audio_extn_dsm_feedback_enable(adev, snd_device, true);
326
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700327 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
328 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
329 audio_extn_spkr_prot_is_enabled()) {
330 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
331 adev->snd_dev_ref_cnt[snd_device]--;
332 return -EINVAL;
333 }
334 if (audio_extn_spkr_prot_start_processing(snd_device)) {
335 ALOGE("%s: spkr_start_processing failed", __func__);
336 return -EINVAL;
337 }
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700338 } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
339 for (i = 0; i < num_devices; i++) {
340 enable_snd_device(adev, new_snd_devices[i]);
341 }
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700342 } else {
343 const char * dev_path = platform_get_snd_device_name(snd_device);
344 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
345 audio_route_apply_and_update_path(adev->audio_route, dev_path);
346 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700347
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800348 return 0;
349}
350
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800351int disable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700352 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800353{
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700354 int i, num_devices = 0;
355 snd_device_t new_snd_devices[2];
356
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800357 if (snd_device < SND_DEVICE_MIN ||
358 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800359 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800360 return -EINVAL;
361 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700362 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
363 ALOGE("%s: device ref cnt is already 0", __func__);
364 return -EINVAL;
365 }
366 adev->snd_dev_ref_cnt[snd_device]--;
367 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700368 const char * dev_path = platform_get_snd_device_name(snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700369 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
zhaoyang yin4211fad2015-06-04 21:13:25 +0800370
371 audio_extn_dsm_feedback_enable(adev, snd_device, false);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700372 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
373 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
374 audio_extn_spkr_prot_is_enabled()) {
375 audio_extn_spkr_prot_stop_processing(snd_device);
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700376 } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
377 for (i = 0; i < num_devices; i++) {
378 disable_snd_device(adev, new_snd_devices[i]);
379 }
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700380 } else {
381 audio_route_reset_and_update_path(adev->audio_route, dev_path);
382 }
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700383 audio_extn_sound_trigger_update_device_status(snd_device,
384 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700385 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800386 return 0;
387}
388
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700389static void check_and_route_playback_usecases(struct audio_device *adev,
390 struct audio_usecase *uc_info,
391 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700392{
393 struct listnode *node;
394 struct audio_usecase *usecase;
395 bool switch_device[AUDIO_USECASE_MAX];
396 int i, num_uc_to_switch = 0;
397
398 /*
399 * This function is to make sure that all the usecases that are active on
400 * the hardware codec backend are always routed to any one device that is
401 * handled by the hardware codec.
402 * For example, if low-latency and deep-buffer usecases are currently active
403 * on speaker and out_set_parameters(headset) is received on low-latency
404 * output, then we have to make sure deep-buffer is also switched to headset,
405 * because of the limitation that both the devices cannot be enabled
406 * at the same time as they share the same backend.
407 */
408 /* Disable all the usecases on the shared backend other than the
409 specified usecase */
410 for (i = 0; i < AUDIO_USECASE_MAX; i++)
411 switch_device[i] = false;
412
413 list_for_each(node, &adev->usecase_list) {
414 usecase = node_to_item(node, struct audio_usecase, list);
415 if (usecase->type != PCM_CAPTURE &&
416 usecase != uc_info &&
417 usecase->out_snd_device != snd_device &&
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700418 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND &&
419 platform_check_backends_match(snd_device, usecase->out_snd_device)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700420 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
421 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700422 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700423 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700424 switch_device[usecase->id] = true;
425 num_uc_to_switch++;
426 }
427 }
428
429 if (num_uc_to_switch) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700430 list_for_each(node, &adev->usecase_list) {
431 usecase = node_to_item(node, struct audio_usecase, list);
432 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700433 disable_snd_device(adev, usecase->out_snd_device);
sangwon.jeon866d5ff2013-10-17 21:42:50 +0900434 }
435 }
436
437 list_for_each(node, &adev->usecase_list) {
438 usecase = node_to_item(node, struct audio_usecase, list);
439 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700440 enable_snd_device(adev, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700441 }
442 }
443
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700444 /* Re-route all the usecases on the shared backend other than the
445 specified usecase to new snd devices */
446 list_for_each(node, &adev->usecase_list) {
447 usecase = node_to_item(node, struct audio_usecase, list);
448 /* Update the out_snd_device only before enabling the audio route */
449 if (switch_device[usecase->id] ) {
450 usecase->out_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700451 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700452 }
453 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700454 }
455}
456
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700457static void check_and_route_capture_usecases(struct audio_device *adev,
458 struct audio_usecase *uc_info,
459 snd_device_t snd_device)
460{
461 struct listnode *node;
462 struct audio_usecase *usecase;
463 bool switch_device[AUDIO_USECASE_MAX];
464 int i, num_uc_to_switch = 0;
465
466 /*
467 * This function is to make sure that all the active capture usecases
468 * are always routed to the same input sound device.
469 * For example, if audio-record and voice-call usecases are currently
470 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
471 * is received for voice call then we have to make sure that audio-record
472 * usecase is also switched to earpiece i.e. voice-dmic-ef,
473 * because of the limitation that two devices cannot be enabled
474 * at the same time if they share the same backend.
475 */
476 for (i = 0; i < AUDIO_USECASE_MAX; i++)
477 switch_device[i] = false;
478
479 list_for_each(node, &adev->usecase_list) {
480 usecase = node_to_item(node, struct audio_usecase, list);
481 if (usecase->type != PCM_PLAYBACK &&
482 usecase != uc_info &&
Anish Kumarff864712015-06-03 13:35:11 -0700483 usecase->in_snd_device != snd_device &&
484 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700485 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
486 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700487 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700488 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700489 switch_device[usecase->id] = true;
490 num_uc_to_switch++;
491 }
492 }
493
494 if (num_uc_to_switch) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700495 list_for_each(node, &adev->usecase_list) {
496 usecase = node_to_item(node, struct audio_usecase, list);
497 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700498 disable_snd_device(adev, usecase->in_snd_device);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700499 }
500 }
501
502 list_for_each(node, &adev->usecase_list) {
503 usecase = node_to_item(node, struct audio_usecase, list);
504 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700505 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700506 }
507 }
508
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700509 /* Re-route all the usecases on the shared backend other than the
510 specified usecase to new snd devices */
511 list_for_each(node, &adev->usecase_list) {
512 usecase = node_to_item(node, struct audio_usecase, list);
513 /* Update the in_snd_device only before enabling the audio route */
514 if (switch_device[usecase->id] ) {
515 usecase->in_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700516 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700517 }
518 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700519 }
520}
521
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800522/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700523static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800524{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700525 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700526 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800527
528 switch (channels) {
529 /*
530 * Do not handle stereo output in Multi-channel cases
531 * Stereo case is handled in normal playback path
532 */
533 case 6:
534 ALOGV("%s: HDMI supports 5.1", __func__);
535 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
536 break;
537 case 8:
538 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
539 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
540 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
541 break;
542 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700543 ALOGE("HDMI does not support multi channel playback");
544 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800545 break;
546 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700547 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800548}
549
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700550static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
551{
552 struct audio_usecase *usecase;
553 struct listnode *node;
554
555 list_for_each(node, &adev->usecase_list) {
556 usecase = node_to_item(node, struct audio_usecase, list);
557 if (usecase->type == VOICE_CALL) {
558 ALOGV("%s: usecase id %d", __func__, usecase->id);
559 return usecase->id;
560 }
561 }
562 return USECASE_INVALID;
563}
564
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800565struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
566 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700567{
568 struct audio_usecase *usecase;
569 struct listnode *node;
570
571 list_for_each(node, &adev->usecase_list) {
572 usecase = node_to_item(node, struct audio_usecase, list);
573 if (usecase->id == uc_id)
574 return usecase;
575 }
576 return NULL;
577}
578
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800579int select_devices(struct audio_device *adev,
580 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800581{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800582 snd_device_t out_snd_device = SND_DEVICE_NONE;
583 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700584 struct audio_usecase *usecase = NULL;
585 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800586 struct audio_usecase *hfp_usecase = NULL;
587 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800588 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700589 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800590
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700591 usecase = get_usecase_from_list(adev, uc_id);
592 if (usecase == NULL) {
593 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
594 return -EINVAL;
595 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800596
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800597 if ((usecase->type == VOICE_CALL) ||
598 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700599 out_snd_device = platform_get_output_snd_device(adev->platform,
600 usecase->stream.out->devices);
601 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700602 usecase->devices = usecase->stream.out->devices;
603 } else {
604 /*
605 * If the voice call is active, use the sound devices of voice call usecase
606 * so that it would not result any device switch. All the usecases will
607 * be switched to new device when select_devices() is called for voice call
608 * usecase. This is to avoid switching devices for voice call when
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700609 * check_and_route_playback_usecases() is called below.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700610 */
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700611 if (voice_is_in_call(adev)) {
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700612 vc_usecase = get_usecase_from_list(adev,
613 get_voice_usecase_id_from_list(adev));
614 if ((vc_usecase != NULL) &&
615 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
616 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700617 in_snd_device = vc_usecase->in_snd_device;
618 out_snd_device = vc_usecase->out_snd_device;
619 }
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800620 } else if (audio_extn_hfp_is_active(adev)) {
621 hfp_ucid = audio_extn_hfp_get_usecase();
622 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
623 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
624 in_snd_device = hfp_usecase->in_snd_device;
625 out_snd_device = hfp_usecase->out_snd_device;
626 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700627 }
628 if (usecase->type == PCM_PLAYBACK) {
629 usecase->devices = usecase->stream.out->devices;
630 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700631 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700632 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700633 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700634 if (usecase->stream.out == adev->primary_output &&
635 adev->active_input &&
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800636 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
637 out_snd_device != usecase->out_snd_device) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700638 select_devices(adev, adev->active_input->usecase);
639 }
640 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700641 } else if (usecase->type == PCM_CAPTURE) {
642 usecase->devices = usecase->stream.in->device;
643 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700644 if (in_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700645 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700646 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
647 adev->primary_output && !adev->primary_output->standby) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700648 out_device = adev->primary_output->devices;
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800649 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700650 } else if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
651 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700652 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700653 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700654 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700655 }
656 }
657
658 if (out_snd_device == usecase->out_snd_device &&
659 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800660 return 0;
661 }
662
sangwoobc677242013-08-08 16:53:43 +0900663 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700664 out_snd_device, platform_get_snd_device_name(out_snd_device),
665 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800666
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800667 /*
668 * Limitation: While in call, to do a device switch we need to disable
669 * and enable both RX and TX devices though one of them is same as current
670 * device.
671 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700672 if ((usecase->type == VOICE_CALL) &&
673 (usecase->in_snd_device != SND_DEVICE_NONE) &&
674 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700675 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800676 }
677
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700678 /* Disable current sound devices */
679 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700680 disable_audio_route(adev, usecase);
681 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800682 }
683
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700684 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700685 disable_audio_route(adev, usecase);
686 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800687 }
688
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700689 /* Applicable only on the targets that has external modem.
690 * New device information should be sent to modem before enabling
691 * the devices to reduce in-call device switch time.
692 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700693 if ((usecase->type == VOICE_CALL) &&
694 (usecase->in_snd_device != SND_DEVICE_NONE) &&
695 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700696 status = platform_switch_voice_call_enable_device_config(adev->platform,
697 out_snd_device,
698 in_snd_device);
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700699 }
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700700
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700701 /* Enable new sound devices */
702 if (out_snd_device != SND_DEVICE_NONE) {
703 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700704 check_and_route_playback_usecases(adev, usecase, out_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700705 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800706 }
707
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700708 if (in_snd_device != SND_DEVICE_NONE) {
709 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700710 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700711 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700712
Eric Laurentb23d5282013-05-14 15:27:20 -0700713 if (usecase->type == VOICE_CALL)
714 status = platform_switch_voice_call_device_post(adev->platform,
715 out_snd_device,
716 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800717
sangwoo170731f2013-06-08 15:36:36 +0900718 usecase->in_snd_device = in_snd_device;
719 usecase->out_snd_device = out_snd_device;
720
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700721 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900722
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700723 /* Applicable only on the targets that has external modem.
724 * Enable device command should be sent to modem only after
725 * enabling voice call mixer controls
726 */
727 if (usecase->type == VOICE_CALL)
728 status = platform_switch_voice_call_usecase_route_post(adev->platform,
729 out_snd_device,
730 in_snd_device);
731
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732 return status;
733}
734
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800735static int stop_input_stream(struct stream_in *in)
736{
737 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800738 struct audio_usecase *uc_info;
739 struct audio_device *adev = in->dev;
740
Eric Laurentc8400632013-02-14 19:04:54 -0800741 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800742
Eric Laurent994a6932013-07-17 11:51:42 -0700743 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700744 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745 uc_info = get_usecase_from_list(adev, in->usecase);
746 if (uc_info == NULL) {
747 ALOGE("%s: Could not find the usecase (%d) in the list",
748 __func__, in->usecase);
749 return -EINVAL;
750 }
751
Eric Laurent150dbfe2013-02-27 14:31:02 -0800752 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700753 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700754
755 /* 2. Disable the tx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700756 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800757
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800758 list_remove(&uc_info->list);
759 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800760
Eric Laurent994a6932013-07-17 11:51:42 -0700761 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800762 return ret;
763}
764
765int start_input_stream(struct stream_in *in)
766{
767 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800768 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800769 struct audio_usecase *uc_info;
770 struct audio_device *adev = in->dev;
771
Eric Laurent994a6932013-07-17 11:51:42 -0700772 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700773 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800774 if (in->pcm_device_id < 0) {
775 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
776 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800777 ret = -EINVAL;
778 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800779 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700780
781 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800782 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
783 uc_info->id = in->usecase;
784 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800785 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700786 uc_info->devices = in->device;
787 uc_info->in_snd_device = SND_DEVICE_NONE;
788 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800789
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800790 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700791 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800792
Eric Laurentc8400632013-02-14 19:04:54 -0800793 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700794 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700795
796 unsigned int flags = PCM_IN;
797 unsigned int pcm_open_retry_count = 0;
798
799 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
800 flags |= PCM_MMAP | PCM_NOIRQ;
801 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800802 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700803
804 while (1) {
805 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
806 flags, &in->config);
807 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
808 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
809 if (in->pcm != NULL) {
810 pcm_close(in->pcm);
811 in->pcm = NULL;
812 }
813 if (pcm_open_retry_count-- == 0) {
814 ret = -EIO;
815 goto error_open;
816 }
817 usleep(PROXY_OPEN_WAIT_TIME * 1000);
818 continue;
819 }
820 break;
821 }
822
Eric Laurent994a6932013-07-17 11:51:42 -0700823 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800824 return ret;
825
826error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800827 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800828
829error_config:
830 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700831 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800832
833 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800834}
835
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700836/* must be called with out->lock locked */
837static int send_offload_cmd_l(struct stream_out* out, int command)
838{
839 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
840
841 ALOGVV("%s %d", __func__, command);
842
843 cmd->cmd = command;
844 list_add_tail(&out->offload_cmd_list, &cmd->node);
845 pthread_cond_signal(&out->offload_cond);
846 return 0;
847}
848
849/* must be called iwth out->lock locked */
850static void stop_compressed_output_l(struct stream_out *out)
851{
852 out->offload_state = OFFLOAD_STATE_IDLE;
853 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700854 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700855 if (out->compr != NULL) {
856 compress_stop(out->compr);
857 while (out->offload_thread_blocked) {
858 pthread_cond_wait(&out->cond, &out->lock);
859 }
860 }
861}
862
863static void *offload_thread_loop(void *context)
864{
865 struct stream_out *out = (struct stream_out *) context;
866 struct listnode *item;
867
868 out->offload_state = OFFLOAD_STATE_IDLE;
869 out->playback_started = 0;
870
871 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
872 set_sched_policy(0, SP_FOREGROUND);
873 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
874
875 ALOGV("%s", __func__);
Eric Laurent6010f702015-06-25 23:39:33 +0000876 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700877 for (;;) {
878 struct offload_cmd *cmd = NULL;
879 stream_callback_event_t event;
880 bool send_callback = false;
881
882 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
883 __func__, list_empty(&out->offload_cmd_list),
884 out->offload_state);
885 if (list_empty(&out->offload_cmd_list)) {
886 ALOGV("%s SLEEPING", __func__);
887 pthread_cond_wait(&out->offload_cond, &out->lock);
888 ALOGV("%s RUNNING", __func__);
889 continue;
890 }
891
892 item = list_head(&out->offload_cmd_list);
893 cmd = node_to_item(item, struct offload_cmd, node);
894 list_remove(item);
895
896 ALOGVV("%s STATE %d CMD %d out->compr %p",
897 __func__, out->offload_state, cmd->cmd, out->compr);
898
899 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
900 free(cmd);
901 break;
902 }
903
904 if (out->compr == NULL) {
905 ALOGE("%s: Compress handle is NULL", __func__);
906 pthread_cond_signal(&out->cond);
907 continue;
908 }
909 out->offload_thread_blocked = true;
910 pthread_mutex_unlock(&out->lock);
911 send_callback = false;
912 switch(cmd->cmd) {
913 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
914 compress_wait(out->compr, -1);
915 send_callback = true;
916 event = STREAM_CBK_EVENT_WRITE_READY;
917 break;
918 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700919 compress_next_track(out->compr);
920 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700921 send_callback = true;
922 event = STREAM_CBK_EVENT_DRAIN_READY;
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800923 /* Resend the metadata for next iteration */
924 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700925 break;
926 case OFFLOAD_CMD_DRAIN:
927 compress_drain(out->compr);
928 send_callback = true;
929 event = STREAM_CBK_EVENT_DRAIN_READY;
930 break;
931 default:
932 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
933 break;
934 }
Eric Laurent6010f702015-06-25 23:39:33 +0000935 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700936 out->offload_thread_blocked = false;
937 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700938 if (send_callback) {
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800939 ALOGVV("%s: sending offload_callback event %d", __func__, event);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700940 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700941 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700942 free(cmd);
943 }
944
945 pthread_cond_signal(&out->cond);
946 while (!list_empty(&out->offload_cmd_list)) {
947 item = list_head(&out->offload_cmd_list);
948 list_remove(item);
949 free(node_to_item(item, struct offload_cmd, node));
950 }
951 pthread_mutex_unlock(&out->lock);
952
953 return NULL;
954}
955
956static int create_offload_callback_thread(struct stream_out *out)
957{
958 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
959 list_init(&out->offload_cmd_list);
960 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
961 offload_thread_loop, out);
962 return 0;
963}
964
965static int destroy_offload_callback_thread(struct stream_out *out)
966{
Eric Laurent6010f702015-06-25 23:39:33 +0000967 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700968 stop_compressed_output_l(out);
969 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
970
971 pthread_mutex_unlock(&out->lock);
972 pthread_join(out->offload_thread, (void **) NULL);
973 pthread_cond_destroy(&out->offload_cond);
974
975 return 0;
976}
977
Eric Laurent07eeafd2013-10-06 12:52:49 -0700978static bool allow_hdmi_channel_config(struct audio_device *adev)
979{
980 struct listnode *node;
981 struct audio_usecase *usecase;
982 bool ret = true;
983
984 list_for_each(node, &adev->usecase_list) {
985 usecase = node_to_item(node, struct audio_usecase, list);
986 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
987 /*
988 * If voice call is already existing, do not proceed further to avoid
989 * disabling/enabling both RX and TX devices, CSD calls, etc.
990 * Once the voice call done, the HDMI channels can be configured to
991 * max channels of remaining use cases.
992 */
993 if (usecase->id == USECASE_VOICE_CALL) {
994 ALOGD("%s: voice call is active, no change in HDMI channels",
995 __func__);
996 ret = false;
997 break;
998 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
999 ALOGD("%s: multi channel playback is active, "
1000 "no change in HDMI channels", __func__);
1001 ret = false;
1002 break;
1003 }
1004 }
1005 }
1006 return ret;
1007}
1008
1009static int check_and_set_hdmi_channels(struct audio_device *adev,
1010 unsigned int channels)
1011{
1012 struct listnode *node;
1013 struct audio_usecase *usecase;
1014
1015 /* Check if change in HDMI channel config is allowed */
1016 if (!allow_hdmi_channel_config(adev))
1017 return 0;
1018
1019 if (channels == adev->cur_hdmi_channels) {
1020 ALOGD("%s: Requested channels are same as current", __func__);
1021 return 0;
1022 }
1023
1024 platform_set_hdmi_channels(adev->platform, channels);
1025 adev->cur_hdmi_channels = channels;
1026
1027 /*
1028 * Deroute all the playback streams routed to HDMI so that
1029 * the back end is deactivated. Note that backend will not
1030 * be deactivated if any one stream is connected to it.
1031 */
1032 list_for_each(node, &adev->usecase_list) {
1033 usecase = node_to_item(node, struct audio_usecase, list);
1034 if (usecase->type == PCM_PLAYBACK &&
1035 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001036 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001037 }
1038 }
1039
1040 /*
1041 * Enable all the streams disabled above. Now the HDMI backend
1042 * will be activated with new channel configuration
1043 */
1044 list_for_each(node, &adev->usecase_list) {
1045 usecase = node_to_item(node, struct audio_usecase, list);
1046 if (usecase->type == PCM_PLAYBACK &&
1047 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001048 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001049 }
1050 }
1051
1052 return 0;
1053}
1054
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001055static int stop_output_stream(struct stream_out *out)
1056{
1057 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001058 struct audio_usecase *uc_info;
1059 struct audio_device *adev = out->dev;
1060
Eric Laurent994a6932013-07-17 11:51:42 -07001061 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001062 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001063 uc_info = get_usecase_from_list(adev, out->usecase);
1064 if (uc_info == NULL) {
1065 ALOGE("%s: Could not find the usecase (%d) in the list",
1066 __func__, out->usecase);
1067 return -EINVAL;
1068 }
1069
Haynes Mathew George41f86652014-06-17 14:22:15 -07001070 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1071 if (adev->visualizer_stop_output != NULL)
1072 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1073 if (adev->offload_effects_stop_output != NULL)
1074 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1075 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001076
Eric Laurent150dbfe2013-02-27 14:31:02 -08001077 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001078 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001079
1080 /* 2. Disable the rx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001081 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001082
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001083 list_remove(&uc_info->list);
1084 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001085
Eric Laurent0499d4f2014-08-25 22:39:29 -05001086 audio_extn_extspk_update(adev->extspk);
1087
Eric Laurent07eeafd2013-10-06 12:52:49 -07001088 /* Must be called after removing the usecase from list */
1089 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1090 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1091
Eric Laurent994a6932013-07-17 11:51:42 -07001092 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001093 return ret;
1094}
1095
1096int start_output_stream(struct stream_out *out)
1097{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001098 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001099 struct audio_usecase *uc_info;
1100 struct audio_device *adev = out->dev;
1101
Eric Laurent994a6932013-07-17 11:51:42 -07001102 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001103 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001104 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001105 if (out->pcm_device_id < 0) {
1106 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1107 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001108 ret = -EINVAL;
1109 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001110 }
1111
1112 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1113 uc_info->id = out->usecase;
1114 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001115 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001116 uc_info->devices = out->devices;
1117 uc_info->in_snd_device = SND_DEVICE_NONE;
1118 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001119
Eric Laurent07eeafd2013-10-06 12:52:49 -07001120 /* This must be called before adding this usecase to the list */
1121 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1122 check_and_set_hdmi_channels(adev, out->config.channels);
1123
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001124 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001125
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001126 select_devices(adev, out->usecase);
1127
Eric Laurent0499d4f2014-08-25 22:39:29 -05001128 audio_extn_extspk_update(adev->extspk);
1129
Andy Hung31aca912014-03-20 17:14:59 -07001130 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001131 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001132 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001133 unsigned int flags = PCM_OUT;
1134 unsigned int pcm_open_retry_count = 0;
1135 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1136 flags |= PCM_MMAP | PCM_NOIRQ;
1137 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1138 } else
1139 flags |= PCM_MONOTONIC;
1140
1141 while (1) {
1142 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1143 flags, &out->config);
1144 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
1145 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1146 if (out->pcm != NULL) {
1147 pcm_close(out->pcm);
1148 out->pcm = NULL;
1149 }
1150 if (pcm_open_retry_count-- == 0) {
1151 ret = -EIO;
1152 goto error_open;
1153 }
1154 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1155 continue;
1156 }
1157 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001158 }
1159 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001160 out->pcm = NULL;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001161 out->compr = compress_open(adev->snd_card, out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001162 COMPRESS_IN, &out->compr_config);
1163 if (out->compr && !is_compress_ready(out->compr)) {
1164 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1165 compress_close(out->compr);
1166 out->compr = NULL;
1167 ret = -EIO;
1168 goto error_open;
1169 }
1170 if (out->offload_callback)
1171 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001172
1173 if (adev->visualizer_start_output != NULL)
Haynes Mathew George41f86652014-06-17 14:22:15 -07001174 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1175 if (adev->offload_effects_start_output != NULL)
1176 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001177 }
Eric Laurent994a6932013-07-17 11:51:42 -07001178 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001179 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001180error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001181 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001182error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001183 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001184}
1185
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001186static int check_input_parameters(uint32_t sample_rate,
1187 audio_format_t format,
1188 int channel_count)
1189{
1190 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1191
1192 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1193
1194 switch (sample_rate) {
1195 case 8000:
1196 case 11025:
1197 case 12000:
1198 case 16000:
1199 case 22050:
1200 case 24000:
1201 case 32000:
1202 case 44100:
1203 case 48000:
1204 break;
1205 default:
1206 return -EINVAL;
1207 }
1208
1209 return 0;
1210}
1211
1212static size_t get_input_buffer_size(uint32_t sample_rate,
1213 audio_format_t format,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001214 int channel_count,
1215 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001216{
1217 size_t size = 0;
1218
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001219 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1220 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001221
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001222 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001223 if (is_low_latency)
Glenn Kasten4f993392014-05-14 07:30:48 -07001224 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001225 /* ToDo: should use frame_size computed based on the format and
1226 channel_count here. */
1227 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001228
Glenn Kasten4f993392014-05-14 07:30:48 -07001229 /* make sure the size is multiple of 32 bytes
1230 * At 48 kHz mono 16-bit PCM:
1231 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1232 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1233 */
1234 size += 0x1f;
1235 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001236
1237 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001238}
1239
1240static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1241{
1242 struct stream_out *out = (struct stream_out *)stream;
1243
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001244 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001245}
1246
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001247static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001248{
1249 return -ENOSYS;
1250}
1251
1252static size_t out_get_buffer_size(const struct audio_stream *stream)
1253{
1254 struct stream_out *out = (struct stream_out *)stream;
1255
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001256 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1257 return out->compr_config.fragment_size;
1258 }
Eric Laurentfdf296a2014-07-03 16:41:51 -07001259 return out->config.period_size *
1260 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001261}
1262
1263static uint32_t out_get_channels(const struct audio_stream *stream)
1264{
1265 struct stream_out *out = (struct stream_out *)stream;
1266
1267 return out->channel_mask;
1268}
1269
1270static audio_format_t out_get_format(const struct audio_stream *stream)
1271{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001272 struct stream_out *out = (struct stream_out *)stream;
1273
1274 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001275}
1276
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001277static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001278{
1279 return -ENOSYS;
1280}
1281
1282static int out_standby(struct audio_stream *stream)
1283{
1284 struct stream_out *out = (struct stream_out *)stream;
1285 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001286
Eric Laurent994a6932013-07-17 11:51:42 -07001287 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001288 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001289
Eric Laurent6010f702015-06-25 23:39:33 +00001290 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001291 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001292 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001293 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001294 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1295 if (out->pcm) {
1296 pcm_close(out->pcm);
1297 out->pcm = NULL;
1298 }
1299 } else {
1300 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001301 out->gapless_mdata.encoder_delay = 0;
1302 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001303 if (out->compr != NULL) {
1304 compress_close(out->compr);
1305 out->compr = NULL;
1306 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001307 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001308 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001309 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001310 }
1311 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001312 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001313 return 0;
1314}
1315
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001316static int out_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001317{
1318 return 0;
1319}
1320
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001321static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1322{
1323 int ret = 0;
1324 char value[32];
1325 struct compr_gapless_mdata tmp_mdata;
1326
1327 if (!out || !parms) {
1328 return -EINVAL;
1329 }
1330
1331 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1332 if (ret >= 0) {
1333 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1334 } else {
1335 return -EINVAL;
1336 }
1337
1338 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1339 if (ret >= 0) {
1340 tmp_mdata.encoder_padding = atoi(value);
1341 } else {
1342 return -EINVAL;
1343 }
1344
1345 out->gapless_mdata = tmp_mdata;
1346 out->send_new_metadata = 1;
1347 ALOGV("%s new encoder delay %u and padding %u", __func__,
1348 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1349
1350 return 0;
1351}
1352
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001353static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1354{
1355 return out == adev->primary_output || out == adev->voice_tx_output;
1356}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001357
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001358static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1359{
1360 struct stream_out *out = (struct stream_out *)stream;
1361 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001362 struct audio_usecase *usecase;
1363 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001364 struct str_parms *parms;
1365 char value[32];
1366 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001367 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001368 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001369
sangwoobc677242013-08-08 16:53:43 +09001370 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001371 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001372 parms = str_parms_create_str(kvpairs);
1373 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1374 if (ret >= 0) {
1375 val = atoi(value);
Eric Laurent6010f702015-06-25 23:39:33 +00001376 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001377 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001378
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001379 /*
1380 * When HDMI cable is unplugged the music playback is paused and
1381 * the policy manager sends routing=0. But the audioflinger
1382 * continues to write data until standby time (3sec).
1383 * As the HDMI core is turned off, the write gets blocked.
1384 * Avoid this by routing audio to speaker until standby.
1385 */
1386 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1387 val == AUDIO_DEVICE_NONE) {
1388 val = AUDIO_DEVICE_OUT_SPEAKER;
1389 }
1390
1391 /*
1392 * select_devices() call below switches all the usecases on the same
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -07001393 * backend to the new device. Refer to check_and_route_playback_usecases() in
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001394 * the select_devices(). But how do we undo this?
1395 *
1396 * For example, music playback is active on headset (deep-buffer usecase)
1397 * and if we go to ringtones and select a ringtone, low-latency usecase
1398 * will be started on headset+speaker. As we can't enable headset+speaker
1399 * and headset devices at the same time, select_devices() switches the music
1400 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1401 * So when the ringtone playback is completed, how do we undo the same?
1402 *
1403 * We are relying on the out_set_parameters() call on deep-buffer output,
1404 * once the ringtone playback is ended.
1405 * NOTE: We should not check if the current devices are same as new devices.
1406 * Because select_devices() must be called to switch back the music
1407 * playback to headset.
1408 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001409 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001410 out->devices = val;
1411
1412 if (!out->standby)
1413 select_devices(adev, out->usecase);
1414
Eric Laurenta7657192014-10-09 21:09:33 -07001415 if (output_drives_call(adev, out)) {
1416 if (!voice_is_in_call(adev)) {
1417 if (adev->mode == AUDIO_MODE_IN_CALL) {
1418 adev->current_call_output = out;
1419 ret = voice_start_call(adev);
1420 }
1421 } else {
1422 adev->current_call_output = out;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001423 voice_update_devices_for_all_voice_usecases(adev);
Eric Laurenta7657192014-10-09 21:09:33 -07001424 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001425 }
1426 }
1427
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001428 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001429 pthread_mutex_unlock(&out->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05001430
1431 /*handles device and call state changes*/
1432 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001433 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001434
1435 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1436 parse_compress_metadata(out, parms);
1437 }
1438
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001439 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001440 ALOGV("%s: exit: code(%d)", __func__, status);
1441 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001442}
1443
1444static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1445{
1446 struct stream_out *out = (struct stream_out *)stream;
1447 struct str_parms *query = str_parms_create_str(keys);
1448 char *str;
1449 char value[256];
1450 struct str_parms *reply = str_parms_create();
1451 size_t i, j;
1452 int ret;
1453 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001454 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001455 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1456 if (ret >= 0) {
1457 value[0] = '\0';
1458 i = 0;
1459 while (out->supported_channel_masks[i] != 0) {
1460 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1461 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1462 if (!first) {
1463 strcat(value, "|");
1464 }
1465 strcat(value, out_channels_name_to_enum_table[j].name);
1466 first = false;
1467 break;
1468 }
1469 }
1470 i++;
1471 }
1472 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1473 str = str_parms_to_str(reply);
1474 } else {
1475 str = strdup(keys);
1476 }
1477 str_parms_destroy(query);
1478 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001479 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001480 return str;
1481}
1482
1483static uint32_t out_get_latency(const struct audio_stream_out *stream)
1484{
1485 struct stream_out *out = (struct stream_out *)stream;
1486
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001487 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1488 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1489
1490 return (out->config.period_count * out->config.period_size * 1000) /
1491 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001492}
1493
1494static int out_set_volume(struct audio_stream_out *stream, float left,
1495 float right)
1496{
Eric Laurenta9024de2013-04-04 09:19:12 -07001497 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001498 int volume[2];
1499
Eric Laurenta9024de2013-04-04 09:19:12 -07001500 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1501 /* only take left channel into account: the API is for stereo anyway */
1502 out->muted = (left == 0.0f);
1503 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001504 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1505 const char *mixer_ctl_name = "Compress Playback Volume";
1506 struct audio_device *adev = out->dev;
1507 struct mixer_ctl *ctl;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001508 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1509 if (!ctl) {
Haynes Mathew George20bcfa82014-06-25 13:37:03 -07001510 /* try with the control based on device id */
1511 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1512 PCM_PLAYBACK);
1513 char ctl_name[128] = {0};
1514 snprintf(ctl_name, sizeof(ctl_name),
1515 "Compress Playback %d Volume", pcm_device_id);
1516 ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name);
1517 if (!ctl) {
1518 ALOGE("%s: Could not get volume ctl mixer cmd", __func__);
1519 return -EINVAL;
1520 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001521 }
1522 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1523 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1524 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1525 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001526 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001527
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001528 return -ENOSYS;
1529}
1530
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001531#ifdef NO_AUDIO_OUT
1532static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
1533 const void *buffer, size_t bytes)
1534{
1535 struct stream_out *out = (struct stream_out *)stream;
1536
1537 /* No Output device supported other than BT for playback.
1538 * Sleep for the amount of buffer duration
1539 */
Eric Laurent6010f702015-06-25 23:39:33 +00001540 pthread_mutex_lock(&out->lock);
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001541 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1542 out_get_sample_rate(&out->stream.common));
1543 pthread_mutex_unlock(&out->lock);
1544 return bytes;
1545}
1546#endif
1547
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001548static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1549 size_t bytes)
1550{
1551 struct stream_out *out = (struct stream_out *)stream;
1552 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001553 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001554
Eric Laurent6010f702015-06-25 23:39:33 +00001555 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001556 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001557 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001558 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001559 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001560 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001561 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001562 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001563 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001564 goto exit;
1565 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001566 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001567
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001568 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001569 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1570 if (out->send_new_metadata) {
1571 ALOGVV("send new gapless metadata");
1572 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1573 out->send_new_metadata = 0;
1574 }
1575
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001576 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001577 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001578 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001579 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1580 }
1581 if (!out->playback_started) {
1582 compress_start(out->compr);
1583 out->playback_started = 1;
1584 out->offload_state = OFFLOAD_STATE_PLAYING;
1585 }
1586 pthread_mutex_unlock(&out->lock);
1587 return ret;
1588 } else {
1589 if (out->pcm) {
1590 if (out->muted)
1591 memset((void *)buffer, 0, bytes);
1592 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001593 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1594 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
1595 }
1596 else
1597 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001598 if (ret == 0)
1599 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001600 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001601 }
1602
1603exit:
1604 pthread_mutex_unlock(&out->lock);
1605
1606 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001607 if (out->pcm)
vivek mehta1a9b7c02015-06-25 11:49:38 -07001608 ALOGE("%s: error %zu - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001609 out_standby(&out->stream.common);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001610 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001611 out_get_sample_rate(&out->stream.common));
1612 }
1613 return bytes;
1614}
1615
1616static int out_get_render_position(const struct audio_stream_out *stream,
1617 uint32_t *dsp_frames)
1618{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001619 struct stream_out *out = (struct stream_out *)stream;
1620 *dsp_frames = 0;
1621 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
Eric Laurent6010f702015-06-25 23:39:33 +00001622 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001623 if (out->compr != NULL) {
1624 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1625 &out->sample_rate);
1626 ALOGVV("%s rendered frames %d sample_rate %d",
1627 __func__, *dsp_frames, out->sample_rate);
1628 }
1629 pthread_mutex_unlock(&out->lock);
1630 return 0;
1631 } else
1632 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001633}
1634
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001635static int out_add_audio_effect(const struct audio_stream *stream __unused,
1636 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001637{
1638 return 0;
1639}
1640
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001641static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1642 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001643{
1644 return 0;
1645}
1646
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001647static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1648 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001649{
1650 return -EINVAL;
1651}
1652
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001653static int out_get_presentation_position(const struct audio_stream_out *stream,
1654 uint64_t *frames, struct timespec *timestamp)
1655{
1656 struct stream_out *out = (struct stream_out *)stream;
1657 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001658 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001659
Eric Laurent6010f702015-06-25 23:39:33 +00001660 pthread_mutex_lock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001661
Eric Laurent949a0892013-09-20 09:20:13 -07001662 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1663 if (out->compr != NULL) {
1664 compress_get_tstamp(out->compr, &dsp_frames,
1665 &out->sample_rate);
1666 ALOGVV("%s rendered frames %ld sample_rate %d",
1667 __func__, dsp_frames, out->sample_rate);
1668 *frames = dsp_frames;
1669 ret = 0;
1670 /* this is the best we can do */
1671 clock_gettime(CLOCK_MONOTONIC, timestamp);
1672 }
1673 } else {
1674 if (out->pcm) {
vivek mehta1a9b7c02015-06-25 11:49:38 -07001675 unsigned int avail;
Eric Laurent949a0892013-09-20 09:20:13 -07001676 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1677 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001678 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001679 // This adjustment accounts for buffering after app processor.
1680 // It is based on estimated DSP latency per use case, rather than exact.
1681 signed_frames -=
1682 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1683
Eric Laurent949a0892013-09-20 09:20:13 -07001684 // It would be unusual for this value to be negative, but check just in case ...
1685 if (signed_frames >= 0) {
1686 *frames = signed_frames;
1687 ret = 0;
1688 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001689 }
1690 }
1691 }
1692
1693 pthread_mutex_unlock(&out->lock);
1694
1695 return ret;
1696}
1697
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001698static int out_set_callback(struct audio_stream_out *stream,
1699 stream_callback_t callback, void *cookie)
1700{
1701 struct stream_out *out = (struct stream_out *)stream;
1702
1703 ALOGV("%s", __func__);
Eric Laurent6010f702015-06-25 23:39:33 +00001704 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001705 out->offload_callback = callback;
1706 out->offload_cookie = cookie;
1707 pthread_mutex_unlock(&out->lock);
1708 return 0;
1709}
1710
1711static int out_pause(struct audio_stream_out* stream)
1712{
1713 struct stream_out *out = (struct stream_out *)stream;
1714 int status = -ENOSYS;
1715 ALOGV("%s", __func__);
1716 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurent6010f702015-06-25 23:39:33 +00001717 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001718 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1719 status = compress_pause(out->compr);
1720 out->offload_state = OFFLOAD_STATE_PAUSED;
1721 }
1722 pthread_mutex_unlock(&out->lock);
1723 }
1724 return status;
1725}
1726
1727static int out_resume(struct audio_stream_out* stream)
1728{
1729 struct stream_out *out = (struct stream_out *)stream;
1730 int status = -ENOSYS;
1731 ALOGV("%s", __func__);
1732 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1733 status = 0;
Eric Laurent6010f702015-06-25 23:39:33 +00001734 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001735 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1736 status = compress_resume(out->compr);
1737 out->offload_state = OFFLOAD_STATE_PLAYING;
1738 }
1739 pthread_mutex_unlock(&out->lock);
1740 }
1741 return status;
1742}
1743
1744static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1745{
1746 struct stream_out *out = (struct stream_out *)stream;
1747 int status = -ENOSYS;
1748 ALOGV("%s", __func__);
1749 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurent6010f702015-06-25 23:39:33 +00001750 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001751 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1752 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1753 else
1754 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1755 pthread_mutex_unlock(&out->lock);
1756 }
1757 return status;
1758}
1759
1760static int out_flush(struct audio_stream_out* stream)
1761{
1762 struct stream_out *out = (struct stream_out *)stream;
1763 ALOGV("%s", __func__);
1764 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurent6010f702015-06-25 23:39:33 +00001765 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001766 stop_compressed_output_l(out);
1767 pthread_mutex_unlock(&out->lock);
1768 return 0;
1769 }
1770 return -ENOSYS;
1771}
1772
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001773/** audio_stream_in implementation **/
1774static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1775{
1776 struct stream_in *in = (struct stream_in *)stream;
1777
1778 return in->config.rate;
1779}
1780
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001781static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001782{
1783 return -ENOSYS;
1784}
1785
1786static size_t in_get_buffer_size(const struct audio_stream *stream)
1787{
1788 struct stream_in *in = (struct stream_in *)stream;
1789
Eric Laurentfdf296a2014-07-03 16:41:51 -07001790 return in->config.period_size *
1791 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001792}
1793
1794static uint32_t in_get_channels(const struct audio_stream *stream)
1795{
1796 struct stream_in *in = (struct stream_in *)stream;
1797
1798 return in->channel_mask;
1799}
1800
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001801static audio_format_t in_get_format(const struct audio_stream *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001802{
1803 return AUDIO_FORMAT_PCM_16_BIT;
1804}
1805
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001806static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001807{
1808 return -ENOSYS;
1809}
1810
1811static int in_standby(struct audio_stream *stream)
1812{
1813 struct stream_in *in = (struct stream_in *)stream;
1814 struct audio_device *adev = in->dev;
1815 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001816 ALOGV("%s: enter", __func__);
Eric Laurent6010f702015-06-25 23:39:33 +00001817 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001818
1819 if (!in->standby && in->is_st_session) {
1820 ALOGD("%s: sound trigger pcm stop lab", __func__);
1821 audio_extn_sound_trigger_stop_lab(in);
1822 in->standby = true;
1823 }
1824
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001825 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001826 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001827 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001828 if (in->pcm) {
1829 pcm_close(in->pcm);
1830 in->pcm = NULL;
1831 }
Eric Laurentcefbbac2014-09-04 13:54:10 -05001832 adev->enable_voicerx = false;
1833 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE );
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001834 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001835 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001836 }
1837 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001838 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001839 return status;
1840}
1841
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001842static int in_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001843{
1844 return 0;
1845}
1846
1847static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1848{
1849 struct stream_in *in = (struct stream_in *)stream;
1850 struct audio_device *adev = in->dev;
1851 struct str_parms *parms;
1852 char *str;
1853 char value[32];
1854 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07001855 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001856
Eric Laurent994a6932013-07-17 11:51:42 -07001857 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001858 parms = str_parms_create_str(kvpairs);
1859
1860 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1861
Eric Laurent6010f702015-06-25 23:39:33 +00001862 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001863 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001864 if (ret >= 0) {
1865 val = atoi(value);
1866 /* no audio source uses val == 0 */
1867 if ((in->source != val) && (val != 0)) {
1868 in->source = val;
1869 }
1870 }
1871
1872 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07001873
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001874 if (ret >= 0) {
1875 val = atoi(value);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001876 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001877 in->device = val;
1878 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001879 if (!in->standby)
Eric Laurent03f09432014-03-25 18:09:11 -07001880 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001881 }
1882 }
1883
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001884 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001885 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001886
1887 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001888 ALOGV("%s: exit: status(%d)", __func__, status);
1889 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001890}
1891
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001892static char* in_get_parameters(const struct audio_stream *stream __unused,
1893 const char *keys __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001894{
1895 return strdup("");
1896}
1897
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001898static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001899{
1900 return 0;
1901}
1902
1903static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1904 size_t bytes)
1905{
1906 struct stream_in *in = (struct stream_in *)stream;
1907 struct audio_device *adev = in->dev;
1908 int i, ret = -1;
1909
Eric Laurent6010f702015-06-25 23:39:33 +00001910 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001911 if (in->is_st_session) {
1912 ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
1913 /* Read from sound trigger HAL */
1914 audio_extn_sound_trigger_read(in, buffer, bytes);
1915 pthread_mutex_unlock(&in->lock);
1916 return bytes;
1917 }
1918
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001919 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001920 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001921 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001922 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001923 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001924 goto exit;
1925 }
1926 in->standby = 0;
1927 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001928
1929 if (in->pcm) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001930 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1931 ret = pcm_mmap_read(in->pcm, buffer, bytes);
1932 } else
1933 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001934 }
1935
1936 /*
1937 * Instead of writing zeroes here, we could trust the hardware
1938 * to always provide zeroes when muted.
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001939 * No need to acquire adev->lock to read mic_muted here as we don't change its state.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001940 */
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001941 if (ret == 0 && adev->mic_muted && in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001942 memset(buffer, 0, bytes);
1943
1944exit:
1945 pthread_mutex_unlock(&in->lock);
1946
1947 if (ret != 0) {
1948 in_standby(&in->stream.common);
1949 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001950 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001951 in_get_sample_rate(&in->stream.common));
1952 }
1953 return bytes;
1954}
1955
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001956static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001957{
1958 return 0;
1959}
1960
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001961static int add_remove_audio_effect(const struct audio_stream *stream,
1962 effect_handle_t effect,
1963 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001964{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001965 struct stream_in *in = (struct stream_in *)stream;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001966 struct audio_device *adev = in->dev;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001967 int status = 0;
1968 effect_descriptor_t desc;
1969
1970 status = (*effect)->get_descriptor(effect, &desc);
1971 if (status != 0)
1972 return status;
1973
Eric Laurent6010f702015-06-25 23:39:33 +00001974 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001975 pthread_mutex_lock(&in->dev->lock);
1976 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1977 in->enable_aec != enable &&
1978 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1979 in->enable_aec = enable;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001980 if (!enable)
1981 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
1982 adev->enable_voicerx = enable;
1983 struct audio_usecase *usecase;
1984 struct listnode *node;
1985 list_for_each(node, &adev->usecase_list) {
1986 usecase = node_to_item(node, struct audio_usecase, list);
1987 if (usecase->type == PCM_PLAYBACK) {
1988 select_devices(adev, usecase->id);
1989 break;
1990 }
1991 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001992 if (!in->standby)
1993 select_devices(in->dev, in->usecase);
1994 }
Ravi Kumar Alamanda3ad4e1b2014-06-03 00:08:15 -07001995 if (in->enable_ns != enable &&
1996 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1997 in->enable_ns = enable;
1998 if (!in->standby)
1999 select_devices(in->dev, in->usecase);
2000 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002001 pthread_mutex_unlock(&in->dev->lock);
2002 pthread_mutex_unlock(&in->lock);
2003
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002004 return 0;
2005}
2006
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002007static int in_add_audio_effect(const struct audio_stream *stream,
2008 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002009{
Eric Laurent994a6932013-07-17 11:51:42 -07002010 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002011 return add_remove_audio_effect(stream, effect, true);
2012}
2013
2014static int in_remove_audio_effect(const struct audio_stream *stream,
2015 effect_handle_t effect)
2016{
Eric Laurent994a6932013-07-17 11:51:42 -07002017 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002018 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002019}
2020
2021static int adev_open_output_stream(struct audio_hw_device *dev,
2022 audio_io_handle_t handle,
2023 audio_devices_t devices,
2024 audio_output_flags_t flags,
2025 struct audio_config *config,
Eric Laurent91975a62014-07-27 17:15:50 -07002026 struct audio_stream_out **stream_out,
2027 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002028{
2029 struct audio_device *adev = (struct audio_device *)dev;
2030 struct stream_out *out;
2031 int i, ret;
2032
Eric Laurent994a6932013-07-17 11:51:42 -07002033 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002034 __func__, config->sample_rate, config->channel_mask, devices, flags);
2035 *stream_out = NULL;
2036 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2037
2038 if (devices == AUDIO_DEVICE_NONE)
2039 devices = AUDIO_DEVICE_OUT_SPEAKER;
2040
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002041 out->flags = flags;
2042 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002043 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002044 out->format = config->format;
2045 out->sample_rate = config->sample_rate;
2046 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2047 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002048 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002049
2050 /* Init use case and pcm_config */
2051 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07002052 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002053 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002054 pthread_mutex_lock(&adev->lock);
2055 ret = read_hdmi_channel_masks(out);
2056 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002057 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002058 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002059
2060 if (config->sample_rate == 0)
2061 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2062 if (config->channel_mask == 0)
2063 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2064
2065 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002066 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002067 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2068 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002069 out->config.rate = config->sample_rate;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002070 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002071 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002072 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2073 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2074 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2075 ALOGE("%s: Unsupported Offload information", __func__);
2076 ret = -EINVAL;
2077 goto error_open;
2078 }
2079 if (!is_supported_format(config->offload_info.format)) {
2080 ALOGE("%s: Unsupported audio format", __func__);
2081 ret = -EINVAL;
2082 goto error_open;
2083 }
2084
2085 out->compr_config.codec = (struct snd_codec *)
2086 calloc(1, sizeof(struct snd_codec));
2087
2088 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2089 if (config->offload_info.channel_mask)
2090 out->channel_mask = config->offload_info.channel_mask;
2091 else if (config->channel_mask)
2092 out->channel_mask = config->channel_mask;
2093 out->format = config->offload_info.format;
2094 out->sample_rate = config->offload_info.sample_rate;
2095
2096 out->stream.set_callback = out_set_callback;
2097 out->stream.pause = out_pause;
2098 out->stream.resume = out_resume;
2099 out->stream.drain = out_drain;
2100 out->stream.flush = out_flush;
2101
2102 out->compr_config.codec->id =
2103 get_snd_codec_id(config->offload_info.format);
2104 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2105 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
Eric Laurent1ccf3972014-10-23 14:42:59 -07002106 out->compr_config.codec->sample_rate = config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002107 out->compr_config.codec->bit_rate =
2108 config->offload_info.bit_rate;
2109 out->compr_config.codec->ch_in =
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002110 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002111 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2112
2113 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2114 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002115
2116 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002117 create_offload_callback_thread(out);
2118 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2119 __func__, config->offload_info.version,
2120 config->offload_info.bit_rate);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002121 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2122 if (config->sample_rate == 0)
2123 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2124 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2125 config->sample_rate != 8000) {
2126 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2127 ret = -EINVAL;
2128 goto error_open;
2129 }
2130 out->sample_rate = config->sample_rate;
2131 out->config.rate = config->sample_rate;
2132 if (config->format == AUDIO_FORMAT_DEFAULT)
2133 config->format = AUDIO_FORMAT_PCM_16_BIT;
2134 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2135 config->format = AUDIO_FORMAT_PCM_16_BIT;
2136 ret = -EINVAL;
2137 goto error_open;
2138 }
2139 out->format = config->format;
2140 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2141 out->config = pcm_config_afe_proxy_playback;
2142 adev->voice_tx_output = out;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002143 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07002144 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2145 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2146 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamandaf78a4d92015-04-24 15:18:23 -07002147 } else if (out->flags & AUDIO_OUTPUT_FLAG_TTS) {
2148 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
2149 out->config = pcm_config_deep_buffer;
Andy Hung6fcba9c2014-03-18 11:53:32 -07002150 } else {
2151 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2152 out->config = pcm_config_low_latency;
2153 }
2154 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2155 if (k_enable_extended_precision
2156 && pcm_params_format_test(adev->use_case_table[out->usecase],
2157 pcm_format_from_audio_format(config->format))) {
2158 out->config.format = pcm_format_from_audio_format(config->format);
2159 /* out->format already set to config->format */
2160 } else {
2161 /* deny the externally proposed config format
2162 * and use the one specified in audio_hw layer configuration.
2163 * Note: out->format is returned by out->stream.common.get_format()
2164 * and is used to set config->format in the code several lines below.
2165 */
2166 out->format = audio_format_from_pcm_format(out->config.format);
2167 }
2168 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002169 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002170 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07002171 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
2172 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002173
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002174 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002175 if (adev->primary_output == NULL)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002176 adev->primary_output = out;
2177 else {
2178 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002179 ret = -EEXIST;
2180 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002181 }
2182 }
2183
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002184 /* Check if this usecase is already existing */
2185 pthread_mutex_lock(&adev->lock);
2186 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2187 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002188 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002189 ret = -EEXIST;
2190 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002191 }
2192 pthread_mutex_unlock(&adev->lock);
2193
2194 out->stream.common.get_sample_rate = out_get_sample_rate;
2195 out->stream.common.set_sample_rate = out_set_sample_rate;
2196 out->stream.common.get_buffer_size = out_get_buffer_size;
2197 out->stream.common.get_channels = out_get_channels;
2198 out->stream.common.get_format = out_get_format;
2199 out->stream.common.set_format = out_set_format;
2200 out->stream.common.standby = out_standby;
2201 out->stream.common.dump = out_dump;
2202 out->stream.common.set_parameters = out_set_parameters;
2203 out->stream.common.get_parameters = out_get_parameters;
2204 out->stream.common.add_audio_effect = out_add_audio_effect;
2205 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2206 out->stream.get_latency = out_get_latency;
2207 out->stream.set_volume = out_set_volume;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002208#ifdef NO_AUDIO_OUT
2209 out->stream.write = out_write_for_no_output;
2210#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002211 out->stream.write = out_write;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002212#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002213 out->stream.get_render_position = out_get_render_position;
2214 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002215 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002216
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002217 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002218 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002219 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002220
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002221 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2222 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2223
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002224 config->format = out->stream.common.get_format(&out->stream.common);
2225 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2226 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2227
2228 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002229 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002230 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002231
2232error_open:
2233 free(out);
2234 *stream_out = NULL;
2235 ALOGD("%s: exit: ret %d", __func__, ret);
2236 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002237}
2238
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002239static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002240 struct audio_stream_out *stream)
2241{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002242 struct stream_out *out = (struct stream_out *)stream;
2243 struct audio_device *adev = out->dev;
2244
Eric Laurent994a6932013-07-17 11:51:42 -07002245 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002246 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002247 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2248 destroy_offload_callback_thread(out);
2249
2250 if (out->compr_config.codec != NULL)
2251 free(out->compr_config.codec);
2252 }
Ravi Kumar Alamandaa4fc9022014-10-08 18:57:46 -07002253
2254 if (adev->voice_tx_output == out)
2255 adev->voice_tx_output = NULL;
2256
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002257 pthread_cond_destroy(&out->cond);
2258 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002259 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002260 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002261}
2262
2263static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2264{
2265 struct audio_device *adev = (struct audio_device *)dev;
2266 struct str_parms *parms;
2267 char *str;
2268 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002269 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002270 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002271 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002272
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002273 ALOGD("%s: enter: %s", __func__, kvpairs);
2274
2275 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002276
2277 parms = str_parms_create_str(kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002278 status = voice_set_parameters(adev, parms);
2279 if (status != 0) {
2280 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002281 }
2282
2283 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2284 if (ret >= 0) {
Ravi Kumar Alamandae258e682015-06-25 13:32:42 -07002285 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002286 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2287 adev->bluetooth_nrec = true;
2288 else
2289 adev->bluetooth_nrec = false;
2290 }
2291
2292 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2293 if (ret >= 0) {
2294 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2295 adev->screen_off = false;
2296 else
2297 adev->screen_off = true;
2298 }
2299
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002300 ret = str_parms_get_int(parms, "rotation", &val);
2301 if (ret >= 0) {
2302 bool reverse_speakers = false;
2303 switch(val) {
2304 // FIXME: note that the code below assumes that the speakers are in the correct placement
2305 // relative to the user when the device is rotated 90deg from its default rotation. This
2306 // assumption is device-specific, not platform-specific like this code.
2307 case 270:
2308 reverse_speakers = true;
2309 break;
2310 case 0:
2311 case 90:
2312 case 180:
2313 break;
2314 default:
2315 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002316 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002317 }
Eric Laurent03f09432014-03-25 18:09:11 -07002318 if (status == 0) {
Ravi Kumar Alamanda1f60cf82015-04-23 19:45:17 -07002319 platform_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002320 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002321 }
2322
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002323 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2324 if (ret >= 0) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002325 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002326 }
2327
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08002328 audio_extn_hfp_set_parameters(adev, parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002329done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002330 str_parms_destroy(parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002331 pthread_mutex_unlock(&adev->lock);
Eric Laurent03f09432014-03-25 18:09:11 -07002332 ALOGV("%s: exit with code(%d)", __func__, status);
2333 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002334}
2335
2336static char* adev_get_parameters(const struct audio_hw_device *dev,
2337 const char *keys)
2338{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002339 struct audio_device *adev = (struct audio_device *)dev;
2340 struct str_parms *reply = str_parms_create();
2341 struct str_parms *query = str_parms_create_str(keys);
2342 char *str;
2343
2344 pthread_mutex_lock(&adev->lock);
2345
2346 voice_get_parameters(adev, query, reply);
2347 str = str_parms_to_str(reply);
2348 str_parms_destroy(query);
2349 str_parms_destroy(reply);
2350
2351 pthread_mutex_unlock(&adev->lock);
2352 ALOGV("%s: exit: returns - %s", __func__, str);
2353 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002354}
2355
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002356static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002357{
2358 return 0;
2359}
2360
Haynes Mathew George5191a852013-09-11 14:19:36 -07002361static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2362{
2363 int ret;
2364 struct audio_device *adev = (struct audio_device *)dev;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002365
Eric Laurent4cc4ce12014-09-10 13:21:01 -05002366 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
2367
Haynes Mathew George5191a852013-09-11 14:19:36 -07002368 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002369 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002370 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002371
Haynes Mathew George5191a852013-09-11 14:19:36 -07002372 return ret;
2373}
2374
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002375static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002376{
2377 return -ENOSYS;
2378}
2379
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002380static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2381 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002382{
2383 return -ENOSYS;
2384}
2385
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002386static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002387{
2388 return -ENOSYS;
2389}
2390
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002391static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002392{
2393 return -ENOSYS;
2394}
2395
2396static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2397{
2398 struct audio_device *adev = (struct audio_device *)dev;
2399
2400 pthread_mutex_lock(&adev->lock);
2401 if (adev->mode != mode) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002402 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002403 adev->mode = mode;
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -07002404 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) &&
2405 voice_is_in_call(adev)) {
2406 voice_stop_call(adev);
2407 adev->current_call_output = NULL;
2408 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002409 }
2410 pthread_mutex_unlock(&adev->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002411
2412 audio_extn_extspk_set_mode(adev->extspk, mode);
2413
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002414 return 0;
2415}
2416
2417static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2418{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002419 int ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002420 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002421
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002422 ALOGD("%s: state %d\n", __func__, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002423 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002424 ret = voice_set_mic_mute(adev, state);
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07002425 adev->mic_muted = state;
Eric Laurenta609e8e2014-06-18 02:15:17 +00002426 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002427
2428 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002429}
2430
2431static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2432{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002433 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002434 return 0;
2435}
2436
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002437static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002438 const struct audio_config *config)
2439{
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002440 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002441
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002442 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
2443 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002444}
2445
2446static int adev_open_input_stream(struct audio_hw_device *dev,
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002447 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002448 audio_devices_t devices,
2449 struct audio_config *config,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002450 struct audio_stream_in **stream_in,
Eric Laurent91975a62014-07-27 17:15:50 -07002451 audio_input_flags_t flags,
2452 const char *address __unused,
Eric Laurentcefbbac2014-09-04 13:54:10 -05002453 audio_source_t source )
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002454{
2455 struct audio_device *adev = (struct audio_device *)dev;
2456 struct stream_in *in;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002457 int ret = 0, buffer_size, frame_size;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002458 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002459 bool is_low_latency = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002460
Eric Laurent994a6932013-07-17 11:51:42 -07002461 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002462 *stream_in = NULL;
2463 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2464 return -EINVAL;
2465
2466 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2467
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002468 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2469
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002470 in->stream.common.get_sample_rate = in_get_sample_rate;
2471 in->stream.common.set_sample_rate = in_set_sample_rate;
2472 in->stream.common.get_buffer_size = in_get_buffer_size;
2473 in->stream.common.get_channels = in_get_channels;
2474 in->stream.common.get_format = in_get_format;
2475 in->stream.common.set_format = in_set_format;
2476 in->stream.common.standby = in_standby;
2477 in->stream.common.dump = in_dump;
2478 in->stream.common.set_parameters = in_set_parameters;
2479 in->stream.common.get_parameters = in_get_parameters;
2480 in->stream.common.add_audio_effect = in_add_audio_effect;
2481 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2482 in->stream.set_gain = in_set_gain;
2483 in->stream.read = in_read;
2484 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2485
2486 in->device = devices;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002487 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002488 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002489 in->standby = 1;
2490 in->channel_mask = config->channel_mask;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002491 in->capture_handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002492
2493 /* Update config params with the requested sample rate and channels */
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002494 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
2495 if (config->sample_rate == 0)
2496 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2497 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2498 config->sample_rate != 8000) {
2499 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2500 ret = -EINVAL;
2501 goto err_open;
2502 }
2503 if (config->format == AUDIO_FORMAT_DEFAULT)
2504 config->format = AUDIO_FORMAT_PCM_16_BIT;
2505 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2506 config->format = AUDIO_FORMAT_PCM_16_BIT;
2507 ret = -EINVAL;
2508 goto err_open;
2509 }
2510
2511 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
2512 in->config = pcm_config_afe_proxy_record;
2513 } else {
2514 in->usecase = USECASE_AUDIO_RECORD;
2515 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
2516 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
2517 is_low_latency = true;
Glenn Kasten4f993392014-05-14 07:30:48 -07002518#if LOW_LATENCY_CAPTURE_USE_CASE
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002519 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Glenn Kasten4f993392014-05-14 07:30:48 -07002520#endif
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002521 }
2522 in->config = pcm_config_audio_capture;
2523
2524 frame_size = audio_stream_in_frame_size(&in->stream);
2525 buffer_size = get_input_buffer_size(config->sample_rate,
2526 config->format,
2527 channel_count,
2528 is_low_latency);
2529 in->config.period_size = buffer_size / frame_size;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002530 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002531 in->config.channels = channel_count;
2532 in->config.rate = config->sample_rate;
2533
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002534 /* This stream could be for sound trigger lab,
2535 get sound trigger pcm if present */
2536 audio_extn_sound_trigger_check_and_get_session(in);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002537
2538 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002539 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002540 return 0;
2541
2542err_open:
2543 free(in);
2544 *stream_in = NULL;
2545 return ret;
2546}
2547
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002548static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002549 struct audio_stream_in *stream)
2550{
Eric Laurent994a6932013-07-17 11:51:42 -07002551 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002552
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002553 in_standby(&stream->common);
2554 free(stream);
2555
2556 return;
2557}
2558
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002559static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002560{
2561 return 0;
2562}
2563
Andy Hung31aca912014-03-20 17:14:59 -07002564/* verifies input and output devices and their capabilities.
2565 *
2566 * This verification is required when enabling extended bit-depth or
2567 * sampling rates, as not all qcom products support it.
2568 *
2569 * Suitable for calling only on initialization such as adev_open().
2570 * It fills the audio_device use_case_table[] array.
2571 *
2572 * Has a side-effect that it needs to configure audio routing / devices
2573 * in order to power up the devices and read the device parameters.
2574 * It does not acquire any hw device lock. Should restore the devices
2575 * back to "normal state" upon completion.
2576 */
2577static int adev_verify_devices(struct audio_device *adev)
2578{
2579 /* enumeration is a bit difficult because one really wants to pull
2580 * the use_case, device id, etc from the hidden pcm_device_table[].
2581 * In this case there are the following use cases and device ids.
2582 *
2583 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
2584 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
2585 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
2586 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
2587 * [USECASE_AUDIO_RECORD] = {0, 0},
2588 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
2589 * [USECASE_VOICE_CALL] = {2, 2},
2590 *
2591 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
2592 * USECASE_VOICE_CALL omitted, but possible for either input or output.
2593 */
2594
2595 /* should be the usecases enabled in adev_open_input_stream() */
2596 static const int test_in_usecases[] = {
2597 USECASE_AUDIO_RECORD,
2598 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
2599 };
2600 /* should be the usecases enabled in adev_open_output_stream()*/
2601 static const int test_out_usecases[] = {
2602 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
2603 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
2604 };
2605 static const usecase_type_t usecase_type_by_dir[] = {
2606 PCM_PLAYBACK,
2607 PCM_CAPTURE,
2608 };
2609 static const unsigned flags_by_dir[] = {
2610 PCM_OUT,
2611 PCM_IN,
2612 };
2613
2614 size_t i;
2615 unsigned dir;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002616 const unsigned card_id = adev->snd_card;
Andy Hung31aca912014-03-20 17:14:59 -07002617 char info[512]; /* for possible debug info */
2618
2619 for (dir = 0; dir < 2; ++dir) {
2620 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
2621 const unsigned flags_dir = flags_by_dir[dir];
2622 const size_t testsize =
2623 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
2624 const int *testcases =
2625 dir ? test_in_usecases : test_out_usecases;
2626 const audio_devices_t audio_device =
2627 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
2628
2629 for (i = 0; i < testsize; ++i) {
2630 const audio_usecase_t audio_usecase = testcases[i];
2631 int device_id;
2632 snd_device_t snd_device;
2633 struct pcm_params **pparams;
2634 struct stream_out out;
2635 struct stream_in in;
2636 struct audio_usecase uc_info;
2637 int retval;
2638
2639 pparams = &adev->use_case_table[audio_usecase];
2640 pcm_params_free(*pparams); /* can accept null input */
2641 *pparams = NULL;
2642
2643 /* find the device ID for the use case (signed, for error) */
2644 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
2645 if (device_id < 0)
2646 continue;
2647
2648 /* prepare structures for device probing */
2649 memset(&uc_info, 0, sizeof(uc_info));
2650 uc_info.id = audio_usecase;
2651 uc_info.type = usecase_type;
2652 if (dir) {
2653 adev->active_input = &in;
2654 memset(&in, 0, sizeof(in));
2655 in.device = audio_device;
2656 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
2657 uc_info.stream.in = &in;
2658 } else {
2659 adev->active_input = NULL;
2660 }
2661 memset(&out, 0, sizeof(out));
2662 out.devices = audio_device; /* only field needed in select_devices */
2663 uc_info.stream.out = &out;
2664 uc_info.devices = audio_device;
2665 uc_info.in_snd_device = SND_DEVICE_NONE;
2666 uc_info.out_snd_device = SND_DEVICE_NONE;
2667 list_add_tail(&adev->usecase_list, &uc_info.list);
2668
2669 /* select device - similar to start_(in/out)put_stream() */
2670 retval = select_devices(adev, audio_usecase);
2671 if (retval >= 0) {
2672 *pparams = pcm_params_get(card_id, device_id, flags_dir);
2673#if LOG_NDEBUG == 0
2674 if (*pparams) {
2675 ALOGV("%s: (%s) card %d device %d", __func__,
2676 dir ? "input" : "output", card_id, device_id);
2677 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
2678 ALOGV(info); /* print parameters */
2679 } else {
2680 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
2681 }
2682#endif
2683 }
2684
2685 /* deselect device - similar to stop_(in/out)put_stream() */
2686 /* 1. Get and set stream specific mixer controls */
Glenn Kastene7137302014-04-28 15:12:18 -07002687 retval = disable_audio_route(adev, &uc_info);
Andy Hung31aca912014-03-20 17:14:59 -07002688 /* 2. Disable the rx device */
2689 retval = disable_snd_device(adev,
Glenn Kastene7137302014-04-28 15:12:18 -07002690 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
Andy Hung31aca912014-03-20 17:14:59 -07002691 list_remove(&uc_info.list);
2692 }
2693 }
2694 adev->active_input = NULL; /* restore adev state */
2695 return 0;
2696}
2697
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002698static int adev_close(hw_device_t *device)
2699{
Andy Hung31aca912014-03-20 17:14:59 -07002700 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002701 struct audio_device *adev = (struct audio_device *)device;
vivek mehta1a9b7c02015-06-25 11:49:38 -07002702
2703 if (!adev)
2704 return 0;
2705
2706 pthread_mutex_lock(&adev_init_lock);
2707
2708 if ((--audio_device_ref_count) == 0) {
2709 audio_route_free(adev->audio_route);
2710 free(adev->snd_dev_ref_cnt);
2711 platform_deinit(adev->platform);
2712 audio_extn_extspk_deinit(adev->extspk);
2713 audio_extn_sound_trigger_deinit(adev);
2714 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
2715 pcm_params_free(adev->use_case_table[i]);
2716 }
2717 free(device);
Andy Hung31aca912014-03-20 17:14:59 -07002718 }
vivek mehta1a9b7c02015-06-25 11:49:38 -07002719
2720 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002721 return 0;
2722}
2723
Glenn Kasten4f993392014-05-14 07:30:48 -07002724/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
2725 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
2726 * just that it _might_ work.
2727 */
2728static int period_size_is_plausible_for_low_latency(int period_size)
2729{
2730 switch (period_size) {
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002731 case 48:
2732 case 96:
2733 case 144:
Glenn Kasten4f993392014-05-14 07:30:48 -07002734 case 160:
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002735 case 192:
Glenn Kasten4f993392014-05-14 07:30:48 -07002736 case 240:
2737 case 320:
2738 case 480:
2739 return 1;
2740 default:
2741 return 0;
2742 }
2743}
2744
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002745static int adev_open(const hw_module_t *module, const char *name,
2746 hw_device_t **device)
2747{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002748 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002749
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002750 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002751 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
vivek mehta1a9b7c02015-06-25 11:49:38 -07002752 pthread_mutex_lock(&adev_init_lock);
2753 if (audio_device_ref_count != 0) {
2754 *device = &adev->device.common;
2755 audio_device_ref_count++;
2756 ALOGV("%s: returning existing instance of adev", __func__);
2757 ALOGV("%s: exit", __func__);
2758 pthread_mutex_unlock(&adev_init_lock);
2759 return 0;
2760 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002761 adev = calloc(1, sizeof(struct audio_device));
2762
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002763 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2764
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002765 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2766 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2767 adev->device.common.module = (struct hw_module_t *)module;
2768 adev->device.common.close = adev_close;
2769
2770 adev->device.init_check = adev_init_check;
2771 adev->device.set_voice_volume = adev_set_voice_volume;
2772 adev->device.set_master_volume = adev_set_master_volume;
2773 adev->device.get_master_volume = adev_get_master_volume;
2774 adev->device.set_master_mute = adev_set_master_mute;
2775 adev->device.get_master_mute = adev_get_master_mute;
2776 adev->device.set_mode = adev_set_mode;
2777 adev->device.set_mic_mute = adev_set_mic_mute;
2778 adev->device.get_mic_mute = adev_get_mic_mute;
2779 adev->device.set_parameters = adev_set_parameters;
2780 adev->device.get_parameters = adev_get_parameters;
2781 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2782 adev->device.open_output_stream = adev_open_output_stream;
2783 adev->device.close_output_stream = adev_close_output_stream;
2784 adev->device.open_input_stream = adev_open_input_stream;
2785 adev->device.close_input_stream = adev_close_input_stream;
2786 adev->device.dump = adev_dump;
2787
2788 /* Set the default route before the PCM stream is opened */
2789 pthread_mutex_lock(&adev->lock);
2790 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002791 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002792 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002793 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002794 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002795 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002796 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002797 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002798 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002799 pthread_mutex_unlock(&adev->lock);
2800
2801 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002802 adev->platform = platform_init(adev);
2803 if (!adev->platform) {
2804 free(adev->snd_dev_ref_cnt);
2805 free(adev);
2806 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2807 *device = NULL;
vivek mehta1a9b7c02015-06-25 11:49:38 -07002808 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07002809 return -EINVAL;
2810 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002811
Eric Laurent0499d4f2014-08-25 22:39:29 -05002812 adev->extspk = audio_extn_extspk_init(adev);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002813 audio_extn_sound_trigger_init(adev);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002814
Eric Laurentc4aef752013-09-12 17:45:53 -07002815 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2816 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2817 if (adev->visualizer_lib == NULL) {
2818 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2819 } else {
2820 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2821 adev->visualizer_start_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002822 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002823 "visualizer_hal_start_output");
2824 adev->visualizer_stop_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002825 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002826 "visualizer_hal_stop_output");
2827 }
2828 }
2829
Haynes Mathew George41f86652014-06-17 14:22:15 -07002830 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2831 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2832 if (adev->offload_effects_lib == NULL) {
2833 ALOGE("%s: DLOPEN failed for %s", __func__,
2834 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2835 } else {
2836 ALOGV("%s: DLOPEN successful for %s", __func__,
2837 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2838 adev->offload_effects_start_output =
2839 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2840 "offload_effects_bundle_hal_start_output");
2841 adev->offload_effects_stop_output =
2842 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2843 "offload_effects_bundle_hal_stop_output");
2844 }
2845 }
2846
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002847 adev->bt_wb_speech_enabled = false;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002848 adev->enable_voicerx = false;
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002849
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002850 *device = &adev->device.common;
vivek mehta1a9b7c02015-06-25 11:49:38 -07002851
Andy Hung31aca912014-03-20 17:14:59 -07002852 if (k_enable_extended_precision)
2853 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002854
Glenn Kasten4f993392014-05-14 07:30:48 -07002855 char value[PROPERTY_VALUE_MAX];
2856 int trial;
2857 if (property_get("audio_hal.period_size", value, NULL) > 0) {
2858 trial = atoi(value);
2859 if (period_size_is_plausible_for_low_latency(trial)) {
2860 pcm_config_low_latency.period_size = trial;
2861 pcm_config_low_latency.start_threshold = trial / 4;
2862 pcm_config_low_latency.avail_min = trial / 4;
2863 configured_low_latency_capture_period_size = trial;
2864 }
2865 }
2866 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
2867 trial = atoi(value);
2868 if (period_size_is_plausible_for_low_latency(trial)) {
2869 configured_low_latency_capture_period_size = trial;
2870 }
2871 }
2872
vivek mehta1a9b7c02015-06-25 11:49:38 -07002873 audio_device_ref_count++;
2874 pthread_mutex_unlock(&adev_init_lock);
2875
Eric Laurent994a6932013-07-17 11:51:42 -07002876 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002877 return 0;
2878}
2879
2880static struct hw_module_methods_t hal_module_methods = {
2881 .open = adev_open,
2882};
2883
2884struct audio_module HAL_MODULE_INFO_SYM = {
2885 .common = {
2886 .tag = HARDWARE_MODULE_TAG,
2887 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2888 .hal_api_version = HARDWARE_HAL_API_VERSION,
2889 .id = AUDIO_HARDWARE_MODULE_ID,
2890 .name = "QCOM Audio HAL",
2891 .author = "Code Aurora Forum",
2892 .methods = &hal_module_methods,
2893 },
2894};