blob: 8451ad7077fc5287d7f9ce5debff037bfdb0bb2b [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,
110};
111
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700112#define AFE_PROXY_CHANNEL_COUNT 2
113#define AFE_PROXY_SAMPLING_RATE 48000
114
115#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
116#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
117
118struct pcm_config pcm_config_afe_proxy_playback = {
119 .channels = AFE_PROXY_CHANNEL_COUNT,
120 .rate = AFE_PROXY_SAMPLING_RATE,
121 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
122 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
123 .format = PCM_FORMAT_S16_LE,
124 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
125 .stop_threshold = INT_MAX,
126 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
127};
128
129#define AFE_PROXY_RECORD_PERIOD_SIZE 768
130#define AFE_PROXY_RECORD_PERIOD_COUNT 4
131
132struct pcm_config pcm_config_afe_proxy_record = {
133 .channels = AFE_PROXY_CHANNEL_COUNT,
134 .rate = AFE_PROXY_SAMPLING_RATE,
135 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
136 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
137 .format = PCM_FORMAT_S16_LE,
138 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
139 .stop_threshold = INT_MAX,
140 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
141};
142
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700143const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700144 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
145 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
146 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700147 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Ravi Kumar Alamandaf78a4d92015-04-24 15:18:23 -0700148 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700149
Eric Laurentb23d5282013-05-14 15:27:20 -0700150 [USECASE_AUDIO_RECORD] = "audio-record",
151 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700152
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800153 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
154 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700155
Eric Laurentb23d5282013-05-14 15:27:20 -0700156 [USECASE_VOICE_CALL] = "voice-call",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700157 [USECASE_VOICE2_CALL] = "voice2-call",
158 [USECASE_VOLTE_CALL] = "volte-call",
159 [USECASE_QCHAT_CALL] = "qchat-call",
160 [USECASE_VOWLAN_CALL] = "vowlan-call",
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700161
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700162 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
163 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
164
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700165 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
166 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700167};
168
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800169
170#define STRING_TO_ENUM(string) { #string, string }
171
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800172struct string_to_enum {
173 const char *name;
174 uint32_t value;
175};
176
177static const struct string_to_enum out_channels_name_to_enum_table[] = {
178 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
179 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
180 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
181};
182
Haynes Mathew George5191a852013-09-11 14:19:36 -0700183static int set_voice_volume_l(struct audio_device *adev, float volume);
vivek mehta1a9b7c02015-06-25 11:49:38 -0700184static struct audio_device *adev = NULL;
185static pthread_mutex_t adev_init_lock;
186static unsigned int audio_device_ref_count;
187
188__attribute__ ((visibility ("default")))
189bool audio_hw_send_gain_dep_calibration(int level) {
190 bool ret_val = false;
191 ALOGV("%s: enter ... ", __func__);
192
193 pthread_mutex_lock(&adev_init_lock);
194
195 if (adev != NULL && adev->platform != NULL) {
196 pthread_mutex_lock(&adev->lock);
197 ret_val = platform_send_gain_dep_cal(adev->platform, level);
198 pthread_mutex_unlock(&adev->lock);
199 } else {
200 ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
201 }
202
203 pthread_mutex_unlock(&adev_init_lock);
204
205 ALOGV("%s: exit with ret_val %d ", __func__, ret_val);
206 return ret_val;
207}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700208
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700209static bool is_supported_format(audio_format_t format)
210{
Eric Laurent8251ac82014-07-23 11:00:25 -0700211 switch (format) {
212 case AUDIO_FORMAT_MP3:
213 case AUDIO_FORMAT_AAC_LC:
214 case AUDIO_FORMAT_AAC_HE_V1:
215 case AUDIO_FORMAT_AAC_HE_V2:
216 return true;
217 default:
218 break;
219 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700220 return false;
221}
222
223static int get_snd_codec_id(audio_format_t format)
224{
225 int id = 0;
226
Eric Laurent8251ac82014-07-23 11:00:25 -0700227 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700228 case AUDIO_FORMAT_MP3:
229 id = SND_AUDIOCODEC_MP3;
230 break;
231 case AUDIO_FORMAT_AAC:
232 id = SND_AUDIOCODEC_AAC;
233 break;
234 default:
235 ALOGE("%s: Unsupported audio format", __func__);
236 }
237
238 return id;
239}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800240
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800241int enable_audio_route(struct audio_device *adev,
242 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800243{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700244 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800245 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800246
247 if (usecase == NULL)
248 return -EINVAL;
249
250 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
251
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800252 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700253 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800254 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700255 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800256
257 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500258 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700259 ALOGD("%s: apply and update mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700260 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800261
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800262 ALOGV("%s: exit", __func__);
263 return 0;
264}
265
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800266int disable_audio_route(struct audio_device *adev,
267 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800268{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700269 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800270 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800271
272 if (usecase == NULL)
273 return -EINVAL;
274
275 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700276 if (usecase->type == PCM_CAPTURE)
277 snd_device = usecase->in_snd_device;
278 else
279 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800280 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500281 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700282 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700283 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800284
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800285 ALOGV("%s: exit", __func__);
286 return 0;
287}
288
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800289int enable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700290 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800291{
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700292 int i, num_devices = 0;
293 snd_device_t new_snd_devices[2];
294
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800295 if (snd_device < SND_DEVICE_MIN ||
296 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800297 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800298 return -EINVAL;
299 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700300
301 adev->snd_dev_ref_cnt[snd_device]++;
302 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700303 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700304 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700305 return 0;
306 }
307
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700308 /* due to the possibility of calibration overwrite between listen
309 and audio, notify sound trigger hal before audio calibration is sent */
310 audio_extn_sound_trigger_update_device_status(snd_device,
311 ST_EVENT_SND_DEVICE_BUSY);
312
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700313 if (audio_extn_spkr_prot_is_enabled())
314 audio_extn_spkr_prot_calib_cancel(adev);
315
Eric Laurentb23d5282013-05-14 15:27:20 -0700316 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700317 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700318 audio_extn_sound_trigger_update_device_status(snd_device,
319 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800320 return -EINVAL;
321 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800322
zhaoyang yin4211fad2015-06-04 21:13:25 +0800323 audio_extn_dsm_feedback_enable(adev, snd_device, true);
324
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700325 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
326 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
327 audio_extn_spkr_prot_is_enabled()) {
328 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
329 adev->snd_dev_ref_cnt[snd_device]--;
330 return -EINVAL;
331 }
332 if (audio_extn_spkr_prot_start_processing(snd_device)) {
333 ALOGE("%s: spkr_start_processing failed", __func__);
334 return -EINVAL;
335 }
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700336 } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
337 for (i = 0; i < num_devices; i++) {
338 enable_snd_device(adev, new_snd_devices[i]);
339 }
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700340 } else {
341 const char * dev_path = platform_get_snd_device_name(snd_device);
342 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
343 audio_route_apply_and_update_path(adev->audio_route, dev_path);
344 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700345
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800346 return 0;
347}
348
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800349int disable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700350 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800351{
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700352 int i, num_devices = 0;
353 snd_device_t new_snd_devices[2];
354
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800355 if (snd_device < SND_DEVICE_MIN ||
356 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800357 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800358 return -EINVAL;
359 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700360 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
361 ALOGE("%s: device ref cnt is already 0", __func__);
362 return -EINVAL;
363 }
364 adev->snd_dev_ref_cnt[snd_device]--;
365 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700366 const char * dev_path = platform_get_snd_device_name(snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700367 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
zhaoyang yin4211fad2015-06-04 21:13:25 +0800368
369 audio_extn_dsm_feedback_enable(adev, snd_device, false);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700370 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
371 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
372 audio_extn_spkr_prot_is_enabled()) {
373 audio_extn_spkr_prot_stop_processing(snd_device);
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700374 } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
375 for (i = 0; i < num_devices; i++) {
376 disable_snd_device(adev, new_snd_devices[i]);
377 }
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700378 } else {
379 audio_route_reset_and_update_path(adev->audio_route, dev_path);
380 }
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700381 audio_extn_sound_trigger_update_device_status(snd_device,
382 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700383 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800384 return 0;
385}
386
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700387static void check_and_route_playback_usecases(struct audio_device *adev,
388 struct audio_usecase *uc_info,
389 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700390{
391 struct listnode *node;
392 struct audio_usecase *usecase;
393 bool switch_device[AUDIO_USECASE_MAX];
394 int i, num_uc_to_switch = 0;
395
396 /*
397 * This function is to make sure that all the usecases that are active on
398 * the hardware codec backend are always routed to any one device that is
399 * handled by the hardware codec.
400 * For example, if low-latency and deep-buffer usecases are currently active
401 * on speaker and out_set_parameters(headset) is received on low-latency
402 * output, then we have to make sure deep-buffer is also switched to headset,
403 * because of the limitation that both the devices cannot be enabled
404 * at the same time as they share the same backend.
405 */
406 /* Disable all the usecases on the shared backend other than the
407 specified usecase */
408 for (i = 0; i < AUDIO_USECASE_MAX; i++)
409 switch_device[i] = false;
410
411 list_for_each(node, &adev->usecase_list) {
412 usecase = node_to_item(node, struct audio_usecase, list);
413 if (usecase->type != PCM_CAPTURE &&
414 usecase != uc_info &&
415 usecase->out_snd_device != snd_device &&
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700416 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND &&
417 platform_check_backends_match(snd_device, usecase->out_snd_device)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700418 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
419 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700420 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700421 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700422 switch_device[usecase->id] = true;
423 num_uc_to_switch++;
424 }
425 }
426
427 if (num_uc_to_switch) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700428 list_for_each(node, &adev->usecase_list) {
429 usecase = node_to_item(node, struct audio_usecase, list);
430 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700431 disable_snd_device(adev, usecase->out_snd_device);
sangwon.jeon866d5ff2013-10-17 21:42:50 +0900432 }
433 }
434
435 list_for_each(node, &adev->usecase_list) {
436 usecase = node_to_item(node, struct audio_usecase, list);
437 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700438 enable_snd_device(adev, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700439 }
440 }
441
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700442 /* Re-route all the usecases on the shared backend other than the
443 specified usecase to new snd devices */
444 list_for_each(node, &adev->usecase_list) {
445 usecase = node_to_item(node, struct audio_usecase, list);
446 /* Update the out_snd_device only before enabling the audio route */
447 if (switch_device[usecase->id] ) {
448 usecase->out_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700449 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700450 }
451 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700452 }
453}
454
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700455static void check_and_route_capture_usecases(struct audio_device *adev,
456 struct audio_usecase *uc_info,
457 snd_device_t snd_device)
458{
459 struct listnode *node;
460 struct audio_usecase *usecase;
461 bool switch_device[AUDIO_USECASE_MAX];
462 int i, num_uc_to_switch = 0;
463
464 /*
465 * This function is to make sure that all the active capture usecases
466 * are always routed to the same input sound device.
467 * For example, if audio-record and voice-call usecases are currently
468 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
469 * is received for voice call then we have to make sure that audio-record
470 * usecase is also switched to earpiece i.e. voice-dmic-ef,
471 * because of the limitation that two devices cannot be enabled
472 * at the same time if they share the same backend.
473 */
474 for (i = 0; i < AUDIO_USECASE_MAX; i++)
475 switch_device[i] = false;
476
477 list_for_each(node, &adev->usecase_list) {
478 usecase = node_to_item(node, struct audio_usecase, list);
479 if (usecase->type != PCM_PLAYBACK &&
480 usecase != uc_info &&
Anish Kumarff864712015-06-03 13:35:11 -0700481 usecase->in_snd_device != snd_device &&
482 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700483 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
484 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700485 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700486 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700487 switch_device[usecase->id] = true;
488 num_uc_to_switch++;
489 }
490 }
491
492 if (num_uc_to_switch) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700493 list_for_each(node, &adev->usecase_list) {
494 usecase = node_to_item(node, struct audio_usecase, list);
495 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700496 disable_snd_device(adev, usecase->in_snd_device);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700497 }
498 }
499
500 list_for_each(node, &adev->usecase_list) {
501 usecase = node_to_item(node, struct audio_usecase, list);
502 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700503 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700504 }
505 }
506
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700507 /* Re-route all the usecases on the shared backend other than the
508 specified usecase to new snd devices */
509 list_for_each(node, &adev->usecase_list) {
510 usecase = node_to_item(node, struct audio_usecase, list);
511 /* Update the in_snd_device only before enabling the audio route */
512 if (switch_device[usecase->id] ) {
513 usecase->in_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700514 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700515 }
516 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700517 }
518}
519
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800520/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700521static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800522{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700523 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700524 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800525
526 switch (channels) {
527 /*
528 * Do not handle stereo output in Multi-channel cases
529 * Stereo case is handled in normal playback path
530 */
531 case 6:
532 ALOGV("%s: HDMI supports 5.1", __func__);
533 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
534 break;
535 case 8:
536 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
537 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
538 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
539 break;
540 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700541 ALOGE("HDMI does not support multi channel playback");
542 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800543 break;
544 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700545 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800546}
547
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700548static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
549{
550 struct audio_usecase *usecase;
551 struct listnode *node;
552
553 list_for_each(node, &adev->usecase_list) {
554 usecase = node_to_item(node, struct audio_usecase, list);
555 if (usecase->type == VOICE_CALL) {
556 ALOGV("%s: usecase id %d", __func__, usecase->id);
557 return usecase->id;
558 }
559 }
560 return USECASE_INVALID;
561}
562
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800563struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
564 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700565{
566 struct audio_usecase *usecase;
567 struct listnode *node;
568
569 list_for_each(node, &adev->usecase_list) {
570 usecase = node_to_item(node, struct audio_usecase, list);
571 if (usecase->id == uc_id)
572 return usecase;
573 }
574 return NULL;
575}
576
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800577int select_devices(struct audio_device *adev,
578 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800579{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800580 snd_device_t out_snd_device = SND_DEVICE_NONE;
581 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700582 struct audio_usecase *usecase = NULL;
583 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800584 struct audio_usecase *hfp_usecase = NULL;
585 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800586 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700587 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800588
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700589 usecase = get_usecase_from_list(adev, uc_id);
590 if (usecase == NULL) {
591 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
592 return -EINVAL;
593 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800594
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800595 if ((usecase->type == VOICE_CALL) ||
596 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700597 out_snd_device = platform_get_output_snd_device(adev->platform,
598 usecase->stream.out->devices);
599 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700600 usecase->devices = usecase->stream.out->devices;
601 } else {
602 /*
603 * If the voice call is active, use the sound devices of voice call usecase
604 * so that it would not result any device switch. All the usecases will
605 * be switched to new device when select_devices() is called for voice call
606 * usecase. This is to avoid switching devices for voice call when
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700607 * check_and_route_playback_usecases() is called below.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700608 */
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700609 if (voice_is_in_call(adev)) {
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700610 vc_usecase = get_usecase_from_list(adev,
611 get_voice_usecase_id_from_list(adev));
612 if ((vc_usecase != NULL) &&
613 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
614 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700615 in_snd_device = vc_usecase->in_snd_device;
616 out_snd_device = vc_usecase->out_snd_device;
617 }
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800618 } else if (audio_extn_hfp_is_active(adev)) {
619 hfp_ucid = audio_extn_hfp_get_usecase();
620 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
621 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
622 in_snd_device = hfp_usecase->in_snd_device;
623 out_snd_device = hfp_usecase->out_snd_device;
624 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700625 }
626 if (usecase->type == PCM_PLAYBACK) {
627 usecase->devices = usecase->stream.out->devices;
628 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700629 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700630 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700631 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700632 if (usecase->stream.out == adev->primary_output &&
633 adev->active_input &&
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800634 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
635 out_snd_device != usecase->out_snd_device) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700636 select_devices(adev, adev->active_input->usecase);
637 }
638 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700639 } else if (usecase->type == PCM_CAPTURE) {
640 usecase->devices = usecase->stream.in->device;
641 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700642 if (in_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700643 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700644 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
645 adev->primary_output && !adev->primary_output->standby) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700646 out_device = adev->primary_output->devices;
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800647 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700648 } else if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
649 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700650 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700651 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700652 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700653 }
654 }
655
656 if (out_snd_device == usecase->out_snd_device &&
657 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800658 return 0;
659 }
660
sangwoobc677242013-08-08 16:53:43 +0900661 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700662 out_snd_device, platform_get_snd_device_name(out_snd_device),
663 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800664
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800665 /*
666 * Limitation: While in call, to do a device switch we need to disable
667 * and enable both RX and TX devices though one of them is same as current
668 * device.
669 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700670 if ((usecase->type == VOICE_CALL) &&
671 (usecase->in_snd_device != SND_DEVICE_NONE) &&
672 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700673 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800674 }
675
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700676 /* Disable current sound devices */
677 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700678 disable_audio_route(adev, usecase);
679 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800680 }
681
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700682 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700683 disable_audio_route(adev, usecase);
684 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800685 }
686
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700687 /* Applicable only on the targets that has external modem.
688 * New device information should be sent to modem before enabling
689 * the devices to reduce in-call device switch time.
690 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700691 if ((usecase->type == VOICE_CALL) &&
692 (usecase->in_snd_device != SND_DEVICE_NONE) &&
693 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700694 status = platform_switch_voice_call_enable_device_config(adev->platform,
695 out_snd_device,
696 in_snd_device);
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700697 }
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700698
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700699 /* Enable new sound devices */
700 if (out_snd_device != SND_DEVICE_NONE) {
701 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700702 check_and_route_playback_usecases(adev, usecase, out_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700703 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800704 }
705
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700706 if (in_snd_device != SND_DEVICE_NONE) {
707 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700708 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700709 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700710
Eric Laurentb23d5282013-05-14 15:27:20 -0700711 if (usecase->type == VOICE_CALL)
712 status = platform_switch_voice_call_device_post(adev->platform,
713 out_snd_device,
714 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800715
sangwoo170731f2013-06-08 15:36:36 +0900716 usecase->in_snd_device = in_snd_device;
717 usecase->out_snd_device = out_snd_device;
718
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700719 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900720
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700721 /* Applicable only on the targets that has external modem.
722 * Enable device command should be sent to modem only after
723 * enabling voice call mixer controls
724 */
725 if (usecase->type == VOICE_CALL)
726 status = platform_switch_voice_call_usecase_route_post(adev->platform,
727 out_snd_device,
728 in_snd_device);
729
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800730 return status;
731}
732
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800733static int stop_input_stream(struct stream_in *in)
734{
735 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800736 struct audio_usecase *uc_info;
737 struct audio_device *adev = in->dev;
738
Eric Laurentc8400632013-02-14 19:04:54 -0800739 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800740
Eric Laurent994a6932013-07-17 11:51:42 -0700741 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700742 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800743 uc_info = get_usecase_from_list(adev, in->usecase);
744 if (uc_info == NULL) {
745 ALOGE("%s: Could not find the usecase (%d) in the list",
746 __func__, in->usecase);
747 return -EINVAL;
748 }
749
Eric Laurent150dbfe2013-02-27 14:31:02 -0800750 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700751 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700752
753 /* 2. Disable the tx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700754 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800755
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800756 list_remove(&uc_info->list);
757 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800758
Eric Laurent994a6932013-07-17 11:51:42 -0700759 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800760 return ret;
761}
762
763int start_input_stream(struct stream_in *in)
764{
765 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800766 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800767 struct audio_usecase *uc_info;
768 struct audio_device *adev = in->dev;
769
Eric Laurent994a6932013-07-17 11:51:42 -0700770 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700771 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800772 if (in->pcm_device_id < 0) {
773 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
774 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800775 ret = -EINVAL;
776 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800777 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700778
779 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800780 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
781 uc_info->id = in->usecase;
782 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800783 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700784 uc_info->devices = in->device;
785 uc_info->in_snd_device = SND_DEVICE_NONE;
786 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800787
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800788 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700789 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800790
Eric Laurentc8400632013-02-14 19:04:54 -0800791 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700792 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700793
794 unsigned int flags = PCM_IN;
795 unsigned int pcm_open_retry_count = 0;
796
797 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
798 flags |= PCM_MMAP | PCM_NOIRQ;
799 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800800 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700801
802 while (1) {
803 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
804 flags, &in->config);
805 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
806 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
807 if (in->pcm != NULL) {
808 pcm_close(in->pcm);
809 in->pcm = NULL;
810 }
811 if (pcm_open_retry_count-- == 0) {
812 ret = -EIO;
813 goto error_open;
814 }
815 usleep(PROXY_OPEN_WAIT_TIME * 1000);
816 continue;
817 }
818 break;
819 }
820
Eric Laurent994a6932013-07-17 11:51:42 -0700821 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800822 return ret;
823
824error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800825 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800826
827error_config:
828 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700829 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800830
831 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800832}
833
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700834/* must be called with out->lock locked */
835static int send_offload_cmd_l(struct stream_out* out, int command)
836{
837 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
838
839 ALOGVV("%s %d", __func__, command);
840
841 cmd->cmd = command;
842 list_add_tail(&out->offload_cmd_list, &cmd->node);
843 pthread_cond_signal(&out->offload_cond);
844 return 0;
845}
846
847/* must be called iwth out->lock locked */
848static void stop_compressed_output_l(struct stream_out *out)
849{
850 out->offload_state = OFFLOAD_STATE_IDLE;
851 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700852 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700853 if (out->compr != NULL) {
854 compress_stop(out->compr);
855 while (out->offload_thread_blocked) {
856 pthread_cond_wait(&out->cond, &out->lock);
857 }
858 }
859}
860
861static void *offload_thread_loop(void *context)
862{
863 struct stream_out *out = (struct stream_out *) context;
864 struct listnode *item;
865
866 out->offload_state = OFFLOAD_STATE_IDLE;
867 out->playback_started = 0;
868
869 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
870 set_sched_policy(0, SP_FOREGROUND);
871 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
872
873 ALOGV("%s", __func__);
Eric Laurent6010f702015-06-25 23:39:33 +0000874 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700875 for (;;) {
876 struct offload_cmd *cmd = NULL;
877 stream_callback_event_t event;
878 bool send_callback = false;
879
880 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
881 __func__, list_empty(&out->offload_cmd_list),
882 out->offload_state);
883 if (list_empty(&out->offload_cmd_list)) {
884 ALOGV("%s SLEEPING", __func__);
885 pthread_cond_wait(&out->offload_cond, &out->lock);
886 ALOGV("%s RUNNING", __func__);
887 continue;
888 }
889
890 item = list_head(&out->offload_cmd_list);
891 cmd = node_to_item(item, struct offload_cmd, node);
892 list_remove(item);
893
894 ALOGVV("%s STATE %d CMD %d out->compr %p",
895 __func__, out->offload_state, cmd->cmd, out->compr);
896
897 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
898 free(cmd);
899 break;
900 }
901
902 if (out->compr == NULL) {
903 ALOGE("%s: Compress handle is NULL", __func__);
904 pthread_cond_signal(&out->cond);
905 continue;
906 }
907 out->offload_thread_blocked = true;
908 pthread_mutex_unlock(&out->lock);
909 send_callback = false;
910 switch(cmd->cmd) {
911 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
912 compress_wait(out->compr, -1);
913 send_callback = true;
914 event = STREAM_CBK_EVENT_WRITE_READY;
915 break;
916 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700917 compress_next_track(out->compr);
918 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700919 send_callback = true;
920 event = STREAM_CBK_EVENT_DRAIN_READY;
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800921 /* Resend the metadata for next iteration */
922 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700923 break;
924 case OFFLOAD_CMD_DRAIN:
925 compress_drain(out->compr);
926 send_callback = true;
927 event = STREAM_CBK_EVENT_DRAIN_READY;
928 break;
929 default:
930 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
931 break;
932 }
Eric Laurent6010f702015-06-25 23:39:33 +0000933 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700934 out->offload_thread_blocked = false;
935 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700936 if (send_callback) {
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800937 ALOGVV("%s: sending offload_callback event %d", __func__, event);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700938 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700939 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700940 free(cmd);
941 }
942
943 pthread_cond_signal(&out->cond);
944 while (!list_empty(&out->offload_cmd_list)) {
945 item = list_head(&out->offload_cmd_list);
946 list_remove(item);
947 free(node_to_item(item, struct offload_cmd, node));
948 }
949 pthread_mutex_unlock(&out->lock);
950
951 return NULL;
952}
953
954static int create_offload_callback_thread(struct stream_out *out)
955{
956 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
957 list_init(&out->offload_cmd_list);
958 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
959 offload_thread_loop, out);
960 return 0;
961}
962
963static int destroy_offload_callback_thread(struct stream_out *out)
964{
Eric Laurent6010f702015-06-25 23:39:33 +0000965 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700966 stop_compressed_output_l(out);
967 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
968
969 pthread_mutex_unlock(&out->lock);
970 pthread_join(out->offload_thread, (void **) NULL);
971 pthread_cond_destroy(&out->offload_cond);
972
973 return 0;
974}
975
Eric Laurent07eeafd2013-10-06 12:52:49 -0700976static bool allow_hdmi_channel_config(struct audio_device *adev)
977{
978 struct listnode *node;
979 struct audio_usecase *usecase;
980 bool ret = true;
981
982 list_for_each(node, &adev->usecase_list) {
983 usecase = node_to_item(node, struct audio_usecase, list);
984 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
985 /*
986 * If voice call is already existing, do not proceed further to avoid
987 * disabling/enabling both RX and TX devices, CSD calls, etc.
988 * Once the voice call done, the HDMI channels can be configured to
989 * max channels of remaining use cases.
990 */
991 if (usecase->id == USECASE_VOICE_CALL) {
992 ALOGD("%s: voice call is active, no change in HDMI channels",
993 __func__);
994 ret = false;
995 break;
996 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
997 ALOGD("%s: multi channel playback is active, "
998 "no change in HDMI channels", __func__);
999 ret = false;
1000 break;
1001 }
1002 }
1003 }
1004 return ret;
1005}
1006
1007static int check_and_set_hdmi_channels(struct audio_device *adev,
1008 unsigned int channels)
1009{
1010 struct listnode *node;
1011 struct audio_usecase *usecase;
1012
1013 /* Check if change in HDMI channel config is allowed */
1014 if (!allow_hdmi_channel_config(adev))
1015 return 0;
1016
1017 if (channels == adev->cur_hdmi_channels) {
1018 ALOGD("%s: Requested channels are same as current", __func__);
1019 return 0;
1020 }
1021
1022 platform_set_hdmi_channels(adev->platform, channels);
1023 adev->cur_hdmi_channels = channels;
1024
1025 /*
1026 * Deroute all the playback streams routed to HDMI so that
1027 * the back end is deactivated. Note that backend will not
1028 * be deactivated if any one stream is connected to it.
1029 */
1030 list_for_each(node, &adev->usecase_list) {
1031 usecase = node_to_item(node, struct audio_usecase, list);
1032 if (usecase->type == PCM_PLAYBACK &&
1033 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001034 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001035 }
1036 }
1037
1038 /*
1039 * Enable all the streams disabled above. Now the HDMI backend
1040 * will be activated with new channel configuration
1041 */
1042 list_for_each(node, &adev->usecase_list) {
1043 usecase = node_to_item(node, struct audio_usecase, list);
1044 if (usecase->type == PCM_PLAYBACK &&
1045 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001046 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001047 }
1048 }
1049
1050 return 0;
1051}
1052
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001053static int stop_output_stream(struct stream_out *out)
1054{
1055 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001056 struct audio_usecase *uc_info;
1057 struct audio_device *adev = out->dev;
1058
Eric Laurent994a6932013-07-17 11:51:42 -07001059 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001060 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001061 uc_info = get_usecase_from_list(adev, out->usecase);
1062 if (uc_info == NULL) {
1063 ALOGE("%s: Could not find the usecase (%d) in the list",
1064 __func__, out->usecase);
1065 return -EINVAL;
1066 }
1067
Haynes Mathew George41f86652014-06-17 14:22:15 -07001068 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1069 if (adev->visualizer_stop_output != NULL)
1070 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1071 if (adev->offload_effects_stop_output != NULL)
1072 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1073 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001074
Eric Laurent150dbfe2013-02-27 14:31:02 -08001075 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001076 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001077
1078 /* 2. Disable the rx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001079 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001080
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001081 list_remove(&uc_info->list);
1082 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001083
Eric Laurent0499d4f2014-08-25 22:39:29 -05001084 audio_extn_extspk_update(adev->extspk);
1085
Eric Laurent07eeafd2013-10-06 12:52:49 -07001086 /* Must be called after removing the usecase from list */
1087 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1088 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1089
Eric Laurent994a6932013-07-17 11:51:42 -07001090 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001091 return ret;
1092}
1093
1094int start_output_stream(struct stream_out *out)
1095{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001096 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001097 struct audio_usecase *uc_info;
1098 struct audio_device *adev = out->dev;
1099
Eric Laurent994a6932013-07-17 11:51:42 -07001100 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001101 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001102 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001103 if (out->pcm_device_id < 0) {
1104 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1105 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001106 ret = -EINVAL;
1107 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001108 }
1109
1110 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1111 uc_info->id = out->usecase;
1112 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001113 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001114 uc_info->devices = out->devices;
1115 uc_info->in_snd_device = SND_DEVICE_NONE;
1116 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001117
Eric Laurent07eeafd2013-10-06 12:52:49 -07001118 /* This must be called before adding this usecase to the list */
1119 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1120 check_and_set_hdmi_channels(adev, out->config.channels);
1121
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001122 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001123
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001124 select_devices(adev, out->usecase);
1125
Eric Laurent0499d4f2014-08-25 22:39:29 -05001126 audio_extn_extspk_update(adev->extspk);
1127
Andy Hung31aca912014-03-20 17:14:59 -07001128 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001129 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001130 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001131 unsigned int flags = PCM_OUT;
1132 unsigned int pcm_open_retry_count = 0;
1133 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1134 flags |= PCM_MMAP | PCM_NOIRQ;
1135 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1136 } else
1137 flags |= PCM_MONOTONIC;
1138
1139 while (1) {
1140 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1141 flags, &out->config);
1142 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
1143 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1144 if (out->pcm != NULL) {
1145 pcm_close(out->pcm);
1146 out->pcm = NULL;
1147 }
1148 if (pcm_open_retry_count-- == 0) {
1149 ret = -EIO;
1150 goto error_open;
1151 }
1152 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1153 continue;
1154 }
1155 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001156 }
1157 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001158 out->pcm = NULL;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001159 out->compr = compress_open(adev->snd_card, out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001160 COMPRESS_IN, &out->compr_config);
1161 if (out->compr && !is_compress_ready(out->compr)) {
1162 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1163 compress_close(out->compr);
1164 out->compr = NULL;
1165 ret = -EIO;
1166 goto error_open;
1167 }
1168 if (out->offload_callback)
1169 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001170
1171 if (adev->visualizer_start_output != NULL)
Haynes Mathew George41f86652014-06-17 14:22:15 -07001172 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1173 if (adev->offload_effects_start_output != NULL)
1174 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001175 }
Eric Laurent994a6932013-07-17 11:51:42 -07001176 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001177 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001178error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001179 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001180error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001181 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001182}
1183
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001184static int check_input_parameters(uint32_t sample_rate,
1185 audio_format_t format,
1186 int channel_count)
1187{
1188 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1189
1190 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1191
1192 switch (sample_rate) {
1193 case 8000:
1194 case 11025:
1195 case 12000:
1196 case 16000:
1197 case 22050:
1198 case 24000:
1199 case 32000:
1200 case 44100:
1201 case 48000:
1202 break;
1203 default:
1204 return -EINVAL;
1205 }
1206
1207 return 0;
1208}
1209
1210static size_t get_input_buffer_size(uint32_t sample_rate,
1211 audio_format_t format,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001212 int channel_count,
1213 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001214{
1215 size_t size = 0;
1216
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001217 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1218 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001219
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001220 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001221 if (is_low_latency)
Glenn Kasten4f993392014-05-14 07:30:48 -07001222 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001223 /* ToDo: should use frame_size computed based on the format and
1224 channel_count here. */
1225 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001226
Glenn Kasten4f993392014-05-14 07:30:48 -07001227 /* make sure the size is multiple of 32 bytes
1228 * At 48 kHz mono 16-bit PCM:
1229 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1230 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1231 */
1232 size += 0x1f;
1233 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001234
1235 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001236}
1237
1238static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1239{
1240 struct stream_out *out = (struct stream_out *)stream;
1241
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001242 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001243}
1244
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001245static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001246{
1247 return -ENOSYS;
1248}
1249
1250static size_t out_get_buffer_size(const struct audio_stream *stream)
1251{
1252 struct stream_out *out = (struct stream_out *)stream;
1253
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001254 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1255 return out->compr_config.fragment_size;
1256 }
Eric Laurentfdf296a2014-07-03 16:41:51 -07001257 return out->config.period_size *
1258 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001259}
1260
1261static uint32_t out_get_channels(const struct audio_stream *stream)
1262{
1263 struct stream_out *out = (struct stream_out *)stream;
1264
1265 return out->channel_mask;
1266}
1267
1268static audio_format_t out_get_format(const struct audio_stream *stream)
1269{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001270 struct stream_out *out = (struct stream_out *)stream;
1271
1272 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273}
1274
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001275static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001276{
1277 return -ENOSYS;
1278}
1279
1280static int out_standby(struct audio_stream *stream)
1281{
1282 struct stream_out *out = (struct stream_out *)stream;
1283 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001284
Eric Laurent994a6932013-07-17 11:51:42 -07001285 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001286 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001287
Eric Laurent6010f702015-06-25 23:39:33 +00001288 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001289 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001290 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001291 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001292 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1293 if (out->pcm) {
1294 pcm_close(out->pcm);
1295 out->pcm = NULL;
1296 }
1297 } else {
1298 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001299 out->gapless_mdata.encoder_delay = 0;
1300 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001301 if (out->compr != NULL) {
1302 compress_close(out->compr);
1303 out->compr = NULL;
1304 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001305 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001306 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001307 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001308 }
1309 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001310 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001311 return 0;
1312}
1313
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001314static int out_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001315{
1316 return 0;
1317}
1318
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001319static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1320{
1321 int ret = 0;
1322 char value[32];
1323 struct compr_gapless_mdata tmp_mdata;
1324
1325 if (!out || !parms) {
1326 return -EINVAL;
1327 }
1328
1329 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1330 if (ret >= 0) {
1331 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1332 } else {
1333 return -EINVAL;
1334 }
1335
1336 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1337 if (ret >= 0) {
1338 tmp_mdata.encoder_padding = atoi(value);
1339 } else {
1340 return -EINVAL;
1341 }
1342
1343 out->gapless_mdata = tmp_mdata;
1344 out->send_new_metadata = 1;
1345 ALOGV("%s new encoder delay %u and padding %u", __func__,
1346 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1347
1348 return 0;
1349}
1350
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001351static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1352{
1353 return out == adev->primary_output || out == adev->voice_tx_output;
1354}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001355
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001356static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1357{
1358 struct stream_out *out = (struct stream_out *)stream;
1359 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001360 struct audio_usecase *usecase;
1361 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001362 struct str_parms *parms;
1363 char value[32];
1364 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001365 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001366 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001367
sangwoobc677242013-08-08 16:53:43 +09001368 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001369 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001370 parms = str_parms_create_str(kvpairs);
1371 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1372 if (ret >= 0) {
1373 val = atoi(value);
Eric Laurent6010f702015-06-25 23:39:33 +00001374 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001375 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001376
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001377 /*
1378 * When HDMI cable is unplugged the music playback is paused and
1379 * the policy manager sends routing=0. But the audioflinger
1380 * continues to write data until standby time (3sec).
1381 * As the HDMI core is turned off, the write gets blocked.
1382 * Avoid this by routing audio to speaker until standby.
1383 */
1384 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1385 val == AUDIO_DEVICE_NONE) {
1386 val = AUDIO_DEVICE_OUT_SPEAKER;
1387 }
1388
1389 /*
1390 * select_devices() call below switches all the usecases on the same
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -07001391 * backend to the new device. Refer to check_and_route_playback_usecases() in
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001392 * the select_devices(). But how do we undo this?
1393 *
1394 * For example, music playback is active on headset (deep-buffer usecase)
1395 * and if we go to ringtones and select a ringtone, low-latency usecase
1396 * will be started on headset+speaker. As we can't enable headset+speaker
1397 * and headset devices at the same time, select_devices() switches the music
1398 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1399 * So when the ringtone playback is completed, how do we undo the same?
1400 *
1401 * We are relying on the out_set_parameters() call on deep-buffer output,
1402 * once the ringtone playback is ended.
1403 * NOTE: We should not check if the current devices are same as new devices.
1404 * Because select_devices() must be called to switch back the music
1405 * playback to headset.
1406 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001407 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001408 out->devices = val;
1409
1410 if (!out->standby)
1411 select_devices(adev, out->usecase);
1412
Eric Laurenta7657192014-10-09 21:09:33 -07001413 if (output_drives_call(adev, out)) {
1414 if (!voice_is_in_call(adev)) {
1415 if (adev->mode == AUDIO_MODE_IN_CALL) {
1416 adev->current_call_output = out;
1417 ret = voice_start_call(adev);
1418 }
1419 } else {
1420 adev->current_call_output = out;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001421 voice_update_devices_for_all_voice_usecases(adev);
Eric Laurenta7657192014-10-09 21:09:33 -07001422 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001423 }
1424 }
1425
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001426 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001427 pthread_mutex_unlock(&out->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05001428
1429 /*handles device and call state changes*/
1430 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001431 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001432
1433 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1434 parse_compress_metadata(out, parms);
1435 }
1436
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001437 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001438 ALOGV("%s: exit: code(%d)", __func__, status);
1439 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001440}
1441
1442static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1443{
1444 struct stream_out *out = (struct stream_out *)stream;
1445 struct str_parms *query = str_parms_create_str(keys);
1446 char *str;
1447 char value[256];
1448 struct str_parms *reply = str_parms_create();
1449 size_t i, j;
1450 int ret;
1451 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001452 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001453 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1454 if (ret >= 0) {
1455 value[0] = '\0';
1456 i = 0;
1457 while (out->supported_channel_masks[i] != 0) {
1458 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1459 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1460 if (!first) {
1461 strcat(value, "|");
1462 }
1463 strcat(value, out_channels_name_to_enum_table[j].name);
1464 first = false;
1465 break;
1466 }
1467 }
1468 i++;
1469 }
1470 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1471 str = str_parms_to_str(reply);
1472 } else {
1473 str = strdup(keys);
1474 }
1475 str_parms_destroy(query);
1476 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001477 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001478 return str;
1479}
1480
1481static uint32_t out_get_latency(const struct audio_stream_out *stream)
1482{
1483 struct stream_out *out = (struct stream_out *)stream;
1484
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001485 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1486 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1487
1488 return (out->config.period_count * out->config.period_size * 1000) /
1489 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001490}
1491
1492static int out_set_volume(struct audio_stream_out *stream, float left,
1493 float right)
1494{
Eric Laurenta9024de2013-04-04 09:19:12 -07001495 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001496 int volume[2];
1497
Eric Laurenta9024de2013-04-04 09:19:12 -07001498 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1499 /* only take left channel into account: the API is for stereo anyway */
1500 out->muted = (left == 0.0f);
1501 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001502 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1503 const char *mixer_ctl_name = "Compress Playback Volume";
1504 struct audio_device *adev = out->dev;
1505 struct mixer_ctl *ctl;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001506 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1507 if (!ctl) {
Haynes Mathew George20bcfa82014-06-25 13:37:03 -07001508 /* try with the control based on device id */
1509 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1510 PCM_PLAYBACK);
1511 char ctl_name[128] = {0};
1512 snprintf(ctl_name, sizeof(ctl_name),
1513 "Compress Playback %d Volume", pcm_device_id);
1514 ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name);
1515 if (!ctl) {
1516 ALOGE("%s: Could not get volume ctl mixer cmd", __func__);
1517 return -EINVAL;
1518 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001519 }
1520 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1521 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1522 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1523 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001524 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001525
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001526 return -ENOSYS;
1527}
1528
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001529#ifdef NO_AUDIO_OUT
1530static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
1531 const void *buffer, size_t bytes)
1532{
1533 struct stream_out *out = (struct stream_out *)stream;
1534
1535 /* No Output device supported other than BT for playback.
1536 * Sleep for the amount of buffer duration
1537 */
Eric Laurent6010f702015-06-25 23:39:33 +00001538 pthread_mutex_lock(&out->lock);
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001539 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1540 out_get_sample_rate(&out->stream.common));
1541 pthread_mutex_unlock(&out->lock);
1542 return bytes;
1543}
1544#endif
1545
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001546static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1547 size_t bytes)
1548{
1549 struct stream_out *out = (struct stream_out *)stream;
1550 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001551 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001552
Eric Laurent6010f702015-06-25 23:39:33 +00001553 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001554 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001555 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001556 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001557 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001558 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001559 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001560 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001561 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001562 goto exit;
1563 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001564 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001565
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001566 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001567 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1568 if (out->send_new_metadata) {
1569 ALOGVV("send new gapless metadata");
1570 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1571 out->send_new_metadata = 0;
1572 }
1573
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001574 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001575 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001576 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001577 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1578 }
1579 if (!out->playback_started) {
1580 compress_start(out->compr);
1581 out->playback_started = 1;
1582 out->offload_state = OFFLOAD_STATE_PLAYING;
1583 }
1584 pthread_mutex_unlock(&out->lock);
1585 return ret;
1586 } else {
1587 if (out->pcm) {
1588 if (out->muted)
1589 memset((void *)buffer, 0, bytes);
1590 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001591 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1592 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
1593 }
1594 else
1595 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001596 if (ret == 0)
1597 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001598 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001599 }
1600
1601exit:
1602 pthread_mutex_unlock(&out->lock);
1603
1604 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001605 if (out->pcm)
vivek mehta1a9b7c02015-06-25 11:49:38 -07001606 ALOGE("%s: error %zu - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001607 out_standby(&out->stream.common);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001608 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001609 out_get_sample_rate(&out->stream.common));
1610 }
1611 return bytes;
1612}
1613
1614static int out_get_render_position(const struct audio_stream_out *stream,
1615 uint32_t *dsp_frames)
1616{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001617 struct stream_out *out = (struct stream_out *)stream;
1618 *dsp_frames = 0;
1619 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
Eric Laurent6010f702015-06-25 23:39:33 +00001620 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001621 if (out->compr != NULL) {
1622 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1623 &out->sample_rate);
1624 ALOGVV("%s rendered frames %d sample_rate %d",
1625 __func__, *dsp_frames, out->sample_rate);
1626 }
1627 pthread_mutex_unlock(&out->lock);
1628 return 0;
1629 } else
1630 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001631}
1632
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001633static int out_add_audio_effect(const struct audio_stream *stream __unused,
1634 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001635{
1636 return 0;
1637}
1638
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001639static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1640 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001641{
1642 return 0;
1643}
1644
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001645static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1646 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001647{
1648 return -EINVAL;
1649}
1650
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001651static int out_get_presentation_position(const struct audio_stream_out *stream,
1652 uint64_t *frames, struct timespec *timestamp)
1653{
1654 struct stream_out *out = (struct stream_out *)stream;
1655 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001656 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001657
Eric Laurent6010f702015-06-25 23:39:33 +00001658 pthread_mutex_lock(&out->lock);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001659
Eric Laurent949a0892013-09-20 09:20:13 -07001660 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1661 if (out->compr != NULL) {
1662 compress_get_tstamp(out->compr, &dsp_frames,
1663 &out->sample_rate);
1664 ALOGVV("%s rendered frames %ld sample_rate %d",
1665 __func__, dsp_frames, out->sample_rate);
1666 *frames = dsp_frames;
1667 ret = 0;
1668 /* this is the best we can do */
1669 clock_gettime(CLOCK_MONOTONIC, timestamp);
1670 }
1671 } else {
1672 if (out->pcm) {
vivek mehta1a9b7c02015-06-25 11:49:38 -07001673 unsigned int avail;
Eric Laurent949a0892013-09-20 09:20:13 -07001674 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1675 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001676 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001677 // This adjustment accounts for buffering after app processor.
1678 // It is based on estimated DSP latency per use case, rather than exact.
1679 signed_frames -=
1680 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1681
Eric Laurent949a0892013-09-20 09:20:13 -07001682 // It would be unusual for this value to be negative, but check just in case ...
1683 if (signed_frames >= 0) {
1684 *frames = signed_frames;
1685 ret = 0;
1686 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001687 }
1688 }
1689 }
1690
1691 pthread_mutex_unlock(&out->lock);
1692
1693 return ret;
1694}
1695
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001696static int out_set_callback(struct audio_stream_out *stream,
1697 stream_callback_t callback, void *cookie)
1698{
1699 struct stream_out *out = (struct stream_out *)stream;
1700
1701 ALOGV("%s", __func__);
Eric Laurent6010f702015-06-25 23:39:33 +00001702 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001703 out->offload_callback = callback;
1704 out->offload_cookie = cookie;
1705 pthread_mutex_unlock(&out->lock);
1706 return 0;
1707}
1708
1709static int out_pause(struct audio_stream_out* stream)
1710{
1711 struct stream_out *out = (struct stream_out *)stream;
1712 int status = -ENOSYS;
1713 ALOGV("%s", __func__);
1714 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurent6010f702015-06-25 23:39:33 +00001715 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001716 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1717 status = compress_pause(out->compr);
1718 out->offload_state = OFFLOAD_STATE_PAUSED;
1719 }
1720 pthread_mutex_unlock(&out->lock);
1721 }
1722 return status;
1723}
1724
1725static int out_resume(struct audio_stream_out* stream)
1726{
1727 struct stream_out *out = (struct stream_out *)stream;
1728 int status = -ENOSYS;
1729 ALOGV("%s", __func__);
1730 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1731 status = 0;
Eric Laurent6010f702015-06-25 23:39:33 +00001732 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001733 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1734 status = compress_resume(out->compr);
1735 out->offload_state = OFFLOAD_STATE_PLAYING;
1736 }
1737 pthread_mutex_unlock(&out->lock);
1738 }
1739 return status;
1740}
1741
1742static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1743{
1744 struct stream_out *out = (struct stream_out *)stream;
1745 int status = -ENOSYS;
1746 ALOGV("%s", __func__);
1747 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurent6010f702015-06-25 23:39:33 +00001748 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001749 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1750 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1751 else
1752 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1753 pthread_mutex_unlock(&out->lock);
1754 }
1755 return status;
1756}
1757
1758static int out_flush(struct audio_stream_out* stream)
1759{
1760 struct stream_out *out = (struct stream_out *)stream;
1761 ALOGV("%s", __func__);
1762 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurent6010f702015-06-25 23:39:33 +00001763 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001764 stop_compressed_output_l(out);
1765 pthread_mutex_unlock(&out->lock);
1766 return 0;
1767 }
1768 return -ENOSYS;
1769}
1770
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001771/** audio_stream_in implementation **/
1772static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1773{
1774 struct stream_in *in = (struct stream_in *)stream;
1775
1776 return in->config.rate;
1777}
1778
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001779static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001780{
1781 return -ENOSYS;
1782}
1783
1784static size_t in_get_buffer_size(const struct audio_stream *stream)
1785{
1786 struct stream_in *in = (struct stream_in *)stream;
1787
Eric Laurentfdf296a2014-07-03 16:41:51 -07001788 return in->config.period_size *
1789 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790}
1791
1792static uint32_t in_get_channels(const struct audio_stream *stream)
1793{
1794 struct stream_in *in = (struct stream_in *)stream;
1795
1796 return in->channel_mask;
1797}
1798
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001799static audio_format_t in_get_format(const struct audio_stream *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001800{
1801 return AUDIO_FORMAT_PCM_16_BIT;
1802}
1803
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001804static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001805{
1806 return -ENOSYS;
1807}
1808
1809static int in_standby(struct audio_stream *stream)
1810{
1811 struct stream_in *in = (struct stream_in *)stream;
1812 struct audio_device *adev = in->dev;
1813 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001814 ALOGV("%s: enter", __func__);
Eric Laurent6010f702015-06-25 23:39:33 +00001815 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001816
1817 if (!in->standby && in->is_st_session) {
1818 ALOGD("%s: sound trigger pcm stop lab", __func__);
1819 audio_extn_sound_trigger_stop_lab(in);
1820 in->standby = true;
1821 }
1822
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001823 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001824 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001825 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001826 if (in->pcm) {
1827 pcm_close(in->pcm);
1828 in->pcm = NULL;
1829 }
Eric Laurentcefbbac2014-09-04 13:54:10 -05001830 adev->enable_voicerx = false;
1831 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE );
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001832 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001833 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001834 }
1835 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001836 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001837 return status;
1838}
1839
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001840static int in_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001841{
1842 return 0;
1843}
1844
1845static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1846{
1847 struct stream_in *in = (struct stream_in *)stream;
1848 struct audio_device *adev = in->dev;
1849 struct str_parms *parms;
1850 char *str;
1851 char value[32];
1852 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07001853 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001854
Eric Laurent994a6932013-07-17 11:51:42 -07001855 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001856 parms = str_parms_create_str(kvpairs);
1857
1858 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1859
Eric Laurent6010f702015-06-25 23:39:33 +00001860 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001861 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001862 if (ret >= 0) {
1863 val = atoi(value);
1864 /* no audio source uses val == 0 */
1865 if ((in->source != val) && (val != 0)) {
1866 in->source = val;
1867 }
1868 }
1869
1870 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07001871
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001872 if (ret >= 0) {
1873 val = atoi(value);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001874 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001875 in->device = val;
1876 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001877 if (!in->standby)
Eric Laurent03f09432014-03-25 18:09:11 -07001878 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001879 }
1880 }
1881
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001882 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001883 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001884
1885 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001886 ALOGV("%s: exit: status(%d)", __func__, status);
1887 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001888}
1889
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001890static char* in_get_parameters(const struct audio_stream *stream __unused,
1891 const char *keys __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001892{
1893 return strdup("");
1894}
1895
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001896static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001897{
1898 return 0;
1899}
1900
1901static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1902 size_t bytes)
1903{
1904 struct stream_in *in = (struct stream_in *)stream;
1905 struct audio_device *adev = in->dev;
1906 int i, ret = -1;
1907
Eric Laurent6010f702015-06-25 23:39:33 +00001908 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001909 if (in->is_st_session) {
1910 ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
1911 /* Read from sound trigger HAL */
1912 audio_extn_sound_trigger_read(in, buffer, bytes);
1913 pthread_mutex_unlock(&in->lock);
1914 return bytes;
1915 }
1916
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001917 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001918 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001919 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001920 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001921 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001922 goto exit;
1923 }
1924 in->standby = 0;
1925 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001926
1927 if (in->pcm) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001928 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1929 ret = pcm_mmap_read(in->pcm, buffer, bytes);
1930 } else
1931 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001932 }
1933
1934 /*
1935 * Instead of writing zeroes here, we could trust the hardware
1936 * to always provide zeroes when muted.
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001937 * 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 -08001938 */
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001939 if (ret == 0 && adev->mic_muted && in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001940 memset(buffer, 0, bytes);
1941
1942exit:
1943 pthread_mutex_unlock(&in->lock);
1944
1945 if (ret != 0) {
1946 in_standby(&in->stream.common);
1947 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001948 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001949 in_get_sample_rate(&in->stream.common));
1950 }
1951 return bytes;
1952}
1953
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001954static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001955{
1956 return 0;
1957}
1958
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001959static int add_remove_audio_effect(const struct audio_stream *stream,
1960 effect_handle_t effect,
1961 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001962{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001963 struct stream_in *in = (struct stream_in *)stream;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001964 struct audio_device *adev = in->dev;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001965 int status = 0;
1966 effect_descriptor_t desc;
1967
1968 status = (*effect)->get_descriptor(effect, &desc);
1969 if (status != 0)
1970 return status;
1971
Eric Laurent6010f702015-06-25 23:39:33 +00001972 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001973 pthread_mutex_lock(&in->dev->lock);
1974 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1975 in->enable_aec != enable &&
1976 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1977 in->enable_aec = enable;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001978 if (!enable)
1979 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
1980 adev->enable_voicerx = enable;
1981 struct audio_usecase *usecase;
1982 struct listnode *node;
1983 list_for_each(node, &adev->usecase_list) {
1984 usecase = node_to_item(node, struct audio_usecase, list);
1985 if (usecase->type == PCM_PLAYBACK) {
1986 select_devices(adev, usecase->id);
1987 break;
1988 }
1989 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001990 if (!in->standby)
1991 select_devices(in->dev, in->usecase);
1992 }
Ravi Kumar Alamanda3ad4e1b2014-06-03 00:08:15 -07001993 if (in->enable_ns != enable &&
1994 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1995 in->enable_ns = enable;
1996 if (!in->standby)
1997 select_devices(in->dev, in->usecase);
1998 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001999 pthread_mutex_unlock(&in->dev->lock);
2000 pthread_mutex_unlock(&in->lock);
2001
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002002 return 0;
2003}
2004
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002005static int in_add_audio_effect(const struct audio_stream *stream,
2006 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002007{
Eric Laurent994a6932013-07-17 11:51:42 -07002008 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002009 return add_remove_audio_effect(stream, effect, true);
2010}
2011
2012static int in_remove_audio_effect(const struct audio_stream *stream,
2013 effect_handle_t effect)
2014{
Eric Laurent994a6932013-07-17 11:51:42 -07002015 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002016 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002017}
2018
2019static int adev_open_output_stream(struct audio_hw_device *dev,
2020 audio_io_handle_t handle,
2021 audio_devices_t devices,
2022 audio_output_flags_t flags,
2023 struct audio_config *config,
Eric Laurent91975a62014-07-27 17:15:50 -07002024 struct audio_stream_out **stream_out,
2025 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002026{
2027 struct audio_device *adev = (struct audio_device *)dev;
2028 struct stream_out *out;
2029 int i, ret;
2030
Eric Laurent994a6932013-07-17 11:51:42 -07002031 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002032 __func__, config->sample_rate, config->channel_mask, devices, flags);
2033 *stream_out = NULL;
2034 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2035
2036 if (devices == AUDIO_DEVICE_NONE)
2037 devices = AUDIO_DEVICE_OUT_SPEAKER;
2038
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002039 out->flags = flags;
2040 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002041 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002042 out->format = config->format;
2043 out->sample_rate = config->sample_rate;
2044 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2045 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002046 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002047
2048 /* Init use case and pcm_config */
2049 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07002050 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002051 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002052 pthread_mutex_lock(&adev->lock);
2053 ret = read_hdmi_channel_masks(out);
2054 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002055 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002056 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002057
2058 if (config->sample_rate == 0)
2059 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2060 if (config->channel_mask == 0)
2061 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2062
2063 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002064 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002065 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2066 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002067 out->config.rate = config->sample_rate;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002068 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002069 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002070 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2071 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2072 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2073 ALOGE("%s: Unsupported Offload information", __func__);
2074 ret = -EINVAL;
2075 goto error_open;
2076 }
2077 if (!is_supported_format(config->offload_info.format)) {
2078 ALOGE("%s: Unsupported audio format", __func__);
2079 ret = -EINVAL;
2080 goto error_open;
2081 }
2082
2083 out->compr_config.codec = (struct snd_codec *)
2084 calloc(1, sizeof(struct snd_codec));
2085
2086 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2087 if (config->offload_info.channel_mask)
2088 out->channel_mask = config->offload_info.channel_mask;
2089 else if (config->channel_mask)
2090 out->channel_mask = config->channel_mask;
2091 out->format = config->offload_info.format;
2092 out->sample_rate = config->offload_info.sample_rate;
2093
2094 out->stream.set_callback = out_set_callback;
2095 out->stream.pause = out_pause;
2096 out->stream.resume = out_resume;
2097 out->stream.drain = out_drain;
2098 out->stream.flush = out_flush;
2099
2100 out->compr_config.codec->id =
2101 get_snd_codec_id(config->offload_info.format);
2102 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2103 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
Eric Laurent1ccf3972014-10-23 14:42:59 -07002104 out->compr_config.codec->sample_rate = config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002105 out->compr_config.codec->bit_rate =
2106 config->offload_info.bit_rate;
2107 out->compr_config.codec->ch_in =
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002108 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002109 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2110
2111 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2112 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002113
2114 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002115 create_offload_callback_thread(out);
2116 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2117 __func__, config->offload_info.version,
2118 config->offload_info.bit_rate);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002119 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2120 if (config->sample_rate == 0)
2121 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2122 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2123 config->sample_rate != 8000) {
2124 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2125 ret = -EINVAL;
2126 goto error_open;
2127 }
2128 out->sample_rate = config->sample_rate;
2129 out->config.rate = config->sample_rate;
2130 if (config->format == AUDIO_FORMAT_DEFAULT)
2131 config->format = AUDIO_FORMAT_PCM_16_BIT;
2132 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2133 config->format = AUDIO_FORMAT_PCM_16_BIT;
2134 ret = -EINVAL;
2135 goto error_open;
2136 }
2137 out->format = config->format;
2138 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2139 out->config = pcm_config_afe_proxy_playback;
2140 adev->voice_tx_output = out;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002141 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07002142 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2143 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2144 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamandaf78a4d92015-04-24 15:18:23 -07002145 } else if (out->flags & AUDIO_OUTPUT_FLAG_TTS) {
2146 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
2147 out->config = pcm_config_deep_buffer;
Andy Hung6fcba9c2014-03-18 11:53:32 -07002148 } else {
2149 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2150 out->config = pcm_config_low_latency;
2151 }
2152 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2153 if (k_enable_extended_precision
2154 && pcm_params_format_test(adev->use_case_table[out->usecase],
2155 pcm_format_from_audio_format(config->format))) {
2156 out->config.format = pcm_format_from_audio_format(config->format);
2157 /* out->format already set to config->format */
2158 } else {
2159 /* deny the externally proposed config format
2160 * and use the one specified in audio_hw layer configuration.
2161 * Note: out->format is returned by out->stream.common.get_format()
2162 * and is used to set config->format in the code several lines below.
2163 */
2164 out->format = audio_format_from_pcm_format(out->config.format);
2165 }
2166 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002167 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002168 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07002169 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
2170 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002171
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002172 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002173 if (adev->primary_output == NULL)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002174 adev->primary_output = out;
2175 else {
2176 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002177 ret = -EEXIST;
2178 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002179 }
2180 }
2181
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002182 /* Check if this usecase is already existing */
2183 pthread_mutex_lock(&adev->lock);
2184 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2185 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002186 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002187 ret = -EEXIST;
2188 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002189 }
2190 pthread_mutex_unlock(&adev->lock);
2191
2192 out->stream.common.get_sample_rate = out_get_sample_rate;
2193 out->stream.common.set_sample_rate = out_set_sample_rate;
2194 out->stream.common.get_buffer_size = out_get_buffer_size;
2195 out->stream.common.get_channels = out_get_channels;
2196 out->stream.common.get_format = out_get_format;
2197 out->stream.common.set_format = out_set_format;
2198 out->stream.common.standby = out_standby;
2199 out->stream.common.dump = out_dump;
2200 out->stream.common.set_parameters = out_set_parameters;
2201 out->stream.common.get_parameters = out_get_parameters;
2202 out->stream.common.add_audio_effect = out_add_audio_effect;
2203 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2204 out->stream.get_latency = out_get_latency;
2205 out->stream.set_volume = out_set_volume;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002206#ifdef NO_AUDIO_OUT
2207 out->stream.write = out_write_for_no_output;
2208#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002209 out->stream.write = out_write;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002210#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002211 out->stream.get_render_position = out_get_render_position;
2212 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002213 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002214
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002215 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002216 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002217 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002218
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002219 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2220 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2221
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002222 config->format = out->stream.common.get_format(&out->stream.common);
2223 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2224 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2225
2226 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002227 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002228 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002229
2230error_open:
2231 free(out);
2232 *stream_out = NULL;
2233 ALOGD("%s: exit: ret %d", __func__, ret);
2234 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002235}
2236
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002237static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002238 struct audio_stream_out *stream)
2239{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002240 struct stream_out *out = (struct stream_out *)stream;
2241 struct audio_device *adev = out->dev;
2242
Eric Laurent994a6932013-07-17 11:51:42 -07002243 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002244 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002245 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2246 destroy_offload_callback_thread(out);
2247
2248 if (out->compr_config.codec != NULL)
2249 free(out->compr_config.codec);
2250 }
Ravi Kumar Alamandaa4fc9022014-10-08 18:57:46 -07002251
2252 if (adev->voice_tx_output == out)
2253 adev->voice_tx_output = NULL;
2254
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002255 pthread_cond_destroy(&out->cond);
2256 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002257 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002258 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002259}
2260
2261static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2262{
2263 struct audio_device *adev = (struct audio_device *)dev;
2264 struct str_parms *parms;
2265 char *str;
2266 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002267 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002268 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002269 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002270
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002271 ALOGD("%s: enter: %s", __func__, kvpairs);
2272
2273 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002274
2275 parms = str_parms_create_str(kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002276 status = voice_set_parameters(adev, parms);
2277 if (status != 0) {
2278 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002279 }
2280
2281 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2282 if (ret >= 0) {
Ravi Kumar Alamandae258e682015-06-25 13:32:42 -07002283 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002284 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2285 adev->bluetooth_nrec = true;
2286 else
2287 adev->bluetooth_nrec = false;
2288 }
2289
2290 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2291 if (ret >= 0) {
2292 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2293 adev->screen_off = false;
2294 else
2295 adev->screen_off = true;
2296 }
2297
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002298 ret = str_parms_get_int(parms, "rotation", &val);
2299 if (ret >= 0) {
2300 bool reverse_speakers = false;
2301 switch(val) {
2302 // FIXME: note that the code below assumes that the speakers are in the correct placement
2303 // relative to the user when the device is rotated 90deg from its default rotation. This
2304 // assumption is device-specific, not platform-specific like this code.
2305 case 270:
2306 reverse_speakers = true;
2307 break;
2308 case 0:
2309 case 90:
2310 case 180:
2311 break;
2312 default:
2313 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002314 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002315 }
Eric Laurent03f09432014-03-25 18:09:11 -07002316 if (status == 0) {
Ravi Kumar Alamanda1f60cf82015-04-23 19:45:17 -07002317 platform_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002318 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002319 }
2320
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002321 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2322 if (ret >= 0) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002323 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002324 }
2325
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08002326 audio_extn_hfp_set_parameters(adev, parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002327done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002328 str_parms_destroy(parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002329 pthread_mutex_unlock(&adev->lock);
Eric Laurent03f09432014-03-25 18:09:11 -07002330 ALOGV("%s: exit with code(%d)", __func__, status);
2331 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002332}
2333
2334static char* adev_get_parameters(const struct audio_hw_device *dev,
2335 const char *keys)
2336{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002337 struct audio_device *adev = (struct audio_device *)dev;
2338 struct str_parms *reply = str_parms_create();
2339 struct str_parms *query = str_parms_create_str(keys);
2340 char *str;
2341
2342 pthread_mutex_lock(&adev->lock);
2343
2344 voice_get_parameters(adev, query, reply);
2345 str = str_parms_to_str(reply);
2346 str_parms_destroy(query);
2347 str_parms_destroy(reply);
2348
2349 pthread_mutex_unlock(&adev->lock);
2350 ALOGV("%s: exit: returns - %s", __func__, str);
2351 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002352}
2353
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002354static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002355{
2356 return 0;
2357}
2358
Haynes Mathew George5191a852013-09-11 14:19:36 -07002359static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2360{
2361 int ret;
2362 struct audio_device *adev = (struct audio_device *)dev;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002363
Eric Laurent4cc4ce12014-09-10 13:21:01 -05002364 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
2365
Haynes Mathew George5191a852013-09-11 14:19:36 -07002366 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002367 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002368 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002369
Haynes Mathew George5191a852013-09-11 14:19:36 -07002370 return ret;
2371}
2372
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002373static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002374{
2375 return -ENOSYS;
2376}
2377
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002378static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2379 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002380{
2381 return -ENOSYS;
2382}
2383
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002384static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002385{
2386 return -ENOSYS;
2387}
2388
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002389static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002390{
2391 return -ENOSYS;
2392}
2393
2394static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2395{
2396 struct audio_device *adev = (struct audio_device *)dev;
2397
2398 pthread_mutex_lock(&adev->lock);
2399 if (adev->mode != mode) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002400 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002401 adev->mode = mode;
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -07002402 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) &&
2403 voice_is_in_call(adev)) {
2404 voice_stop_call(adev);
2405 adev->current_call_output = NULL;
2406 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002407 }
2408 pthread_mutex_unlock(&adev->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002409
2410 audio_extn_extspk_set_mode(adev->extspk, mode);
2411
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002412 return 0;
2413}
2414
2415static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2416{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002417 int ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002418 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002419
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002420 ALOGD("%s: state %d\n", __func__, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002421 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002422 ret = voice_set_mic_mute(adev, state);
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07002423 adev->mic_muted = state;
Eric Laurenta609e8e2014-06-18 02:15:17 +00002424 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002425
2426 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002427}
2428
2429static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2430{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002431 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002432 return 0;
2433}
2434
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002435static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002436 const struct audio_config *config)
2437{
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002438 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002439
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002440 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
2441 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002442}
2443
2444static int adev_open_input_stream(struct audio_hw_device *dev,
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002445 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002446 audio_devices_t devices,
2447 struct audio_config *config,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002448 struct audio_stream_in **stream_in,
Eric Laurent91975a62014-07-27 17:15:50 -07002449 audio_input_flags_t flags,
2450 const char *address __unused,
Eric Laurentcefbbac2014-09-04 13:54:10 -05002451 audio_source_t source )
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002452{
2453 struct audio_device *adev = (struct audio_device *)dev;
2454 struct stream_in *in;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002455 int ret = 0, buffer_size, frame_size;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002456 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002457 bool is_low_latency = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002458
Eric Laurent994a6932013-07-17 11:51:42 -07002459 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002460 *stream_in = NULL;
2461 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2462 return -EINVAL;
2463
2464 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2465
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002466 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2467
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002468 in->stream.common.get_sample_rate = in_get_sample_rate;
2469 in->stream.common.set_sample_rate = in_set_sample_rate;
2470 in->stream.common.get_buffer_size = in_get_buffer_size;
2471 in->stream.common.get_channels = in_get_channels;
2472 in->stream.common.get_format = in_get_format;
2473 in->stream.common.set_format = in_set_format;
2474 in->stream.common.standby = in_standby;
2475 in->stream.common.dump = in_dump;
2476 in->stream.common.set_parameters = in_set_parameters;
2477 in->stream.common.get_parameters = in_get_parameters;
2478 in->stream.common.add_audio_effect = in_add_audio_effect;
2479 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2480 in->stream.set_gain = in_set_gain;
2481 in->stream.read = in_read;
2482 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2483
2484 in->device = devices;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002485 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002486 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002487 in->standby = 1;
2488 in->channel_mask = config->channel_mask;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002489 in->capture_handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002490
2491 /* Update config params with the requested sample rate and channels */
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002492 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
2493 if (config->sample_rate == 0)
2494 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2495 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2496 config->sample_rate != 8000) {
2497 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2498 ret = -EINVAL;
2499 goto err_open;
2500 }
2501 if (config->format == AUDIO_FORMAT_DEFAULT)
2502 config->format = AUDIO_FORMAT_PCM_16_BIT;
2503 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2504 config->format = AUDIO_FORMAT_PCM_16_BIT;
2505 ret = -EINVAL;
2506 goto err_open;
2507 }
2508
2509 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
2510 in->config = pcm_config_afe_proxy_record;
2511 } else {
2512 in->usecase = USECASE_AUDIO_RECORD;
2513 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
2514 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
2515 is_low_latency = true;
Glenn Kasten4f993392014-05-14 07:30:48 -07002516#if LOW_LATENCY_CAPTURE_USE_CASE
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002517 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Glenn Kasten4f993392014-05-14 07:30:48 -07002518#endif
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002519 }
2520 in->config = pcm_config_audio_capture;
2521
2522 frame_size = audio_stream_in_frame_size(&in->stream);
2523 buffer_size = get_input_buffer_size(config->sample_rate,
2524 config->format,
2525 channel_count,
2526 is_low_latency);
2527 in->config.period_size = buffer_size / frame_size;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002528 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002529 in->config.channels = channel_count;
2530 in->config.rate = config->sample_rate;
2531
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002532 /* This stream could be for sound trigger lab,
2533 get sound trigger pcm if present */
2534 audio_extn_sound_trigger_check_and_get_session(in);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002535
2536 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002537 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002538 return 0;
2539
2540err_open:
2541 free(in);
2542 *stream_in = NULL;
2543 return ret;
2544}
2545
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002546static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002547 struct audio_stream_in *stream)
2548{
Eric Laurent994a6932013-07-17 11:51:42 -07002549 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002550
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002551 in_standby(&stream->common);
2552 free(stream);
2553
2554 return;
2555}
2556
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002557static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002558{
2559 return 0;
2560}
2561
Andy Hung31aca912014-03-20 17:14:59 -07002562/* verifies input and output devices and their capabilities.
2563 *
2564 * This verification is required when enabling extended bit-depth or
2565 * sampling rates, as not all qcom products support it.
2566 *
2567 * Suitable for calling only on initialization such as adev_open().
2568 * It fills the audio_device use_case_table[] array.
2569 *
2570 * Has a side-effect that it needs to configure audio routing / devices
2571 * in order to power up the devices and read the device parameters.
2572 * It does not acquire any hw device lock. Should restore the devices
2573 * back to "normal state" upon completion.
2574 */
2575static int adev_verify_devices(struct audio_device *adev)
2576{
2577 /* enumeration is a bit difficult because one really wants to pull
2578 * the use_case, device id, etc from the hidden pcm_device_table[].
2579 * In this case there are the following use cases and device ids.
2580 *
2581 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
2582 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
2583 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
2584 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
2585 * [USECASE_AUDIO_RECORD] = {0, 0},
2586 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
2587 * [USECASE_VOICE_CALL] = {2, 2},
2588 *
2589 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
2590 * USECASE_VOICE_CALL omitted, but possible for either input or output.
2591 */
2592
2593 /* should be the usecases enabled in adev_open_input_stream() */
2594 static const int test_in_usecases[] = {
2595 USECASE_AUDIO_RECORD,
2596 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
2597 };
2598 /* should be the usecases enabled in adev_open_output_stream()*/
2599 static const int test_out_usecases[] = {
2600 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
2601 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
2602 };
2603 static const usecase_type_t usecase_type_by_dir[] = {
2604 PCM_PLAYBACK,
2605 PCM_CAPTURE,
2606 };
2607 static const unsigned flags_by_dir[] = {
2608 PCM_OUT,
2609 PCM_IN,
2610 };
2611
2612 size_t i;
2613 unsigned dir;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002614 const unsigned card_id = adev->snd_card;
Andy Hung31aca912014-03-20 17:14:59 -07002615 char info[512]; /* for possible debug info */
2616
2617 for (dir = 0; dir < 2; ++dir) {
2618 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
2619 const unsigned flags_dir = flags_by_dir[dir];
2620 const size_t testsize =
2621 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
2622 const int *testcases =
2623 dir ? test_in_usecases : test_out_usecases;
2624 const audio_devices_t audio_device =
2625 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
2626
2627 for (i = 0; i < testsize; ++i) {
2628 const audio_usecase_t audio_usecase = testcases[i];
2629 int device_id;
2630 snd_device_t snd_device;
2631 struct pcm_params **pparams;
2632 struct stream_out out;
2633 struct stream_in in;
2634 struct audio_usecase uc_info;
2635 int retval;
2636
2637 pparams = &adev->use_case_table[audio_usecase];
2638 pcm_params_free(*pparams); /* can accept null input */
2639 *pparams = NULL;
2640
2641 /* find the device ID for the use case (signed, for error) */
2642 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
2643 if (device_id < 0)
2644 continue;
2645
2646 /* prepare structures for device probing */
2647 memset(&uc_info, 0, sizeof(uc_info));
2648 uc_info.id = audio_usecase;
2649 uc_info.type = usecase_type;
2650 if (dir) {
2651 adev->active_input = &in;
2652 memset(&in, 0, sizeof(in));
2653 in.device = audio_device;
2654 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
2655 uc_info.stream.in = &in;
2656 } else {
2657 adev->active_input = NULL;
2658 }
2659 memset(&out, 0, sizeof(out));
2660 out.devices = audio_device; /* only field needed in select_devices */
2661 uc_info.stream.out = &out;
2662 uc_info.devices = audio_device;
2663 uc_info.in_snd_device = SND_DEVICE_NONE;
2664 uc_info.out_snd_device = SND_DEVICE_NONE;
2665 list_add_tail(&adev->usecase_list, &uc_info.list);
2666
2667 /* select device - similar to start_(in/out)put_stream() */
2668 retval = select_devices(adev, audio_usecase);
2669 if (retval >= 0) {
2670 *pparams = pcm_params_get(card_id, device_id, flags_dir);
2671#if LOG_NDEBUG == 0
2672 if (*pparams) {
2673 ALOGV("%s: (%s) card %d device %d", __func__,
2674 dir ? "input" : "output", card_id, device_id);
2675 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
2676 ALOGV(info); /* print parameters */
2677 } else {
2678 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
2679 }
2680#endif
2681 }
2682
2683 /* deselect device - similar to stop_(in/out)put_stream() */
2684 /* 1. Get and set stream specific mixer controls */
Glenn Kastene7137302014-04-28 15:12:18 -07002685 retval = disable_audio_route(adev, &uc_info);
Andy Hung31aca912014-03-20 17:14:59 -07002686 /* 2. Disable the rx device */
2687 retval = disable_snd_device(adev,
Glenn Kastene7137302014-04-28 15:12:18 -07002688 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
Andy Hung31aca912014-03-20 17:14:59 -07002689 list_remove(&uc_info.list);
2690 }
2691 }
2692 adev->active_input = NULL; /* restore adev state */
2693 return 0;
2694}
2695
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002696static int adev_close(hw_device_t *device)
2697{
Andy Hung31aca912014-03-20 17:14:59 -07002698 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002699 struct audio_device *adev = (struct audio_device *)device;
vivek mehta1a9b7c02015-06-25 11:49:38 -07002700
2701 if (!adev)
2702 return 0;
2703
2704 pthread_mutex_lock(&adev_init_lock);
2705
2706 if ((--audio_device_ref_count) == 0) {
2707 audio_route_free(adev->audio_route);
2708 free(adev->snd_dev_ref_cnt);
2709 platform_deinit(adev->platform);
2710 audio_extn_extspk_deinit(adev->extspk);
2711 audio_extn_sound_trigger_deinit(adev);
2712 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
2713 pcm_params_free(adev->use_case_table[i]);
2714 }
2715 free(device);
Andy Hung31aca912014-03-20 17:14:59 -07002716 }
vivek mehta1a9b7c02015-06-25 11:49:38 -07002717
2718 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002719 return 0;
2720}
2721
Glenn Kasten4f993392014-05-14 07:30:48 -07002722/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
2723 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
2724 * just that it _might_ work.
2725 */
2726static int period_size_is_plausible_for_low_latency(int period_size)
2727{
2728 switch (period_size) {
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002729 case 48:
2730 case 96:
2731 case 144:
Glenn Kasten4f993392014-05-14 07:30:48 -07002732 case 160:
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002733 case 192:
Glenn Kasten4f993392014-05-14 07:30:48 -07002734 case 240:
2735 case 320:
2736 case 480:
2737 return 1;
2738 default:
2739 return 0;
2740 }
2741}
2742
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002743static int adev_open(const hw_module_t *module, const char *name,
2744 hw_device_t **device)
2745{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002746 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002747
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002748 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002749 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
vivek mehta1a9b7c02015-06-25 11:49:38 -07002750 pthread_mutex_lock(&adev_init_lock);
2751 if (audio_device_ref_count != 0) {
2752 *device = &adev->device.common;
2753 audio_device_ref_count++;
2754 ALOGV("%s: returning existing instance of adev", __func__);
2755 ALOGV("%s: exit", __func__);
2756 pthread_mutex_unlock(&adev_init_lock);
2757 return 0;
2758 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002759 adev = calloc(1, sizeof(struct audio_device));
2760
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002761 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2762
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002763 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2764 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2765 adev->device.common.module = (struct hw_module_t *)module;
2766 adev->device.common.close = adev_close;
2767
2768 adev->device.init_check = adev_init_check;
2769 adev->device.set_voice_volume = adev_set_voice_volume;
2770 adev->device.set_master_volume = adev_set_master_volume;
2771 adev->device.get_master_volume = adev_get_master_volume;
2772 adev->device.set_master_mute = adev_set_master_mute;
2773 adev->device.get_master_mute = adev_get_master_mute;
2774 adev->device.set_mode = adev_set_mode;
2775 adev->device.set_mic_mute = adev_set_mic_mute;
2776 adev->device.get_mic_mute = adev_get_mic_mute;
2777 adev->device.set_parameters = adev_set_parameters;
2778 adev->device.get_parameters = adev_get_parameters;
2779 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2780 adev->device.open_output_stream = adev_open_output_stream;
2781 adev->device.close_output_stream = adev_close_output_stream;
2782 adev->device.open_input_stream = adev_open_input_stream;
2783 adev->device.close_input_stream = adev_close_input_stream;
2784 adev->device.dump = adev_dump;
2785
2786 /* Set the default route before the PCM stream is opened */
2787 pthread_mutex_lock(&adev->lock);
2788 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002789 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002790 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002791 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002792 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002793 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002794 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002795 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002796 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002797 pthread_mutex_unlock(&adev->lock);
2798
2799 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002800 adev->platform = platform_init(adev);
2801 if (!adev->platform) {
2802 free(adev->snd_dev_ref_cnt);
2803 free(adev);
2804 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2805 *device = NULL;
vivek mehta1a9b7c02015-06-25 11:49:38 -07002806 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07002807 return -EINVAL;
2808 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002809
Eric Laurent0499d4f2014-08-25 22:39:29 -05002810 adev->extspk = audio_extn_extspk_init(adev);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002811 audio_extn_sound_trigger_init(adev);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002812
Eric Laurentc4aef752013-09-12 17:45:53 -07002813 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2814 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2815 if (adev->visualizer_lib == NULL) {
2816 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2817 } else {
2818 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2819 adev->visualizer_start_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002820 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002821 "visualizer_hal_start_output");
2822 adev->visualizer_stop_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002823 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002824 "visualizer_hal_stop_output");
2825 }
2826 }
2827
Haynes Mathew George41f86652014-06-17 14:22:15 -07002828 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2829 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2830 if (adev->offload_effects_lib == NULL) {
2831 ALOGE("%s: DLOPEN failed for %s", __func__,
2832 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2833 } else {
2834 ALOGV("%s: DLOPEN successful for %s", __func__,
2835 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2836 adev->offload_effects_start_output =
2837 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2838 "offload_effects_bundle_hal_start_output");
2839 adev->offload_effects_stop_output =
2840 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2841 "offload_effects_bundle_hal_stop_output");
2842 }
2843 }
2844
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002845 adev->bt_wb_speech_enabled = false;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002846 adev->enable_voicerx = false;
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002847
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002848 *device = &adev->device.common;
vivek mehta1a9b7c02015-06-25 11:49:38 -07002849
Andy Hung31aca912014-03-20 17:14:59 -07002850 if (k_enable_extended_precision)
2851 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002852
Glenn Kasten4f993392014-05-14 07:30:48 -07002853 char value[PROPERTY_VALUE_MAX];
2854 int trial;
2855 if (property_get("audio_hal.period_size", value, NULL) > 0) {
2856 trial = atoi(value);
2857 if (period_size_is_plausible_for_low_latency(trial)) {
2858 pcm_config_low_latency.period_size = trial;
2859 pcm_config_low_latency.start_threshold = trial / 4;
2860 pcm_config_low_latency.avail_min = trial / 4;
2861 configured_low_latency_capture_period_size = trial;
2862 }
2863 }
2864 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
2865 trial = atoi(value);
2866 if (period_size_is_plausible_for_low_latency(trial)) {
2867 configured_low_latency_capture_period_size = trial;
2868 }
2869 }
2870
vivek mehta1a9b7c02015-06-25 11:49:38 -07002871 audio_device_ref_count++;
2872 pthread_mutex_unlock(&adev_init_lock);
2873
Eric Laurent994a6932013-07-17 11:51:42 -07002874 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002875 return 0;
2876}
2877
2878static struct hw_module_methods_t hal_module_methods = {
2879 .open = adev_open,
2880};
2881
2882struct audio_module HAL_MODULE_INFO_SYM = {
2883 .common = {
2884 .tag = HARDWARE_MODULE_TAG,
2885 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2886 .hal_api_version = HARDWARE_HAL_API_VERSION,
2887 .id = AUDIO_HARDWARE_MODULE_ID,
2888 .name = "QCOM Audio HAL",
2889 .author = "Code Aurora Forum",
2890 .methods = &hal_module_methods,
2891 },
2892};