blob: 6b056ce5c89995bc04efc9c91932b87827814aae [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"
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080056#include "sound/asound.h"
ApurupaPattapu2e084df2013-12-18 15:47:59 -080057
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
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080063
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080064#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
65
Eric Laurentb23d5282013-05-14 15:27:20 -070066struct pcm_config pcm_config_deep_buffer = {
67 .channels = 2,
68 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
69 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
70 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
71 .format = PCM_FORMAT_S16_LE,
72 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
73 .stop_threshold = INT_MAX,
74 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
75};
76
77struct pcm_config pcm_config_low_latency = {
78 .channels = 2,
79 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
80 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
81 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
82 .format = PCM_FORMAT_S16_LE,
83 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
84 .stop_threshold = INT_MAX,
85 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
86};
87
88struct pcm_config pcm_config_hdmi_multi = {
89 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
90 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
91 .period_size = HDMI_MULTI_PERIOD_SIZE,
92 .period_count = HDMI_MULTI_PERIOD_COUNT,
93 .format = PCM_FORMAT_S16_LE,
94 .start_threshold = 0,
95 .stop_threshold = INT_MAX,
96 .avail_min = 0,
97};
98
99struct pcm_config pcm_config_audio_capture = {
100 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700101 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
102 .format = PCM_FORMAT_S16_LE,
103};
104
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800105const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700106 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
107 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
108 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700109 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700110#ifdef MULTIPLE_OFFLOAD_ENABLED
111 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
112 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
113 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
114 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
115 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
116 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
117 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
118 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
119#endif
Eric Laurentb23d5282013-05-14 15:27:20 -0700120 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700121 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700122 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700123 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700124 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800125 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800126 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700127 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700128
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700129 [USECASE_VOICE2_CALL] = "voice2-call",
130 [USECASE_VOLTE_CALL] = "volte-call",
131 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800132 [USECASE_VOWLAN_CALL] = "vowlan-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800133 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700134 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
135 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
136 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800137 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
138 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
139 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
140
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700141 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
142 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700143 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
144 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700145};
146
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700147static const audio_usecase_t offload_usecases[] = {
148 USECASE_AUDIO_PLAYBACK_OFFLOAD,
149#ifdef MULTIPLE_OFFLOAD_ENABLED
150 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
151 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
152 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
153 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
154 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
155 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
156 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
157 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
158#endif
159};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800160
161#define STRING_TO_ENUM(string) { #string, string }
162
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163struct string_to_enum {
164 const char *name;
165 uint32_t value;
166};
167
168static const struct string_to_enum out_channels_name_to_enum_table[] = {
169 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
170 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
171 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
172};
173
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700174static struct audio_device *adev = NULL;
175static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700176static unsigned int audio_device_ref_count;
177
Haynes Mathew George5191a852013-09-11 14:19:36 -0700178static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800179
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800180static int check_and_set_gapless_mode(struct audio_device *adev) {
181
182
183 char value[PROPERTY_VALUE_MAX] = {0};
184 bool gapless_enabled = false;
185 const char *mixer_ctl_name = "Compress Gapless Playback";
186 struct mixer_ctl *ctl;
187
188 ALOGV("%s:", __func__);
189 property_get("audio.offload.gapless.enabled", value, NULL);
190 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
191
192 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
193 if (!ctl) {
194 ALOGE("%s: Could not get ctl for mixer cmd - %s",
195 __func__, mixer_ctl_name);
196 return -EINVAL;
197 }
198
199 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
200 ALOGE("%s: Could not set gapless mode %d",
201 __func__, gapless_enabled);
202 return -EINVAL;
203 }
204 return 0;
205}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700206
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700207static bool is_supported_format(audio_format_t format)
208{
Eric Laurent86e17132013-09-12 17:49:30 -0700209 if (format == AUDIO_FORMAT_MP3 ||
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800210 format == AUDIO_FORMAT_AAC ||
211 format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD ||
212 format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
213 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700214
215 return false;
216}
217
218static int get_snd_codec_id(audio_format_t format)
219{
220 int id = 0;
221
222 switch (format) {
223 case AUDIO_FORMAT_MP3:
224 id = SND_AUDIOCODEC_MP3;
225 break;
226 case AUDIO_FORMAT_AAC:
227 id = SND_AUDIOCODEC_AAC;
228 break;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800229 case AUDIO_FORMAT_PCM_16_BIT_OFFLOAD:
230 case AUDIO_FORMAT_PCM_24_BIT_OFFLOAD:
231 id = SND_AUDIOCODEC_PCM;
232 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700233 default:
Mingming Yin90310102013-11-13 16:57:00 -0800234 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700235 }
236
237 return id;
238}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800239
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700240int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700241 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800242{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700243 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700244 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800245
246 if (usecase == NULL)
247 return -EINVAL;
248
249 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
250
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800251 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700252 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800253 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700254 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800255
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -0800256#ifdef DS1_DOLBY_DAP_ENABLED
257 audio_extn_dolby_set_dmid(adev);
258 audio_extn_dolby_set_endpoint(adev);
259#endif
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800260 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700261 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700262 ALOGV("%s: apply mixer and update path: %s", __func__, mixer_path);
263 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800264 ALOGV("%s: exit", __func__);
265 return 0;
266}
267
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700268int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700269 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800270{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700271 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700272 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800273
274 if (usecase == NULL)
275 return -EINVAL;
276
277 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700278 if (usecase->type == PCM_CAPTURE)
279 snd_device = usecase->in_snd_device;
280 else
281 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800282 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700283 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700284 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
285 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800286 ALOGV("%s: exit", __func__);
287 return 0;
288}
289
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700290int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700291 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800292{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700293 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
294
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800295 if (snd_device < SND_DEVICE_MIN ||
296 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800297 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800298 return -EINVAL;
299 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700300
301 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700302
303 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
304 ALOGE("%s: Invalid sound device returned", __func__);
305 return -EINVAL;
306 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700307 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700308 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700309 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700310 return 0;
311 }
312
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700313 /* start usb playback thread */
314 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
315 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
316 audio_extn_usb_start_playback(adev);
317
318 /* start usb capture thread */
319 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
320 audio_extn_usb_start_capture(adev);
321
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800322 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
323 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700324 audio_extn_spkr_prot_is_enabled()) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800325 if (audio_extn_spkr_prot_start_processing(snd_device)) {
326 ALOGE("%s: spkr_start_processing failed", __func__);
327 return -EINVAL;
328 }
329 } else {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700330 ALOGV("%s: snd_device(%d: %s)", __func__,
331 snd_device, device_name);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -0700332 /* due to the possibility of calibration overwrite between listen
333 and audio, notify listen hal before audio calibration is sent */
334 audio_extn_listen_update_status(snd_device,
335 LISTEN_EVENT_SND_DEVICE_BUSY);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700336 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
337 adev->snd_dev_ref_cnt[snd_device]--;
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -0700338 audio_extn_listen_update_status(snd_device,
339 LISTEN_EVENT_SND_DEVICE_FREE);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700340 return -EINVAL;
341 }
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700342 audio_route_apply_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800343 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800344 return 0;
345}
346
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700347int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700348 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800349{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700350 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
351
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800352 if (snd_device < SND_DEVICE_MIN ||
353 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800354 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800355 return -EINVAL;
356 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700357 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
358 ALOGE("%s: device ref cnt is already 0", __func__);
359 return -EINVAL;
360 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700361
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700362 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700363
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700364 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
365 ALOGE("%s: Invalid sound device returned", __func__);
366 return -EINVAL;
367 }
368
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700369 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700370 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700371 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800372 /* exit usb play back thread */
373 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
374 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
375 audio_extn_usb_stop_playback();
376
377 /* exit usb capture thread */
378 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
379 audio_extn_usb_stop_capture(adev);
380
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800381 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
382 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700383 audio_extn_spkr_prot_is_enabled()) {
384 audio_extn_spkr_prot_stop_processing();
385 } else
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700386 audio_route_reset_and_update_path(adev->audio_route, device_name);
Kiran Kandide144c82013-11-20 15:58:32 -0800387
388 audio_extn_listen_update_status(snd_device,
389 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700390 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700391
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800392 return 0;
393}
394
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700395static void check_usecases_codec_backend(struct audio_device *adev,
396 struct audio_usecase *uc_info,
397 snd_device_t snd_device)
398{
399 struct listnode *node;
400 struct audio_usecase *usecase;
401 bool switch_device[AUDIO_USECASE_MAX];
402 int i, num_uc_to_switch = 0;
403
404 /*
405 * This function is to make sure that all the usecases that are active on
406 * the hardware codec backend are always routed to any one device that is
407 * handled by the hardware codec.
408 * For example, if low-latency and deep-buffer usecases are currently active
409 * on speaker and out_set_parameters(headset) is received on low-latency
410 * output, then we have to make sure deep-buffer is also switched to headset,
411 * because of the limitation that both the devices cannot be enabled
412 * at the same time as they share the same backend.
413 */
414 /* Disable all the usecases on the shared backend other than the
415 specified usecase */
416 for (i = 0; i < AUDIO_USECASE_MAX; i++)
417 switch_device[i] = false;
418
419 list_for_each(node, &adev->usecase_list) {
420 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800421 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700422 usecase != uc_info &&
423 usecase->out_snd_device != snd_device &&
424 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
425 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
426 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700427 platform_get_snd_device_name(usecase->out_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700428 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700429 switch_device[usecase->id] = true;
430 num_uc_to_switch++;
431 }
432 }
433
434 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700435 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700436
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530437 /* Make sure the previous devices to be disabled first and then enable the
438 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700439 list_for_each(node, &adev->usecase_list) {
440 usecase = node_to_item(node, struct audio_usecase, list);
441 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700442 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700443 }
444 }
445
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700446 list_for_each(node, &adev->usecase_list) {
447 usecase = node_to_item(node, struct audio_usecase, list);
448 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700449 enable_snd_device(adev, snd_device);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700450 }
451 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700452
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700453 /* Re-route all the usecases on the shared backend other than the
454 specified usecase to new snd devices */
455 list_for_each(node, &adev->usecase_list) {
456 usecase = node_to_item(node, struct audio_usecase, list);
457 /* Update the out_snd_device only before enabling the audio route */
458 if (switch_device[usecase->id] ) {
459 usecase->out_snd_device = snd_device;
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700460 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700461 }
462 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700463 }
464}
465
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700466static void check_and_route_capture_usecases(struct audio_device *adev,
467 struct audio_usecase *uc_info,
468 snd_device_t snd_device)
469{
470 struct listnode *node;
471 struct audio_usecase *usecase;
472 bool switch_device[AUDIO_USECASE_MAX];
473 int i, num_uc_to_switch = 0;
474
475 /*
476 * This function is to make sure that all the active capture usecases
477 * are always routed to the same input sound device.
478 * For example, if audio-record and voice-call usecases are currently
479 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
480 * is received for voice call then we have to make sure that audio-record
481 * usecase is also switched to earpiece i.e. voice-dmic-ef,
482 * because of the limitation that two devices cannot be enabled
483 * at the same time if they share the same backend.
484 */
485 for (i = 0; i < AUDIO_USECASE_MAX; i++)
486 switch_device[i] = false;
487
488 list_for_each(node, &adev->usecase_list) {
489 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800490 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700491 usecase != uc_info &&
492 usecase->in_snd_device != snd_device) {
493 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
494 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700495 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700496 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700497 switch_device[usecase->id] = true;
498 num_uc_to_switch++;
499 }
500 }
501
502 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700503 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700504
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530505 /* Make sure the previous devices to be disabled first and then enable the
506 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700507 list_for_each(node, &adev->usecase_list) {
508 usecase = node_to_item(node, struct audio_usecase, list);
509 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700510 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800511 }
512 }
513
514 list_for_each(node, &adev->usecase_list) {
515 usecase = node_to_item(node, struct audio_usecase, list);
516 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700517 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700518 }
519 }
520
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700521 /* Re-route all the usecases on the shared backend other than the
522 specified usecase to new snd devices */
523 list_for_each(node, &adev->usecase_list) {
524 usecase = node_to_item(node, struct audio_usecase, list);
525 /* Update the in_snd_device only before enabling the audio route */
526 if (switch_device[usecase->id] ) {
527 usecase->in_snd_device = snd_device;
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700528 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700529 }
530 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700531 }
532}
533
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800534/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700535static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800536{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700537 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700538 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800539
540 switch (channels) {
541 /*
542 * Do not handle stereo output in Multi-channel cases
543 * Stereo case is handled in normal playback path
544 */
545 case 6:
546 ALOGV("%s: HDMI supports 5.1", __func__);
547 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
548 break;
549 case 8:
550 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
551 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
552 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
553 break;
554 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700555 ALOGE("HDMI does not support multi channel playback");
556 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800557 break;
558 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700559 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800560}
561
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700562static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
563{
564 struct audio_usecase *usecase;
565 struct listnode *node;
566
567 list_for_each(node, &adev->usecase_list) {
568 usecase = node_to_item(node, struct audio_usecase, list);
569 if (usecase->type == VOICE_CALL) {
570 ALOGV("%s: usecase id %d", __func__, usecase->id);
571 return usecase->id;
572 }
573 }
574 return USECASE_INVALID;
575}
576
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700577struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700578 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700579{
580 struct audio_usecase *usecase;
581 struct listnode *node;
582
583 list_for_each(node, &adev->usecase_list) {
584 usecase = node_to_item(node, struct audio_usecase, list);
585 if (usecase->id == uc_id)
586 return usecase;
587 }
588 return NULL;
589}
590
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700591int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800592{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800593 snd_device_t out_snd_device = SND_DEVICE_NONE;
594 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700595 struct audio_usecase *usecase = NULL;
596 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800597 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800598 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800599 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800600 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700601 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800602
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700603 usecase = get_usecase_from_list(adev, uc_id);
604 if (usecase == NULL) {
605 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
606 return -EINVAL;
607 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800608
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800609 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800610 (usecase->type == VOIP_CALL) ||
611 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700612 out_snd_device = platform_get_output_snd_device(adev->platform,
613 usecase->stream.out->devices);
614 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700615 usecase->devices = usecase->stream.out->devices;
616 } else {
617 /*
618 * If the voice call is active, use the sound devices of voice call usecase
619 * so that it would not result any device switch. All the usecases will
620 * be switched to new device when select_devices() is called for voice call
621 * usecase. This is to avoid switching devices for voice call when
622 * check_usecases_codec_backend() is called below.
623 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700624 if (voice_is_in_call(adev)) {
625 vc_usecase = get_usecase_from_list(adev,
626 get_voice_usecase_id_from_list(adev));
Helen Zeng067b96b2013-11-26 12:10:29 -0800627 if ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
628 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700629 in_snd_device = vc_usecase->in_snd_device;
630 out_snd_device = vc_usecase->out_snd_device;
631 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800632 } else if (voice_extn_compress_voip_is_active(adev)) {
633 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Narsinga Rao Chellac17cab62014-04-11 17:20:46 -0700634 if ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
635 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800636 in_snd_device = voip_usecase->in_snd_device;
637 out_snd_device = voip_usecase->out_snd_device;
638 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800639 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800640 hfp_ucid = audio_extn_hfp_get_usecase();
641 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800642 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
643 in_snd_device = hfp_usecase->in_snd_device;
644 out_snd_device = hfp_usecase->out_snd_device;
645 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700646 }
647 if (usecase->type == PCM_PLAYBACK) {
648 usecase->devices = usecase->stream.out->devices;
649 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700650 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700651 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700652 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700653 if (usecase->stream.out == adev->primary_output &&
654 adev->active_input &&
655 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
656 select_devices(adev, adev->active_input->usecase);
657 }
658 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700659 } else if (usecase->type == PCM_CAPTURE) {
660 usecase->devices = usecase->stream.in->device;
661 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700662 if (in_snd_device == SND_DEVICE_NONE) {
663 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
664 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700665 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700666 adev->primary_output->devices);
667 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700668 in_snd_device = platform_get_input_snd_device(adev->platform,
669 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700670 }
671 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700672 }
673 }
674
675 if (out_snd_device == usecase->out_snd_device &&
676 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800677 return 0;
678 }
679
sangwoobc677242013-08-08 16:53:43 +0900680 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700681 out_snd_device, platform_get_snd_device_name(out_snd_device),
682 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800683
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800684 /*
685 * Limitation: While in call, to do a device switch we need to disable
686 * and enable both RX and TX devices though one of them is same as current
687 * device.
688 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800689 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700690 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800691 }
692
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700693 /* Disable current sound devices */
694 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700695 disable_audio_route(adev, usecase);
696 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800697 }
698
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700699 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700700 disable_audio_route(adev, usecase);
701 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800702 }
703
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800704 /* Applicable only on the targets that has external modem.
705 * New device information should be sent to modem before enabling
706 * the devices to reduce in-call device switch time.
707 */
708 if (usecase->type == VOICE_CALL)
709 status = platform_switch_voice_call_enable_device_config(adev->platform,
710 out_snd_device,
711 in_snd_device);
712
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700713 /* Enable new sound devices */
714 if (out_snd_device != SND_DEVICE_NONE) {
715 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
716 check_usecases_codec_backend(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700717 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800718 }
719
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700720 if (in_snd_device != SND_DEVICE_NONE) {
721 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700722 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700723 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700724
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_post(adev->platform,
727 out_snd_device,
728 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800729
sangwoo170731f2013-06-08 15:36:36 +0900730 usecase->in_snd_device = in_snd_device;
731 usecase->out_snd_device = out_snd_device;
732
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700733 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900734
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800735 /* Applicable only on the targets that has external modem.
736 * Enable device command should be sent to modem only after
737 * enabling voice call mixer controls
738 */
739 if (usecase->type == VOICE_CALL)
740 status = platform_switch_voice_call_usecase_route_post(adev->platform,
741 out_snd_device,
742 in_snd_device);
743
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530744 ALOGD("%s: done",__func__);
745
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800746 return status;
747}
748
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800749static int stop_input_stream(struct stream_in *in)
750{
751 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800752 struct audio_usecase *uc_info;
753 struct audio_device *adev = in->dev;
754
Eric Laurentc8400632013-02-14 19:04:54 -0800755 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800756
Eric Laurent994a6932013-07-17 11:51:42 -0700757 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700758 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800759 uc_info = get_usecase_from_list(adev, in->usecase);
760 if (uc_info == NULL) {
761 ALOGE("%s: Could not find the usecase (%d) in the list",
762 __func__, in->usecase);
763 return -EINVAL;
764 }
765
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800766 /* Close in-call recording streams */
767 voice_check_and_stop_incall_rec_usecase(adev, in);
768
Eric Laurent150dbfe2013-02-27 14:31:02 -0800769 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700770 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700771
772 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700773 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800774
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800775 list_remove(&uc_info->list);
776 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800777
Eric Laurent994a6932013-07-17 11:51:42 -0700778 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800779 return ret;
780}
781
782int start_input_stream(struct stream_in *in)
783{
784 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800785 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800786 struct audio_usecase *uc_info;
787 struct audio_device *adev = in->dev;
788
Mingming Yine62d7842013-10-25 16:26:03 -0700789 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530790 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
791 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700792
793 /* Check if source matches incall recording usecase criteria */
794 ret = voice_check_and_set_incall_rec_usecase(adev, in);
795 if (ret)
796 goto error_config;
797 else
798 ALOGV("%s: usecase(%d)", __func__, in->usecase);
799
Eric Laurentb23d5282013-05-14 15:27:20 -0700800 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800801 if (in->pcm_device_id < 0) {
802 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
803 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800804 ret = -EINVAL;
805 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800806 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700807
808 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800809 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
810 uc_info->id = in->usecase;
811 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800812 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700813 uc_info->devices = in->device;
814 uc_info->in_snd_device = SND_DEVICE_NONE;
815 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800816
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800817 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700818 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800819
Eric Laurentc8400632013-02-14 19:04:54 -0800820 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800821 __func__, adev->snd_card,
822 in->pcm_device_id, in->config.channels);
823 in->pcm = pcm_open(adev->snd_card,
824 in->pcm_device_id, PCM_IN, &in->config);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800825 if (in->pcm && !pcm_is_ready(in->pcm)) {
826 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
827 pcm_close(in->pcm);
828 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800829 ret = -EIO;
830 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800831 }
Eric Laurent994a6932013-07-17 11:51:42 -0700832 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800833 return ret;
834
835error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800836 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800837
838error_config:
839 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700840 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800841
842 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800843}
844
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700845/* must be called with out->lock locked */
846static int send_offload_cmd_l(struct stream_out* out, int command)
847{
848 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
849
850 ALOGVV("%s %d", __func__, command);
851
852 cmd->cmd = command;
853 list_add_tail(&out->offload_cmd_list, &cmd->node);
854 pthread_cond_signal(&out->offload_cond);
855 return 0;
856}
857
858/* must be called iwth out->lock locked */
859static void stop_compressed_output_l(struct stream_out *out)
860{
861 out->offload_state = OFFLOAD_STATE_IDLE;
862 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700863 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700864 if (out->compr != NULL) {
865 compress_stop(out->compr);
866 while (out->offload_thread_blocked) {
867 pthread_cond_wait(&out->cond, &out->lock);
868 }
869 }
870}
871
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700872bool is_offload_usecase(audio_usecase_t uc_id)
873{
874 unsigned int i;
875 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
876 if (uc_id == offload_usecases[i])
877 return true;
878 }
879 return false;
880}
881
882static audio_usecase_t get_offload_usecase(struct audio_device *adev)
883{
884 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
885 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
886 char value[PROPERTY_VALUE_MAX] = {0};
887
888 property_get("audio.offload.multiple.enabled", value, NULL);
889 if (!(atoi(value) || !strncmp("true", value, 4)))
890 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
891
892 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
893 for (i = 0; i < num_usecase; i++) {
894 if (!(adev->offload_usecases_state & (0x1<<i))) {
895 adev->offload_usecases_state |= 0x1 << i;
896 ret = offload_usecases[i];
897 break;
898 }
899 }
900 ALOGV("%s: offload usecase is %d", __func__, ret);
901 return ret;
902}
903
904static void free_offload_usecase(struct audio_device *adev,
905 audio_usecase_t uc_id)
906{
907 unsigned int i;
908 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
909 if (offload_usecases[i] == uc_id) {
910 adev->offload_usecases_state &= ~(0x1<<i);
911 break;
912 }
913 }
914 ALOGV("%s: free offload usecase %d", __func__, uc_id);
915}
916
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700917static void *offload_thread_loop(void *context)
918{
919 struct stream_out *out = (struct stream_out *) context;
920 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -0800921 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700922
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700923 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
924 set_sched_policy(0, SP_FOREGROUND);
925 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
926
927 ALOGV("%s", __func__);
928 pthread_mutex_lock(&out->lock);
929 for (;;) {
930 struct offload_cmd *cmd = NULL;
931 stream_callback_event_t event;
932 bool send_callback = false;
933
934 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
935 __func__, list_empty(&out->offload_cmd_list),
936 out->offload_state);
937 if (list_empty(&out->offload_cmd_list)) {
938 ALOGV("%s SLEEPING", __func__);
939 pthread_cond_wait(&out->offload_cond, &out->lock);
940 ALOGV("%s RUNNING", __func__);
941 continue;
942 }
943
944 item = list_head(&out->offload_cmd_list);
945 cmd = node_to_item(item, struct offload_cmd, node);
946 list_remove(item);
947
948 ALOGVV("%s STATE %d CMD %d out->compr %p",
949 __func__, out->offload_state, cmd->cmd, out->compr);
950
951 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
952 free(cmd);
953 break;
954 }
955
956 if (out->compr == NULL) {
957 ALOGE("%s: Compress handle is NULL", __func__);
958 pthread_cond_signal(&out->cond);
959 continue;
960 }
961 out->offload_thread_blocked = true;
962 pthread_mutex_unlock(&out->lock);
963 send_callback = false;
964 switch(cmd->cmd) {
965 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Sidipotu Ashok55820562014-02-10 16:16:38 +0530966 ALOGD("copl(%x):calling compress_wait", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700967 compress_wait(out->compr, -1);
Sidipotu Ashok55820562014-02-10 16:16:38 +0530968 ALOGD("copl(%x):out of compress_wait", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700969 send_callback = true;
970 event = STREAM_CBK_EVENT_WRITE_READY;
971 break;
972 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -0800973 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +0530974 if(ret == 0) {
975 ALOGD("copl(%x):calling compress_partial_drain", (unsigned int)out);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -0800976 compress_partial_drain(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +0530977 ALOGD("copl(%x):out of compress_partial_drain", (unsigned int)out);
978 }
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -0800979 else if(ret == -ETIMEDOUT)
980 compress_drain(out->compr);
981 else
982 ALOGE("%s: Next track returned error %d",__func__, ret);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700983 send_callback = true;
984 event = STREAM_CBK_EVENT_DRAIN_READY;
985 break;
986 case OFFLOAD_CMD_DRAIN:
Sidipotu Ashok55820562014-02-10 16:16:38 +0530987 ALOGD("copl(%x):calling compress_drain", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700988 compress_drain(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +0530989 ALOGD("copl(%x):calling compress_drain", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700990 send_callback = true;
991 event = STREAM_CBK_EVENT_DRAIN_READY;
992 break;
993 default:
994 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
995 break;
996 }
997 pthread_mutex_lock(&out->lock);
998 out->offload_thread_blocked = false;
999 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001000 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001001 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001002 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001003 free(cmd);
1004 }
1005
1006 pthread_cond_signal(&out->cond);
1007 while (!list_empty(&out->offload_cmd_list)) {
1008 item = list_head(&out->offload_cmd_list);
1009 list_remove(item);
1010 free(node_to_item(item, struct offload_cmd, node));
1011 }
1012 pthread_mutex_unlock(&out->lock);
1013
1014 return NULL;
1015}
1016
1017static int create_offload_callback_thread(struct stream_out *out)
1018{
1019 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1020 list_init(&out->offload_cmd_list);
1021 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1022 offload_thread_loop, out);
1023 return 0;
1024}
1025
1026static int destroy_offload_callback_thread(struct stream_out *out)
1027{
1028 pthread_mutex_lock(&out->lock);
1029 stop_compressed_output_l(out);
1030 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1031
1032 pthread_mutex_unlock(&out->lock);
1033 pthread_join(out->offload_thread, (void **) NULL);
1034 pthread_cond_destroy(&out->offload_cond);
1035
1036 return 0;
1037}
1038
Eric Laurent07eeafd2013-10-06 12:52:49 -07001039static bool allow_hdmi_channel_config(struct audio_device *adev)
1040{
1041 struct listnode *node;
1042 struct audio_usecase *usecase;
1043 bool ret = true;
1044
1045 list_for_each(node, &adev->usecase_list) {
1046 usecase = node_to_item(node, struct audio_usecase, list);
1047 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1048 /*
1049 * If voice call is already existing, do not proceed further to avoid
1050 * disabling/enabling both RX and TX devices, CSD calls, etc.
1051 * Once the voice call done, the HDMI channels can be configured to
1052 * max channels of remaining use cases.
1053 */
1054 if (usecase->id == USECASE_VOICE_CALL) {
1055 ALOGD("%s: voice call is active, no change in HDMI channels",
1056 __func__);
1057 ret = false;
1058 break;
1059 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1060 ALOGD("%s: multi channel playback is active, "
1061 "no change in HDMI channels", __func__);
1062 ret = false;
1063 break;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001064 } else if (is_offload_usecase(usecase->id) &&
Mingming Yin139f1072014-02-24 17:56:01 -08001065 popcount(usecase->stream.out->channel_mask) > 2) {
1066 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1067 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1068 ret = false;
1069 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001070 }
1071 }
1072 }
1073 return ret;
1074}
1075
1076static int check_and_set_hdmi_channels(struct audio_device *adev,
1077 unsigned int channels)
1078{
1079 struct listnode *node;
1080 struct audio_usecase *usecase;
1081
1082 /* Check if change in HDMI channel config is allowed */
1083 if (!allow_hdmi_channel_config(adev))
1084 return 0;
1085
1086 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001087 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001088 return 0;
1089 }
1090
1091 platform_set_hdmi_channels(adev->platform, channels);
1092 adev->cur_hdmi_channels = channels;
1093
1094 /*
1095 * Deroute all the playback streams routed to HDMI so that
1096 * the back end is deactivated. Note that backend will not
1097 * be deactivated if any one stream is connected to it.
1098 */
1099 list_for_each(node, &adev->usecase_list) {
1100 usecase = node_to_item(node, struct audio_usecase, list);
1101 if (usecase->type == PCM_PLAYBACK &&
1102 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001103 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001104 }
1105 }
1106
1107 /*
1108 * Enable all the streams disabled above. Now the HDMI backend
1109 * will be activated with new channel configuration
1110 */
1111 list_for_each(node, &adev->usecase_list) {
1112 usecase = node_to_item(node, struct audio_usecase, list);
1113 if (usecase->type == PCM_PLAYBACK &&
1114 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001115 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001116 }
1117 }
1118
1119 return 0;
1120}
1121
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001122static int stop_output_stream(struct stream_out *out)
1123{
1124 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001125 struct audio_usecase *uc_info;
1126 struct audio_device *adev = out->dev;
1127
Eric Laurent994a6932013-07-17 11:51:42 -07001128 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001129 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001130 uc_info = get_usecase_from_list(adev, out->usecase);
1131 if (uc_info == NULL) {
1132 ALOGE("%s: Could not find the usecase (%d) in the list",
1133 __func__, out->usecase);
1134 return -EINVAL;
1135 }
1136
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001137 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001138 if (adev->visualizer_stop_output != NULL)
1139 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1140 if (adev->offload_effects_stop_output != NULL)
1141 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1142 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001143
Eric Laurent150dbfe2013-02-27 14:31:02 -08001144 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001145 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001146
1147 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001148 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001149
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001150 list_remove(&uc_info->list);
1151 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001152
Eric Laurent07eeafd2013-10-06 12:52:49 -07001153 /* Must be called after removing the usecase from list */
1154 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1155 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1156
Eric Laurent994a6932013-07-17 11:51:42 -07001157 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001158 return ret;
1159}
1160
1161int start_output_stream(struct stream_out *out)
1162{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001163 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001164 struct audio_usecase *uc_info;
1165 struct audio_device *adev = out->dev;
1166
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301167 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1168 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1169 out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001170 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001171 if (out->pcm_device_id < 0) {
1172 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1173 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001174 ret = -EINVAL;
1175 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001176 }
1177
1178 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1179 uc_info->id = out->usecase;
1180 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001181 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001182 uc_info->devices = out->devices;
1183 uc_info->in_snd_device = SND_DEVICE_NONE;
1184 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001185
Eric Laurent07eeafd2013-10-06 12:52:49 -07001186 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001187 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001188 if (is_offload_usecase(out->usecase))
Mingming Yin10fef6a2013-11-26 17:17:01 -08001189 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1190 else
1191 check_and_set_hdmi_channels(adev, out->config.channels);
1192 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001193
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001194 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001195
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001196 select_devices(adev, out->usecase);
1197
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001198 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1199 __func__, 0, out->pcm_device_id);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001200 if (!is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001201 out->pcm = pcm_open(adev->snd_card,
1202 out->pcm_device_id,
1203 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001204 if (out->pcm && !pcm_is_ready(out->pcm)) {
1205 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1206 pcm_close(out->pcm);
1207 out->pcm = NULL;
1208 ret = -EIO;
1209 goto error_open;
1210 }
1211 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001212 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001213 out->compr = compress_open(adev->snd_card,
1214 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001215 COMPRESS_IN, &out->compr_config);
1216 if (out->compr && !is_compress_ready(out->compr)) {
1217 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1218 compress_close(out->compr);
1219 out->compr = NULL;
1220 ret = -EIO;
1221 goto error_open;
1222 }
1223 if (out->offload_callback)
1224 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001225
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001226#ifdef DS1_DOLBY_DDP_ENABLED
1227 if (audio_extn_is_dolby_format(out->format))
1228 audio_extn_dolby_send_ddp_endp_params(adev);
1229#endif
1230
Eric Laurentc4aef752013-09-12 17:45:53 -07001231 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001232 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1233 if (adev->offload_effects_start_output != NULL)
1234 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001235 }
Eric Laurent994a6932013-07-17 11:51:42 -07001236 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001237 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001238error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001239 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001240error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001241 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001242}
1243
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001244static int check_input_parameters(uint32_t sample_rate,
1245 audio_format_t format,
1246 int channel_count)
1247{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001248 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001249
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001250 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001251 !voice_extn_compress_voip_is_format_supported(format) &&
1252 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001253
1254 switch (channel_count) {
1255 case 1:
1256 case 2:
1257 case 6:
1258 break;
1259 default:
1260 ret = -EINVAL;
1261 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262
1263 switch (sample_rate) {
1264 case 8000:
1265 case 11025:
1266 case 12000:
1267 case 16000:
1268 case 22050:
1269 case 24000:
1270 case 32000:
1271 case 44100:
1272 case 48000:
1273 break;
1274 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001275 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001276 }
1277
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001278 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001279}
1280
1281static size_t get_input_buffer_size(uint32_t sample_rate,
1282 audio_format_t format,
1283 int channel_count)
1284{
1285 size_t size = 0;
1286
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001287 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1288 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001289
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001290 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1291 /* ToDo: should use frame_size computed based on the format and
1292 channel_count here. */
1293 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001294
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001295 /* make sure the size is multiple of 64 */
1296 size += 0x3f;
1297 size &= ~0x3f;
1298
1299 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001300}
1301
1302static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1303{
1304 struct stream_out *out = (struct stream_out *)stream;
1305
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001306 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001307}
1308
1309static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1310{
1311 return -ENOSYS;
1312}
1313
1314static size_t out_get_buffer_size(const struct audio_stream *stream)
1315{
1316 struct stream_out *out = (struct stream_out *)stream;
1317
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001318 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001319 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001320 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1321 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001322
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323 return out->config.period_size * audio_stream_frame_size(stream);
1324}
1325
1326static uint32_t out_get_channels(const struct audio_stream *stream)
1327{
1328 struct stream_out *out = (struct stream_out *)stream;
1329
1330 return out->channel_mask;
1331}
1332
1333static audio_format_t out_get_format(const struct audio_stream *stream)
1334{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001335 struct stream_out *out = (struct stream_out *)stream;
1336
1337 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001338}
1339
1340static int out_set_format(struct audio_stream *stream, audio_format_t format)
1341{
1342 return -ENOSYS;
1343}
1344
1345static int out_standby(struct audio_stream *stream)
1346{
1347 struct stream_out *out = (struct stream_out *)stream;
1348 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001349
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301350 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1351 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001352 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1353 /* Ignore standby in case of voip call because the voip output
1354 * stream is closed in adev_close_output_stream()
1355 */
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301356 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001357 return 0;
1358 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001359
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001360 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001361 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001362 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001363 out->standby = true;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001364 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001365 if (out->pcm) {
1366 pcm_close(out->pcm);
1367 out->pcm = NULL;
1368 }
1369 } else {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301370 ALOGD("copl(%x):standby", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001371 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001372 out->gapless_mdata.encoder_delay = 0;
1373 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001374 if (out->compr != NULL) {
1375 compress_close(out->compr);
1376 out->compr = NULL;
1377 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001378 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001379 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001380 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001381 }
1382 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001383 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001384 return 0;
1385}
1386
1387static int out_dump(const struct audio_stream *stream, int fd)
1388{
1389 return 0;
1390}
1391
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001392static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1393{
1394 int ret = 0;
1395 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001396 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001397 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001398 tmp_mdata.encoder_delay = 0;
1399 tmp_mdata.encoder_padding = 0;
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001400
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001401 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001402 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001403 return -EINVAL;
1404 }
1405
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001406 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1407 if (ret >= 0) {
1408 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1409 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1410 ALOGV("ADTS format is set in offload mode");
1411 }
1412 out->send_new_metadata = 1;
1413 }
1414
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001415 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1416 if(ret >= 0)
1417 is_meta_data_params = true;
1418 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1419 if(ret >= 0 )
1420 is_meta_data_params = true;
1421 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1422 if(ret >= 0 )
1423 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001424 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1425 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001426 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001427 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001428 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001429 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1430 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001431 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001432 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001433 }
1434
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001435 if(!is_meta_data_params) {
1436 ALOGV("%s: Not gapless meta data params", __func__);
1437 return 0;
1438 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001439 out->gapless_mdata = tmp_mdata;
1440 out->send_new_metadata = 1;
1441 ALOGV("%s new encoder delay %u and padding %u", __func__,
1442 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1443
1444 return 0;
1445}
1446
1447
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001448static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1449{
1450 struct stream_out *out = (struct stream_out *)stream;
1451 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001452 struct audio_usecase *usecase;
1453 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001454 struct str_parms *parms;
1455 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001456 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001457 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001458
sangwoobc677242013-08-08 16:53:43 +09001459 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001460 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001461 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001462 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1463 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001464 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001465 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001466 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001467
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001468 /*
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301469 * When HDMI cable is unplugged/usb hs is disconnected the
1470 * music playback is paused and the policy manager sends routing=0
1471 * But the audioflingercontinues to write data until standby time
1472 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001473 * Avoid this by routing audio to speaker until standby.
1474 */
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301475 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1476 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001477 val == AUDIO_DEVICE_NONE) {
1478 val = AUDIO_DEVICE_OUT_SPEAKER;
1479 }
1480
1481 /*
1482 * select_devices() call below switches all the usecases on the same
1483 * backend to the new device. Refer to check_usecases_codec_backend() in
1484 * the select_devices(). But how do we undo this?
1485 *
1486 * For example, music playback is active on headset (deep-buffer usecase)
1487 * and if we go to ringtones and select a ringtone, low-latency usecase
1488 * will be started on headset+speaker. As we can't enable headset+speaker
1489 * and headset devices at the same time, select_devices() switches the music
1490 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1491 * So when the ringtone playback is completed, how do we undo the same?
1492 *
1493 * We are relying on the out_set_parameters() call on deep-buffer output,
1494 * once the ringtone playback is ended.
1495 * NOTE: We should not check if the current devices are same as new devices.
1496 * Because select_devices() must be called to switch back the music
1497 * playback to headset.
1498 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001499 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001500 out->devices = val;
1501
1502 if (!out->standby)
1503 select_devices(adev, out->usecase);
1504
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001505 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1506 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001507 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001508 ret = voice_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001509 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1510 voice_is_in_call(adev) &&
1511 (out == adev->primary_output)) {
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -08001512 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001513 }
1514 }
1515
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001516 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1517 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001518 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001519 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001520 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001521
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001522 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001523 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001524 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001525
1526 if (out == adev->primary_output) {
1527 pthread_mutex_lock(&adev->lock);
1528 audio_extn_set_parameters(adev, parms);
1529 pthread_mutex_unlock(&adev->lock);
1530 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001531 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001532 pthread_mutex_lock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001533 parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001534 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001535 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001536
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001537 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001538 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001539 return ret;
1540}
1541
1542static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1543{
1544 struct stream_out *out = (struct stream_out *)stream;
1545 struct str_parms *query = str_parms_create_str(keys);
1546 char *str;
1547 char value[256];
1548 struct str_parms *reply = str_parms_create();
1549 size_t i, j;
1550 int ret;
1551 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001552 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001553 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1554 if (ret >= 0) {
1555 value[0] = '\0';
1556 i = 0;
1557 while (out->supported_channel_masks[i] != 0) {
1558 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1559 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1560 if (!first) {
1561 strcat(value, "|");
1562 }
1563 strcat(value, out_channels_name_to_enum_table[j].name);
1564 first = false;
1565 break;
1566 }
1567 }
1568 i++;
1569 }
1570 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1571 str = str_parms_to_str(reply);
1572 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001573 voice_extn_out_get_parameters(out, query, reply);
1574 str = str_parms_to_str(reply);
1575 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella29b8fc72014-01-29 12:52:19 -08001576 free(str);
1577 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001578 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001579 }
1580 str_parms_destroy(query);
1581 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001582 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001583 return str;
1584}
1585
1586static uint32_t out_get_latency(const struct audio_stream_out *stream)
1587{
1588 struct stream_out *out = (struct stream_out *)stream;
1589
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001590 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001591 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1592
1593 return (out->config.period_count * out->config.period_size * 1000) /
1594 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001595}
1596
1597static int out_set_volume(struct audio_stream_out *stream, float left,
1598 float right)
1599{
Eric Laurenta9024de2013-04-04 09:19:12 -07001600 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001601 int volume[2];
1602
Eric Laurenta9024de2013-04-04 09:19:12 -07001603 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1604 /* only take left channel into account: the API is for stereo anyway */
1605 out->muted = (left == 0.0f);
1606 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001607 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001608 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001609 struct audio_device *adev = out->dev;
1610 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001611 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1612 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001613
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001614 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1615 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001616 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1617 if (!ctl) {
1618 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1619 __func__, mixer_ctl_name);
1620 return -EINVAL;
1621 }
1622 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1623 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1624 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1625 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001626 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001627
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001628 return -ENOSYS;
1629}
1630
1631static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1632 size_t bytes)
1633{
1634 struct stream_out *out = (struct stream_out *)stream;
1635 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001636 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001637
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001638 pthread_mutex_lock(&out->lock);
1639 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001640 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001641 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001642 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1643 ret = voice_extn_compress_voip_start_output_stream(out);
1644 else
1645 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001646 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001647 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001648 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001649 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001650 goto exit;
1651 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001652 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001653
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001654 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301655 ALOGD("copl(%x): writing buffer (%d bytes) to compress device", (unsigned int)out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001656 if (out->send_new_metadata) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301657 ALOGD("copl(%x):send new gapless metadata", (unsigned int)out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001658 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1659 out->send_new_metadata = 0;
1660 }
1661
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001662 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001663 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001664 if (ret >= 0 && ret < (ssize_t)bytes) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301665 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001666 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1667 }
1668 if (!out->playback_started) {
1669 compress_start(out->compr);
1670 out->playback_started = 1;
1671 out->offload_state = OFFLOAD_STATE_PLAYING;
1672 }
1673 pthread_mutex_unlock(&out->lock);
1674 return ret;
1675 } else {
1676 if (out->pcm) {
1677 if (out->muted)
1678 memset((void *)buffer, 0, bytes);
1679 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1680 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001681 if (ret == 0)
1682 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001683 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001684 }
1685
1686exit:
1687 pthread_mutex_unlock(&out->lock);
1688
1689 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001690 if (out->pcm)
1691 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001692 out_standby(&out->stream.common);
1693 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1694 out_get_sample_rate(&out->stream.common));
1695 }
1696 return bytes;
1697}
1698
1699static int out_get_render_position(const struct audio_stream_out *stream,
1700 uint32_t *dsp_frames)
1701{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001702 struct stream_out *out = (struct stream_out *)stream;
1703 *dsp_frames = 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001704 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001705 pthread_mutex_lock(&out->lock);
1706 if (out->compr != NULL) {
1707 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1708 &out->sample_rate);
1709 ALOGVV("%s rendered frames %d sample_rate %d",
1710 __func__, *dsp_frames, out->sample_rate);
1711 }
1712 pthread_mutex_unlock(&out->lock);
1713 return 0;
1714 } else
1715 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001716}
1717
1718static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1719{
1720 return 0;
1721}
1722
1723static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1724{
1725 return 0;
1726}
1727
1728static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1729 int64_t *timestamp)
1730{
1731 return -EINVAL;
1732}
1733
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001734static int out_get_presentation_position(const struct audio_stream_out *stream,
1735 uint64_t *frames, struct timespec *timestamp)
1736{
1737 struct stream_out *out = (struct stream_out *)stream;
1738 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001739 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001740
1741 pthread_mutex_lock(&out->lock);
1742
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001743 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07001744 if (out->compr != NULL) {
1745 compress_get_tstamp(out->compr, &dsp_frames,
1746 &out->sample_rate);
1747 ALOGVV("%s rendered frames %ld sample_rate %d",
1748 __func__, dsp_frames, out->sample_rate);
1749 *frames = dsp_frames;
1750 ret = 0;
1751 /* this is the best we can do */
1752 clock_gettime(CLOCK_MONOTONIC, timestamp);
1753 }
1754 } else {
1755 if (out->pcm) {
1756 size_t avail;
1757 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1758 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001759 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001760 // This adjustment accounts for buffering after app processor.
1761 // It is based on estimated DSP latency per use case, rather than exact.
1762 signed_frames -=
1763 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1764
Eric Laurent949a0892013-09-20 09:20:13 -07001765 // It would be unusual for this value to be negative, but check just in case ...
1766 if (signed_frames >= 0) {
1767 *frames = signed_frames;
1768 ret = 0;
1769 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001770 }
1771 }
1772 }
1773
1774 pthread_mutex_unlock(&out->lock);
1775
1776 return ret;
1777}
1778
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001779static int out_set_callback(struct audio_stream_out *stream,
1780 stream_callback_t callback, void *cookie)
1781{
1782 struct stream_out *out = (struct stream_out *)stream;
1783
1784 ALOGV("%s", __func__);
1785 pthread_mutex_lock(&out->lock);
1786 out->offload_callback = callback;
1787 out->offload_cookie = cookie;
1788 pthread_mutex_unlock(&out->lock);
1789 return 0;
1790}
1791
1792static int out_pause(struct audio_stream_out* stream)
1793{
1794 struct stream_out *out = (struct stream_out *)stream;
1795 int status = -ENOSYS;
1796 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001797 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301798 ALOGD("copl(%x):pause compress driver", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001799 pthread_mutex_lock(&out->lock);
1800 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1801 status = compress_pause(out->compr);
1802 out->offload_state = OFFLOAD_STATE_PAUSED;
1803 }
1804 pthread_mutex_unlock(&out->lock);
1805 }
1806 return status;
1807}
1808
1809static int out_resume(struct audio_stream_out* stream)
1810{
1811 struct stream_out *out = (struct stream_out *)stream;
1812 int status = -ENOSYS;
1813 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001814 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301815 ALOGD("copl(%x):resume compress driver", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001816 status = 0;
1817 pthread_mutex_lock(&out->lock);
1818 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1819 status = compress_resume(out->compr);
1820 out->offload_state = OFFLOAD_STATE_PLAYING;
1821 }
1822 pthread_mutex_unlock(&out->lock);
1823 }
1824 return status;
1825}
1826
1827static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1828{
1829 struct stream_out *out = (struct stream_out *)stream;
1830 int status = -ENOSYS;
1831 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001832 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001833 pthread_mutex_lock(&out->lock);
1834 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1835 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1836 else
1837 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1838 pthread_mutex_unlock(&out->lock);
1839 }
1840 return status;
1841}
1842
1843static int out_flush(struct audio_stream_out* stream)
1844{
1845 struct stream_out *out = (struct stream_out *)stream;
1846 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001847 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301848 ALOGD("copl(%x):calling compress flush", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001849 pthread_mutex_lock(&out->lock);
1850 stop_compressed_output_l(out);
1851 pthread_mutex_unlock(&out->lock);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301852 ALOGD("copl(%x):out of compress flush", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001853 return 0;
1854 }
1855 return -ENOSYS;
1856}
1857
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001858/** audio_stream_in implementation **/
1859static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1860{
1861 struct stream_in *in = (struct stream_in *)stream;
1862
1863 return in->config.rate;
1864}
1865
1866static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1867{
1868 return -ENOSYS;
1869}
1870
1871static size_t in_get_buffer_size(const struct audio_stream *stream)
1872{
1873 struct stream_in *in = (struct stream_in *)stream;
1874
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001875 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1876 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001877 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1878 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001879
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001880 return in->config.period_size * audio_stream_frame_size(stream);
1881}
1882
1883static uint32_t in_get_channels(const struct audio_stream *stream)
1884{
1885 struct stream_in *in = (struct stream_in *)stream;
1886
1887 return in->channel_mask;
1888}
1889
1890static audio_format_t in_get_format(const struct audio_stream *stream)
1891{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001892 struct stream_in *in = (struct stream_in *)stream;
1893
1894 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001895}
1896
1897static int in_set_format(struct audio_stream *stream, audio_format_t format)
1898{
1899 return -ENOSYS;
1900}
1901
1902static int in_standby(struct audio_stream *stream)
1903{
1904 struct stream_in *in = (struct stream_in *)stream;
1905 struct audio_device *adev = in->dev;
1906 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301907 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1908 stream, in->usecase, use_case_table[in->usecase]);
1909
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001910
1911 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1912 /* Ignore standby in case of voip call because the voip input
1913 * stream is closed in adev_close_input_stream()
1914 */
1915 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1916 return status;
1917 }
1918
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001919 pthread_mutex_lock(&in->lock);
1920 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001921 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001922 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001923 if (in->pcm) {
1924 pcm_close(in->pcm);
1925 in->pcm = NULL;
1926 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001927 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001928 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001929 }
1930 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001931 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001932 return status;
1933}
1934
1935static int in_dump(const struct audio_stream *stream, int fd)
1936{
1937 return 0;
1938}
1939
1940static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1941{
1942 struct stream_in *in = (struct stream_in *)stream;
1943 struct audio_device *adev = in->dev;
1944 struct str_parms *parms;
1945 char *str;
1946 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001947 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001948
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301949 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001950 parms = str_parms_create_str(kvpairs);
1951
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001952 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001953 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001954
1955 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1956 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001957 val = atoi(value);
1958 /* no audio source uses val == 0 */
1959 if ((in->source != val) && (val != 0)) {
1960 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08001961 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1962 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
1963 (voice_extn_compress_voip_is_format_supported(in->format)) &&
1964 (in->config.rate == 8000 || in->config.rate == 16000) &&
1965 (popcount(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08001966 err = voice_extn_compress_voip_open_input_stream(in);
1967 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08001968 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08001969 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08001970 }
1971 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001972 }
1973 }
1974
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001975 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1976 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001977 val = atoi(value);
1978 if ((in->device != val) && (val != 0)) {
1979 in->device = val;
1980 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001981 if (!in->standby)
1982 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001983 }
1984 }
1985
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08001986done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001987 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001988 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001989
1990 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001991 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001992 return ret;
1993}
1994
1995static char* in_get_parameters(const struct audio_stream *stream,
1996 const char *keys)
1997{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001998 struct stream_in *in = (struct stream_in *)stream;
1999 struct str_parms *query = str_parms_create_str(keys);
2000 char *str;
2001 char value[256];
2002 struct str_parms *reply = str_parms_create();
2003 ALOGV("%s: enter: keys - %s", __func__, keys);
2004
2005 voice_extn_in_get_parameters(in, query, reply);
2006
2007 str = str_parms_to_str(reply);
2008 str_parms_destroy(query);
2009 str_parms_destroy(reply);
2010
2011 ALOGV("%s: exit: returns - %s", __func__, str);
2012 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002013}
2014
2015static int in_set_gain(struct audio_stream_in *stream, float gain)
2016{
2017 return 0;
2018}
2019
2020static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2021 size_t bytes)
2022{
2023 struct stream_in *in = (struct stream_in *)stream;
2024 struct audio_device *adev = in->dev;
2025 int i, ret = -1;
2026
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002027 pthread_mutex_lock(&in->lock);
2028 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002029 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002030 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2031 ret = voice_extn_compress_voip_start_input_stream(in);
2032 else
2033 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002034 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002035 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002036 goto exit;
2037 }
2038 in->standby = 0;
2039 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002040
2041 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002042 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
2043 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002044 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2045 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002046 else
2047 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002048 }
2049
2050 /*
2051 * Instead of writing zeroes here, we could trust the hardware
2052 * to always provide zeroes when muted.
2053 */
Helen Zenge0b186f2013-10-23 14:00:59 -07002054 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002055 memset(buffer, 0, bytes);
2056
2057exit:
2058 pthread_mutex_unlock(&in->lock);
2059
2060 if (ret != 0) {
2061 in_standby(&in->stream.common);
2062 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
2063 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
2064 in_get_sample_rate(&in->stream.common));
2065 }
2066 return bytes;
2067}
2068
2069static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
2070{
2071 return 0;
2072}
2073
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002074static int add_remove_audio_effect(const struct audio_stream *stream,
2075 effect_handle_t effect,
2076 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002077{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002078 struct stream_in *in = (struct stream_in *)stream;
2079 int status = 0;
2080 effect_descriptor_t desc;
2081
2082 status = (*effect)->get_descriptor(effect, &desc);
2083 if (status != 0)
2084 return status;
2085
2086 pthread_mutex_lock(&in->lock);
2087 pthread_mutex_lock(&in->dev->lock);
2088 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2089 in->enable_aec != enable &&
2090 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2091 in->enable_aec = enable;
2092 if (!in->standby)
2093 select_devices(in->dev, in->usecase);
2094 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002095 if (in->enable_ns != enable &&
2096 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2097 in->enable_ns = enable;
2098 if (!in->standby)
2099 select_devices(in->dev, in->usecase);
2100 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002101 pthread_mutex_unlock(&in->dev->lock);
2102 pthread_mutex_unlock(&in->lock);
2103
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002104 return 0;
2105}
2106
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002107static int in_add_audio_effect(const struct audio_stream *stream,
2108 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002109{
Eric Laurent994a6932013-07-17 11:51:42 -07002110 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002111 return add_remove_audio_effect(stream, effect, true);
2112}
2113
2114static int in_remove_audio_effect(const struct audio_stream *stream,
2115 effect_handle_t effect)
2116{
Eric Laurent994a6932013-07-17 11:51:42 -07002117 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002118 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002119}
2120
2121static int adev_open_output_stream(struct audio_hw_device *dev,
2122 audio_io_handle_t handle,
2123 audio_devices_t devices,
2124 audio_output_flags_t flags,
2125 struct audio_config *config,
2126 struct audio_stream_out **stream_out)
2127{
2128 struct audio_device *adev = (struct audio_device *)dev;
2129 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002130 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002131
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002132 *stream_out = NULL;
2133 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2134
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302135 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2136 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2137 devices, flags, &out->stream);
2138
2139
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002140 if (!out) {
2141 return -ENOMEM;
2142 }
2143
2144 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2145 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2146
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002147 if (devices == AUDIO_DEVICE_NONE)
2148 devices = AUDIO_DEVICE_OUT_SPEAKER;
2149
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002150 out->flags = flags;
2151 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002152 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002153 out->format = config->format;
2154 out->sample_rate = config->sample_rate;
2155 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2156 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002157 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002158
2159 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002160 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2161 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2162 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2163
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002164 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002165 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2166 ret = read_hdmi_channel_masks(out);
2167
2168 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2169 ret = audio_extn_read_afe_proxy_channel_masks(out);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002170 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002171 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002172 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002173
2174 if (config->sample_rate == 0)
2175 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2176 if (config->channel_mask == 0)
2177 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2178
2179 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002180 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002181 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2182 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002183 out->config.rate = config->sample_rate;
2184 out->config.channels = popcount(out->channel_mask);
2185 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002186 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2187 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002188 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002189 ret = voice_extn_compress_voip_open_output_stream(out);
2190 if (ret != 0) {
2191 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2192 __func__, ret);
2193 goto error_open;
2194 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002195 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2196 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2197 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2198 ALOGE("%s: Unsupported Offload information", __func__);
2199 ret = -EINVAL;
2200 goto error_open;
2201 }
Mingming Yin90310102013-11-13 16:57:00 -08002202 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002203 !audio_extn_is_dolby_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002204 ALOGE("%s: Unsupported audio format", __func__);
2205 ret = -EINVAL;
2206 goto error_open;
2207 }
2208
2209 out->compr_config.codec = (struct snd_codec *)
2210 calloc(1, sizeof(struct snd_codec));
2211
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002212 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002213 if (config->offload_info.channel_mask)
2214 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002215 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002216 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002217 config->offload_info.channel_mask = config->channel_mask;
2218 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002219 out->format = config->offload_info.format;
2220 out->sample_rate = config->offload_info.sample_rate;
2221
2222 out->stream.set_callback = out_set_callback;
2223 out->stream.pause = out_pause;
2224 out->stream.resume = out_resume;
2225 out->stream.drain = out_drain;
2226 out->stream.flush = out_flush;
2227
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002228 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002229 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002230 audio_extn_dolby_get_snd_codec_id(adev, out,
2231 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002232 else
2233 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002234 get_snd_codec_id(config->offload_info.format);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002235 if (audio_is_offload_pcm(config->offload_info.format)) {
2236 out->compr_config.fragment_size =
2237 platform_get_pcm_offload_buffer_size(&config->offload_info);
2238 } else {
2239 out->compr_config.fragment_size =
2240 platform_get_compress_offload_buffer_size(&config->offload_info);
2241 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002242 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2243 out->compr_config.codec->sample_rate =
2244 compress_get_alsa_rate(config->offload_info.sample_rate);
2245 out->compr_config.codec->bit_rate =
2246 config->offload_info.bit_rate;
2247 out->compr_config.codec->ch_in =
2248 popcount(config->channel_mask);
2249 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
ApurupaPattapu2e084df2013-12-18 15:47:59 -08002250 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002251
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002252 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2253 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
2254 else if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
2255 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2256
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002257 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2258 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002259
2260 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002261 out->offload_state = OFFLOAD_STATE_IDLE;
2262 out->playback_started = 0;
2263
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002264 create_offload_callback_thread(out);
2265 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2266 __func__, config->offload_info.version,
2267 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002268 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -08002269 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002270
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002271 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2272 ret = voice_check_and_set_incall_music_usecase(adev, out);
2273 if (ret != 0) {
2274 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2275 __func__, ret);
2276 goto error_open;
2277 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002278 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002279 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2280 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002281 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002282 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002283 /* primary path is the default path selected if no other outputs are available/suitable */
2284 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002285 out->config = pcm_config_deep_buffer;
2286 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002287 }
2288
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002289 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2290 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2291 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002292 if(adev->primary_output == NULL)
2293 adev->primary_output = out;
2294 else {
2295 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002296 ret = -EEXIST;
2297 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002298 }
2299 }
2300
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002301 /* Check if this usecase is already existing */
2302 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07002303 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2304 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002305 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002306 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002307 ret = -EEXIST;
2308 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002309 }
2310 pthread_mutex_unlock(&adev->lock);
2311
2312 out->stream.common.get_sample_rate = out_get_sample_rate;
2313 out->stream.common.set_sample_rate = out_set_sample_rate;
2314 out->stream.common.get_buffer_size = out_get_buffer_size;
2315 out->stream.common.get_channels = out_get_channels;
2316 out->stream.common.get_format = out_get_format;
2317 out->stream.common.set_format = out_set_format;
2318 out->stream.common.standby = out_standby;
2319 out->stream.common.dump = out_dump;
2320 out->stream.common.set_parameters = out_set_parameters;
2321 out->stream.common.get_parameters = out_get_parameters;
2322 out->stream.common.add_audio_effect = out_add_audio_effect;
2323 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2324 out->stream.get_latency = out_get_latency;
2325 out->stream.set_volume = out_set_volume;
2326 out->stream.write = out_write;
2327 out->stream.get_render_position = out_get_render_position;
2328 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002329 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002330
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002331 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002332 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002333 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002334
2335 config->format = out->stream.common.get_format(&out->stream.common);
2336 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2337 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2338
2339 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302340 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2341 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002342 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002343 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002344
2345error_open:
2346 free(out);
2347 *stream_out = NULL;
2348 ALOGD("%s: exit: ret %d", __func__, ret);
2349 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002350}
2351
2352static void adev_close_output_stream(struct audio_hw_device *dev,
2353 struct audio_stream_out *stream)
2354{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002355 struct stream_out *out = (struct stream_out *)stream;
2356 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002357 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002358
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302359 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2360
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002361 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2362 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2363 if(ret != 0)
2364 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2365 __func__, ret);
2366 }
2367 else
2368 out_standby(&stream->common);
2369
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002370 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002371 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002372 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002373 if (out->compr_config.codec != NULL)
2374 free(out->compr_config.codec);
2375 }
2376 pthread_cond_destroy(&out->cond);
2377 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002378 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002379 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002380}
2381
2382static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2383{
2384 struct audio_device *adev = (struct audio_device *)dev;
2385 struct str_parms *parms;
2386 char *str;
2387 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002388 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002389 int ret = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002390
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002391 ALOGD("%s: enter: %s", __func__, kvpairs);
2392
2393 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002394 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002395
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002396 ret = voice_set_parameters(adev, parms);
2397 if (ret != 0)
2398 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002399
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002400 ret = platform_set_parameters(adev->platform, parms);
2401 if (ret != 0)
2402 goto done;
2403
2404 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2405 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002406 /* When set to false, HAL should disable EC and NS
2407 * But it is currently not supported.
2408 */
2409 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2410 adev->bluetooth_nrec = true;
2411 else
2412 adev->bluetooth_nrec = false;
2413 }
2414
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002415 err = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2416 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002417 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2418 adev->screen_off = false;
2419 else
2420 adev->screen_off = true;
2421 }
2422
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002423 err = str_parms_get_int(parms, "rotation", &val);
2424 if (err >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002425 bool reverse_speakers = false;
2426 switch(val) {
2427 // FIXME: note that the code below assumes that the speakers are in the correct placement
2428 // relative to the user when the device is rotated 90deg from its default rotation. This
2429 // assumption is device-specific, not platform-specific like this code.
2430 case 270:
2431 reverse_speakers = true;
2432 break;
2433 case 0:
2434 case 90:
2435 case 180:
2436 break;
2437 default:
2438 ALOGE("%s: unexpected rotation of %d", __func__, val);
2439 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002440 if (adev->speaker_lr_swap != reverse_speakers) {
2441 adev->speaker_lr_swap = reverse_speakers;
2442 // only update the selected device if there is active pcm playback
2443 struct audio_usecase *usecase;
2444 struct listnode *node;
2445 list_for_each(node, &adev->usecase_list) {
2446 usecase = node_to_item(node, struct audio_usecase, list);
2447 if (usecase->type == PCM_PLAYBACK) {
2448 select_devices(adev, usecase->id);
2449 break;
2450 }
2451 }
2452 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002453 }
2454
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002455 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002456
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002457done:
2458 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002459 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002460 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002461 return ret;
2462}
2463
2464static char* adev_get_parameters(const struct audio_hw_device *dev,
2465 const char *keys)
2466{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002467 struct audio_device *adev = (struct audio_device *)dev;
2468 struct str_parms *reply = str_parms_create();
2469 struct str_parms *query = str_parms_create_str(keys);
2470 char *str;
2471
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002472 pthread_mutex_lock(&adev->lock);
2473
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002474 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002475 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002476 platform_get_parameters(adev->platform, query, reply);
2477 str = str_parms_to_str(reply);
2478 str_parms_destroy(query);
2479 str_parms_destroy(reply);
2480
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002481 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002482 ALOGV("%s: exit: returns - %s", __func__, str);
2483 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002484}
2485
2486static int adev_init_check(const struct audio_hw_device *dev)
2487{
2488 return 0;
2489}
2490
2491static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2492{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002493 int ret;
2494 struct audio_device *adev = (struct audio_device *)dev;
2495 pthread_mutex_lock(&adev->lock);
2496 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002497 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002498 pthread_mutex_unlock(&adev->lock);
2499 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002500}
2501
2502static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2503{
2504 return -ENOSYS;
2505}
2506
2507static int adev_get_master_volume(struct audio_hw_device *dev,
2508 float *volume)
2509{
2510 return -ENOSYS;
2511}
2512
2513static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2514{
2515 return -ENOSYS;
2516}
2517
2518static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2519{
2520 return -ENOSYS;
2521}
2522
2523static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2524{
2525 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002526 pthread_mutex_lock(&adev->lock);
2527 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002528 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002529 adev->mode = mode;
2530 }
2531 pthread_mutex_unlock(&adev->lock);
2532 return 0;
2533}
2534
2535static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2536{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002537 int ret;
2538
2539 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002540 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002541 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2542 pthread_mutex_unlock(&adev->lock);
2543
2544 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002545}
2546
2547static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2548{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002549 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002550 return 0;
2551}
2552
2553static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2554 const struct audio_config *config)
2555{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002556 int channel_count = popcount(config->channel_mask);
2557
2558 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2559}
2560
2561static int adev_open_input_stream(struct audio_hw_device *dev,
2562 audio_io_handle_t handle,
2563 audio_devices_t devices,
2564 struct audio_config *config,
2565 struct audio_stream_in **stream_in)
2566{
2567 struct audio_device *adev = (struct audio_device *)dev;
2568 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002569 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002570 int channel_count = popcount(config->channel_mask);
2571
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302572
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002573 *stream_in = NULL;
2574 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2575 return -EINVAL;
2576
2577 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302578 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
2579 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2580 devices, &in->stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002581
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07002582 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2583
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002584 in->stream.common.get_sample_rate = in_get_sample_rate;
2585 in->stream.common.set_sample_rate = in_set_sample_rate;
2586 in->stream.common.get_buffer_size = in_get_buffer_size;
2587 in->stream.common.get_channels = in_get_channels;
2588 in->stream.common.get_format = in_get_format;
2589 in->stream.common.set_format = in_set_format;
2590 in->stream.common.standby = in_standby;
2591 in->stream.common.dump = in_dump;
2592 in->stream.common.set_parameters = in_set_parameters;
2593 in->stream.common.get_parameters = in_get_parameters;
2594 in->stream.common.add_audio_effect = in_add_audio_effect;
2595 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2596 in->stream.set_gain = in_set_gain;
2597 in->stream.read = in_read;
2598 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2599
2600 in->device = devices;
2601 in->source = AUDIO_SOURCE_DEFAULT;
2602 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002603 in->standby = 1;
2604 in->channel_mask = config->channel_mask;
2605
2606 /* Update config params with the requested sample rate and channels */
2607 in->usecase = USECASE_AUDIO_RECORD;
2608 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002609 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002610 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002611
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002612 if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002613 if(audio_extn_ssr_get_enabled()) {
2614 if(audio_extn_ssr_init(adev, in)) {
2615 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2616 ret = -EINVAL;
2617 goto err_open;
2618 }
2619 } else {
2620 ret = -EINVAL;
2621 goto err_open;
2622 }
Mingming Yine62d7842013-10-25 16:26:03 -07002623 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002624 audio_extn_compr_cap_format_supported(config->format) &&
2625 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Mingming Yine62d7842013-10-25 16:26:03 -07002626 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002627 } else {
2628 in->config.channels = channel_count;
2629 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2630 buffer_size = get_input_buffer_size(config->sample_rate,
2631 config->format,
2632 channel_count);
2633 in->config.period_size = buffer_size / frame_size;
2634 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002635
2636 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002637 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002638 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002639
2640err_open:
2641 free(in);
2642 *stream_in = NULL;
2643 return ret;
2644}
2645
2646static void adev_close_input_stream(struct audio_hw_device *dev,
2647 struct audio_stream_in *stream)
2648{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002649 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002650 struct stream_in *in = (struct stream_in *)stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302651 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002652
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002653 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2654 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2655 if (ret != 0)
2656 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2657 __func__, ret);
2658 } else
2659 in_standby(&stream->common);
2660
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002661 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2662 audio_extn_ssr_deinit();
2663 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002664 free(stream);
2665
Mingming Yine62d7842013-10-25 16:26:03 -07002666 if(audio_extn_compr_cap_enabled() &&
2667 audio_extn_compr_cap_format_supported(in->config.format))
2668 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002669 return;
2670}
2671
2672static int adev_dump(const audio_hw_device_t *device, int fd)
2673{
2674 return 0;
2675}
2676
2677static int adev_close(hw_device_t *device)
2678{
2679 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002680
2681 if (!adev)
2682 return 0;
2683
2684 pthread_mutex_lock(&adev_init_lock);
2685
2686 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002687 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07002688 audio_route_free(adev->audio_route);
2689 free(adev->snd_dev_ref_cnt);
2690 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002691 free(device);
2692 adev = NULL;
2693 }
2694 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002695 return 0;
2696}
2697
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002698static int adev_open(const hw_module_t *module, const char *name,
2699 hw_device_t **device)
2700{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002701 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002702
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002703 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002704 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2705
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002706 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002707 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002708 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002709 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002710 ALOGD("%s: returning existing instance of adev", __func__);
2711 ALOGD("%s: exit", __func__);
2712 pthread_mutex_unlock(&adev_init_lock);
2713 return 0;
2714 }
2715
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002716 adev = calloc(1, sizeof(struct audio_device));
2717
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07002718 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2719
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002720 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2721 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2722 adev->device.common.module = (struct hw_module_t *)module;
2723 adev->device.common.close = adev_close;
2724
2725 adev->device.init_check = adev_init_check;
2726 adev->device.set_voice_volume = adev_set_voice_volume;
2727 adev->device.set_master_volume = adev_set_master_volume;
2728 adev->device.get_master_volume = adev_get_master_volume;
2729 adev->device.set_master_mute = adev_set_master_mute;
2730 adev->device.get_master_mute = adev_get_master_mute;
2731 adev->device.set_mode = adev_set_mode;
2732 adev->device.set_mic_mute = adev_set_mic_mute;
2733 adev->device.get_mic_mute = adev_get_mic_mute;
2734 adev->device.set_parameters = adev_set_parameters;
2735 adev->device.get_parameters = adev_get_parameters;
2736 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2737 adev->device.open_output_stream = adev_open_output_stream;
2738 adev->device.close_output_stream = adev_close_output_stream;
2739 adev->device.open_input_stream = adev_open_input_stream;
2740 adev->device.close_input_stream = adev_close_input_stream;
2741 adev->device.dump = adev_dump;
2742
2743 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002744 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002745 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002746 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002747 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002748 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002749 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002750 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002751 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002752 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002753 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08002754 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002755 adev->offload_usecases_state = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002756 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002757 adev->platform = platform_init(adev);
2758 if (!adev->platform) {
2759 free(adev->snd_dev_ref_cnt);
2760 free(adev);
2761 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2762 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08002763 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07002764 return -EINVAL;
2765 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002766
2767 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2768 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2769 if (adev->visualizer_lib == NULL) {
2770 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2771 } else {
2772 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2773 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002774 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002775 "visualizer_hal_start_output");
2776 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002777 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002778 "visualizer_hal_stop_output");
2779 }
2780 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08002781 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07002782
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002783 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2784 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2785 if (adev->offload_effects_lib == NULL) {
2786 ALOGE("%s: DLOPEN failed for %s", __func__,
2787 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2788 } else {
2789 ALOGV("%s: DLOPEN successful for %s", __func__,
2790 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2791 adev->offload_effects_start_output =
2792 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2793 "offload_effects_bundle_hal_start_output");
2794 adev->offload_effects_stop_output =
2795 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2796 "offload_effects_bundle_hal_stop_output");
2797 }
2798 }
2799
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002800 *device = &adev->device.common;
2801
Kiran Kandi910e1862013-10-29 13:29:42 -07002802 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002803 pthread_mutex_unlock(&adev_init_lock);
2804
Eric Laurent994a6932013-07-17 11:51:42 -07002805 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002806 return 0;
2807}
2808
2809static struct hw_module_methods_t hal_module_methods = {
2810 .open = adev_open,
2811};
2812
2813struct audio_module HAL_MODULE_INFO_SYM = {
2814 .common = {
2815 .tag = HARDWARE_MODULE_TAG,
2816 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2817 .hal_api_version = HARDWARE_HAL_API_VERSION,
2818 .id = AUDIO_HARDWARE_MODULE_ID,
2819 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08002820 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002821 .methods = &hal_module_methods,
2822 },
2823};