blob: b707809960db5c442710751a4eb5a2d9f3f8224f [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
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -070066static unsigned int configured_low_latency_capture_period_size =
67 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
68
Eric Laurentb23d5282013-05-14 15:27:20 -070069struct pcm_config pcm_config_deep_buffer = {
70 .channels = 2,
71 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
72 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
73 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
74 .format = PCM_FORMAT_S16_LE,
75 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
76 .stop_threshold = INT_MAX,
77 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
78};
79
80struct pcm_config pcm_config_low_latency = {
81 .channels = 2,
82 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
83 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
84 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
85 .format = PCM_FORMAT_S16_LE,
86 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
87 .stop_threshold = INT_MAX,
88 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
89};
90
91struct pcm_config pcm_config_hdmi_multi = {
92 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
93 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
94 .period_size = HDMI_MULTI_PERIOD_SIZE,
95 .period_count = HDMI_MULTI_PERIOD_COUNT,
96 .format = PCM_FORMAT_S16_LE,
97 .start_threshold = 0,
98 .stop_threshold = INT_MAX,
99 .avail_min = 0,
100};
101
102struct pcm_config pcm_config_audio_capture = {
103 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700104 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
105 .format = PCM_FORMAT_S16_LE,
106};
107
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800108const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700109 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
110 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
111 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700112 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700113#ifdef MULTIPLE_OFFLOAD_ENABLED
114 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
115 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
116 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
117 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
118 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
119 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
120 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
121 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
122#endif
Eric Laurentb23d5282013-05-14 15:27:20 -0700123 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700124 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700125 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700126 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700127 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800128 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800129 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700130 [USECASE_VOICE_CALL] = "voice-call",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700131
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700132 [USECASE_VOICE2_CALL] = "voice2-call",
133 [USECASE_VOLTE_CALL] = "volte-call",
134 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800135 [USECASE_VOWLAN_CALL] = "vowlan-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800136 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700137 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
138 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
139 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800140 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
141 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
142 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
143
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700144 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
145 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700146 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
147 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700148};
149
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700150static const audio_usecase_t offload_usecases[] = {
151 USECASE_AUDIO_PLAYBACK_OFFLOAD,
152#ifdef MULTIPLE_OFFLOAD_ENABLED
153 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
154 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
155 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
156 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
157 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
158 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
159 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
160 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
161#endif
162};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163
164#define STRING_TO_ENUM(string) { #string, string }
165
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800166struct string_to_enum {
167 const char *name;
168 uint32_t value;
169};
170
171static const struct string_to_enum out_channels_name_to_enum_table[] = {
172 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
173 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
174 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
175};
176
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700177static struct audio_device *adev = NULL;
178static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700179static unsigned int audio_device_ref_count;
180
Haynes Mathew George5191a852013-09-11 14:19:36 -0700181static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800182
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800183static int check_and_set_gapless_mode(struct audio_device *adev) {
184
185
186 char value[PROPERTY_VALUE_MAX] = {0};
187 bool gapless_enabled = false;
188 const char *mixer_ctl_name = "Compress Gapless Playback";
189 struct mixer_ctl *ctl;
190
191 ALOGV("%s:", __func__);
192 property_get("audio.offload.gapless.enabled", value, NULL);
193 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
194
195 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
196 if (!ctl) {
197 ALOGE("%s: Could not get ctl for mixer cmd - %s",
198 __func__, mixer_ctl_name);
199 return -EINVAL;
200 }
201
202 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
203 ALOGE("%s: Could not set gapless mode %d",
204 __func__, gapless_enabled);
205 return -EINVAL;
206 }
207 return 0;
208}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700209
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700210static bool is_supported_format(audio_format_t format)
211{
Eric Laurent86e17132013-09-12 17:49:30 -0700212 if (format == AUDIO_FORMAT_MP3 ||
Ashish Jainf9b78162014-08-25 20:36:25 +0530213 format == AUDIO_FORMAT_AAC_LC ||
214 format == AUDIO_FORMAT_AAC_HE_V1 ||
215 format == AUDIO_FORMAT_AAC_HE_V2 ||
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800216 format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD ||
Mingming Yin3ee55c62014-08-04 14:23:35 -0700217 format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD ||
218 format == AUDIO_FORMAT_FLAC)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800219 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700220
221 return false;
222}
223
224static int get_snd_codec_id(audio_format_t format)
225{
226 int id = 0;
227
Ashish Jainf9b78162014-08-25 20:36:25 +0530228 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700229 case AUDIO_FORMAT_MP3:
230 id = SND_AUDIOCODEC_MP3;
231 break;
232 case AUDIO_FORMAT_AAC:
233 id = SND_AUDIOCODEC_AAC;
234 break;
Ashish Jainf9b78162014-08-25 20:36:25 +0530235 case AUDIO_FORMAT_PCM_OFFLOAD:
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800236 id = SND_AUDIOCODEC_PCM;
237 break;
Mingming Yin3ee55c62014-08-04 14:23:35 -0700238 case AUDIO_FORMAT_FLAC:
239 id = SND_AUDIOCODEC_FLAC;
240 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700241 default:
Mingming Yin90310102013-11-13 16:57:00 -0800242 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700243 }
244
245 return id;
246}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800247
Venkata Narendra Kumar Guttaed0f94f2014-07-09 16:29:28 +0530248int get_snd_card_state(struct audio_device *adev)
Naresh Tanniru80659832014-06-04 18:17:56 +0530249{
250 int snd_scard_state;
251
252 if (!adev)
253 return SND_CARD_STATE_OFFLINE;
254
255 pthread_mutex_lock(&adev->snd_card_status.lock);
256 snd_scard_state = adev->snd_card_status.state;
257 pthread_mutex_unlock(&adev->snd_card_status.lock);
258
259 return snd_scard_state;
260}
261
262static int set_snd_card_state(struct audio_device *adev, int snd_scard_state)
263{
264 if (!adev)
265 return -ENOSYS;
266
267 pthread_mutex_lock(&adev->snd_card_status.lock);
268 adev->snd_card_status.state = snd_scard_state;
269 pthread_mutex_unlock(&adev->snd_card_status.lock);
270
271 return 0;
272}
273
Avinash Vaish71a8b972014-07-24 15:36:33 +0530274static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
275 struct audio_usecase *uc_info)
276{
277 struct listnode *node;
278 struct audio_usecase *usecase;
279
280 if (uc_info == NULL)
281 return -EINVAL;
282
283 /* Re-route all voice usecases on the shared backend other than the
284 specified usecase to new snd devices */
285 list_for_each(node, &adev->usecase_list) {
286 usecase = node_to_item(node, struct audio_usecase, list);
287 if ((usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) &&
288 (usecase != uc_info))
289 enable_audio_route(adev, usecase);
290 }
291 return 0;
292}
293
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -0700294int pcm_ioctl(struct pcm *pcm, int request, ...)
295{
296 va_list ap;
297 void * arg;
298 int pcm_fd = *(int*)pcm;
299
300 va_start(ap, request);
301 arg = va_arg(ap, void *);
302 va_end(ap);
303
304 return ioctl(pcm_fd, request, arg);
305}
306
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700307int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700308 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800309{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700310 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700311 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800312
313 if (usecase == NULL)
314 return -EINVAL;
315
316 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
317
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800318 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700319 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800320 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700321 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800322
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -0800323#ifdef DS1_DOLBY_DAP_ENABLED
324 audio_extn_dolby_set_dmid(adev);
325 audio_extn_dolby_set_endpoint(adev);
326#endif
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700327 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530328 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700329 audio_extn_utils_send_audio_calibration(adev, usecase);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700330 audio_extn_utils_send_app_type_cfg(usecase);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800331 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700332 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700333 ALOGV("%s: apply mixer and update path: %s", __func__, mixer_path);
334 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800335 ALOGV("%s: exit", __func__);
336 return 0;
337}
338
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700339int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700340 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800341{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700342 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700343 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800344
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +0530345 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800346 return -EINVAL;
347
348 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700349 if (usecase->type == PCM_CAPTURE)
350 snd_device = usecase->in_snd_device;
351 else
352 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800353 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700354 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700355 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
356 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700357 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530358 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800359 ALOGV("%s: exit", __func__);
360 return 0;
361}
362
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700363int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700364 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800365{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700366 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
367
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800368 if (snd_device < SND_DEVICE_MIN ||
369 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800370 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800371 return -EINVAL;
372 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700373
374 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700375
376 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
377 ALOGE("%s: Invalid sound device returned", __func__);
378 return -EINVAL;
379 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700380 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700381 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700382 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700383 return 0;
384 }
385
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -0700386 if (audio_extn_spkr_prot_is_enabled())
387 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700388 /* start usb playback thread */
389 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
390 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
391 audio_extn_usb_start_playback(adev);
392
393 /* start usb capture thread */
394 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
395 audio_extn_usb_start_capture(adev);
396
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800397 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
398 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700399 audio_extn_spkr_prot_is_enabled()) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700400 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
401 adev->snd_dev_ref_cnt[snd_device]--;
402 return -EINVAL;
403 }
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800404 if (audio_extn_spkr_prot_start_processing(snd_device)) {
405 ALOGE("%s: spkr_start_processing failed", __func__);
406 return -EINVAL;
407 }
408 } else {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700409 ALOGV("%s: snd_device(%d: %s)", __func__,
410 snd_device, device_name);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -0700411 /* due to the possibility of calibration overwrite between listen
412 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700413 audio_extn_sound_trigger_update_device_status(snd_device,
414 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530415 audio_extn_listen_update_device_status(snd_device,
416 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700417 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700418 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700419 audio_extn_sound_trigger_update_device_status(snd_device,
420 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530421 audio_extn_listen_update_device_status(snd_device,
422 LISTEN_EVENT_SND_DEVICE_FREE);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700423 return -EINVAL;
424 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300425 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700426 audio_route_apply_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800427 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800428 return 0;
429}
430
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700431int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700432 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800433{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700434 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
435
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800436 if (snd_device < SND_DEVICE_MIN ||
437 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800438 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800439 return -EINVAL;
440 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700441 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
442 ALOGE("%s: device ref cnt is already 0", __func__);
443 return -EINVAL;
444 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700445
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700446 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700447
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700448 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
449 ALOGE("%s: Invalid sound device returned", __func__);
450 return -EINVAL;
451 }
452
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700453 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700454 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700455 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800456 /* exit usb play back thread */
457 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
458 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
459 audio_extn_usb_stop_playback();
460
461 /* exit usb capture thread */
462 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -0700463 audio_extn_usb_stop_capture();
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800464
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800465 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
466 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700467 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -0700468 audio_extn_spkr_prot_stop_processing(snd_device);
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300469 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700470 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300471 audio_extn_dev_arbi_release(snd_device);
472 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700473
474 audio_extn_sound_trigger_update_device_status(snd_device,
475 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530476 audio_extn_listen_update_device_status(snd_device,
Kiran Kandide144c82013-11-20 15:58:32 -0800477 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700478 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700479
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800480 return 0;
481}
482
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700483static void check_usecases_codec_backend(struct audio_device *adev,
484 struct audio_usecase *uc_info,
485 snd_device_t snd_device)
486{
487 struct listnode *node;
488 struct audio_usecase *usecase;
489 bool switch_device[AUDIO_USECASE_MAX];
490 int i, num_uc_to_switch = 0;
491
492 /*
493 * This function is to make sure that all the usecases that are active on
494 * the hardware codec backend are always routed to any one device that is
495 * handled by the hardware codec.
496 * For example, if low-latency and deep-buffer usecases are currently active
497 * on speaker and out_set_parameters(headset) is received on low-latency
498 * output, then we have to make sure deep-buffer is also switched to headset,
499 * because of the limitation that both the devices cannot be enabled
500 * at the same time as they share the same backend.
501 */
Mingming Yin3ee55c62014-08-04 14:23:35 -0700502 /*
503 * This call is to check if we need to force routing for a particular stream
504 * If there is a backend configuration change for the device when a
505 * new stream starts, then ADM needs to be closed and re-opened with the new
506 * configuraion. This call check if we need to re-route all the streams
507 * associated with the backend. Touch tone + 24 bit playback.
508 */
509 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info);
510
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700511 /* Disable all the usecases on the shared backend other than the
512 specified usecase */
513 for (i = 0; i < AUDIO_USECASE_MAX; i++)
514 switch_device[i] = false;
515
516 list_for_each(node, &adev->usecase_list) {
517 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800518 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700519 usecase != uc_info &&
Mingming Yin3ee55c62014-08-04 14:23:35 -0700520 (usecase->out_snd_device != snd_device || force_routing) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700521 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
522 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
523 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700524 platform_get_snd_device_name(usecase->out_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700525 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700526 switch_device[usecase->id] = true;
527 num_uc_to_switch++;
528 }
529 }
530
531 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700532 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700533
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530534 /* Make sure the previous devices to be disabled first and then enable the
535 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700536 list_for_each(node, &adev->usecase_list) {
537 usecase = node_to_item(node, struct audio_usecase, list);
538 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700539 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700540 }
541 }
542
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700543 list_for_each(node, &adev->usecase_list) {
544 usecase = node_to_item(node, struct audio_usecase, list);
545 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700546 enable_snd_device(adev, snd_device);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700547 }
548 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700549
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700550 /* Re-route all the usecases on the shared backend other than the
551 specified usecase to new snd devices */
552 list_for_each(node, &adev->usecase_list) {
553 usecase = node_to_item(node, struct audio_usecase, list);
554 /* Update the out_snd_device only before enabling the audio route */
555 if (switch_device[usecase->id] ) {
556 usecase->out_snd_device = snd_device;
Avinash Vaish71a8b972014-07-24 15:36:33 +0530557 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
558 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700559 }
560 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700561 }
562}
563
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700564static void check_and_route_capture_usecases(struct audio_device *adev,
565 struct audio_usecase *uc_info,
566 snd_device_t snd_device)
567{
568 struct listnode *node;
569 struct audio_usecase *usecase;
570 bool switch_device[AUDIO_USECASE_MAX];
571 int i, num_uc_to_switch = 0;
572
573 /*
574 * This function is to make sure that all the active capture usecases
575 * are always routed to the same input sound device.
576 * For example, if audio-record and voice-call usecases are currently
577 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
578 * is received for voice call then we have to make sure that audio-record
579 * usecase is also switched to earpiece i.e. voice-dmic-ef,
580 * because of the limitation that two devices cannot be enabled
581 * at the same time if they share the same backend.
582 */
583 for (i = 0; i < AUDIO_USECASE_MAX; i++)
584 switch_device[i] = false;
585
586 list_for_each(node, &adev->usecase_list) {
587 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800588 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700589 usecase != uc_info &&
590 usecase->in_snd_device != snd_device) {
591 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
592 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700593 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700594 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700595 switch_device[usecase->id] = true;
596 num_uc_to_switch++;
597 }
598 }
599
600 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700601 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700602
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530603 /* Make sure the previous devices to be disabled first and then enable the
604 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700605 list_for_each(node, &adev->usecase_list) {
606 usecase = node_to_item(node, struct audio_usecase, list);
607 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700608 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800609 }
610 }
611
612 list_for_each(node, &adev->usecase_list) {
613 usecase = node_to_item(node, struct audio_usecase, list);
614 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700615 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700616 }
617 }
618
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700619 /* Re-route all the usecases on the shared backend other than the
620 specified usecase to new snd devices */
621 list_for_each(node, &adev->usecase_list) {
622 usecase = node_to_item(node, struct audio_usecase, list);
623 /* Update the in_snd_device only before enabling the audio route */
624 if (switch_device[usecase->id] ) {
625 usecase->in_snd_device = snd_device;
Avinash Vaish71a8b972014-07-24 15:36:33 +0530626 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
627 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700628 }
629 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700630 }
631}
632
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800633/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700634static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800635{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700636 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700637 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800638
639 switch (channels) {
640 /*
641 * Do not handle stereo output in Multi-channel cases
642 * Stereo case is handled in normal playback path
643 */
644 case 6:
645 ALOGV("%s: HDMI supports 5.1", __func__);
646 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
647 break;
648 case 8:
649 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
650 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
651 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
652 break;
653 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700654 ALOGE("HDMI does not support multi channel playback");
655 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800656 break;
657 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700658 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800659}
660
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700661static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
662{
663 struct audio_usecase *usecase;
664 struct listnode *node;
665
666 list_for_each(node, &adev->usecase_list) {
667 usecase = node_to_item(node, struct audio_usecase, list);
668 if (usecase->type == VOICE_CALL) {
669 ALOGV("%s: usecase id %d", __func__, usecase->id);
670 return usecase->id;
671 }
672 }
673 return USECASE_INVALID;
674}
675
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700676struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700677 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700678{
679 struct audio_usecase *usecase;
680 struct listnode *node;
681
682 list_for_each(node, &adev->usecase_list) {
683 usecase = node_to_item(node, struct audio_usecase, list);
684 if (usecase->id == uc_id)
685 return usecase;
686 }
687 return NULL;
688}
689
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700690int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800692 snd_device_t out_snd_device = SND_DEVICE_NONE;
693 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700694 struct audio_usecase *usecase = NULL;
695 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800696 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800697 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800698 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800699 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700700 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800701
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700702 usecase = get_usecase_from_list(adev, uc_id);
703 if (usecase == NULL) {
704 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
705 return -EINVAL;
706 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800707
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800708 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800709 (usecase->type == VOIP_CALL) ||
710 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700711 out_snd_device = platform_get_output_snd_device(adev->platform,
712 usecase->stream.out->devices);
713 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700714 usecase->devices = usecase->stream.out->devices;
715 } else {
716 /*
717 * If the voice call is active, use the sound devices of voice call usecase
718 * so that it would not result any device switch. All the usecases will
719 * be switched to new device when select_devices() is called for voice call
720 * usecase. This is to avoid switching devices for voice call when
721 * check_usecases_codec_backend() is called below.
722 */
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700723 if (adev->voice.in_call && adev->mode == AUDIO_MODE_IN_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700724 vc_usecase = get_usecase_from_list(adev,
725 get_voice_usecase_id_from_list(adev));
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700726 if ((vc_usecase) && ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
727 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700728 in_snd_device = vc_usecase->in_snd_device;
729 out_snd_device = vc_usecase->out_snd_device;
730 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800731 } else if (voice_extn_compress_voip_is_active(adev)) {
732 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700733 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Avinash Vaish4d6167d2014-06-25 12:20:37 +0530734 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700735 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800736 in_snd_device = voip_usecase->in_snd_device;
737 out_snd_device = voip_usecase->out_snd_device;
738 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800739 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800740 hfp_ucid = audio_extn_hfp_get_usecase();
741 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700742 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800743 in_snd_device = hfp_usecase->in_snd_device;
744 out_snd_device = hfp_usecase->out_snd_device;
745 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700746 }
747 if (usecase->type == PCM_PLAYBACK) {
748 usecase->devices = usecase->stream.out->devices;
749 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700750 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700751 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700752 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700753 if (usecase->stream.out == adev->primary_output &&
754 adev->active_input &&
755 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
756 select_devices(adev, adev->active_input->usecase);
757 }
758 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700759 } else if (usecase->type == PCM_CAPTURE) {
760 usecase->devices = usecase->stream.in->device;
761 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700762 if (in_snd_device == SND_DEVICE_NONE) {
763 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
764 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700765 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700766 adev->primary_output->devices);
767 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700768 in_snd_device = platform_get_input_snd_device(adev->platform,
769 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700770 }
771 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700772 }
773 }
774
775 if (out_snd_device == usecase->out_snd_device &&
776 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800777 return 0;
778 }
779
sangwoobc677242013-08-08 16:53:43 +0900780 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700781 out_snd_device, platform_get_snd_device_name(out_snd_device),
782 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800783
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800784 /*
785 * Limitation: While in call, to do a device switch we need to disable
786 * and enable both RX and TX devices though one of them is same as current
787 * device.
788 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700789 if ((usecase->type == VOICE_CALL) &&
790 (usecase->in_snd_device != SND_DEVICE_NONE) &&
791 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700792 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800793 }
794
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700795 /* Disable current sound devices */
796 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700797 disable_audio_route(adev, usecase);
798 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800799 }
800
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700801 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700802 disable_audio_route(adev, usecase);
803 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800804 }
805
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800806 /* Applicable only on the targets that has external modem.
807 * New device information should be sent to modem before enabling
808 * the devices to reduce in-call device switch time.
809 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700810 if ((usecase->type == VOICE_CALL) &&
811 (usecase->in_snd_device != SND_DEVICE_NONE) &&
812 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800813 status = platform_switch_voice_call_enable_device_config(adev->platform,
814 out_snd_device,
815 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700816 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800817
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700818 /* Enable new sound devices */
819 if (out_snd_device != SND_DEVICE_NONE) {
820 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
821 check_usecases_codec_backend(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700822 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800823 }
824
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700825 if (in_snd_device != SND_DEVICE_NONE) {
826 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700827 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700828 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700829
Avinash Vaish71a8b972014-07-24 15:36:33 +0530830 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700831 status = platform_switch_voice_call_device_post(adev->platform,
832 out_snd_device,
833 in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +0530834 enable_audio_route_for_voice_usecases(adev, usecase);
835 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800836
sangwoo170731f2013-06-08 15:36:36 +0900837 usecase->in_snd_device = in_snd_device;
838 usecase->out_snd_device = out_snd_device;
839
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530840 if (usecase->type == PCM_PLAYBACK) {
841 if ((24 == usecase->stream.out->bit_width) &&
842 (AUDIO_DEVICE_OUT_SPEAKER == usecase->stream.out->devices)) {
843 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
844 &adev->streams_output_cfg_list,
845 usecase->stream.out->flags,
846 usecase->stream.out->format,
847 DEFAULT_OUTPUT_SAMPLING_RATE,
848 usecase->stream.out->bit_width,
849 &usecase->stream.out->app_type_cfg);
850 } else {
851 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
852 &adev->streams_output_cfg_list,
853 usecase->stream.out->flags,
854 usecase->stream.out->format,
855 usecase->stream.out->sample_rate,
856 usecase->stream.out->bit_width,
857 &usecase->stream.out->app_type_cfg);
858 }
859 ALOGI("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
860 }
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700861 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900862
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800863 /* Applicable only on the targets that has external modem.
864 * Enable device command should be sent to modem only after
865 * enabling voice call mixer controls
866 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -0700867 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800868 status = platform_switch_voice_call_usecase_route_post(adev->platform,
869 out_snd_device,
870 in_snd_device);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530871 ALOGD("%s: done",__func__);
872
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800873 return status;
874}
875
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800876static int stop_input_stream(struct stream_in *in)
877{
878 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800879 struct audio_usecase *uc_info;
880 struct audio_device *adev = in->dev;
881
Eric Laurentc8400632013-02-14 19:04:54 -0800882 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800883
Eric Laurent994a6932013-07-17 11:51:42 -0700884 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700885 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800886 uc_info = get_usecase_from_list(adev, in->usecase);
887 if (uc_info == NULL) {
888 ALOGE("%s: Could not find the usecase (%d) in the list",
889 __func__, in->usecase);
890 return -EINVAL;
891 }
892
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800893 /* Close in-call recording streams */
894 voice_check_and_stop_incall_rec_usecase(adev, in);
895
Eric Laurent150dbfe2013-02-27 14:31:02 -0800896 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700897 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700898
899 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700900 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800901
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800902 list_remove(&uc_info->list);
903 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800904
Eric Laurent994a6932013-07-17 11:51:42 -0700905 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800906 return ret;
907}
908
909int start_input_stream(struct stream_in *in)
910{
911 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800912 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800913 struct audio_usecase *uc_info;
914 struct audio_device *adev = in->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +0530915 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800916
Mingming Yine62d7842013-10-25 16:26:03 -0700917 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530918 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
919 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700920
Naresh Tanniru80659832014-06-04 18:17:56 +0530921
922 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +0530923 ALOGE("%s: sound card is not active/SSR returning error", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +0530924 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +0530925 goto error_config;
926 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530927
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700928 /* Check if source matches incall recording usecase criteria */
929 ret = voice_check_and_set_incall_rec_usecase(adev, in);
930 if (ret)
931 goto error_config;
932 else
933 ALOGV("%s: usecase(%d)", __func__, in->usecase);
934
Eric Laurentb23d5282013-05-14 15:27:20 -0700935 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800936 if (in->pcm_device_id < 0) {
937 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
938 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800939 ret = -EINVAL;
940 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800941 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700942
943 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800944 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700945
946 if (!uc_info) {
947 ret = -ENOMEM;
948 goto error_config;
949 }
950
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800951 uc_info->id = in->usecase;
952 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800953 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700954 uc_info->devices = in->device;
955 uc_info->in_snd_device = SND_DEVICE_NONE;
956 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800957
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800958 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700959 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800960
Eric Laurentc8400632013-02-14 19:04:54 -0800961 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800962 __func__, adev->snd_card,
963 in->pcm_device_id, in->config.channels);
964 in->pcm = pcm_open(adev->snd_card,
965 in->pcm_device_id, PCM_IN, &in->config);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800966 if (in->pcm && !pcm_is_ready(in->pcm)) {
967 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
968 pcm_close(in->pcm);
969 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800970 ret = -EIO;
971 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800972 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530973
Eric Laurent994a6932013-07-17 11:51:42 -0700974 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800975 return ret;
976
977error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800978 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800979
980error_config:
981 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700982 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800983
984 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800985}
986
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700987/* must be called with out->lock locked */
988static int send_offload_cmd_l(struct stream_out* out, int command)
989{
990 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
991
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700992 if (!cmd) {
993 ALOGE("failed to allocate mem for command 0x%x", command);
994 return -ENOMEM;
995 }
996
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700997 ALOGVV("%s %d", __func__, command);
998
999 cmd->cmd = command;
1000 list_add_tail(&out->offload_cmd_list, &cmd->node);
1001 pthread_cond_signal(&out->offload_cond);
1002 return 0;
1003}
1004
1005/* must be called iwth out->lock locked */
1006static void stop_compressed_output_l(struct stream_out *out)
1007{
1008 out->offload_state = OFFLOAD_STATE_IDLE;
1009 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001010 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001011 if (out->compr != NULL) {
1012 compress_stop(out->compr);
1013 while (out->offload_thread_blocked) {
1014 pthread_cond_wait(&out->cond, &out->lock);
1015 }
1016 }
1017}
1018
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001019bool is_offload_usecase(audio_usecase_t uc_id)
1020{
1021 unsigned int i;
1022 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1023 if (uc_id == offload_usecases[i])
1024 return true;
1025 }
1026 return false;
1027}
1028
1029static audio_usecase_t get_offload_usecase(struct audio_device *adev)
1030{
1031 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1032 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
1033 char value[PROPERTY_VALUE_MAX] = {0};
1034
1035 property_get("audio.offload.multiple.enabled", value, NULL);
1036 if (!(atoi(value) || !strncmp("true", value, 4)))
1037 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
1038
1039 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
1040 for (i = 0; i < num_usecase; i++) {
1041 if (!(adev->offload_usecases_state & (0x1<<i))) {
1042 adev->offload_usecases_state |= 0x1 << i;
1043 ret = offload_usecases[i];
1044 break;
1045 }
1046 }
1047 ALOGV("%s: offload usecase is %d", __func__, ret);
1048 return ret;
1049}
1050
1051static void free_offload_usecase(struct audio_device *adev,
1052 audio_usecase_t uc_id)
1053{
1054 unsigned int i;
1055 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1056 if (offload_usecases[i] == uc_id) {
1057 adev->offload_usecases_state &= ~(0x1<<i);
1058 break;
1059 }
1060 }
1061 ALOGV("%s: free offload usecase %d", __func__, uc_id);
1062}
1063
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001064static void *offload_thread_loop(void *context)
1065{
1066 struct stream_out *out = (struct stream_out *) context;
1067 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001068 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001069
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001070 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1071 set_sched_policy(0, SP_FOREGROUND);
1072 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1073
1074 ALOGV("%s", __func__);
1075 pthread_mutex_lock(&out->lock);
1076 for (;;) {
1077 struct offload_cmd *cmd = NULL;
1078 stream_callback_event_t event;
1079 bool send_callback = false;
1080
1081 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1082 __func__, list_empty(&out->offload_cmd_list),
1083 out->offload_state);
1084 if (list_empty(&out->offload_cmd_list)) {
1085 ALOGV("%s SLEEPING", __func__);
1086 pthread_cond_wait(&out->offload_cond, &out->lock);
1087 ALOGV("%s RUNNING", __func__);
1088 continue;
1089 }
1090
1091 item = list_head(&out->offload_cmd_list);
1092 cmd = node_to_item(item, struct offload_cmd, node);
1093 list_remove(item);
1094
1095 ALOGVV("%s STATE %d CMD %d out->compr %p",
1096 __func__, out->offload_state, cmd->cmd, out->compr);
1097
1098 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1099 free(cmd);
1100 break;
1101 }
1102
1103 if (out->compr == NULL) {
1104 ALOGE("%s: Compress handle is NULL", __func__);
1105 pthread_cond_signal(&out->cond);
1106 continue;
1107 }
1108 out->offload_thread_blocked = true;
1109 pthread_mutex_unlock(&out->lock);
1110 send_callback = false;
1111 switch(cmd->cmd) {
1112 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001113 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001114 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001115 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001116 send_callback = true;
1117 event = STREAM_CBK_EVENT_WRITE_READY;
1118 break;
1119 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001120 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301121 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001122 ALOGD("copl(%p):calling compress_partial_drain", out);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001123 compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001124 ALOGD("copl(%p):out of compress_partial_drain", out);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301125 }
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001126 else if(ret == -ETIMEDOUT)
1127 compress_drain(out->compr);
1128 else
1129 ALOGE("%s: Next track returned error %d",__func__, ret);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001130 send_callback = true;
1131 event = STREAM_CBK_EVENT_DRAIN_READY;
1132 break;
1133 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001134 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001135 compress_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001136 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001137 send_callback = true;
1138 event = STREAM_CBK_EVENT_DRAIN_READY;
1139 break;
1140 default:
1141 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1142 break;
1143 }
1144 pthread_mutex_lock(&out->lock);
1145 out->offload_thread_blocked = false;
1146 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001147 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001148 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001149 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001150 free(cmd);
1151 }
1152
1153 pthread_cond_signal(&out->cond);
1154 while (!list_empty(&out->offload_cmd_list)) {
1155 item = list_head(&out->offload_cmd_list);
1156 list_remove(item);
1157 free(node_to_item(item, struct offload_cmd, node));
1158 }
1159 pthread_mutex_unlock(&out->lock);
1160
1161 return NULL;
1162}
1163
1164static int create_offload_callback_thread(struct stream_out *out)
1165{
1166 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1167 list_init(&out->offload_cmd_list);
1168 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1169 offload_thread_loop, out);
1170 return 0;
1171}
1172
1173static int destroy_offload_callback_thread(struct stream_out *out)
1174{
1175 pthread_mutex_lock(&out->lock);
1176 stop_compressed_output_l(out);
1177 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1178
1179 pthread_mutex_unlock(&out->lock);
1180 pthread_join(out->offload_thread, (void **) NULL);
1181 pthread_cond_destroy(&out->offload_cond);
1182
1183 return 0;
1184}
1185
Eric Laurent07eeafd2013-10-06 12:52:49 -07001186static bool allow_hdmi_channel_config(struct audio_device *adev)
1187{
1188 struct listnode *node;
1189 struct audio_usecase *usecase;
1190 bool ret = true;
1191
1192 list_for_each(node, &adev->usecase_list) {
1193 usecase = node_to_item(node, struct audio_usecase, list);
1194 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1195 /*
1196 * If voice call is already existing, do not proceed further to avoid
1197 * disabling/enabling both RX and TX devices, CSD calls, etc.
1198 * Once the voice call done, the HDMI channels can be configured to
1199 * max channels of remaining use cases.
1200 */
1201 if (usecase->id == USECASE_VOICE_CALL) {
1202 ALOGD("%s: voice call is active, no change in HDMI channels",
1203 __func__);
1204 ret = false;
1205 break;
1206 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1207 ALOGD("%s: multi channel playback is active, "
1208 "no change in HDMI channels", __func__);
1209 ret = false;
1210 break;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001211 } else if (is_offload_usecase(usecase->id) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07001212 audio_channel_count_from_out_mask(usecase->stream.out->channel_mask) > 2) {
Mingming Yin139f1072014-02-24 17:56:01 -08001213 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1214 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1215 ret = false;
1216 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001217 }
1218 }
1219 }
1220 return ret;
1221}
1222
1223static int check_and_set_hdmi_channels(struct audio_device *adev,
1224 unsigned int channels)
1225{
1226 struct listnode *node;
1227 struct audio_usecase *usecase;
1228
1229 /* Check if change in HDMI channel config is allowed */
1230 if (!allow_hdmi_channel_config(adev))
1231 return 0;
1232
1233 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001234 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001235 return 0;
1236 }
1237
1238 platform_set_hdmi_channels(adev->platform, channels);
1239 adev->cur_hdmi_channels = channels;
1240
1241 /*
1242 * Deroute all the playback streams routed to HDMI so that
1243 * the back end is deactivated. Note that backend will not
1244 * be deactivated if any one stream is connected to it.
1245 */
1246 list_for_each(node, &adev->usecase_list) {
1247 usecase = node_to_item(node, struct audio_usecase, list);
1248 if (usecase->type == PCM_PLAYBACK &&
1249 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001250 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001251 }
1252 }
1253
1254 /*
1255 * Enable all the streams disabled above. Now the HDMI backend
1256 * will be activated with new channel configuration
1257 */
1258 list_for_each(node, &adev->usecase_list) {
1259 usecase = node_to_item(node, struct audio_usecase, list);
1260 if (usecase->type == PCM_PLAYBACK &&
1261 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001262 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001263 }
1264 }
1265
1266 return 0;
1267}
1268
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001269static int stop_output_stream(struct stream_out *out)
1270{
1271 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001272 struct audio_usecase *uc_info;
1273 struct audio_device *adev = out->dev;
1274
Eric Laurent994a6932013-07-17 11:51:42 -07001275 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001276 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277 uc_info = get_usecase_from_list(adev, out->usecase);
1278 if (uc_info == NULL) {
1279 ALOGE("%s: Could not find the usecase (%d) in the list",
1280 __func__, out->usecase);
1281 return -EINVAL;
1282 }
1283
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001284 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001285 if (adev->visualizer_stop_output != NULL)
1286 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1287 if (adev->offload_effects_stop_output != NULL)
1288 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1289 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001290
Eric Laurent150dbfe2013-02-27 14:31:02 -08001291 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001292 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001293
1294 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001295 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001296
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001297 list_remove(&uc_info->list);
1298 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001299
Eric Laurent07eeafd2013-10-06 12:52:49 -07001300 /* Must be called after removing the usecase from list */
1301 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1302 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1303
Eric Laurent994a6932013-07-17 11:51:42 -07001304 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001305 return ret;
1306}
1307
1308int start_output_stream(struct stream_out *out)
1309{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001310 int ret = 0;
Mingming Yin9c041392014-05-01 15:37:31 -07001311 int sink_channels = 0;
1312 char prop_value[PROPERTY_VALUE_MAX] = {0};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001313 struct audio_usecase *uc_info;
1314 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301315 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001316
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001317 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
1318 ret = -EINVAL;
1319 goto error_config;
1320 }
1321
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301322 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1323 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1324 out->devices);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301325
Naresh Tanniru80659832014-06-04 18:17:56 +05301326 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301327 ALOGE("%s: sound card is not active/SSR returning error", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05301328 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301329 goto error_config;
1330 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05301331
Eric Laurentb23d5282013-05-14 15:27:20 -07001332 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001333 if (out->pcm_device_id < 0) {
1334 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1335 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001336 ret = -EINVAL;
1337 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001338 }
1339
1340 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001341
1342 if (!uc_info) {
1343 ret = -ENOMEM;
1344 goto error_config;
1345 }
1346
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001347 uc_info->id = out->usecase;
1348 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001349 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001350 uc_info->devices = out->devices;
1351 uc_info->in_snd_device = SND_DEVICE_NONE;
1352 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001353
Eric Laurent07eeafd2013-10-06 12:52:49 -07001354 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001355 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Mingming Yin9c041392014-05-01 15:37:31 -07001356 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1357 if (!strncmp("true", prop_value, 4)) {
1358 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1359 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1360 check_and_set_hdmi_channels(adev, sink_channels);
1361 } else {
1362 if (is_offload_usecase(out->usecase))
1363 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1364 else
1365 check_and_set_hdmi_channels(adev, out->config.channels);
1366 }
Mingming Yin10fef6a2013-11-26 17:17:01 -08001367 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001368
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001369 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001370
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001371 select_devices(adev, out->usecase);
1372
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001373 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1374 __func__, 0, out->pcm_device_id);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001375 if (!is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001376 out->pcm = pcm_open(adev->snd_card,
1377 out->pcm_device_id,
1378 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001379 if (out->pcm && !pcm_is_ready(out->pcm)) {
1380 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1381 pcm_close(out->pcm);
1382 out->pcm = NULL;
1383 ret = -EIO;
1384 goto error_open;
1385 }
1386 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001387 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001388 out->compr = compress_open(adev->snd_card,
1389 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001390 COMPRESS_IN, &out->compr_config);
1391 if (out->compr && !is_compress_ready(out->compr)) {
1392 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1393 compress_close(out->compr);
1394 out->compr = NULL;
1395 ret = -EIO;
1396 goto error_open;
1397 }
1398 if (out->offload_callback)
1399 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001400
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001401#ifdef DS1_DOLBY_DDP_ENABLED
1402 if (audio_extn_is_dolby_format(out->format))
1403 audio_extn_dolby_send_ddp_endp_params(adev);
1404#endif
1405
Eric Laurentc4aef752013-09-12 17:45:53 -07001406 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001407 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1408 if (adev->offload_effects_start_output != NULL)
1409 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001410 }
Eric Laurent994a6932013-07-17 11:51:42 -07001411 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001412 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001413error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001414 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001415error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001416 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001417}
1418
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001419static int check_input_parameters(uint32_t sample_rate,
1420 audio_format_t format,
1421 int channel_count)
1422{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001423 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001424
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001425 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001426 !voice_extn_compress_voip_is_format_supported(format) &&
1427 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001428
1429 switch (channel_count) {
1430 case 1:
1431 case 2:
1432 case 6:
1433 break;
1434 default:
1435 ret = -EINVAL;
1436 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001437
1438 switch (sample_rate) {
1439 case 8000:
1440 case 11025:
1441 case 12000:
1442 case 16000:
1443 case 22050:
1444 case 24000:
1445 case 32000:
1446 case 44100:
1447 case 48000:
1448 break;
1449 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001450 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001451 }
1452
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001453 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001454}
1455
1456static size_t get_input_buffer_size(uint32_t sample_rate,
1457 audio_format_t format,
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001458 int channel_count,
1459 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001460{
1461 size_t size = 0;
1462
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001463 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1464 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001465
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001466 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001467 if (is_low_latency)
1468 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001469 /* ToDo: should use frame_size computed based on the format and
1470 channel_count here. */
1471 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001472
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07001473 /* make sure the size is multiple of 32 bytes
1474 * At 48 kHz mono 16-bit PCM:
1475 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1476 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1477 */
1478 size += 0x1f;
1479 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001480
1481 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001482}
1483
1484static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1485{
1486 struct stream_out *out = (struct stream_out *)stream;
1487
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001488 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001489}
1490
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001491static int out_set_sample_rate(struct audio_stream *stream __unused,
1492 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001493{
1494 return -ENOSYS;
1495}
1496
1497static size_t out_get_buffer_size(const struct audio_stream *stream)
1498{
1499 struct stream_out *out = (struct stream_out *)stream;
1500
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001501 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001502 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001503 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1504 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001505
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07001506 return out->config.period_size *
1507 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001508}
1509
1510static uint32_t out_get_channels(const struct audio_stream *stream)
1511{
1512 struct stream_out *out = (struct stream_out *)stream;
1513
1514 return out->channel_mask;
1515}
1516
1517static audio_format_t out_get_format(const struct audio_stream *stream)
1518{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001519 struct stream_out *out = (struct stream_out *)stream;
1520
1521 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001522}
1523
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001524static int out_set_format(struct audio_stream *stream __unused,
1525 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001526{
1527 return -ENOSYS;
1528}
1529
1530static int out_standby(struct audio_stream *stream)
1531{
1532 struct stream_out *out = (struct stream_out *)stream;
1533 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001534
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301535 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1536 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001537 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1538 /* Ignore standby in case of voip call because the voip output
1539 * stream is closed in adev_close_output_stream()
1540 */
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301541 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001542 return 0;
1543 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001544
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001545 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001546 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001547 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001548 out->standby = true;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001549 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001550 if (out->pcm) {
1551 pcm_close(out->pcm);
1552 out->pcm = NULL;
1553 }
1554 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001555 ALOGD("copl(%p):standby", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001556 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001557 out->gapless_mdata.encoder_delay = 0;
1558 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001559 if (out->compr != NULL) {
1560 compress_close(out->compr);
1561 out->compr = NULL;
1562 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001563 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001564 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001565 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001566 }
1567 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001568 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001569 return 0;
1570}
1571
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001572static int out_dump(const struct audio_stream *stream __unused,
1573 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001574{
1575 return 0;
1576}
1577
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001578static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1579{
1580 int ret = 0;
1581 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001582 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001583 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001584 tmp_mdata.encoder_delay = 0;
1585 tmp_mdata.encoder_padding = 0;
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001586
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001587 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001588 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001589 return -EINVAL;
1590 }
1591
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001592 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1593 if (ret >= 0) {
1594 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1595 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1596 ALOGV("ADTS format is set in offload mode");
1597 }
1598 out->send_new_metadata = 1;
1599 }
1600
Mingming Yin3ee55c62014-08-04 14:23:35 -07001601 if (out->format == AUDIO_FORMAT_FLAC) {
1602 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_BLK_SIZE, value, sizeof(value));
1603 if (ret >= 0) {
1604 out->compr_config.codec->options.flac_dec.min_blk_size = atoi(value);
1605 out->send_new_metadata = 1;
1606 }
1607 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_BLK_SIZE, value, sizeof(value));
1608 if (ret >= 0) {
1609 out->compr_config.codec->options.flac_dec.max_blk_size = atoi(value);
1610 out->send_new_metadata = 1;
1611 }
1612 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_FRAME_SIZE, value, sizeof(value));
1613 if (ret >= 0) {
1614 out->compr_config.codec->options.flac_dec.min_frame_size = atoi(value);
1615 out->send_new_metadata = 1;
1616 }
1617 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_FRAME_SIZE, value, sizeof(value));
1618 if (ret >= 0) {
1619 out->compr_config.codec->options.flac_dec.max_frame_size = atoi(value);
1620 out->send_new_metadata = 1;
1621 }
1622 }
1623
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001624 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1625 if(ret >= 0)
1626 is_meta_data_params = true;
1627 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1628 if(ret >= 0 )
1629 is_meta_data_params = true;
1630 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1631 if(ret >= 0 )
1632 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001633 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1634 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001635 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001636 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001637 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001638 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1639 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001640 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001641 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001642 }
1643
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001644 if(!is_meta_data_params) {
1645 ALOGV("%s: Not gapless meta data params", __func__);
1646 return 0;
1647 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001648 out->gapless_mdata = tmp_mdata;
1649 out->send_new_metadata = 1;
1650 ALOGV("%s new encoder delay %u and padding %u", __func__,
1651 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1652
1653 return 0;
1654}
1655
1656
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001657static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1658{
1659 struct stream_out *out = (struct stream_out *)stream;
1660 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001661 struct audio_usecase *usecase;
1662 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001663 struct str_parms *parms;
1664 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001665 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001666 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001667
sangwoobc677242013-08-08 16:53:43 +09001668 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001669 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001670 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301671 if (!parms)
1672 goto error;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001673 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1674 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001675 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001676 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001677 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001678
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001679 /*
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301680 * When HDMI cable is unplugged/usb hs is disconnected the
1681 * music playback is paused and the policy manager sends routing=0
1682 * But the audioflingercontinues to write data until standby time
1683 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001684 * Avoid this by routing audio to speaker until standby.
1685 */
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301686 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1687 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001688 val == AUDIO_DEVICE_NONE) {
1689 val = AUDIO_DEVICE_OUT_SPEAKER;
1690 }
1691
1692 /*
1693 * select_devices() call below switches all the usecases on the same
1694 * backend to the new device. Refer to check_usecases_codec_backend() in
1695 * the select_devices(). But how do we undo this?
1696 *
1697 * For example, music playback is active on headset (deep-buffer usecase)
1698 * and if we go to ringtones and select a ringtone, low-latency usecase
1699 * will be started on headset+speaker. As we can't enable headset+speaker
1700 * and headset devices at the same time, select_devices() switches the music
1701 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1702 * So when the ringtone playback is completed, how do we undo the same?
1703 *
1704 * We are relying on the out_set_parameters() call on deep-buffer output,
1705 * once the ringtone playback is ended.
1706 * NOTE: We should not check if the current devices are same as new devices.
1707 * Because select_devices() must be called to switch back the music
1708 * playback to headset.
1709 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001710 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001711 out->devices = val;
1712
1713 if (!out->standby)
1714 select_devices(adev, out->usecase);
1715
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001716 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001717 !adev->voice.in_call &&
1718 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001719 ret = voice_start_call(adev);
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001720 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1721 adev->voice.in_call &&
1722 (out == adev->primary_output)) {
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -08001723 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001724 }
1725 }
1726
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001727 if ((adev->mode == AUDIO_MODE_NORMAL) &&
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001728 adev->voice.in_call &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001729 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001730 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001731 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001732
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001733 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001734 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001735 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001736
1737 if (out == adev->primary_output) {
1738 pthread_mutex_lock(&adev->lock);
1739 audio_extn_set_parameters(adev, parms);
1740 pthread_mutex_unlock(&adev->lock);
1741 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001742 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001743 pthread_mutex_lock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001744 parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001745 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001746 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001747
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001748 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301749error:
Eric Laurent994a6932013-07-17 11:51:42 -07001750 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001751 return ret;
1752}
1753
1754static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1755{
1756 struct stream_out *out = (struct stream_out *)stream;
1757 struct str_parms *query = str_parms_create_str(keys);
1758 char *str;
1759 char value[256];
1760 struct str_parms *reply = str_parms_create();
1761 size_t i, j;
1762 int ret;
1763 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001764
1765 if (!query || !reply) {
1766 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
1767 return NULL;
1768 }
1769
Eric Laurent994a6932013-07-17 11:51:42 -07001770 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001771 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1772 if (ret >= 0) {
1773 value[0] = '\0';
1774 i = 0;
1775 while (out->supported_channel_masks[i] != 0) {
1776 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1777 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1778 if (!first) {
1779 strcat(value, "|");
1780 }
1781 strcat(value, out_channels_name_to_enum_table[j].name);
1782 first = false;
1783 break;
1784 }
1785 }
1786 i++;
1787 }
1788 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1789 str = str_parms_to_str(reply);
1790 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001791 voice_extn_out_get_parameters(out, query, reply);
1792 str = str_parms_to_str(reply);
1793 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella29b8fc72014-01-29 12:52:19 -08001794 free(str);
1795 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001796 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001797 }
1798 str_parms_destroy(query);
1799 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001800 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001801 return str;
1802}
1803
1804static uint32_t out_get_latency(const struct audio_stream_out *stream)
1805{
1806 struct stream_out *out = (struct stream_out *)stream;
1807
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001808 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001809 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1810
1811 return (out->config.period_count * out->config.period_size * 1000) /
1812 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001813}
1814
1815static int out_set_volume(struct audio_stream_out *stream, float left,
1816 float right)
1817{
Eric Laurenta9024de2013-04-04 09:19:12 -07001818 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001819 int volume[2];
1820
Eric Laurenta9024de2013-04-04 09:19:12 -07001821 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1822 /* only take left channel into account: the API is for stereo anyway */
1823 out->muted = (left == 0.0f);
1824 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001825 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001826 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001827 struct audio_device *adev = out->dev;
1828 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001829 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1830 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001831
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001832 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1833 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001834 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1835 if (!ctl) {
1836 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1837 __func__, mixer_ctl_name);
1838 return -EINVAL;
1839 }
1840 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1841 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1842 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1843 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001844 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001845
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001846 return -ENOSYS;
1847}
1848
1849static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1850 size_t bytes)
1851{
1852 struct stream_out *out = (struct stream_out *)stream;
1853 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301854 int snd_scard_state = get_snd_card_state(adev);
Eric Laurent6e895242013-09-05 16:10:57 -07001855 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001856
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001857 pthread_mutex_lock(&out->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301858
Naresh Tanniru80659832014-06-04 18:17:56 +05301859 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
1860 if (out->pcm) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301861 ALOGD(" %s: sound card is not active/SSR state", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05301862 ret= -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301863 goto exit;
Naresh Tanniru80659832014-06-04 18:17:56 +05301864 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1865 //during SSR for compress usecase we should return error to flinger
1866 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
1867 pthread_mutex_unlock(&out->lock);
1868 return -ENETRESET;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301869 }
1870 }
1871
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001872 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001873 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001874 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001875 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1876 ret = voice_extn_compress_voip_start_output_stream(out);
1877 else
1878 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001879 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001880 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001881 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001882 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001883 goto exit;
1884 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001885 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001886
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001887 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001888 ALOGD("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001889 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001890 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001891 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1892 out->send_new_metadata = 0;
1893 }
1894
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001895 ret = compress_write(out->compr, buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301896 if (ret < 0)
1897 ret = -errno;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001898 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001899 if (ret >= 0 && ret < (ssize_t)bytes) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301900 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001901 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05301902 } else if (-ENETRESET == ret) {
1903 ALOGE("copl %s: received sound card offline state on compress write", __func__);
1904 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1905 pthread_mutex_unlock(&out->lock);
1906 out_standby(&out->stream.common);
1907 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001908 }
Naresh Tanniru80659832014-06-04 18:17:56 +05301909 if (!out->playback_started && ret >= 0) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001910 compress_start(out->compr);
1911 out->playback_started = 1;
1912 out->offload_state = OFFLOAD_STATE_PLAYING;
1913 }
1914 pthread_mutex_unlock(&out->lock);
1915 return ret;
1916 } else {
1917 if (out->pcm) {
1918 if (out->muted)
1919 memset((void *)buffer, 0, bytes);
1920 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1921 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301922 if (ret < 0)
1923 ret = -errno;
1924 else if (ret == 0)
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001925 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001926 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001927 }
1928
1929exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05301930 /* ToDo: There may be a corner case when SSR happens back to back during
1931 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05301932 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301933 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301934 }
1935
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001936 pthread_mutex_unlock(&out->lock);
1937
1938 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001939 if (out->pcm)
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001940 ALOGE("%s: error %ld - %s", __func__, ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301941 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05301942 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301943 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05301944 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301945 out->standby = true;
1946 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001947 out_standby(&out->stream.common);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07001948 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05301949 out_get_sample_rate(&out->stream.common));
1950
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001951 }
1952 return bytes;
1953}
1954
1955static int out_get_render_position(const struct audio_stream_out *stream,
1956 uint32_t *dsp_frames)
1957{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001958 struct stream_out *out = (struct stream_out *)stream;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001959 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301960 ssize_t ret = -EINVAL;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001961 *dsp_frames = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001962 pthread_mutex_lock(&out->lock);
1963 if (out->compr != NULL) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301964 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001965 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301966 if (ret < 0)
1967 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001968 ALOGVV("%s rendered frames %d sample_rate %d",
1969 __func__, *dsp_frames, out->sample_rate);
1970 }
1971 pthread_mutex_unlock(&out->lock);
Naresh Tanniru80659832014-06-04 18:17:56 +05301972 if (-ENETRESET == ret) {
1973 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
1974 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1975 return -EINVAL;
1976 } else if(ret < 0) {
1977 ALOGE(" ERROR: Unable to get time stamp from compress driver");
1978 return -EINVAL;
1979 } else {
1980 return 0;
1981 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001982 } else
1983 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001984}
1985
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001986static int out_add_audio_effect(const struct audio_stream *stream __unused,
1987 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001988{
1989 return 0;
1990}
1991
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001992static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1993 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001994{
1995 return 0;
1996}
1997
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001998static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1999 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000{
2001 return -EINVAL;
2002}
2003
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002004static int out_get_presentation_position(const struct audio_stream_out *stream,
2005 uint64_t *frames, struct timespec *timestamp)
2006{
2007 struct stream_out *out = (struct stream_out *)stream;
2008 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07002009 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002010
2011 pthread_mutex_lock(&out->lock);
2012
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002013 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07002014 if (out->compr != NULL) {
2015 compress_get_tstamp(out->compr, &dsp_frames,
2016 &out->sample_rate);
2017 ALOGVV("%s rendered frames %ld sample_rate %d",
2018 __func__, dsp_frames, out->sample_rate);
2019 *frames = dsp_frames;
2020 ret = 0;
2021 /* this is the best we can do */
2022 clock_gettime(CLOCK_MONOTONIC, timestamp);
2023 }
2024 } else {
2025 if (out->pcm) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002026 unsigned int avail;
Eric Laurent949a0892013-09-20 09:20:13 -07002027 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
2028 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07002029 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07002030 // This adjustment accounts for buffering after app processor.
2031 // It is based on estimated DSP latency per use case, rather than exact.
2032 signed_frames -=
2033 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
2034
Eric Laurent949a0892013-09-20 09:20:13 -07002035 // It would be unusual for this value to be negative, but check just in case ...
2036 if (signed_frames >= 0) {
2037 *frames = signed_frames;
2038 ret = 0;
2039 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002040 }
2041 }
2042 }
2043
2044 pthread_mutex_unlock(&out->lock);
2045
2046 return ret;
2047}
2048
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002049static int out_set_callback(struct audio_stream_out *stream,
2050 stream_callback_t callback, void *cookie)
2051{
2052 struct stream_out *out = (struct stream_out *)stream;
2053
2054 ALOGV("%s", __func__);
2055 pthread_mutex_lock(&out->lock);
2056 out->offload_callback = callback;
2057 out->offload_cookie = cookie;
2058 pthread_mutex_unlock(&out->lock);
2059 return 0;
2060}
2061
2062static int out_pause(struct audio_stream_out* stream)
2063{
2064 struct stream_out *out = (struct stream_out *)stream;
2065 int status = -ENOSYS;
2066 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002067 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002068 ALOGD("copl(%p):pause compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002069 pthread_mutex_lock(&out->lock);
2070 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302071 struct audio_device *adev = out->dev;
2072 int snd_scard_state = get_snd_card_state(adev);
2073
2074 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2075 status = compress_pause(out->compr);
2076
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002077 out->offload_state = OFFLOAD_STATE_PAUSED;
2078 }
2079 pthread_mutex_unlock(&out->lock);
2080 }
2081 return status;
2082}
2083
2084static int out_resume(struct audio_stream_out* stream)
2085{
2086 struct stream_out *out = (struct stream_out *)stream;
2087 int status = -ENOSYS;
2088 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002089 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002090 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002091 status = 0;
2092 pthread_mutex_lock(&out->lock);
2093 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302094 struct audio_device *adev = out->dev;
2095 int snd_scard_state = get_snd_card_state(adev);
2096
2097 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2098 status = compress_resume(out->compr);
2099
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002100 out->offload_state = OFFLOAD_STATE_PLAYING;
2101 }
2102 pthread_mutex_unlock(&out->lock);
2103 }
2104 return status;
2105}
2106
2107static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2108{
2109 struct stream_out *out = (struct stream_out *)stream;
2110 int status = -ENOSYS;
2111 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002112 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002113 pthread_mutex_lock(&out->lock);
2114 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2115 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2116 else
2117 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2118 pthread_mutex_unlock(&out->lock);
2119 }
2120 return status;
2121}
2122
2123static int out_flush(struct audio_stream_out* stream)
2124{
2125 struct stream_out *out = (struct stream_out *)stream;
2126 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002127 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002128 ALOGD("copl(%p):calling compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002129 pthread_mutex_lock(&out->lock);
2130 stop_compressed_output_l(out);
2131 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002132 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002133 return 0;
2134 }
2135 return -ENOSYS;
2136}
2137
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002138/** audio_stream_in implementation **/
2139static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2140{
2141 struct stream_in *in = (struct stream_in *)stream;
2142
2143 return in->config.rate;
2144}
2145
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002146static int in_set_sample_rate(struct audio_stream *stream __unused,
2147 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002148{
2149 return -ENOSYS;
2150}
2151
2152static size_t in_get_buffer_size(const struct audio_stream *stream)
2153{
2154 struct stream_in *in = (struct stream_in *)stream;
2155
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002156 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
2157 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07002158 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
2159 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002160
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002161 return in->config.period_size *
2162 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002163}
2164
2165static uint32_t in_get_channels(const struct audio_stream *stream)
2166{
2167 struct stream_in *in = (struct stream_in *)stream;
2168
2169 return in->channel_mask;
2170}
2171
2172static audio_format_t in_get_format(const struct audio_stream *stream)
2173{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002174 struct stream_in *in = (struct stream_in *)stream;
2175
2176 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002177}
2178
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002179static int in_set_format(struct audio_stream *stream __unused,
2180 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002181{
2182 return -ENOSYS;
2183}
2184
2185static int in_standby(struct audio_stream *stream)
2186{
2187 struct stream_in *in = (struct stream_in *)stream;
2188 struct audio_device *adev = in->dev;
2189 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302190 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2191 stream, in->usecase, use_case_table[in->usecase]);
2192
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002193 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2194 /* Ignore standby in case of voip call because the voip input
2195 * stream is closed in adev_close_input_stream()
2196 */
2197 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2198 return status;
2199 }
2200
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002201 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002202 if (!in->standby && in->is_st_session) {
2203 ALOGD("%s: sound trigger pcm stop lab", __func__);
2204 audio_extn_sound_trigger_stop_lab(in);
2205 in->standby = 1;
2206 }
2207
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002208 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002209 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002210 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002211 if (in->pcm) {
2212 pcm_close(in->pcm);
2213 in->pcm = NULL;
2214 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002215 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002216 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002217 }
2218 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002219 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002220 return status;
2221}
2222
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002223static int in_dump(const struct audio_stream *stream __unused,
2224 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002225{
2226 return 0;
2227}
2228
2229static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2230{
2231 struct stream_in *in = (struct stream_in *)stream;
2232 struct audio_device *adev = in->dev;
2233 struct str_parms *parms;
2234 char *str;
2235 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002236 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002237
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302238 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002239 parms = str_parms_create_str(kvpairs);
2240
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302241 if (!parms)
2242 goto error;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002243 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002244 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002245
2246 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2247 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002248 val = atoi(value);
2249 /* no audio source uses val == 0 */
2250 if ((in->source != val) && (val != 0)) {
2251 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002252 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2253 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2254 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2255 (in->config.rate == 8000 || in->config.rate == 16000) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002256 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002257 err = voice_extn_compress_voip_open_input_stream(in);
2258 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002259 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002260 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002261 }
2262 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002263 }
2264 }
2265
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002266 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2267 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002268 val = atoi(value);
2269 if ((in->device != val) && (val != 0)) {
2270 in->device = val;
2271 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002272 if (!in->standby && !in->is_st_session)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002273 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002274 }
2275 }
2276
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002277done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002278 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002279 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002280
2281 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302282error:
Eric Laurent994a6932013-07-17 11:51:42 -07002283 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002284 return ret;
2285}
2286
2287static char* in_get_parameters(const struct audio_stream *stream,
2288 const char *keys)
2289{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002290 struct stream_in *in = (struct stream_in *)stream;
2291 struct str_parms *query = str_parms_create_str(keys);
2292 char *str;
2293 char value[256];
2294 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002295
2296 if (!query || !reply) {
2297 ALOGE("in_get_parameters: failed to create query or reply");
2298 return NULL;
2299 }
2300
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002301 ALOGV("%s: enter: keys - %s", __func__, keys);
2302
2303 voice_extn_in_get_parameters(in, query, reply);
2304
2305 str = str_parms_to_str(reply);
2306 str_parms_destroy(query);
2307 str_parms_destroy(reply);
2308
2309 ALOGV("%s: exit: returns - %s", __func__, str);
2310 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002311}
2312
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002313static int in_set_gain(struct audio_stream_in *stream __unused,
2314 float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002315{
2316 return 0;
2317}
2318
2319static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2320 size_t bytes)
2321{
2322 struct stream_in *in = (struct stream_in *)stream;
2323 struct audio_device *adev = in->dev;
2324 int i, ret = -1;
Naresh Tanniru80659832014-06-04 18:17:56 +05302325 int snd_scard_state = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002326
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002327 pthread_mutex_lock(&in->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302328
2329 if (in->pcm) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302330 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05302331 ALOGD(" %s: sound card is not active/SSR state", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302332 ret= -EIO;;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302333 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302334 }
2335 }
2336
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002337 if (in->standby) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002338 if (!in->is_st_session) {
2339 pthread_mutex_lock(&adev->lock);
2340 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2341 ret = voice_extn_compress_voip_start_input_stream(in);
2342 else
2343 ret = start_input_stream(in);
2344 pthread_mutex_unlock(&adev->lock);
2345 if (ret != 0) {
2346 goto exit;
2347 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002348 }
2349 in->standby = 0;
2350 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002351
2352 if (in->pcm) {
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002353 if (audio_extn_ssr_get_enabled() &&
2354 audio_channel_count_from_in_mask(in->channel_mask) == 6)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002355 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002356 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2357 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002358 else
2359 ret = pcm_read(in->pcm, buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05302360 if (ret < 0)
2361 ret = -errno;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002362 }
2363
2364 /*
2365 * Instead of writing zeroes here, we could trust the hardware
2366 * to always provide zeroes when muted.
2367 */
kunleizc5a639b2014-04-24 18:46:22 +08002368 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002369 memset(buffer, 0, bytes);
2370
2371exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05302372 /* ToDo: There may be a corner case when SSR happens back to back during
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302373 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05302374 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302375 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302376 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002377 pthread_mutex_unlock(&in->lock);
2378
2379 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302380 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302381 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302382 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302383 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302384 in->standby = true;
2385 }
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302386 memset(buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002387 in_standby(&in->stream.common);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002388 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002389 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05302390 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002391 }
2392 return bytes;
2393}
2394
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002395static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002396{
2397 return 0;
2398}
2399
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002400static int add_remove_audio_effect(const struct audio_stream *stream,
2401 effect_handle_t effect,
2402 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002403{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002404 struct stream_in *in = (struct stream_in *)stream;
2405 int status = 0;
2406 effect_descriptor_t desc;
2407
2408 status = (*effect)->get_descriptor(effect, &desc);
2409 if (status != 0)
2410 return status;
2411
2412 pthread_mutex_lock(&in->lock);
2413 pthread_mutex_lock(&in->dev->lock);
2414 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2415 in->enable_aec != enable &&
2416 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2417 in->enable_aec = enable;
2418 if (!in->standby)
2419 select_devices(in->dev, in->usecase);
2420 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002421 if (in->enable_ns != enable &&
2422 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2423 in->enable_ns = enable;
2424 if (!in->standby)
2425 select_devices(in->dev, in->usecase);
2426 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002427 pthread_mutex_unlock(&in->dev->lock);
2428 pthread_mutex_unlock(&in->lock);
2429
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002430 return 0;
2431}
2432
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002433static int in_add_audio_effect(const struct audio_stream *stream,
2434 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002435{
Eric Laurent994a6932013-07-17 11:51:42 -07002436 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002437 return add_remove_audio_effect(stream, effect, true);
2438}
2439
2440static int in_remove_audio_effect(const struct audio_stream *stream,
2441 effect_handle_t effect)
2442{
Eric Laurent994a6932013-07-17 11:51:42 -07002443 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002444 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002445}
2446
2447static int adev_open_output_stream(struct audio_hw_device *dev,
2448 audio_io_handle_t handle,
2449 audio_devices_t devices,
2450 audio_output_flags_t flags,
2451 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002452 struct audio_stream_out **stream_out,
2453 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002454{
2455 struct audio_device *adev = (struct audio_device *)dev;
2456 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002457 int i, ret = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002458 audio_format_t format;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302459 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002460
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002461 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05302462
2463 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2464 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2465 ALOGE(" sound card is not active rejecting compress output open request");
2466 return -EINVAL;
2467 }
2468
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002469 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2470
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302471 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2472 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2473 devices, flags, &out->stream);
2474
2475
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002476 if (!out) {
2477 return -ENOMEM;
2478 }
2479
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002480 if (devices == AUDIO_DEVICE_NONE)
2481 devices = AUDIO_DEVICE_OUT_SPEAKER;
2482
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002483 out->flags = flags;
2484 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002485 out->dev = adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002486 format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002487 out->sample_rate = config->sample_rate;
2488 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2489 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002490 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002491 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002492
2493 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002494 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2495 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2496 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2497
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002498 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002499 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2500 ret = read_hdmi_channel_masks(out);
2501
2502 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2503 ret = audio_extn_read_afe_proxy_channel_masks(out);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002504 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002505 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002506 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002507
2508 if (config->sample_rate == 0)
2509 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2510 if (config->channel_mask == 0)
2511 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2512
2513 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002514 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002515 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2516 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002517 out->config.rate = config->sample_rate;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002518 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002519 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002520 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2521 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002522 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002523 ret = voice_extn_compress_voip_open_output_stream(out);
2524 if (ret != 0) {
2525 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2526 __func__, ret);
2527 goto error_open;
2528 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002529 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2530 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2531 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2532 ALOGE("%s: Unsupported Offload information", __func__);
2533 ret = -EINVAL;
2534 goto error_open;
2535 }
Mingming Yin90310102013-11-13 16:57:00 -08002536 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002537 !audio_extn_is_dolby_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002538 ALOGE("%s: Unsupported audio format", __func__);
2539 ret = -EINVAL;
2540 goto error_open;
2541 }
2542
2543 out->compr_config.codec = (struct snd_codec *)
2544 calloc(1, sizeof(struct snd_codec));
2545
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002546 if (!out->compr_config.codec) {
2547 ret = -ENOMEM;
2548 goto error_open;
2549 }
2550
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002551 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002552 if (config->offload_info.channel_mask)
2553 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002554 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002555 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002556 config->offload_info.channel_mask = config->channel_mask;
2557 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002558 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002559 out->sample_rate = config->offload_info.sample_rate;
2560
2561 out->stream.set_callback = out_set_callback;
2562 out->stream.pause = out_pause;
2563 out->stream.resume = out_resume;
2564 out->stream.drain = out_drain;
2565 out->stream.flush = out_flush;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002566 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002567
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002568 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002569 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002570 audio_extn_dolby_get_snd_codec_id(adev, out,
2571 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002572 else
2573 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002574 get_snd_codec_id(config->offload_info.format);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002575 if (audio_is_offload_pcm(config->offload_info.format)) {
2576 out->compr_config.fragment_size =
2577 platform_get_pcm_offload_buffer_size(&config->offload_info);
2578 } else {
2579 out->compr_config.fragment_size =
2580 platform_get_compress_offload_buffer_size(&config->offload_info);
2581 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002582 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2583 out->compr_config.codec->sample_rate =
2584 compress_get_alsa_rate(config->offload_info.sample_rate);
2585 out->compr_config.codec->bit_rate =
2586 config->offload_info.bit_rate;
2587 out->compr_config.codec->ch_in =
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002588 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002589 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002590 out->bit_width = PCM_OUTPUT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002591
Mingming Yin3ee55c62014-08-04 14:23:35 -07002592 if (config->offload_info.format == AUDIO_FORMAT_AAC)
2593 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002594 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2595 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002596 if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002597 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2598
Mingming Yin3ee55c62014-08-04 14:23:35 -07002599 if (out->bit_width == 24) {
2600 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2601 }
2602
2603 if (out->bit_width == 24 && !platform_check_24_bit_support()) {
2604 ALOGW("24 bit support is not enabled, using 16 bit backend");
2605 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
2606 }
2607
Amit Shekhar6f461b12014-08-01 14:52:58 -07002608 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
Mingming Yinc9efb4f2014-09-19 12:16:36 -07002609 out->compr_config.codec->options.flac_dec.sample_size = PCM_OUTPUT_BIT_WIDTH;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002610
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002611 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2612 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002613
2614 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002615 out->offload_state = OFFLOAD_STATE_IDLE;
2616 out->playback_started = 0;
2617
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002618 create_offload_callback_thread(out);
2619 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2620 __func__, config->offload_info.version,
2621 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002622 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -08002623 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002624
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002625 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2626 ret = voice_check_and_set_incall_music_usecase(adev, out);
2627 if (ret != 0) {
2628 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2629 __func__, ret);
2630 goto error_open;
2631 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002632 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002633 format = AUDIO_FORMAT_PCM_16_BIT;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002634 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2635 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002636 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002637 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002638 /* primary path is the default path selected if no other outputs are available/suitable */
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002639 format = AUDIO_FORMAT_PCM_16_BIT;
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002640 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002641 out->config = pcm_config_deep_buffer;
2642 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002643 }
2644
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302645 if ((24 == out->bit_width) &&
2646 (devices == AUDIO_DEVICE_OUT_SPEAKER)) {
2647 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2648 ALOGI("%s 24-bit playback on Speaker is allowed ONLY at 48khz. Hence changing sample rate to: %d",
2649 __func__, sample_rate);
2650 } else {
2651 sample_rate = out->sample_rate;
2652 }
2653
2654 ALOGV("%s flags %x, format %x, sample_rate %d, out->bit_width %d",
2655 __func__, flags, format, sample_rate, out->bit_width);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002656 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
2657 &adev->streams_output_cfg_list,
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302658 flags, format, sample_rate,
Amit Shekhar6f461b12014-08-01 14:52:58 -07002659 out->bit_width, &out->app_type_cfg);
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002660 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2661 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2662 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002663 if(adev->primary_output == NULL)
2664 adev->primary_output = out;
2665 else {
2666 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002667 ret = -EEXIST;
2668 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002669 }
2670 }
2671
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002672 /* Check if this usecase is already existing */
2673 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07002674 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2675 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002676 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002677 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002678 ret = -EEXIST;
2679 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002680 }
2681 pthread_mutex_unlock(&adev->lock);
2682
2683 out->stream.common.get_sample_rate = out_get_sample_rate;
2684 out->stream.common.set_sample_rate = out_set_sample_rate;
2685 out->stream.common.get_buffer_size = out_get_buffer_size;
2686 out->stream.common.get_channels = out_get_channels;
2687 out->stream.common.get_format = out_get_format;
2688 out->stream.common.set_format = out_set_format;
2689 out->stream.common.standby = out_standby;
2690 out->stream.common.dump = out_dump;
2691 out->stream.common.set_parameters = out_set_parameters;
2692 out->stream.common.get_parameters = out_get_parameters;
2693 out->stream.common.add_audio_effect = out_add_audio_effect;
2694 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2695 out->stream.get_latency = out_get_latency;
2696 out->stream.set_volume = out_set_volume;
2697 out->stream.write = out_write;
2698 out->stream.get_render_position = out_get_render_position;
2699 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002700 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002701
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002702 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002703 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002704 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002705
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002706 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2707 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2708
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002709 config->format = out->stream.common.get_format(&out->stream.common);
2710 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2711 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2712
2713 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302714 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2715 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002716 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002717 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002718
2719error_open:
2720 free(out);
2721 *stream_out = NULL;
2722 ALOGD("%s: exit: ret %d", __func__, ret);
2723 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002724}
2725
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002726static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002727 struct audio_stream_out *stream)
2728{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002729 struct stream_out *out = (struct stream_out *)stream;
2730 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002731 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002732
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302733 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2734
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002735 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302736 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002737 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302738 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002739 if(ret != 0)
2740 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2741 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002742 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002743 out_standby(&stream->common);
2744
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002745 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002746 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002747 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002748 if (out->compr_config.codec != NULL)
2749 free(out->compr_config.codec);
2750 }
2751 pthread_cond_destroy(&out->cond);
2752 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002753 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002754 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002755}
2756
2757static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2758{
2759 struct audio_device *adev = (struct audio_device *)dev;
2760 struct str_parms *parms;
2761 char *str;
2762 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002763 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002764 int ret;
2765 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002766
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002767 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002768 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002769
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302770 if (!parms)
2771 goto error;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002772 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
2773 if (ret >= 0) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05302774 char *snd_card_status = value+2;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302775 if (strstr(snd_card_status, "OFFLINE")) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302776 struct listnode *node;
2777 struct audio_usecase *usecase;
2778
Naresh Tanniru4c630392014-05-12 01:05:52 +05302779 ALOGD("Received sound card OFFLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302780 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2781
2782 pthread_mutex_lock(&adev->lock);
2783 //close compress session on OFFLINE status
2784 usecase = get_usecase_from_list(adev,USECASE_AUDIO_PLAYBACK_OFFLOAD);
2785 if (usecase && usecase->stream.out) {
2786 ALOGD(" %s closing compress session on OFFLINE state", __func__);
2787
2788 struct stream_out *out = usecase->stream.out;
2789
2790 pthread_mutex_unlock(&adev->lock);
2791 out_standby(&out->stream.common);
2792 } else
2793 pthread_mutex_unlock(&adev->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302794 } else if (strstr(snd_card_status, "ONLINE")) {
2795 ALOGD("Received sound card ONLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302796 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302797 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05302798 }
2799
2800 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002801 status = voice_set_parameters(adev, parms);
2802 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002803 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002804
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002805 status = platform_set_parameters(adev->platform, parms);
2806 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002807 goto done;
2808
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002809 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2810 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07002811 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002812 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2813 adev->bluetooth_nrec = true;
2814 else
2815 adev->bluetooth_nrec = false;
2816 }
2817
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002818 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2819 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002820 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2821 adev->screen_off = false;
2822 else
2823 adev->screen_off = true;
2824 }
2825
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002826 ret = str_parms_get_int(parms, "rotation", &val);
2827 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002828 bool reverse_speakers = false;
2829 switch(val) {
2830 // FIXME: note that the code below assumes that the speakers are in the correct placement
2831 // relative to the user when the device is rotated 90deg from its default rotation. This
2832 // assumption is device-specific, not platform-specific like this code.
2833 case 270:
2834 reverse_speakers = true;
2835 break;
2836 case 0:
2837 case 90:
2838 case 180:
2839 break;
2840 default:
2841 ALOGE("%s: unexpected rotation of %d", __func__, val);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002842 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002843 }
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002844 if (status == 0) {
2845 if (adev->speaker_lr_swap != reverse_speakers) {
2846 adev->speaker_lr_swap = reverse_speakers;
2847 // only update the selected device if there is active pcm playback
2848 struct audio_usecase *usecase;
2849 struct listnode *node;
2850 list_for_each(node, &adev->usecase_list) {
2851 usecase = node_to_item(node, struct audio_usecase, list);
2852 if (usecase->type == PCM_PLAYBACK) {
2853 select_devices(adev, usecase->id);
2854 break;
2855 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002856 }
2857 }
2858 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002859 }
2860
Mingming Yin514a8bc2014-07-29 15:22:21 -07002861 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2862 if (ret >= 0) {
2863 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2864 adev->bt_wb_speech_enabled = true;
2865 else
2866 adev->bt_wb_speech_enabled = false;
2867 }
2868
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002869 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002870
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002871done:
2872 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002873 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302874error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002875 ALOGV("%s: exit with code(%d)", __func__, status);
2876 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002877}
2878
2879static char* adev_get_parameters(const struct audio_hw_device *dev,
2880 const char *keys)
2881{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002882 struct audio_device *adev = (struct audio_device *)dev;
2883 struct str_parms *reply = str_parms_create();
2884 struct str_parms *query = str_parms_create_str(keys);
2885 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05302886 char value[256] = {0};
2887 int ret = 0;
2888
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002889 if (!query || !reply) {
2890 ALOGE("adev_get_parameters: failed to create query or reply");
2891 return NULL;
2892 }
2893
Naresh Tannirud7205b62014-06-20 02:54:48 +05302894 ret = str_parms_get_str(query, "SND_CARD_STATUS", value,
2895 sizeof(value));
2896 if (ret >=0) {
2897 int val = 1;
2898 pthread_mutex_lock(&adev->snd_card_status.lock);
2899 if (SND_CARD_STATE_OFFLINE == adev->snd_card_status.state)
2900 val = 0;
2901 pthread_mutex_unlock(&adev->snd_card_status.lock);
2902 str_parms_add_int(reply, "SND_CARD_STATUS", val);
2903 goto exit;
2904 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002905
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002906 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002907 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002908 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002909 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05302910 pthread_mutex_unlock(&adev->lock);
2911
Naresh Tannirud7205b62014-06-20 02:54:48 +05302912exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002913 str = str_parms_to_str(reply);
2914 str_parms_destroy(query);
2915 str_parms_destroy(reply);
2916
2917 ALOGV("%s: exit: returns - %s", __func__, str);
2918 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002919}
2920
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002921static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002922{
2923 return 0;
2924}
2925
2926static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2927{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002928 int ret;
2929 struct audio_device *adev = (struct audio_device *)dev;
2930 pthread_mutex_lock(&adev->lock);
2931 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002932 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002933 pthread_mutex_unlock(&adev->lock);
2934 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002935}
2936
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002937static int adev_set_master_volume(struct audio_hw_device *dev __unused,
2938 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002939{
2940 return -ENOSYS;
2941}
2942
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002943static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2944 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002945{
2946 return -ENOSYS;
2947}
2948
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002949static int adev_set_master_mute(struct audio_hw_device *dev __unused,
2950 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002951{
2952 return -ENOSYS;
2953}
2954
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002955static int adev_get_master_mute(struct audio_hw_device *dev __unused,
2956 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002957{
2958 return -ENOSYS;
2959}
2960
2961static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2962{
2963 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002964 pthread_mutex_lock(&adev->lock);
2965 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002966 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002967 adev->mode = mode;
2968 }
2969 pthread_mutex_unlock(&adev->lock);
2970 return 0;
2971}
2972
2973static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2974{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002975 int ret;
2976
2977 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002978 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002979 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2980 pthread_mutex_unlock(&adev->lock);
2981
2982 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002983}
2984
2985static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2986{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002987 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002988 return 0;
2989}
2990
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002991static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002992 const struct audio_config *config)
2993{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002994 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002995
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07002996 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
2997 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002998}
2999
3000static int adev_open_input_stream(struct audio_hw_device *dev,
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003001 audio_io_handle_t handle __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003002 audio_devices_t devices,
3003 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003004 struct audio_stream_in **stream_in,
3005 audio_input_flags_t flags __unused,
3006 const char *address __unused,
3007 audio_source_t source __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003008{
3009 struct audio_device *adev = (struct audio_device *)dev;
3010 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003011 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003012 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003013 bool is_low_latency = false;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303014
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003015 *stream_in = NULL;
3016 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
3017 return -EINVAL;
3018
3019 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003020
3021 if (!in) {
3022 ALOGE("failed to allocate input stream");
3023 return -ENOMEM;
3024 }
3025
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303026 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003027 stream_handle(%p) io_handle(%d)",__func__, config->sample_rate, config->channel_mask,
3028 devices, &in->stream, handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003029
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07003030 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
3031
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003032 in->stream.common.get_sample_rate = in_get_sample_rate;
3033 in->stream.common.set_sample_rate = in_set_sample_rate;
3034 in->stream.common.get_buffer_size = in_get_buffer_size;
3035 in->stream.common.get_channels = in_get_channels;
3036 in->stream.common.get_format = in_get_format;
3037 in->stream.common.set_format = in_set_format;
3038 in->stream.common.standby = in_standby;
3039 in->stream.common.dump = in_dump;
3040 in->stream.common.set_parameters = in_set_parameters;
3041 in->stream.common.get_parameters = in_get_parameters;
3042 in->stream.common.add_audio_effect = in_add_audio_effect;
3043 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3044 in->stream.set_gain = in_set_gain;
3045 in->stream.read = in_read;
3046 in->stream.get_input_frames_lost = in_get_input_frames_lost;
3047
3048 in->device = devices;
3049 in->source = AUDIO_SOURCE_DEFAULT;
3050 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003051 in->standby = 1;
3052 in->channel_mask = config->channel_mask;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003053 in->capture_handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003054
3055 /* Update config params with the requested sample rate and channels */
3056 in->usecase = USECASE_AUDIO_RECORD;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003057 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
3058 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
3059 is_low_latency = true;
3060#if LOW_LATENCY_CAPTURE_USE_CASE
3061 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
3062#endif
3063 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003064 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003065 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003066 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003067
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08003068 if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003069 if(audio_extn_ssr_get_enabled()) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003070 if(audio_extn_ssr_init(in)) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003071 ALOGE("%s: audio_extn_ssr_init failed", __func__);
3072 ret = -EINVAL;
3073 goto err_open;
3074 }
3075 } else {
Mingming Yin3cf99da2014-09-16 17:41:33 -07003076 ALOGW("%s: surround sound recording is not supported", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003077 }
Mingming Yine62d7842013-10-25 16:26:03 -07003078 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08003079 audio_extn_compr_cap_format_supported(config->format) &&
3080 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003081 audio_extn_compr_cap_init(in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003082 } else {
3083 in->config.channels = channel_count;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003084 frame_size = audio_stream_in_frame_size(&in->stream);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003085 buffer_size = get_input_buffer_size(config->sample_rate,
3086 config->format,
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003087 channel_count,
3088 is_low_latency);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003089 in->config.period_size = buffer_size / frame_size;
3090 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003091
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003092 /* This stream could be for sound trigger lab,
3093 get sound trigger pcm if present */
3094 audio_extn_sound_trigger_check_and_get_session(in);
3095
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003096 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07003097 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003098 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003099
3100err_open:
3101 free(in);
3102 *stream_in = NULL;
3103 return ret;
3104}
3105
3106static void adev_close_input_stream(struct audio_hw_device *dev,
3107 struct audio_stream_in *stream)
3108{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003109 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003110 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003111 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05303112
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303113 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003114
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05303115 /* Disable echo reference while closing input stream */
3116 platform_set_echo_reference(adev->platform, false);
3117
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003118 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05303119 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003120 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05303121 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003122 if (ret != 0)
3123 ALOGE("%s: Compress voip input cannot be closed, error:%d",
3124 __func__, ret);
3125 } else
3126 in_standby(&stream->common);
3127
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003128 if (audio_extn_ssr_get_enabled() &&
3129 (audio_channel_count_from_in_mask(in->channel_mask) == 6)) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003130 audio_extn_ssr_deinit();
3131 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003132
Mingming Yine62d7842013-10-25 16:26:03 -07003133 if(audio_extn_compr_cap_enabled() &&
3134 audio_extn_compr_cap_format_supported(in->config.format))
3135 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003136
3137 free(stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003138 return;
3139}
3140
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003141static int adev_dump(const audio_hw_device_t *device __unused,
3142 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003143{
3144 return 0;
3145}
3146
3147static int adev_close(hw_device_t *device)
3148{
3149 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07003150
3151 if (!adev)
3152 return 0;
3153
3154 pthread_mutex_lock(&adev_init_lock);
3155
3156 if ((--audio_device_ref_count) == 0) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003157 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -08003158 audio_extn_listen_deinit(adev);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003159 audio_extn_utils_release_streams_output_cfg_list(&adev->streams_output_cfg_list);
Kiran Kandi910e1862013-10-29 13:29:42 -07003160 audio_route_free(adev->audio_route);
3161 free(adev->snd_dev_ref_cnt);
3162 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07003163 free(device);
3164 adev = NULL;
3165 }
3166 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003167 return 0;
3168}
3169
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003170/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
3171 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
3172 * just that it _might_ work.
3173 */
3174static int period_size_is_plausible_for_low_latency(int period_size)
3175{
3176 switch (period_size) {
3177 case 160:
3178 case 240:
3179 case 320:
3180 case 480:
3181 return 1;
3182 default:
3183 return 0;
3184 }
3185}
3186
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003187static int adev_open(const hw_module_t *module, const char *name,
3188 hw_device_t **device)
3189{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003190 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003191
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003192 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003193 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3194
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003195 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07003196 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003197 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07003198 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003199 ALOGD("%s: returning existing instance of adev", __func__);
3200 ALOGD("%s: exit", __func__);
3201 pthread_mutex_unlock(&adev_init_lock);
3202 return 0;
3203 }
3204
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003205 adev = calloc(1, sizeof(struct audio_device));
3206
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003207 if (!adev) {
3208 pthread_mutex_unlock(&adev_init_lock);
3209 return -ENOMEM;
3210 }
3211
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07003212 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3213
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003214 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3215 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3216 adev->device.common.module = (struct hw_module_t *)module;
3217 adev->device.common.close = adev_close;
3218
3219 adev->device.init_check = adev_init_check;
3220 adev->device.set_voice_volume = adev_set_voice_volume;
3221 adev->device.set_master_volume = adev_set_master_volume;
3222 adev->device.get_master_volume = adev_get_master_volume;
3223 adev->device.set_master_mute = adev_set_master_mute;
3224 adev->device.get_master_mute = adev_get_master_mute;
3225 adev->device.set_mode = adev_set_mode;
3226 adev->device.set_mic_mute = adev_set_mic_mute;
3227 adev->device.get_mic_mute = adev_get_mic_mute;
3228 adev->device.set_parameters = adev_set_parameters;
3229 adev->device.get_parameters = adev_get_parameters;
3230 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3231 adev->device.open_output_stream = adev_open_output_stream;
3232 adev->device.close_output_stream = adev_close_output_stream;
3233 adev->device.open_input_stream = adev_open_input_stream;
3234 adev->device.close_input_stream = adev_close_input_stream;
3235 adev->device.dump = adev_dump;
3236
3237 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003238 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003239 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003240 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003241 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003242 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003243 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003244 /* adev->cur_hdmi_channels = 0; by calloc() */
Mingming Yin3ee55c62014-08-04 14:23:35 -07003245 adev->cur_codec_backend_samplerate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
3246 adev->cur_codec_backend_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Eric Laurentb23d5282013-05-14 15:27:20 -07003247 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003248 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003249 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08003250 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003251 adev->offload_usecases_state = 0;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303252
3253 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
3254 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003255 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003256 adev->platform = platform_init(adev);
3257 if (!adev->platform) {
3258 free(adev->snd_dev_ref_cnt);
3259 free(adev);
3260 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3261 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08003262 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003263 return -EINVAL;
3264 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003265
Naresh Tanniru4c630392014-05-12 01:05:52 +05303266 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
3267
Eric Laurentc4aef752013-09-12 17:45:53 -07003268 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
3269 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3270 if (adev->visualizer_lib == NULL) {
3271 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3272 } else {
3273 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3274 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003275 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003276 "visualizer_hal_start_output");
3277 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003278 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003279 "visualizer_hal_stop_output");
3280 }
3281 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003282 audio_extn_listen_init(adev, adev->snd_card);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003283 audio_extn_sound_trigger_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07003284
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003285 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
3286 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3287 if (adev->offload_effects_lib == NULL) {
3288 ALOGE("%s: DLOPEN failed for %s", __func__,
3289 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3290 } else {
3291 ALOGV("%s: DLOPEN successful for %s", __func__,
3292 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3293 adev->offload_effects_start_output =
3294 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3295 "offload_effects_bundle_hal_start_output");
3296 adev->offload_effects_stop_output =
3297 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3298 "offload_effects_bundle_hal_stop_output");
3299 }
3300 }
3301
Mingming Yin514a8bc2014-07-29 15:22:21 -07003302 adev->bt_wb_speech_enabled = false;
3303
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003304 *device = &adev->device.common;
3305
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003306 audio_extn_utils_update_streams_output_cfg_list(adev->platform, adev->mixer,
3307 &adev->streams_output_cfg_list);
3308
Kiran Kandi910e1862013-10-29 13:29:42 -07003309 audio_device_ref_count++;
Ravi Kumar Alamandadc9bb152014-09-08 15:59:58 -07003310
3311 char value[PROPERTY_VALUE_MAX];
3312 int trial;
3313 if (property_get("audio_hal.period_size", value, NULL) > 0) {
3314 trial = atoi(value);
3315 if (period_size_is_plausible_for_low_latency(trial)) {
3316 pcm_config_low_latency.period_size = trial;
3317 pcm_config_low_latency.start_threshold = trial / 4;
3318 pcm_config_low_latency.avail_min = trial / 4;
3319 configured_low_latency_capture_period_size = trial;
3320 }
3321 }
3322 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
3323 trial = atoi(value);
3324 if (period_size_is_plausible_for_low_latency(trial)) {
3325 configured_low_latency_capture_period_size = trial;
3326 }
3327 }
3328
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003329 pthread_mutex_unlock(&adev_init_lock);
3330
Eric Laurent994a6932013-07-17 11:51:42 -07003331 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003332 return 0;
3333}
3334
3335static struct hw_module_methods_t hal_module_methods = {
3336 .open = adev_open,
3337};
3338
3339struct audio_module HAL_MODULE_INFO_SYM = {
3340 .common = {
3341 .tag = HARDWARE_MODULE_TAG,
3342 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3343 .hal_api_version = HARDWARE_HAL_API_VERSION,
3344 .id = AUDIO_HARDWARE_MODULE_ID,
3345 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08003346 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003347 .methods = &hal_module_methods,
3348 },
3349};