blob: 3038d44f9858e175f63687d5c7166546743c2201 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003 * Not a Contribution.
4 *
Shiv Maliyappanahalli8911f282014-01-10 15:56:19 -08005 * Copyright (C) 2013 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_primary"
21/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070022/*#define VERY_VERY_VERBOSE_LOGGING*/
23#ifdef VERY_VERY_VERBOSE_LOGGING
24#define ALOGVV ALOGV
25#else
26#define ALOGVV(a...) do { } while(0)
27#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080028
29#include <errno.h>
30#include <pthread.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080034#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070035#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070036#include <sys/resource.h>
37#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038
39#include <cutils/log.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070042#include <cutils/atomic.h>
43#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080044
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <hardware/audio_effect.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070050#include "platform_api.h"
51#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070052#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080053#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include "sound/compress_params.h"
ApurupaPattapu0c566872014-01-10 14:46:02 -080056#include "sound/asound.h"
57
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070058#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
59/* ToDo: Check and update a proper value in msec */
60#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
61#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
62
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +053063#define PROXY_OPEN_RETRY_COUNT 100
64#define PROXY_OPEN_WAIT_TIME 20
ApurupaPattapu0c566872014-01-10 14:46:02 -080065
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080066#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
67
Eric Laurentb23d5282013-05-14 15:27:20 -070068struct pcm_config pcm_config_deep_buffer = {
69 .channels = 2,
70 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
71 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
72 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
73 .format = PCM_FORMAT_S16_LE,
74 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
75 .stop_threshold = INT_MAX,
76 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
77};
78
79struct pcm_config pcm_config_low_latency = {
80 .channels = 2,
81 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
82 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
83 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
84 .format = PCM_FORMAT_S16_LE,
85 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
86 .stop_threshold = INT_MAX,
87 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
88};
89
90struct pcm_config pcm_config_hdmi_multi = {
91 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
92 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
93 .period_size = HDMI_MULTI_PERIOD_SIZE,
94 .period_count = HDMI_MULTI_PERIOD_COUNT,
95 .format = PCM_FORMAT_S16_LE,
96 .start_threshold = 0,
97 .stop_threshold = INT_MAX,
98 .avail_min = 0,
99};
100
101struct pcm_config pcm_config_audio_capture = {
102 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700103 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
104 .format = PCM_FORMAT_S16_LE,
105};
106
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530107#define AFE_PROXY_CHANNEL_COUNT 2
108#define AFE_PROXY_SAMPLING_RATE 48000
109
110#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
111#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
112
113struct pcm_config pcm_config_afe_proxy_playback = {
114 .channels = AFE_PROXY_CHANNEL_COUNT,
115 .rate = AFE_PROXY_SAMPLING_RATE,
116 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
117 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
118 .format = PCM_FORMAT_S16_LE,
119 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
120 .stop_threshold = INT_MAX,
121 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
122};
123
124#define AFE_PROXY_RECORD_PERIOD_SIZE 768
125#define AFE_PROXY_RECORD_PERIOD_COUNT 4
126
127struct pcm_config pcm_config_afe_proxy_record = {
128 .channels = AFE_PROXY_CHANNEL_COUNT,
129 .rate = AFE_PROXY_SAMPLING_RATE,
130 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
131 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
132 .format = PCM_FORMAT_S16_LE,
133 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
134 .stop_threshold = INT_MAX,
135 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
136};
137
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800138const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700139 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
140 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
141 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700142 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700143 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700144 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700145 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700146 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700147 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800148 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800149 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700150 [USECASE_VOICE_CALL] = "voice-call",
Mingming Yinee733602014-04-03 17:47:22 -0700151
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700152 [USECASE_VOICE2_CALL] = "voice2-call",
153 [USECASE_VOLTE_CALL] = "volte-call",
154 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat111aeb32014-02-12 17:58:59 -0800155 [USECASE_VOWLAN_CALL] = "vowlan-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800156 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700157 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
158 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
159 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800160 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
161 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
162 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
163
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700164 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
165 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700166 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
167 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530168
169 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
170 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700171};
172
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800173
174#define STRING_TO_ENUM(string) { #string, string }
175
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800176struct string_to_enum {
177 const char *name;
178 uint32_t value;
179};
180
181static const struct string_to_enum out_channels_name_to_enum_table[] = {
182 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
183 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
184 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
185};
186
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700187static struct audio_device *adev = NULL;
188static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700189static unsigned int audio_device_ref_count;
190
Haynes Mathew George5191a852013-09-11 14:19:36 -0700191static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -0800192
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -0800193static int check_and_set_gapless_mode(struct audio_device *adev) {
194
195
196 char value[PROPERTY_VALUE_MAX] = {0};
197 bool gapless_enabled = false;
198 const char *mixer_ctl_name = "Compress Gapless Playback";
199 struct mixer_ctl *ctl;
200
201 ALOGV("%s:", __func__);
202 property_get("audio.offload.gapless.enabled", value, NULL);
203 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
204
205 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
206 if (!ctl) {
207 ALOGE("%s: Could not get ctl for mixer cmd - %s",
208 __func__, mixer_ctl_name);
209 return -EINVAL;
210 }
211
212 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
213 ALOGE("%s: Could not set gapless mode %d",
214 __func__, gapless_enabled);
215 return -EINVAL;
216 }
217 return 0;
218}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700219
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700220static bool is_supported_format(audio_format_t format)
221{
Eric Laurent86e17132013-09-12 17:49:30 -0700222 if (format == AUDIO_FORMAT_MP3 ||
Mingming Yinee733602014-04-03 17:47:22 -0700223#ifdef EXTN_OFFLOAD_ENABLED
ApurupaPattapu0c566872014-01-10 14:46:02 -0800224 format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD ||
Mingming Yinee733602014-04-03 17:47:22 -0700225 format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD ||
226#endif
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530227 format == AUDIO_FORMAT_AAC_LC ||
228 format == AUDIO_FORMAT_AAC_HE_V1 ||
229 format == AUDIO_FORMAT_AAC_HE_V2 ){
230 return true;
231 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700232 return false;
233}
234
235static int get_snd_codec_id(audio_format_t format)
236{
237 int id = 0;
238
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530239 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700240 case AUDIO_FORMAT_MP3:
241 id = SND_AUDIOCODEC_MP3;
242 break;
243 case AUDIO_FORMAT_AAC:
244 id = SND_AUDIOCODEC_AAC;
245 break;
Mingming Yinee733602014-04-03 17:47:22 -0700246#ifdef EXTN_OFFLOAD_ENABLED
Ashish Jaincff6e3e2014-08-25 20:36:25 +0530247 case AUDIO_FORMAT_PCM_OFFLOAD:
ApurupaPattapu0c566872014-01-10 14:46:02 -0800248 id = SND_AUDIOCODEC_PCM;
249 break;
Mingming Yinee733602014-04-03 17:47:22 -0700250#endif
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700251 default:
Mingming Yin90310102013-11-13 16:57:00 -0800252 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700253 }
254
255 return id;
256}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800257
Avinash Vaish8005dc82014-07-24 15:36:33 +0530258static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
259 struct audio_usecase *uc_info)
260{
261 struct listnode *node;
262 struct audio_usecase *usecase;
263
264 if (uc_info == NULL)
265 return -EINVAL;
266
267 /* Re-route all voice usecases on the shared backend other than the
268 specified usecase to new snd devices */
269 list_for_each(node, &adev->usecase_list) {
270 usecase = node_to_item(node, struct audio_usecase, list);
271 if ((usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) &&
272 (usecase != uc_info))
273 enable_audio_route(adev, usecase);
274 }
275 return 0;
276}
277
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700278int enable_audio_route(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700279 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800280{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700281 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700282 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800283
284 if (usecase == NULL)
285 return -EINVAL;
286
287 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
288
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800289 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700290 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800291 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700292 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800293
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -0800294#ifdef DS1_DOLBY_DAP_ENABLED
295 audio_extn_dolby_set_dmid(adev);
296 audio_extn_dolby_set_endpoint(adev);
297#endif
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800298 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700299 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700300 ALOGV("%s: apply mixer and update path: %s", __func__, mixer_path);
301 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800302 ALOGV("%s: exit", __func__);
303 return 0;
304}
305
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700306int disable_audio_route(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700307 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800308{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700309 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700310 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800311
312 if (usecase == NULL)
313 return -EINVAL;
314
315 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700316 if (usecase->type == PCM_CAPTURE)
317 snd_device = usecase->in_snd_device;
318 else
319 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800320 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700321 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700322 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
323 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800324 ALOGV("%s: exit", __func__);
325 return 0;
326}
327
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700328int enable_snd_device(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700329 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800330{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700331 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
332
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800333 if (snd_device < SND_DEVICE_MIN ||
334 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800335 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800336 return -EINVAL;
337 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700338
339 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700340
341 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
342 ALOGE("%s: Invalid sound device returned", __func__);
343 return -EINVAL;
344 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700345 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700346 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700347 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700348 return 0;
349 }
350
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700351 /* start usb playback thread */
352 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
353 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
354 audio_extn_usb_start_playback(adev);
355
356 /* start usb capture thread */
357 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
358 audio_extn_usb_start_capture(adev);
359
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700360 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
361 audio_extn_spkr_prot_is_enabled()) {
362 if (audio_extn_spkr_prot_start_processing(snd_device)) {
363 ALOGE("%s: spkr_start_processing failed", __func__);
364 return -EINVAL;
365 }
366 } else {
367 ALOGV("%s: snd_device(%d: %s)", __func__,
368 snd_device, device_name);
369 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
370 adev->snd_dev_ref_cnt[snd_device]--;
371 return -EINVAL;
372 }
Kiran Kandide144c82013-11-20 15:58:32 -0800373 audio_extn_listen_update_status(snd_device,
374 LISTEN_EVENT_SND_DEVICE_BUSY);
375
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700376 audio_route_apply_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800377 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800378 return 0;
379}
380
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700381int disable_snd_device(struct audio_device *adev,
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700382 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800383{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700384 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
385
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800386 if (snd_device < SND_DEVICE_MIN ||
387 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800388 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800389 return -EINVAL;
390 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700391 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
392 ALOGE("%s: device ref cnt is already 0", __func__);
393 return -EINVAL;
394 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700395
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700396 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700397
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700398 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
399 ALOGE("%s: Invalid sound device returned", __func__);
400 return -EINVAL;
401 }
402
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700403 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700404 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700405 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800406 /* exit usb play back thread */
407 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
408 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
409 audio_extn_usb_stop_playback();
410
411 /* exit usb capture thread */
412 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
Rajshekar Eashwarappa4dfa0752014-10-02 20:18:20 +0530413 audio_extn_usb_stop_capture();
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700414 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
415 audio_extn_spkr_prot_is_enabled()) {
416 audio_extn_spkr_prot_stop_processing();
417 } else
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700418 audio_route_reset_and_update_path(adev->audio_route, device_name);
Kiran Kandide144c82013-11-20 15:58:32 -0800419
420 audio_extn_listen_update_status(snd_device,
421 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700422 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700423
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800424 return 0;
425}
426
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700427static void check_usecases_codec_backend(struct audio_device *adev,
428 struct audio_usecase *uc_info,
429 snd_device_t snd_device)
430{
431 struct listnode *node;
432 struct audio_usecase *usecase;
433 bool switch_device[AUDIO_USECASE_MAX];
434 int i, num_uc_to_switch = 0;
435
436 /*
437 * This function is to make sure that all the usecases that are active on
438 * the hardware codec backend are always routed to any one device that is
439 * handled by the hardware codec.
440 * For example, if low-latency and deep-buffer usecases are currently active
441 * on speaker and out_set_parameters(headset) is received on low-latency
442 * output, then we have to make sure deep-buffer is also switched to headset,
443 * because of the limitation that both the devices cannot be enabled
444 * at the same time as they share the same backend.
445 */
446 /* Disable all the usecases on the shared backend other than the
447 specified usecase */
448 for (i = 0; i < AUDIO_USECASE_MAX; i++)
449 switch_device[i] = false;
450
451 list_for_each(node, &adev->usecase_list) {
452 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800453 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700454 usecase != uc_info &&
455 usecase->out_snd_device != snd_device &&
456 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
457 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
458 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700459 platform_get_snd_device_name(usecase->out_snd_device));
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700460 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700461 switch_device[usecase->id] = true;
462 num_uc_to_switch++;
463 }
464 }
465
466 if (num_uc_to_switch) {
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700467 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700468
469 list_for_each(node, &adev->usecase_list) {
470 usecase = node_to_item(node, struct audio_usecase, list);
471 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700472 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700473 }
474 }
475
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700476 list_for_each(node, &adev->usecase_list) {
477 usecase = node_to_item(node, struct audio_usecase, list);
478 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700479 enable_snd_device(adev, snd_device);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700480 }
481 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700482
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700483 /* Re-route all the usecases on the shared backend other than the
484 specified usecase to new snd devices */
485 list_for_each(node, &adev->usecase_list) {
486 usecase = node_to_item(node, struct audio_usecase, list);
487 /* Update the out_snd_device only before enabling the audio route */
488 if (switch_device[usecase->id] ) {
489 usecase->out_snd_device = snd_device;
Avinash Vaish8005dc82014-07-24 15:36:33 +0530490 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
491 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700492 }
493 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700494 }
495}
496
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700497static void check_and_route_capture_usecases(struct audio_device *adev,
498 struct audio_usecase *uc_info,
499 snd_device_t snd_device)
500{
501 struct listnode *node;
502 struct audio_usecase *usecase;
503 bool switch_device[AUDIO_USECASE_MAX];
504 int i, num_uc_to_switch = 0;
505
506 /*
507 * This function is to make sure that all the active capture usecases
508 * are always routed to the same input sound device.
509 * For example, if audio-record and voice-call usecases are currently
510 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
511 * is received for voice call then we have to make sure that audio-record
512 * usecase is also switched to earpiece i.e. voice-dmic-ef,
513 * because of the limitation that two devices cannot be enabled
514 * at the same time if they share the same backend.
515 */
516 for (i = 0; i < AUDIO_USECASE_MAX; i++)
517 switch_device[i] = false;
518
519 list_for_each(node, &adev->usecase_list) {
520 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800521 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700522 usecase != uc_info &&
523 usecase->in_snd_device != snd_device) {
524 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
525 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700526 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700527 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700528 switch_device[usecase->id] = true;
529 num_uc_to_switch++;
530 }
531 }
532
533 if (num_uc_to_switch) {
Haynes Mathew Georgef9306752014-04-24 11:53:44 -0700534 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700535
536 list_for_each(node, &adev->usecase_list) {
537 usecase = node_to_item(node, struct audio_usecase, list);
538 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700539 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800540 }
541 }
542
543 list_for_each(node, &adev->usecase_list) {
544 usecase = node_to_item(node, struct audio_usecase, list);
545 if (switch_device[usecase->id]) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700546 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700547 }
548 }
549
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700550 /* Re-route all the usecases on the shared backend other than the
551 specified usecase to new snd devices */
552 list_for_each(node, &adev->usecase_list) {
553 usecase = node_to_item(node, struct audio_usecase, list);
554 /* Update the in_snd_device only before enabling the audio route */
555 if (switch_device[usecase->id] ) {
556 usecase->in_snd_device = snd_device;
Avinash Vaish8005dc82014-07-24 15:36:33 +0530557 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
558 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700559 }
560 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700561 }
562}
563
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800564/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700565static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800566{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700567 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700568 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800569
570 switch (channels) {
571 /*
572 * Do not handle stereo output in Multi-channel cases
573 * Stereo case is handled in normal playback path
574 */
575 case 6:
576 ALOGV("%s: HDMI supports 5.1", __func__);
577 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
578 break;
579 case 8:
580 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
581 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
582 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
583 break;
584 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700585 ALOGE("HDMI does not support multi channel playback");
586 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800587 break;
588 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700589 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800590}
591
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700592static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
593{
594 struct audio_usecase *usecase;
595 struct listnode *node;
596
597 list_for_each(node, &adev->usecase_list) {
598 usecase = node_to_item(node, struct audio_usecase, list);
599 if (usecase->type == VOICE_CALL) {
600 ALOGV("%s: usecase id %d", __func__, usecase->id);
601 return usecase->id;
602 }
603 }
604 return USECASE_INVALID;
605}
606
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700607struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700608 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700609{
610 struct audio_usecase *usecase;
611 struct listnode *node;
612
613 list_for_each(node, &adev->usecase_list) {
614 usecase = node_to_item(node, struct audio_usecase, list);
615 if (usecase->id == uc_id)
616 return usecase;
617 }
618 return NULL;
619}
620
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700621int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800622{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800623 snd_device_t out_snd_device = SND_DEVICE_NONE;
624 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700625 struct audio_usecase *usecase = NULL;
626 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800627 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800628 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed739e7152014-01-23 15:56:53 -0800629 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800630 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700631 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800632
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700633 usecase = get_usecase_from_list(adev, uc_id);
634 if (usecase == NULL) {
635 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
636 return -EINVAL;
637 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800638
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800639 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800640 (usecase->type == VOIP_CALL) ||
641 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700642 out_snd_device = platform_get_output_snd_device(adev->platform,
643 usecase->stream.out->devices);
644 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700645 usecase->devices = usecase->stream.out->devices;
646 } else {
647 /*
648 * If the voice call is active, use the sound devices of voice call usecase
649 * so that it would not result any device switch. All the usecases will
650 * be switched to new device when select_devices() is called for voice call
651 * usecase. This is to avoid switching devices for voice call when
652 * check_usecases_codec_backend() is called below.
653 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700654 if (voice_is_in_call(adev)) {
655 vc_usecase = get_usecase_from_list(adev,
656 get_voice_usecase_id_from_list(adev));
Helen Zeng067b96b2013-11-26 12:10:29 -0800657 if ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
658 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700659 in_snd_device = vc_usecase->in_snd_device;
660 out_snd_device = vc_usecase->out_snd_device;
661 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800662 } else if (voice_extn_compress_voip_is_active(adev)) {
663 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Narsinga Rao Chella5856f9b2014-04-11 17:20:46 -0700664 if ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Avinash Vaish6a4c3372014-06-25 12:20:37 +0530665 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
666 (voip_usecase->stream.out != adev->primary_output)) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800667 in_snd_device = voip_usecase->in_snd_device;
668 out_snd_device = voip_usecase->out_snd_device;
669 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800670 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed739e7152014-01-23 15:56:53 -0800671 hfp_ucid = audio_extn_hfp_get_usecase();
672 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800673 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
674 in_snd_device = hfp_usecase->in_snd_device;
675 out_snd_device = hfp_usecase->out_snd_device;
676 }
Vimal Puthanveed8fa9eab2014-01-07 16:47:47 -0800677 } else if (audio_extn_hfp_is_active(adev)) {
678 hfp_usecase = get_usecase_from_list(adev, USECASE_AUDIO_HFP_SCO);
679 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
680 in_snd_device = hfp_usecase->in_snd_device;
681 out_snd_device = hfp_usecase->out_snd_device;
682 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700683 }
684 if (usecase->type == PCM_PLAYBACK) {
685 usecase->devices = usecase->stream.out->devices;
686 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700687 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700688 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700689 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700690 if (usecase->stream.out == adev->primary_output &&
Shreyas Nagasandra Chandrasekhar23b3af22014-09-03 19:10:01 +0530691 adev->active_input ) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700692 select_devices(adev, adev->active_input->usecase);
693 }
694 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700695 } else if (usecase->type == PCM_CAPTURE) {
696 usecase->devices = usecase->stream.in->device;
697 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700698 if (in_snd_device == SND_DEVICE_NONE) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530699 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Shreyas Nagasandra Chandrasekhar23b3af22014-09-03 19:10:01 +0530700 if ((adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION || (adev->mode == AUDIO_MODE_IN_COMMUNICATION && adev->active_input->source == AUDIO_SOURCE_MIC)) &&
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700701 adev->primary_output && !adev->primary_output->standby) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530702 out_device = adev->primary_output->devices;
703 } else if(usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
704 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700705 }
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530706 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700707 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700708 }
709 }
710
711 if (out_snd_device == usecase->out_snd_device &&
712 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800713 return 0;
714 }
715
sangwoobc677242013-08-08 16:53:43 +0900716 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700717 out_snd_device, platform_get_snd_device_name(out_snd_device),
718 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800719
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800720 /*
721 * Limitation: While in call, to do a device switch we need to disable
722 * and enable both RX and TX devices though one of them is same as current
723 * device.
724 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800725 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700726 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800727 }
728
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700729 /* Disable current sound devices */
730 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700731 disable_audio_route(adev, usecase);
732 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800733 }
734
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700735 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700736 disable_audio_route(adev, usecase);
737 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800738 }
739
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700740 /* Enable new sound devices */
741 if (out_snd_device != SND_DEVICE_NONE) {
742 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
743 check_usecases_codec_backend(adev, usecase, out_snd_device);
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700744 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745 }
746
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700747 if (in_snd_device != SND_DEVICE_NONE) {
748 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700749 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700750 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700751
Avinash Vaish8005dc82014-07-24 15:36:33 +0530752 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700753 status = platform_switch_voice_call_device_post(adev->platform,
754 out_snd_device,
755 in_snd_device);
Avinash Vaish8005dc82014-07-24 15:36:33 +0530756 enable_audio_route_for_voice_usecases(adev, usecase);
757 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800758
sangwoo170731f2013-06-08 15:36:36 +0900759 usecase->in_snd_device = in_snd_device;
760 usecase->out_snd_device = out_snd_device;
761
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700762 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900763
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800764 /* Applicable only on the targets that has external modem.
765 * Enable device command should be sent to modem only after
766 * enabling voice call mixer controls
767 */
768 if (usecase->type == VOICE_CALL)
769 status = platform_switch_voice_call_usecase_route_post(adev->platform,
770 out_snd_device,
771 in_snd_device);
772
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +0530773 ALOGD("%s: done",__func__);
774
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800775 return status;
776}
777
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800778static int stop_input_stream(struct stream_in *in)
779{
780 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800781 struct audio_usecase *uc_info;
782 struct audio_device *adev = in->dev;
783
Eric Laurentc8400632013-02-14 19:04:54 -0800784 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800785
Eric Laurent994a6932013-07-17 11:51:42 -0700786 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700787 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800788 uc_info = get_usecase_from_list(adev, in->usecase);
789 if (uc_info == NULL) {
790 ALOGE("%s: Could not find the usecase (%d) in the list",
791 __func__, in->usecase);
792 return -EINVAL;
793 }
794
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800795 /* Close in-call recording streams */
796 voice_check_and_stop_incall_rec_usecase(adev, in);
797
Eric Laurent150dbfe2013-02-27 14:31:02 -0800798 /* 1. Disable stream specific mixer controls */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700799 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700800
801 /* 2. Disable the tx device */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -0700802 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800803
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800804 list_remove(&uc_info->list);
805 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800806
Eric Laurent994a6932013-07-17 11:51:42 -0700807 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800808 return ret;
809}
810
811int start_input_stream(struct stream_in *in)
812{
813 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800814 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800815 struct audio_usecase *uc_info;
816 struct audio_device *adev = in->dev;
817
Mingming Yine62d7842013-10-25 16:26:03 -0700818 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +0530819 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
820 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700821
822 /* Check if source matches incall recording usecase criteria */
823 ret = voice_check_and_set_incall_rec_usecase(adev, in);
824 if (ret)
825 goto error_config;
826 else
827 ALOGV("%s: usecase(%d)", __func__, in->usecase);
828
Eric Laurentb23d5282013-05-14 15:27:20 -0700829 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800830 if (in->pcm_device_id < 0) {
831 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
832 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800833 ret = -EINVAL;
834 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800835 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700836
837 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800838 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
839 uc_info->id = in->usecase;
840 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800841 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700842 uc_info->devices = in->device;
843 uc_info->in_snd_device = SND_DEVICE_NONE;
844 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800845
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800846 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700847 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800848
Eric Laurentc8400632013-02-14 19:04:54 -0800849 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +0530850 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
851
852 unsigned int flags = PCM_IN;
853 unsigned int pcm_open_retry_entry_count = 0;
854
855 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
856 flags |= PCM_MMAP | PCM_NOIRQ;
857 pcm_open_retry_entry_count = PROXY_OPEN_RETRY_COUNT;
858 }
859
860 while(1) {
861 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
862 flags, &in->config);
863 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
864 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
865 if (in->pcm != NULL) {
866 pcm_close(in->pcm);
867 in->pcm = NULL;
868 }
869 if (pcm_open_retry_entry_count-- == 0) {
870 ret = -EIO;
871 goto error_open;
872 }
873 usleep(PROXY_OPEN_WAIT_TIME * 1000);
874 continue;
875 }
876 break;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800877 }
Eric Laurent994a6932013-07-17 11:51:42 -0700878 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800879 return ret;
880
881error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800882 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800883
884error_config:
885 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700886 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800887
888 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800889}
890
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700891/* must be called with out->lock locked */
892static int send_offload_cmd_l(struct stream_out* out, int command)
893{
894 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
895
896 ALOGVV("%s %d", __func__, command);
897
898 cmd->cmd = command;
899 list_add_tail(&out->offload_cmd_list, &cmd->node);
900 pthread_cond_signal(&out->offload_cond);
901 return 0;
902}
903
904/* must be called iwth out->lock locked */
905static void stop_compressed_output_l(struct stream_out *out)
906{
907 out->offload_state = OFFLOAD_STATE_IDLE;
908 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700909 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700910 if (out->compr != NULL) {
911 compress_stop(out->compr);
912 while (out->offload_thread_blocked) {
913 pthread_cond_wait(&out->cond, &out->lock);
914 }
915 }
916}
917
918static void *offload_thread_loop(void *context)
919{
920 struct stream_out *out = (struct stream_out *) context;
921 struct listnode *item;
Krishnankutty Kolathappilly0fe78f02014-01-06 18:33:58 -0800922 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700923
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700924 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
925 set_sched_policy(0, SP_FOREGROUND);
926 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
927
928 ALOGV("%s", __func__);
929 pthread_mutex_lock(&out->lock);
930 for (;;) {
931 struct offload_cmd *cmd = NULL;
932 stream_callback_event_t event;
933 bool send_callback = false;
934
935 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
936 __func__, list_empty(&out->offload_cmd_list),
937 out->offload_state);
938 if (list_empty(&out->offload_cmd_list)) {
939 ALOGV("%s SLEEPING", __func__);
940 pthread_cond_wait(&out->offload_cond, &out->lock);
941 ALOGV("%s RUNNING", __func__);
942 continue;
943 }
944
945 item = list_head(&out->offload_cmd_list);
946 cmd = node_to_item(item, struct offload_cmd, node);
947 list_remove(item);
948
949 ALOGVV("%s STATE %d CMD %d out->compr %p",
950 __func__, out->offload_state, cmd->cmd, out->compr);
951
952 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
953 free(cmd);
954 break;
955 }
956
957 if (out->compr == NULL) {
958 ALOGE("%s: Compress handle is NULL", __func__);
959 pthread_cond_signal(&out->cond);
960 continue;
961 }
962 out->offload_thread_blocked = true;
963 pthread_mutex_unlock(&out->lock);
964 send_callback = false;
965 switch(cmd->cmd) {
966 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
967 compress_wait(out->compr, -1);
968 send_callback = true;
969 event = STREAM_CBK_EVENT_WRITE_READY;
970 break;
971 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappilly0fe78f02014-01-06 18:33:58 -0800972 ret = compress_next_track(out->compr);
973 if(ret == 0)
974 compress_partial_drain(out->compr);
975 else if(ret == -ETIMEDOUT)
976 compress_drain(out->compr);
977 else
978 ALOGE("%s: Next track returned error %d",__func__, ret);
979
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700980 send_callback = true;
981 event = STREAM_CBK_EVENT_DRAIN_READY;
982 break;
983 case OFFLOAD_CMD_DRAIN:
984 compress_drain(out->compr);
985 send_callback = true;
986 event = STREAM_CBK_EVENT_DRAIN_READY;
987 break;
988 default:
989 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
990 break;
991 }
992 pthread_mutex_lock(&out->lock);
993 out->offload_thread_blocked = false;
994 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700995 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700996 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700997 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700998 free(cmd);
999 }
1000
1001 pthread_cond_signal(&out->cond);
1002 while (!list_empty(&out->offload_cmd_list)) {
1003 item = list_head(&out->offload_cmd_list);
1004 list_remove(item);
1005 free(node_to_item(item, struct offload_cmd, node));
1006 }
1007 pthread_mutex_unlock(&out->lock);
1008
1009 return NULL;
1010}
1011
1012static int create_offload_callback_thread(struct stream_out *out)
1013{
1014 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1015 list_init(&out->offload_cmd_list);
1016 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1017 offload_thread_loop, out);
1018 return 0;
1019}
1020
1021static int destroy_offload_callback_thread(struct stream_out *out)
1022{
1023 pthread_mutex_lock(&out->lock);
1024 stop_compressed_output_l(out);
1025 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1026
1027 pthread_mutex_unlock(&out->lock);
1028 pthread_join(out->offload_thread, (void **) NULL);
1029 pthread_cond_destroy(&out->offload_cond);
1030
1031 return 0;
1032}
1033
Eric Laurent07eeafd2013-10-06 12:52:49 -07001034static bool allow_hdmi_channel_config(struct audio_device *adev)
1035{
1036 struct listnode *node;
1037 struct audio_usecase *usecase;
1038 bool ret = true;
1039
1040 list_for_each(node, &adev->usecase_list) {
1041 usecase = node_to_item(node, struct audio_usecase, list);
1042 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1043 /*
1044 * If voice call is already existing, do not proceed further to avoid
1045 * disabling/enabling both RX and TX devices, CSD calls, etc.
1046 * Once the voice call done, the HDMI channels can be configured to
1047 * max channels of remaining use cases.
1048 */
1049 if (usecase->id == USECASE_VOICE_CALL) {
1050 ALOGD("%s: voice call is active, no change in HDMI channels",
1051 __func__);
1052 ret = false;
1053 break;
1054 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1055 ALOGD("%s: multi channel playback is active, "
1056 "no change in HDMI channels", __func__);
1057 ret = false;
1058 break;
Mingming Yin3ed162b2014-02-24 17:56:01 -08001059 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
1060 popcount(usecase->stream.out->channel_mask) > 2) {
1061 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1062 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1063 ret = false;
1064 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001065 }
1066 }
1067 }
1068 return ret;
1069}
1070
1071static int check_and_set_hdmi_channels(struct audio_device *adev,
1072 unsigned int channels)
1073{
1074 struct listnode *node;
1075 struct audio_usecase *usecase;
1076
1077 /* Check if change in HDMI channel config is allowed */
1078 if (!allow_hdmi_channel_config(adev))
1079 return 0;
1080
1081 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001082 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001083 return 0;
1084 }
1085
1086 platform_set_hdmi_channels(adev->platform, channels);
1087 adev->cur_hdmi_channels = channels;
1088
1089 /*
1090 * Deroute all the playback streams routed to HDMI so that
1091 * the back end is deactivated. Note that backend will not
1092 * be deactivated if any one stream is connected to it.
1093 */
1094 list_for_each(node, &adev->usecase_list) {
1095 usecase = node_to_item(node, struct audio_usecase, list);
1096 if (usecase->type == PCM_PLAYBACK &&
1097 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001098 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001099 }
1100 }
1101
1102 /*
1103 * Enable all the streams disabled above. Now the HDMI backend
1104 * will be activated with new channel configuration
1105 */
1106 list_for_each(node, &adev->usecase_list) {
1107 usecase = node_to_item(node, struct audio_usecase, list);
1108 if (usecase->type == PCM_PLAYBACK &&
1109 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001110 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001111 }
1112 }
1113
1114 return 0;
1115}
1116
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001117static int stop_output_stream(struct stream_out *out)
1118{
1119 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001120 struct audio_usecase *uc_info;
1121 struct audio_device *adev = out->dev;
1122
Eric Laurent994a6932013-07-17 11:51:42 -07001123 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001124 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001125 uc_info = get_usecase_from_list(adev, out->usecase);
1126 if (uc_info == NULL) {
1127 ALOGE("%s: Could not find the usecase (%d) in the list",
1128 __func__, out->usecase);
1129 return -EINVAL;
1130 }
1131
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001132 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1133 if (adev->visualizer_stop_output != NULL)
1134 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1135 if (adev->offload_effects_stop_output != NULL)
1136 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1137 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001138
Eric Laurent150dbfe2013-02-27 14:31:02 -08001139 /* 1. Get and set stream specific mixer controls */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001140 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001141
1142 /* 2. Disable the rx device */
Haynes Mathew Georgeea098922014-04-24 17:53:50 -07001143 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001144
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001145 list_remove(&uc_info->list);
1146 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001147
Eric Laurent07eeafd2013-10-06 12:52:49 -07001148 /* Must be called after removing the usecase from list */
1149 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1150 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1151
Eric Laurent994a6932013-07-17 11:51:42 -07001152 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001153 return ret;
1154}
1155
1156int start_output_stream(struct stream_out *out)
1157{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001158 int ret = 0;
Mingming Yin6c344432014-05-01 15:37:31 -07001159 int sink_channels = 0;
1160 char prop_value[PROPERTY_VALUE_MAX] = {0};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001161 struct audio_usecase *uc_info;
1162 struct audio_device *adev = out->dev;
1163
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301164 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1165 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1166 out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001167 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001168 if (out->pcm_device_id < 0) {
1169 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1170 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001171 ret = -EINVAL;
1172 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001173 }
1174
1175 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1176 uc_info->id = out->usecase;
1177 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001178 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001179 uc_info->devices = out->devices;
1180 uc_info->in_snd_device = SND_DEVICE_NONE;
1181 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001182
Eric Laurent07eeafd2013-10-06 12:52:49 -07001183 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001184 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Mingming Yin6c344432014-05-01 15:37:31 -07001185 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1186 if (!strncmp("true", prop_value, 4)) {
1187 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1188 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1189 check_and_set_hdmi_channels(adev, sink_channels);
1190 } else {
1191 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1192 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1193 else
1194 check_and_set_hdmi_channels(adev, out->config.channels);
1195 }
Mingming Yin10fef6a2013-11-26 17:17:01 -08001196 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001197
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001198 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001199
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001200 select_devices(adev, out->usecase);
1201
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001202 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1203 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001204 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301205 unsigned int flags = PCM_OUT;
1206 unsigned int pcm_open_retry_count = 0;
1207 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1208 flags |= PCM_MMAP | PCM_NOIRQ;
1209 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1210 } else
1211 flags |= PCM_MONOTONIC;
1212
1213 while (1) {
1214 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1215 flags, &out->config);
1216 if (out->pcm && !pcm_is_ready(out->pcm)) {
1217 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1218 if (out->pcm != NULL) {
1219 pcm_close(out->pcm);
1220 out->pcm = NULL;
1221 }
1222 if (pcm_open_retry_count-- == 0) {
1223 ret = -EIO;
1224 goto error_open;
1225 }
1226 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1227 continue;
1228 }
1229 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001230 }
1231 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001232 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001233 out->compr = compress_open(adev->snd_card,
1234 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001235 COMPRESS_IN, &out->compr_config);
1236 if (out->compr && !is_compress_ready(out->compr)) {
1237 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1238 compress_close(out->compr);
1239 out->compr = NULL;
1240 ret = -EIO;
1241 goto error_open;
1242 }
1243 if (out->offload_callback)
1244 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001245
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08001246#ifdef DS1_DOLBY_DDP_ENABLED
1247 if (audio_extn_is_dolby_format(out->format))
1248 audio_extn_dolby_send_ddp_endp_params(adev);
1249#endif
1250
Eric Laurentc4aef752013-09-12 17:45:53 -07001251 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001252 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1253 if (adev->offload_effects_start_output != NULL)
1254 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001255 }
Eric Laurent994a6932013-07-17 11:51:42 -07001256 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001257 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001258error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001259 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001260error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001261 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262}
1263
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001264static int check_input_parameters(uint32_t sample_rate,
1265 audio_format_t format,
1266 int channel_count)
1267{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001268 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001269
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001270 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001271 !voice_extn_compress_voip_is_format_supported(format) &&
1272 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001273
1274 switch (channel_count) {
1275 case 1:
1276 case 2:
1277 case 6:
1278 break;
1279 default:
1280 ret = -EINVAL;
1281 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001282
1283 switch (sample_rate) {
1284 case 8000:
1285 case 11025:
1286 case 12000:
1287 case 16000:
1288 case 22050:
1289 case 24000:
1290 case 32000:
1291 case 44100:
1292 case 48000:
1293 break;
1294 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001295 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001296 }
1297
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001298 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001299}
1300
1301static size_t get_input_buffer_size(uint32_t sample_rate,
1302 audio_format_t format,
1303 int channel_count)
1304{
1305 size_t size = 0;
1306
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001307 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1308 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001309
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001310 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1311 /* ToDo: should use frame_size computed based on the format and
1312 channel_count here. */
1313 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001314
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001315 /* make sure the size is multiple of 64 */
1316 size += 0x3f;
1317 size &= ~0x3f;
1318
1319 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001320}
1321
1322static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1323{
1324 struct stream_out *out = (struct stream_out *)stream;
1325
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001326 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001327}
1328
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301329static int out_set_sample_rate(struct audio_stream *stream __unused,
1330 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001331{
1332 return -ENOSYS;
1333}
1334
1335static size_t out_get_buffer_size(const struct audio_stream *stream)
1336{
1337 struct stream_out *out = (struct stream_out *)stream;
1338
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001339 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001340 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001341 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1342 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001343
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001344 return out->config.period_size * audio_stream_frame_size(stream);
1345}
1346
1347static uint32_t out_get_channels(const struct audio_stream *stream)
1348{
1349 struct stream_out *out = (struct stream_out *)stream;
1350
1351 return out->channel_mask;
1352}
1353
1354static audio_format_t out_get_format(const struct audio_stream *stream)
1355{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001356 struct stream_out *out = (struct stream_out *)stream;
1357
1358 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001359}
1360
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301361static int out_set_format(struct audio_stream *stream __unused,
1362 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001363{
1364 return -ENOSYS;
1365}
1366
1367static int out_standby(struct audio_stream *stream)
1368{
1369 struct stream_out *out = (struct stream_out *)stream;
1370 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001371
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301372 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1373 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001374 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1375 /* Ignore standby in case of voip call because the voip output
1376 * stream is closed in adev_close_output_stream()
1377 */
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301378 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001379 return 0;
1380 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001381
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001382 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001383 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001384 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001385 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001386 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1387 if (out->pcm) {
1388 pcm_close(out->pcm);
1389 out->pcm = NULL;
1390 }
1391 } else {
1392 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001393 out->gapless_mdata.encoder_delay = 0;
1394 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001395 if (out->compr != NULL) {
1396 compress_close(out->compr);
1397 out->compr = NULL;
1398 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001399 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001400 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001401 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001402 }
1403 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001404 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001405 return 0;
1406}
1407
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301408static int out_dump(const struct audio_stream *stream __unused,
1409 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001410{
1411 return 0;
1412}
1413
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001414static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1415{
1416 int ret = 0;
1417 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001418 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001419 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001420 tmp_mdata.encoder_delay = 0;
1421 tmp_mdata.encoder_padding = 0;
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001422
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001423 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001424 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001425 return -EINVAL;
1426 }
1427
Mingming Yinee733602014-04-03 17:47:22 -07001428#ifdef EXTN_OFFLOAD_ENABLED
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001429 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1430 if (ret >= 0) {
1431 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1432 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1433 ALOGV("ADTS format is set in offload mode");
1434 }
1435 out->send_new_metadata = 1;
1436 }
Mingming Yinee733602014-04-03 17:47:22 -07001437#endif
ApurupaPattapudaa708c2013-12-18 15:47:59 -08001438
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001439 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1440 if(ret >= 0)
1441 is_meta_data_params = true;
1442 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1443 if(ret >= 0 )
1444 is_meta_data_params = true;
1445 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1446 if(ret >= 0 )
1447 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001448 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1449 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001450 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001451 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001452 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001453 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1454 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001455 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001456 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001457 }
1458
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001459 if(!is_meta_data_params) {
1460 ALOGV("%s: Not gapless meta data params", __func__);
1461 return 0;
1462 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001463 out->gapless_mdata = tmp_mdata;
1464 out->send_new_metadata = 1;
1465 ALOGV("%s new encoder delay %u and padding %u", __func__,
1466 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1467
1468 return 0;
1469}
1470
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301471static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1472{
1473 return out == adev->primary_output || out == adev->voice_tx_output;
1474}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001475
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001476static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1477{
1478 struct stream_out *out = (struct stream_out *)stream;
1479 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001480 struct audio_usecase *usecase;
1481 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001482 struct str_parms *parms;
1483 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001484 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001485 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001486
sangwoobc677242013-08-08 16:53:43 +09001487 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001488 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001489 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001490 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1491 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001492 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001493 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001494 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001495
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001496 /*
Dhanalakshmi Siddani523ae412014-04-18 22:26:56 +05301497 * When HDMI cable is unplugged/usb hs is disconnected the
1498 * music playback is paused and the policy manager sends routing=0
1499 * But the audioflingercontinues to write data until standby time
1500 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001501 * Avoid this by routing audio to speaker until standby.
1502 */
Dhanalakshmi Siddani523ae412014-04-18 22:26:56 +05301503 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1504 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001505 val == AUDIO_DEVICE_NONE) {
1506 val = AUDIO_DEVICE_OUT_SPEAKER;
1507 }
1508
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301509 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1510 voice_is_in_call(adev) &&
1511 output_drives_call(adev, out)) {
1512 ret = voice_stop_call(adev);
1513 adev->current_call_output = NULL;
1514 }
1515
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001516 /*
1517 * select_devices() call below switches all the usecases on the same
1518 * backend to the new device. Refer to check_usecases_codec_backend() in
1519 * the select_devices(). But how do we undo this?
1520 *
1521 * For example, music playback is active on headset (deep-buffer usecase)
1522 * and if we go to ringtones and select a ringtone, low-latency usecase
1523 * will be started on headset+speaker. As we can't enable headset+speaker
1524 * and headset devices at the same time, select_devices() switches the music
1525 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1526 * So when the ringtone playback is completed, how do we undo the same?
1527 *
1528 * We are relying on the out_set_parameters() call on deep-buffer output,
1529 * once the ringtone playback is ended.
1530 * NOTE: We should not check if the current devices are same as new devices.
1531 * Because select_devices() must be called to switch back the music
1532 * playback to headset.
1533 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001534 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001535 out->devices = val;
1536
1537 if (!out->standby)
1538 select_devices(adev, out->usecase);
1539
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001540 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301541 output_drives_call(adev, out)) {
1542 adev->current_call_output = out;
1543 if (!voice_is_in_call(adev))
1544 ret = voice_start_call(adev);
1545 else
1546 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001547 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001548
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001549 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001550
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001551 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001552 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001553 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001554
1555 if (out == adev->primary_output) {
1556 pthread_mutex_lock(&adev->lock);
1557 audio_extn_set_parameters(adev, parms);
1558 pthread_mutex_unlock(&adev->lock);
1559 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001560 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001561 pthread_mutex_lock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001562 parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001563 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001564 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001565
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001566 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001567 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001568 return ret;
1569}
1570
1571static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1572{
1573 struct stream_out *out = (struct stream_out *)stream;
1574 struct str_parms *query = str_parms_create_str(keys);
1575 char *str;
1576 char value[256];
1577 struct str_parms *reply = str_parms_create();
1578 size_t i, j;
1579 int ret;
1580 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001581 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001582 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1583 if (ret >= 0) {
1584 value[0] = '\0';
1585 i = 0;
1586 while (out->supported_channel_masks[i] != 0) {
1587 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1588 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1589 if (!first) {
1590 strcat(value, "|");
1591 }
1592 strcat(value, out_channels_name_to_enum_table[j].name);
1593 first = false;
1594 break;
1595 }
1596 }
1597 i++;
1598 }
1599 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1600 str = str_parms_to_str(reply);
1601 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001602 voice_extn_out_get_parameters(out, query, reply);
1603 str = str_parms_to_str(reply);
1604 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella2e032352014-01-29 12:52:19 -08001605 free(str);
1606 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001607 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001608 }
1609 str_parms_destroy(query);
1610 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001611 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001612 return str;
1613}
1614
1615static uint32_t out_get_latency(const struct audio_stream_out *stream)
1616{
1617 struct stream_out *out = (struct stream_out *)stream;
1618
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001619 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1620 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1621
1622 return (out->config.period_count * out->config.period_size * 1000) /
1623 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001624}
1625
1626static int out_set_volume(struct audio_stream_out *stream, float left,
1627 float right)
1628{
Eric Laurenta9024de2013-04-04 09:19:12 -07001629 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001630 int volume[2];
1631
Eric Laurenta9024de2013-04-04 09:19:12 -07001632 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1633 /* only take left channel into account: the API is for stereo anyway */
1634 out->muted = (left == 0.0f);
1635 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001636 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001637 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001638 struct audio_device *adev = out->dev;
1639 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001640 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1641 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001642
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001643 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1644 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001645 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1646 if (!ctl) {
1647 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1648 __func__, mixer_ctl_name);
1649 return -EINVAL;
1650 }
1651 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1652 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1653 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1654 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001655 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001656
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001657 return -ENOSYS;
1658}
1659
1660static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1661 size_t bytes)
1662{
1663 struct stream_out *out = (struct stream_out *)stream;
1664 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001665 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001666
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001667 pthread_mutex_lock(&out->lock);
1668 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001669 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001670 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001671 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1672 ret = voice_extn_compress_voip_start_output_stream(out);
1673 else
1674 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001675 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001676 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001677 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001678 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001679 goto exit;
1680 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001681 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001682
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001683 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001684 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1685 if (out->send_new_metadata) {
1686 ALOGVV("send new gapless metadata");
1687 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1688 out->send_new_metadata = 0;
1689 }
1690
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001691 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001692 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001693 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001694 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1695 }
1696 if (!out->playback_started) {
1697 compress_start(out->compr);
1698 out->playback_started = 1;
1699 out->offload_state = OFFLOAD_STATE_PLAYING;
1700 }
1701 pthread_mutex_unlock(&out->lock);
1702 return ret;
1703 } else {
1704 if (out->pcm) {
1705 if (out->muted)
1706 memset((void *)buffer, 0, bytes);
1707 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05301708 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY)
1709 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
1710 else
1711 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001712 if (ret == 0)
1713 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001714 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001715 }
1716
1717exit:
1718 pthread_mutex_unlock(&out->lock);
1719
1720 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001721 if (out->pcm)
1722 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001723 out_standby(&out->stream.common);
1724 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1725 out_get_sample_rate(&out->stream.common));
1726 }
1727 return bytes;
1728}
1729
1730static int out_get_render_position(const struct audio_stream_out *stream,
1731 uint32_t *dsp_frames)
1732{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001733 struct stream_out *out = (struct stream_out *)stream;
1734 *dsp_frames = 0;
1735 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1736 pthread_mutex_lock(&out->lock);
1737 if (out->compr != NULL) {
1738 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1739 &out->sample_rate);
1740 ALOGVV("%s rendered frames %d sample_rate %d",
1741 __func__, *dsp_frames, out->sample_rate);
1742 }
1743 pthread_mutex_unlock(&out->lock);
1744 return 0;
1745 } else
1746 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001747}
1748
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301749static int out_add_audio_effect(const struct audio_stream *stream __unused,
1750 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001751{
1752 return 0;
1753}
1754
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301755static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1756 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001757{
1758 return 0;
1759}
1760
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301761static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1762 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001763{
1764 return -EINVAL;
1765}
1766
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001767static int out_get_presentation_position(const struct audio_stream_out *stream,
1768 uint64_t *frames, struct timespec *timestamp)
1769{
1770 struct stream_out *out = (struct stream_out *)stream;
1771 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001772 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001773
1774 pthread_mutex_lock(&out->lock);
1775
Eric Laurent949a0892013-09-20 09:20:13 -07001776 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1777 if (out->compr != NULL) {
1778 compress_get_tstamp(out->compr, &dsp_frames,
1779 &out->sample_rate);
1780 ALOGVV("%s rendered frames %ld sample_rate %d",
1781 __func__, dsp_frames, out->sample_rate);
1782 *frames = dsp_frames;
1783 ret = 0;
1784 /* this is the best we can do */
1785 clock_gettime(CLOCK_MONOTONIC, timestamp);
1786 }
1787 } else {
1788 if (out->pcm) {
1789 size_t avail;
1790 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1791 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001792 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001793 // This adjustment accounts for buffering after app processor.
1794 // It is based on estimated DSP latency per use case, rather than exact.
1795 signed_frames -=
1796 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1797
Eric Laurent949a0892013-09-20 09:20:13 -07001798 // It would be unusual for this value to be negative, but check just in case ...
1799 if (signed_frames >= 0) {
1800 *frames = signed_frames;
1801 ret = 0;
1802 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001803 }
1804 }
1805 }
1806
1807 pthread_mutex_unlock(&out->lock);
1808
1809 return ret;
1810}
1811
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001812static int out_set_callback(struct audio_stream_out *stream,
1813 stream_callback_t callback, void *cookie)
1814{
1815 struct stream_out *out = (struct stream_out *)stream;
1816
1817 ALOGV("%s", __func__);
1818 pthread_mutex_lock(&out->lock);
1819 out->offload_callback = callback;
1820 out->offload_cookie = cookie;
1821 pthread_mutex_unlock(&out->lock);
1822 return 0;
1823}
1824
1825static int out_pause(struct audio_stream_out* stream)
1826{
1827 struct stream_out *out = (struct stream_out *)stream;
1828 int status = -ENOSYS;
1829 ALOGV("%s", __func__);
1830 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1831 pthread_mutex_lock(&out->lock);
1832 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1833 status = compress_pause(out->compr);
1834 out->offload_state = OFFLOAD_STATE_PAUSED;
1835 }
1836 pthread_mutex_unlock(&out->lock);
1837 }
1838 return status;
1839}
1840
1841static int out_resume(struct audio_stream_out* stream)
1842{
1843 struct stream_out *out = (struct stream_out *)stream;
1844 int status = -ENOSYS;
1845 ALOGV("%s", __func__);
1846 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1847 status = 0;
1848 pthread_mutex_lock(&out->lock);
1849 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1850 status = compress_resume(out->compr);
1851 out->offload_state = OFFLOAD_STATE_PLAYING;
1852 }
1853 pthread_mutex_unlock(&out->lock);
1854 }
1855 return status;
1856}
1857
1858static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1859{
1860 struct stream_out *out = (struct stream_out *)stream;
1861 int status = -ENOSYS;
1862 ALOGV("%s", __func__);
1863 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1864 pthread_mutex_lock(&out->lock);
1865 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1866 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1867 else
1868 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1869 pthread_mutex_unlock(&out->lock);
1870 }
1871 return status;
1872}
1873
1874static int out_flush(struct audio_stream_out* stream)
1875{
1876 struct stream_out *out = (struct stream_out *)stream;
1877 ALOGV("%s", __func__);
1878 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1879 pthread_mutex_lock(&out->lock);
1880 stop_compressed_output_l(out);
1881 pthread_mutex_unlock(&out->lock);
1882 return 0;
1883 }
1884 return -ENOSYS;
1885}
1886
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001887/** audio_stream_in implementation **/
1888static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1889{
1890 struct stream_in *in = (struct stream_in *)stream;
1891
1892 return in->config.rate;
1893}
1894
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301895static int in_set_sample_rate(struct audio_stream *stream __unused,
1896 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001897{
1898 return -ENOSYS;
1899}
1900
1901static size_t in_get_buffer_size(const struct audio_stream *stream)
1902{
1903 struct stream_in *in = (struct stream_in *)stream;
1904
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001905 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1906 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001907 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1908 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001909
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001910 return in->config.period_size * audio_stream_frame_size(stream);
1911}
1912
1913static uint32_t in_get_channels(const struct audio_stream *stream)
1914{
1915 struct stream_in *in = (struct stream_in *)stream;
1916
1917 return in->channel_mask;
1918}
1919
1920static audio_format_t in_get_format(const struct audio_stream *stream)
1921{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001922 struct stream_in *in = (struct stream_in *)stream;
1923
1924 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001925}
1926
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301927static int in_set_format(struct audio_stream *stream __unused,
1928 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001929{
1930 return -ENOSYS;
1931}
1932
1933static int in_standby(struct audio_stream *stream)
1934{
1935 struct stream_in *in = (struct stream_in *)stream;
1936 struct audio_device *adev = in->dev;
1937 int status = 0;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301938 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1939 stream, in->usecase, use_case_table[in->usecase]);
1940
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001941
1942 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1943 /* Ignore standby in case of voip call because the voip input
1944 * stream is closed in adev_close_input_stream()
1945 */
1946 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1947 return status;
1948 }
1949
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001950 pthread_mutex_lock(&in->lock);
1951 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001952 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001953 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001954 if (in->pcm) {
1955 pcm_close(in->pcm);
1956 in->pcm = NULL;
1957 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001958 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001959 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001960 }
1961 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001962 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001963 return status;
1964}
1965
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05301966static int in_dump(const struct audio_stream *stream __unused,
1967 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001968{
1969 return 0;
1970}
1971
1972static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1973{
1974 struct stream_in *in = (struct stream_in *)stream;
1975 struct audio_device *adev = in->dev;
1976 struct str_parms *parms;
1977 char *str;
1978 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001979 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001980
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05301981 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001982 parms = str_parms_create_str(kvpairs);
1983
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001984 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001985 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001986
1987 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1988 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001989 val = atoi(value);
1990 /* no audio source uses val == 0 */
1991 if ((in->source != val) && (val != 0)) {
1992 in->source = val;
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08001993 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1994 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
1995 (voice_extn_compress_voip_is_format_supported(in->format)) &&
1996 (in->config.rate == 8000 || in->config.rate == 16000) &&
1997 (popcount(in->channel_mask) == 1)) {
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08001998 err = voice_extn_compress_voip_open_input_stream(in);
1999 if (err != 0) {
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002000 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella287b8162014-02-04 16:23:52 -08002001 __func__, err);
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002002 }
2003 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002004 }
2005 }
2006
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002007 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2008 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002009 val = atoi(value);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302010 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002011 in->device = val;
2012 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002013 if (!in->standby)
2014 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002015 }
2016 }
2017
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002018done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002019 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002020 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002021
2022 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002023 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002024 return ret;
2025}
2026
2027static char* in_get_parameters(const struct audio_stream *stream,
2028 const char *keys)
2029{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002030 struct stream_in *in = (struct stream_in *)stream;
2031 struct str_parms *query = str_parms_create_str(keys);
2032 char *str;
2033 char value[256];
2034 struct str_parms *reply = str_parms_create();
2035 ALOGV("%s: enter: keys - %s", __func__, keys);
2036
2037 voice_extn_in_get_parameters(in, query, reply);
2038
2039 str = str_parms_to_str(reply);
2040 str_parms_destroy(query);
2041 str_parms_destroy(reply);
2042
2043 ALOGV("%s: exit: returns - %s", __func__, str);
2044 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002045}
2046
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302047static int in_set_gain(struct audio_stream_in *stream __unused,
2048 float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002049{
2050 return 0;
2051}
2052
2053static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2054 size_t bytes)
2055{
2056 struct stream_in *in = (struct stream_in *)stream;
2057 struct audio_device *adev = in->dev;
2058 int i, ret = -1;
2059
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002060 pthread_mutex_lock(&in->lock);
2061 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002062 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002063 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2064 ret = voice_extn_compress_voip_start_input_stream(in);
2065 else
2066 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002067 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002068 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002069 goto exit;
2070 }
2071 in->standby = 0;
2072 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002073
2074 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002075 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
2076 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002077 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2078 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302079 else if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY)
2080 ret = pcm_mmap_read(in->pcm, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002081 else
2082 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002083 }
2084
2085 /*
2086 * Instead of writing zeroes here, we could trust the hardware
2087 * to always provide zeroes when muted.
2088 */
kunleiza1a9ee02014-04-24 18:46:22 +08002089 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002090 memset(buffer, 0, bytes);
2091
2092exit:
2093 pthread_mutex_unlock(&in->lock);
2094
2095 if (ret != 0) {
2096 in_standby(&in->stream.common);
2097 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
2098 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
2099 in_get_sample_rate(&in->stream.common));
2100 }
2101 return bytes;
2102}
2103
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302104static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002105{
2106 return 0;
2107}
2108
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002109static int add_remove_audio_effect(const struct audio_stream *stream,
2110 effect_handle_t effect,
2111 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002112{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002113 struct stream_in *in = (struct stream_in *)stream;
2114 int status = 0;
2115 effect_descriptor_t desc;
2116
2117 status = (*effect)->get_descriptor(effect, &desc);
2118 if (status != 0)
2119 return status;
2120
2121 pthread_mutex_lock(&in->lock);
2122 pthread_mutex_lock(&in->dev->lock);
2123 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2124 in->enable_aec != enable &&
2125 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2126 in->enable_aec = enable;
2127 if (!in->standby)
2128 select_devices(in->dev, in->usecase);
2129 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002130 if (in->enable_ns != enable &&
2131 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2132 in->enable_ns = enable;
2133 if (!in->standby)
2134 select_devices(in->dev, in->usecase);
2135 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002136 pthread_mutex_unlock(&in->dev->lock);
2137 pthread_mutex_unlock(&in->lock);
2138
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002139 return 0;
2140}
2141
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002142static int in_add_audio_effect(const struct audio_stream *stream,
2143 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002144{
Eric Laurent994a6932013-07-17 11:51:42 -07002145 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002146 return add_remove_audio_effect(stream, effect, true);
2147}
2148
2149static int in_remove_audio_effect(const struct audio_stream *stream,
2150 effect_handle_t effect)
2151{
Eric Laurent994a6932013-07-17 11:51:42 -07002152 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002153 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002154}
2155
2156static int adev_open_output_stream(struct audio_hw_device *dev,
2157 audio_io_handle_t handle,
2158 audio_devices_t devices,
2159 audio_output_flags_t flags,
2160 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302161 struct audio_stream_out **stream_out,
2162 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002163{
2164 struct audio_device *adev = (struct audio_device *)dev;
2165 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002166 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002167
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002168 *stream_out = NULL;
2169 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2170
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302171 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2172 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2173 devices, flags, &out->stream);
2174
2175
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002176 if (!out) {
2177 return -ENOMEM;
2178 }
2179
2180 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2181 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2182
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002183 if (devices == AUDIO_DEVICE_NONE)
2184 devices = AUDIO_DEVICE_OUT_SPEAKER;
2185
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002186 out->flags = flags;
2187 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002188 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002189 out->format = config->format;
2190 out->sample_rate = config->sample_rate;
2191 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2192 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002193 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002194
2195 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002196 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
Mingming Yinee733602014-04-03 17:47:22 -07002197 (
2198#ifdef AFE_PROXY_ENABLED
2199 out->devices & AUDIO_DEVICE_OUT_PROXY ||
2200#endif
2201 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)) {
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002202
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002203 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002204 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2205 ret = read_hdmi_channel_masks(out);
2206
Mingming Yinee733602014-04-03 17:47:22 -07002207#ifdef AFE_PROXY_ENABLED
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002208 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2209 ret = audio_extn_read_afe_proxy_channel_masks(out);
Mingming Yinee733602014-04-03 17:47:22 -07002210#endif
2211
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002212 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002213 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002214 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002215
2216 if (config->sample_rate == 0)
2217 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2218 if (config->channel_mask == 0)
2219 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2220
2221 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002222 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002223 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2224 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002225 out->config.rate = config->sample_rate;
2226 out->config.channels = popcount(out->channel_mask);
2227 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Mingming Yinee733602014-04-03 17:47:22 -07002228#ifdef COMPRESS_VOIP_ENABLED
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002229 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2230 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002231 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002232 ret = voice_extn_compress_voip_open_output_stream(out);
2233 if (ret != 0) {
2234 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2235 __func__, ret);
2236 goto error_open;
2237 }
Mingming Yinee733602014-04-03 17:47:22 -07002238#endif
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002239 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2240 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2241 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2242 ALOGE("%s: Unsupported Offload information", __func__);
2243 ret = -EINVAL;
2244 goto error_open;
2245 }
Mingming Yin90310102013-11-13 16:57:00 -08002246 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002247 !audio_extn_is_dolby_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002248 ALOGE("%s: Unsupported audio format", __func__);
2249 ret = -EINVAL;
2250 goto error_open;
2251 }
2252
2253 out->compr_config.codec = (struct snd_codec *)
2254 calloc(1, sizeof(struct snd_codec));
2255
2256 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2257 if (config->offload_info.channel_mask)
2258 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002259 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002260 out->channel_mask = config->channel_mask;
ApurupaPattapu0c566872014-01-10 14:46:02 -08002261 config->offload_info.channel_mask = config->channel_mask;
2262 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002263 out->format = config->offload_info.format;
2264 out->sample_rate = config->offload_info.sample_rate;
2265
2266 out->stream.set_callback = out_set_callback;
2267 out->stream.pause = out_pause;
2268 out->stream.resume = out_resume;
2269 out->stream.drain = out_drain;
2270 out->stream.flush = out_flush;
2271
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002272 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002273 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddye0a07122013-12-14 00:34:53 -08002274 audio_extn_dolby_get_snd_codec_id(adev, out,
2275 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002276 else
2277 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002278 get_snd_codec_id(config->offload_info.format);
Mingming Yinee733602014-04-03 17:47:22 -07002279#ifdef EXTN_OFFLOAD_ENABLED
ApurupaPattapu0c566872014-01-10 14:46:02 -08002280 if (audio_is_offload_pcm(config->offload_info.format)) {
2281 out->compr_config.fragment_size =
2282 platform_get_pcm_offload_buffer_size(&config->offload_info);
Mingming Yinee733602014-04-03 17:47:22 -07002283 } else
2284#endif
2285 {
ApurupaPattapu0c566872014-01-10 14:46:02 -08002286 out->compr_config.fragment_size =
2287 platform_get_compress_offload_buffer_size(&config->offload_info);
2288 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002289 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2290 out->compr_config.codec->sample_rate =
2291 compress_get_alsa_rate(config->offload_info.sample_rate);
2292 out->compr_config.codec->bit_rate =
2293 config->offload_info.bit_rate;
2294 out->compr_config.codec->ch_in =
2295 popcount(config->channel_mask);
2296 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
ApurupaPattapudaa708c2013-12-18 15:47:59 -08002297 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002298
Mingming Yinee733602014-04-03 17:47:22 -07002299#ifdef EXTN_OFFLOAD_ENABLED
ApurupaPattapu0c566872014-01-10 14:46:02 -08002300 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2301 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
2302 else if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
2303 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
Mingming Yinee733602014-04-03 17:47:22 -07002304#endif
ApurupaPattapu0c566872014-01-10 14:46:02 -08002305
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002306 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2307 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002308
2309 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002310 out->offload_state = OFFLOAD_STATE_IDLE;
2311 out->playback_started = 0;
2312
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002313 create_offload_callback_thread(out);
2314 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2315 __func__, config->offload_info.version,
2316 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002317 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly9d1632f2014-01-09 12:45:31 -08002318 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002319
Mingming Yinee733602014-04-03 17:47:22 -07002320#ifdef INCALL_MUSIC_ENABLED
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002321 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2322 ret = voice_check_and_set_incall_music_usecase(adev, out);
2323 if (ret != 0) {
2324 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2325 __func__, ret);
2326 goto error_open;
2327 }
Mingming Yinee733602014-04-03 17:47:22 -07002328#endif
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302329 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2330 if (config->sample_rate == 0)
2331 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2332 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2333 config->sample_rate != 8000) {
2334 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2335 ret = -EINVAL;
2336 goto error_open;
2337 }
2338 out->sample_rate = config->sample_rate;
2339 out->config.rate = config->sample_rate;
2340 if (config->format == AUDIO_FORMAT_DEFAULT)
2341 config->format = AUDIO_FORMAT_PCM_16_BIT;
2342 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2343 config->format = AUDIO_FORMAT_PCM_16_BIT;
2344 ret = -EINVAL;
2345 goto error_open;
2346 }
2347 out->format = config->format;
2348 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2349 out->config = pcm_config_afe_proxy_playback;
2350 adev->voice_tx_output = out;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002351 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002352 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2353 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002354 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002355 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002356 /* primary path is the default path selected if no other outputs are available/suitable */
2357 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002358 out->config = pcm_config_deep_buffer;
2359 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002360 }
2361
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002362 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2363 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2364 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002365 if(adev->primary_output == NULL)
2366 adev->primary_output = out;
2367 else {
2368 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002369 ret = -EEXIST;
2370 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002371 }
2372 }
2373
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002374 /* Check if this usecase is already existing */
2375 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella0d06c8e2014-04-17 20:00:41 -07002376 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2377 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002378 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002379 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002380 ret = -EEXIST;
2381 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002382 }
2383 pthread_mutex_unlock(&adev->lock);
2384
2385 out->stream.common.get_sample_rate = out_get_sample_rate;
2386 out->stream.common.set_sample_rate = out_set_sample_rate;
2387 out->stream.common.get_buffer_size = out_get_buffer_size;
2388 out->stream.common.get_channels = out_get_channels;
2389 out->stream.common.get_format = out_get_format;
2390 out->stream.common.set_format = out_set_format;
2391 out->stream.common.standby = out_standby;
2392 out->stream.common.dump = out_dump;
2393 out->stream.common.set_parameters = out_set_parameters;
2394 out->stream.common.get_parameters = out_get_parameters;
2395 out->stream.common.add_audio_effect = out_add_audio_effect;
2396 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2397 out->stream.get_latency = out_get_latency;
2398 out->stream.set_volume = out_set_volume;
2399 out->stream.write = out_write;
2400 out->stream.get_render_position = out_get_render_position;
2401 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002402 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002403
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002404 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002405 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002406 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002407
2408 config->format = out->stream.common.get_format(&out->stream.common);
2409 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2410 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2411
2412 *stream_out = &out->stream;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302413 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2414 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002415 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002416 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002417
2418error_open:
2419 free(out);
2420 *stream_out = NULL;
2421 ALOGD("%s: exit: ret %d", __func__, ret);
2422 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002423}
2424
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302425static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002426 struct audio_stream_out *stream)
2427{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002428 struct stream_out *out = (struct stream_out *)stream;
2429 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002430 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002431
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302432 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2433
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002434 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2435 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2436 if(ret != 0)
2437 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2438 __func__, ret);
2439 }
2440 else
2441 out_standby(&stream->common);
2442
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002443 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2444 destroy_offload_callback_thread(out);
2445
2446 if (out->compr_config.codec != NULL)
2447 free(out->compr_config.codec);
2448 }
2449 pthread_cond_destroy(&out->cond);
2450 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002451 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002452 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002453}
2454
2455static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2456{
2457 struct audio_device *adev = (struct audio_device *)dev;
2458 struct str_parms *parms;
2459 char *str;
2460 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002461 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002462 int ret = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002463
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002464 ALOGD("%s: enter: %s", __func__, kvpairs);
2465
2466 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002467 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002468
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002469 ret = voice_set_parameters(adev, parms);
2470 if (ret != 0)
2471 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002472
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002473 ret = platform_set_parameters(adev->platform, parms);
2474 if (ret != 0)
2475 goto done;
2476
2477 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2478 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002479 /* When set to false, HAL should disable EC and NS
2480 * But it is currently not supported.
2481 */
2482 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2483 adev->bluetooth_nrec = true;
2484 else
2485 adev->bluetooth_nrec = false;
2486 }
2487
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002488 err = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2489 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002490 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2491 adev->screen_off = false;
2492 else
2493 adev->screen_off = true;
2494 }
2495
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002496 err = str_parms_get_int(parms, "rotation", &val);
2497 if (err >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002498 bool reverse_speakers = false;
2499 switch(val) {
2500 // FIXME: note that the code below assumes that the speakers are in the correct placement
2501 // relative to the user when the device is rotated 90deg from its default rotation. This
2502 // assumption is device-specific, not platform-specific like this code.
2503 case 270:
2504 reverse_speakers = true;
2505 break;
2506 case 0:
2507 case 90:
2508 case 180:
2509 break;
2510 default:
2511 ALOGE("%s: unexpected rotation of %d", __func__, val);
2512 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002513 if (adev->speaker_lr_swap != reverse_speakers) {
2514 adev->speaker_lr_swap = reverse_speakers;
2515 // only update the selected device if there is active pcm playback
2516 struct audio_usecase *usecase;
2517 struct listnode *node;
2518 list_for_each(node, &adev->usecase_list) {
2519 usecase = node_to_item(node, struct audio_usecase, list);
2520 if (usecase->type == PCM_PLAYBACK) {
2521 select_devices(adev, usecase->id);
2522 break;
2523 }
2524 }
2525 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002526 }
2527
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002528 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002529
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002530done:
2531 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002532 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002533 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002534 return ret;
2535}
2536
2537static char* adev_get_parameters(const struct audio_hw_device *dev,
2538 const char *keys)
2539{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002540 struct audio_device *adev = (struct audio_device *)dev;
2541 struct str_parms *reply = str_parms_create();
2542 struct str_parms *query = str_parms_create_str(keys);
2543 char *str;
2544
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002545 pthread_mutex_lock(&adev->lock);
2546
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002547 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002548 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002549 platform_get_parameters(adev->platform, query, reply);
2550 str = str_parms_to_str(reply);
2551 str_parms_destroy(query);
2552 str_parms_destroy(reply);
2553
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002554 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002555 ALOGV("%s: exit: returns - %s", __func__, str);
2556 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002557}
2558
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302559static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002560{
2561 return 0;
2562}
2563
2564static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2565{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002566 int ret;
2567 struct audio_device *adev = (struct audio_device *)dev;
2568 pthread_mutex_lock(&adev->lock);
2569 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002570 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002571 pthread_mutex_unlock(&adev->lock);
2572 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002573}
2574
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302575static int adev_set_master_volume(struct audio_hw_device *dev __unused,
2576 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002577{
2578 return -ENOSYS;
2579}
2580
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302581static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2582 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002583{
2584 return -ENOSYS;
2585}
2586
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302587static int adev_set_master_mute(struct audio_hw_device *dev __unused,
2588 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002589{
2590 return -ENOSYS;
2591}
2592
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302593static int adev_get_master_mute(struct audio_hw_device *dev __unused,
2594 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002595{
2596 return -ENOSYS;
2597}
2598
2599static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2600{
2601 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002602 pthread_mutex_lock(&adev->lock);
2603 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002604 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002605 adev->mode = mode;
2606 }
2607 pthread_mutex_unlock(&adev->lock);
2608 return 0;
2609}
2610
2611static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2612{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002613 int ret;
2614
2615 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002616 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002617 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2618 pthread_mutex_unlock(&adev->lock);
2619
2620 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002621}
2622
2623static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2624{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002625 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002626 return 0;
2627}
2628
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302629static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002630 const struct audio_config *config)
2631{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002632 int channel_count = popcount(config->channel_mask);
2633
2634 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2635}
2636
2637static int adev_open_input_stream(struct audio_hw_device *dev,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302638 audio_io_handle_t handle __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002639 audio_devices_t devices,
2640 struct audio_config *config,
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302641 struct audio_stream_in **stream_in,
2642 audio_input_flags_t flags __unused,
2643 const char *address __unused,
2644 audio_source_t source __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002645{
2646 struct audio_device *adev = (struct audio_device *)dev;
2647 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002648 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002649 int channel_count = popcount(config->channel_mask);
2650
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302651
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002652 *stream_in = NULL;
2653 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2654 return -EINVAL;
2655
2656 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302657 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
2658 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2659 devices, &in->stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002660
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07002661 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2662
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002663 in->stream.common.get_sample_rate = in_get_sample_rate;
2664 in->stream.common.set_sample_rate = in_set_sample_rate;
2665 in->stream.common.get_buffer_size = in_get_buffer_size;
2666 in->stream.common.get_channels = in_get_channels;
2667 in->stream.common.get_format = in_get_format;
2668 in->stream.common.set_format = in_set_format;
2669 in->stream.common.standby = in_standby;
2670 in->stream.common.dump = in_dump;
2671 in->stream.common.set_parameters = in_set_parameters;
2672 in->stream.common.get_parameters = in_get_parameters;
2673 in->stream.common.add_audio_effect = in_add_audio_effect;
2674 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2675 in->stream.set_gain = in_set_gain;
2676 in->stream.read = in_read;
2677 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2678
2679 in->device = devices;
2680 in->source = AUDIO_SOURCE_DEFAULT;
2681 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002682 in->standby = 1;
2683 in->channel_mask = config->channel_mask;
2684
2685 /* Update config params with the requested sample rate and channels */
2686 in->usecase = USECASE_AUDIO_RECORD;
2687 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002688 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002689 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002690
Divya Narayanan Poojaryab9b1ef2014-09-12 15:52:36 +05302691 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
2692 if (config->sample_rate == 0)
2693 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2694 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2695 config->sample_rate != 8000) {
2696 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2697 ret = -EINVAL;
2698 goto err_open;
2699 }
2700 if (config->format == AUDIO_FORMAT_DEFAULT)
2701 config->format = AUDIO_FORMAT_PCM_16_BIT;
2702 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2703 config->format = AUDIO_FORMAT_PCM_16_BIT;
2704 ret = -EINVAL;
2705 goto err_open;
2706 }
2707 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
2708 in->config = pcm_config_afe_proxy_record;
2709 in->config.channels = channel_count;
2710 in->config.rate = config->sample_rate;
2711 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002712 if(audio_extn_ssr_get_enabled()) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302713 if(audio_extn_ssr_init(in)) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002714 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2715 ret = -EINVAL;
2716 goto err_open;
2717 }
2718 } else {
Mingming Yindaf9c542014-09-16 17:41:33 -07002719 ALOGW("%s: surround sound recording is not supported", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002720 }
Mingming Yine62d7842013-10-25 16:26:03 -07002721 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chellab0668ee2014-01-24 15:33:23 -08002722 audio_extn_compr_cap_format_supported(config->format) &&
2723 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302724 audio_extn_compr_cap_init(in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002725 } else {
2726 in->config.channels = channel_count;
2727 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2728 buffer_size = get_input_buffer_size(config->sample_rate,
2729 config->format,
2730 channel_count);
2731 in->config.period_size = buffer_size / frame_size;
2732 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002733
2734 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002735 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002736 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002737
2738err_open:
2739 free(in);
2740 *stream_in = NULL;
2741 return ret;
2742}
2743
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302744static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002745 struct audio_stream_in *stream)
2746{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002747 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002748 struct stream_in *in = (struct stream_in *)stream;
Sidipotu Ashoke4514fa2014-05-02 16:21:50 +05302749 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002750
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002751 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2752 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2753 if (ret != 0)
2754 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2755 __func__, ret);
2756 } else
2757 in_standby(&stream->common);
2758
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002759 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2760 audio_extn_ssr_deinit();
2761 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002762 free(stream);
2763
Mingming Yine62d7842013-10-25 16:26:03 -07002764 if(audio_extn_compr_cap_enabled() &&
2765 audio_extn_compr_cap_format_supported(in->config.format))
2766 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002767 return;
2768}
2769
Divya Narayanan Poojarybd9f33f2014-09-17 17:35:59 +05302770static int adev_dump(const audio_hw_device_t *device __unused,
2771 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002772{
2773 return 0;
2774}
2775
2776static int adev_close(hw_device_t *device)
2777{
2778 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002779
2780 if (!adev)
2781 return 0;
2782
2783 pthread_mutex_lock(&adev_init_lock);
2784
2785 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002786 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07002787 audio_route_free(adev->audio_route);
2788 free(adev->snd_dev_ref_cnt);
2789 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002790 free(device);
2791 adev = NULL;
2792 }
2793 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002794 return 0;
2795}
2796
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002797static int adev_open(const hw_module_t *module, const char *name,
2798 hw_device_t **device)
2799{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002800 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002801
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002802 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002803 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2804
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002805 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002806 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002807 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002808 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002809 ALOGD("%s: returning existing instance of adev", __func__);
2810 ALOGD("%s: exit", __func__);
2811 pthread_mutex_unlock(&adev_init_lock);
2812 return 0;
2813 }
2814
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002815 adev = calloc(1, sizeof(struct audio_device));
2816
Ravi Kumar Alamanda33de8142014-04-24 10:34:41 -07002817 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2818
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002819 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2820 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2821 adev->device.common.module = (struct hw_module_t *)module;
2822 adev->device.common.close = adev_close;
2823
2824 adev->device.init_check = adev_init_check;
2825 adev->device.set_voice_volume = adev_set_voice_volume;
2826 adev->device.set_master_volume = adev_set_master_volume;
2827 adev->device.get_master_volume = adev_get_master_volume;
2828 adev->device.set_master_mute = adev_set_master_mute;
2829 adev->device.get_master_mute = adev_get_master_mute;
2830 adev->device.set_mode = adev_set_mode;
2831 adev->device.set_mic_mute = adev_set_mic_mute;
2832 adev->device.get_mic_mute = adev_get_mic_mute;
2833 adev->device.set_parameters = adev_set_parameters;
2834 adev->device.get_parameters = adev_get_parameters;
2835 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2836 adev->device.open_output_stream = adev_open_output_stream;
2837 adev->device.close_output_stream = adev_close_output_stream;
2838 adev->device.open_input_stream = adev_open_input_stream;
2839 adev->device.close_input_stream = adev_close_input_stream;
2840 adev->device.dump = adev_dump;
2841
2842 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002843 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002844 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002845 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002846 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002847 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002848 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002849 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002850 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002851 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002852 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly9b7e96b2014-02-14 14:45:49 -08002853 adev->cur_wfd_channels = 2;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002854
2855 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002856 adev->platform = platform_init(adev);
2857 if (!adev->platform) {
2858 free(adev->snd_dev_ref_cnt);
2859 free(adev);
2860 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2861 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08002862 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07002863 return -EINVAL;
2864 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002865
2866 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2867 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2868 if (adev->visualizer_lib == NULL) {
2869 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2870 } else {
2871 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2872 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002873 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002874 "visualizer_hal_start_output");
2875 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002876 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002877 "visualizer_hal_stop_output");
2878 }
2879 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08002880 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07002881
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002882 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2883 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2884 if (adev->offload_effects_lib == NULL) {
2885 ALOGE("%s: DLOPEN failed for %s", __func__,
2886 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2887 } else {
2888 ALOGV("%s: DLOPEN successful for %s", __func__,
2889 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2890 adev->offload_effects_start_output =
2891 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2892 "offload_effects_bundle_hal_start_output");
2893 adev->offload_effects_stop_output =
2894 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2895 "offload_effects_bundle_hal_stop_output");
2896 }
2897 }
2898
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002899 *device = &adev->device.common;
2900
Kiran Kandi910e1862013-10-29 13:29:42 -07002901 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002902 pthread_mutex_unlock(&adev_init_lock);
2903
Eric Laurent994a6932013-07-17 11:51:42 -07002904 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002905 return 0;
2906}
2907
2908static struct hw_module_methods_t hal_module_methods = {
2909 .open = adev_open,
2910};
2911
2912struct audio_module HAL_MODULE_INFO_SYM = {
2913 .common = {
2914 .tag = HARDWARE_MODULE_TAG,
2915 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2916 .hal_api_version = HARDWARE_HAL_API_VERSION,
2917 .id = AUDIO_HARDWARE_MODULE_ID,
2918 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08002919 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002920 .methods = &hal_module_methods,
2921 },
2922};