blob: 4d8925d5fcede4ffe10462cb033f925dd64bf150 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003 * Not a Contribution.
4 *
Shiv Maliyappanahalli8911f282014-01-10 15:56:19 -08005 * Copyright (C) 2013 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_primary"
21/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070022/*#define VERY_VERY_VERBOSE_LOGGING*/
23#ifdef VERY_VERY_VERBOSE_LOGGING
24#define ALOGVV ALOGV
25#else
26#define ALOGVV(a...) do { } while(0)
27#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080028
29#include <errno.h>
30#include <pthread.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080034#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070035#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070036#include <sys/resource.h>
37#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038
39#include <cutils/log.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070042#include <cutils/atomic.h>
43#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080044
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <hardware/audio_effect.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070050#include "platform_api.h"
51#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070052#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080053#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include "sound/compress_params.h"
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080056#include "sound/asound.h"
ApurupaPattapu2e084df2013-12-18 15:47:59 -080057
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070058#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
59/* ToDo: Check and update a proper value in msec */
60#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
61#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
62
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080063
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080064#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
65
Eric Laurentb23d5282013-05-14 15:27:20 -070066struct pcm_config pcm_config_deep_buffer = {
67 .channels = 2,
68 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
69 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
70 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
71 .format = PCM_FORMAT_S16_LE,
72 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
73 .stop_threshold = INT_MAX,
74 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
75};
76
77struct pcm_config pcm_config_low_latency = {
78 .channels = 2,
79 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
80 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
81 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
82 .format = PCM_FORMAT_S16_LE,
83 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
84 .stop_threshold = INT_MAX,
85 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
86};
87
88struct pcm_config pcm_config_hdmi_multi = {
89 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
90 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
91 .period_size = HDMI_MULTI_PERIOD_SIZE,
92 .period_count = HDMI_MULTI_PERIOD_COUNT,
93 .format = PCM_FORMAT_S16_LE,
94 .start_threshold = 0,
95 .stop_threshold = INT_MAX,
96 .avail_min = 0,
97};
98
99struct pcm_config pcm_config_audio_capture = {
100 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700101 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
102 .format = PCM_FORMAT_S16_LE,
103};
104
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800105const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700106 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
107 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
108 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700109 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700110#ifdef MULTIPLE_OFFLOAD_ENABLED
111 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
112 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
113 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
114 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
115 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
116 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
117 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
118 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
119#endif
Eric Laurentb23d5282013-05-14 15:27:20 -0700120 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700121 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700122 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700123 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700124 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800125 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800126 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700127 [USECASE_VOICE_CALL] = "voice-call",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700128
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700129 [USECASE_VOICE2_CALL] = "voice2-call",
130 [USECASE_VOLTE_CALL] = "volte-call",
131 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800132 [USECASE_VOWLAN_CALL] = "vowlan-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800133 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700134 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
135 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
136 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800137 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
138 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
139 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
140
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700141 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
142 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700143 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
144 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700145};
146
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700147static const audio_usecase_t offload_usecases[] = {
148 USECASE_AUDIO_PLAYBACK_OFFLOAD,
149#ifdef MULTIPLE_OFFLOAD_ENABLED
150 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
151 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
152 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
153 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
154 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
155 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
156 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
157 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
158#endif
159};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800160
161#define STRING_TO_ENUM(string) { #string, string }
162
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163struct string_to_enum {
164 const char *name;
165 uint32_t value;
166};
167
168static const struct string_to_enum out_channels_name_to_enum_table[] = {
169 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
170 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
171 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
172};
173
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700174static struct audio_device *adev = NULL;
175static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700176static unsigned int audio_device_ref_count;
177
Haynes Mathew George5191a852013-09-11 14:19:36 -0700178static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800179
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800180static int check_and_set_gapless_mode(struct audio_device *adev) {
181
182
183 char value[PROPERTY_VALUE_MAX] = {0};
184 bool gapless_enabled = false;
185 const char *mixer_ctl_name = "Compress Gapless Playback";
186 struct mixer_ctl *ctl;
187
188 ALOGV("%s:", __func__);
189 property_get("audio.offload.gapless.enabled", value, NULL);
190 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
191
192 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
193 if (!ctl) {
194 ALOGE("%s: Could not get ctl for mixer cmd - %s",
195 __func__, mixer_ctl_name);
196 return -EINVAL;
197 }
198
199 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
200 ALOGE("%s: Could not set gapless mode %d",
201 __func__, gapless_enabled);
202 return -EINVAL;
203 }
204 return 0;
205}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700206
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700207static bool is_supported_format(audio_format_t format)
208{
Eric Laurent86e17132013-09-12 17:49:30 -0700209 if (format == AUDIO_FORMAT_MP3 ||
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800210 format == AUDIO_FORMAT_AAC ||
211 format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD ||
Mingming Yin3ee55c62014-08-04 14:23:35 -0700212 format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD ||
213 format == AUDIO_FORMAT_FLAC)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800214 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700215
216 return false;
217}
218
219static int get_snd_codec_id(audio_format_t format)
220{
221 int id = 0;
222
223 switch (format) {
224 case AUDIO_FORMAT_MP3:
225 id = SND_AUDIOCODEC_MP3;
226 break;
227 case AUDIO_FORMAT_AAC:
228 id = SND_AUDIOCODEC_AAC;
229 break;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800230 case AUDIO_FORMAT_PCM_16_BIT_OFFLOAD:
231 case AUDIO_FORMAT_PCM_24_BIT_OFFLOAD:
232 id = SND_AUDIOCODEC_PCM;
233 break;
Mingming Yin3ee55c62014-08-04 14:23:35 -0700234 case AUDIO_FORMAT_FLAC:
235 id = SND_AUDIOCODEC_FLAC;
236 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700237 default:
Mingming Yin90310102013-11-13 16:57:00 -0800238 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700239 }
240
241 return id;
242}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800243
Venkata Narendra Kumar Guttaed0f94f2014-07-09 16:29:28 +0530244int get_snd_card_state(struct audio_device *adev)
Naresh Tanniru80659832014-06-04 18:17:56 +0530245{
246 int snd_scard_state;
247
248 if (!adev)
249 return SND_CARD_STATE_OFFLINE;
250
251 pthread_mutex_lock(&adev->snd_card_status.lock);
252 snd_scard_state = adev->snd_card_status.state;
253 pthread_mutex_unlock(&adev->snd_card_status.lock);
254
255 return snd_scard_state;
256}
257
258static int set_snd_card_state(struct audio_device *adev, int snd_scard_state)
259{
260 if (!adev)
261 return -ENOSYS;
262
263 pthread_mutex_lock(&adev->snd_card_status.lock);
264 adev->snd_card_status.state = snd_scard_state;
265 pthread_mutex_unlock(&adev->snd_card_status.lock);
266
267 return 0;
268}
269
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700270int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700271 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800272{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700273 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700274 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800275
276 if (usecase == NULL)
277 return -EINVAL;
278
279 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
280
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800281 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700282 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800283 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700284 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800285
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -0800286#ifdef DS1_DOLBY_DAP_ENABLED
287 audio_extn_dolby_set_dmid(adev);
288 audio_extn_dolby_set_endpoint(adev);
289#endif
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530290 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700291 audio_extn_utils_send_audio_calibration(adev, usecase);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700292 audio_extn_utils_send_app_type_cfg(usecase);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800293 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700294 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700295 ALOGV("%s: apply mixer and update path: %s", __func__, mixer_path);
296 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800297 ALOGV("%s: exit", __func__);
298 return 0;
299}
300
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700301int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700302 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800303{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700304 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700305 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800306
307 if (usecase == NULL)
308 return -EINVAL;
309
310 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700311 if (usecase->type == PCM_CAPTURE)
312 snd_device = usecase->in_snd_device;
313 else
314 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800315 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700316 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700317 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
318 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530319 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800320 ALOGV("%s: exit", __func__);
321 return 0;
322}
323
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700324int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700325 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800326{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700327 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
328
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800329 if (snd_device < SND_DEVICE_MIN ||
330 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800331 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800332 return -EINVAL;
333 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700334
335 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700336
337 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
338 ALOGE("%s: Invalid sound device returned", __func__);
339 return -EINVAL;
340 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700341 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700342 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700343 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700344 return 0;
345 }
346
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -0700347 if (audio_extn_spkr_prot_is_enabled())
348 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700349 /* start usb playback thread */
350 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
351 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
352 audio_extn_usb_start_playback(adev);
353
354 /* start usb capture thread */
355 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
356 audio_extn_usb_start_capture(adev);
357
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800358 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
359 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700360 audio_extn_spkr_prot_is_enabled()) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700361 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
362 adev->snd_dev_ref_cnt[snd_device]--;
363 return -EINVAL;
364 }
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800365 if (audio_extn_spkr_prot_start_processing(snd_device)) {
366 ALOGE("%s: spkr_start_processing failed", __func__);
367 return -EINVAL;
368 }
369 } else {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700370 ALOGV("%s: snd_device(%d: %s)", __func__,
371 snd_device, device_name);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -0700372 /* due to the possibility of calibration overwrite between listen
373 and audio, notify listen hal before audio calibration is sent */
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530374 audio_extn_listen_update_device_status(snd_device,
375 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700376 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700377 adev->snd_dev_ref_cnt[snd_device]--;
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530378 audio_extn_listen_update_device_status(snd_device,
379 LISTEN_EVENT_SND_DEVICE_FREE);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700380 return -EINVAL;
381 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300382 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700383 audio_route_apply_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800384 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800385 return 0;
386}
387
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700388int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700389 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800390{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700391 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
392
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800393 if (snd_device < SND_DEVICE_MIN ||
394 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800395 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800396 return -EINVAL;
397 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700398 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
399 ALOGE("%s: device ref cnt is already 0", __func__);
400 return -EINVAL;
401 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700402
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700403 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700404
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700405 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
406 ALOGE("%s: Invalid sound device returned", __func__);
407 return -EINVAL;
408 }
409
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700410 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700411 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700412 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800413 /* exit usb play back thread */
414 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
415 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
416 audio_extn_usb_stop_playback();
417
418 /* exit usb capture thread */
419 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -0700420 audio_extn_usb_stop_capture();
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800421
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800422 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
423 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700424 audio_extn_spkr_prot_is_enabled()) {
425 audio_extn_spkr_prot_stop_processing();
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300426 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700427 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300428 audio_extn_dev_arbi_release(snd_device);
429 }
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530430 audio_extn_listen_update_device_status(snd_device,
Kiran Kandide144c82013-11-20 15:58:32 -0800431 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700432 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700433
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800434 return 0;
435}
436
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700437static void check_usecases_codec_backend(struct audio_device *adev,
438 struct audio_usecase *uc_info,
439 snd_device_t snd_device)
440{
441 struct listnode *node;
442 struct audio_usecase *usecase;
443 bool switch_device[AUDIO_USECASE_MAX];
444 int i, num_uc_to_switch = 0;
445
446 /*
447 * This function is to make sure that all the usecases that are active on
448 * the hardware codec backend are always routed to any one device that is
449 * handled by the hardware codec.
450 * For example, if low-latency and deep-buffer usecases are currently active
451 * on speaker and out_set_parameters(headset) is received on low-latency
452 * output, then we have to make sure deep-buffer is also switched to headset,
453 * because of the limitation that both the devices cannot be enabled
454 * at the same time as they share the same backend.
455 */
Mingming Yin3ee55c62014-08-04 14:23:35 -0700456 /*
457 * This call is to check if we need to force routing for a particular stream
458 * If there is a backend configuration change for the device when a
459 * new stream starts, then ADM needs to be closed and re-opened with the new
460 * configuraion. This call check if we need to re-route all the streams
461 * associated with the backend. Touch tone + 24 bit playback.
462 */
463 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info);
464
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700465 /* Disable all the usecases on the shared backend other than the
466 specified usecase */
467 for (i = 0; i < AUDIO_USECASE_MAX; i++)
468 switch_device[i] = false;
469
470 list_for_each(node, &adev->usecase_list) {
471 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800472 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700473 usecase != uc_info &&
Mingming Yin3ee55c62014-08-04 14:23:35 -0700474 (usecase->out_snd_device != snd_device || force_routing) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700475 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
476 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
477 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700478 platform_get_snd_device_name(usecase->out_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700479 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700480 switch_device[usecase->id] = true;
481 num_uc_to_switch++;
482 }
483 }
484
485 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700486 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700487
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530488 /* Make sure the previous devices to be disabled first and then enable the
489 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700490 list_for_each(node, &adev->usecase_list) {
491 usecase = node_to_item(node, struct audio_usecase, list);
492 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700493 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700494 }
495 }
496
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700497 list_for_each(node, &adev->usecase_list) {
498 usecase = node_to_item(node, struct audio_usecase, list);
499 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700500 enable_snd_device(adev, snd_device);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700501 }
502 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700503
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700504 /* Re-route all the usecases on the shared backend other than the
505 specified usecase to new snd devices */
506 list_for_each(node, &adev->usecase_list) {
507 usecase = node_to_item(node, struct audio_usecase, list);
508 /* Update the out_snd_device only before enabling the audio route */
509 if (switch_device[usecase->id] ) {
510 usecase->out_snd_device = snd_device;
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700511 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700512 }
513 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700514 }
515}
516
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700517static void check_and_route_capture_usecases(struct audio_device *adev,
518 struct audio_usecase *uc_info,
519 snd_device_t snd_device)
520{
521 struct listnode *node;
522 struct audio_usecase *usecase;
523 bool switch_device[AUDIO_USECASE_MAX];
524 int i, num_uc_to_switch = 0;
525
526 /*
527 * This function is to make sure that all the active capture usecases
528 * are always routed to the same input sound device.
529 * For example, if audio-record and voice-call usecases are currently
530 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
531 * is received for voice call then we have to make sure that audio-record
532 * usecase is also switched to earpiece i.e. voice-dmic-ef,
533 * because of the limitation that two devices cannot be enabled
534 * at the same time if they share the same backend.
535 */
536 for (i = 0; i < AUDIO_USECASE_MAX; i++)
537 switch_device[i] = false;
538
539 list_for_each(node, &adev->usecase_list) {
540 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800541 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700542 usecase != uc_info &&
543 usecase->in_snd_device != snd_device) {
544 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
545 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700546 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700547 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700548 switch_device[usecase->id] = true;
549 num_uc_to_switch++;
550 }
551 }
552
553 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700554 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700555
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530556 /* Make sure the previous devices to be disabled first and then enable the
557 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700558 list_for_each(node, &adev->usecase_list) {
559 usecase = node_to_item(node, struct audio_usecase, list);
560 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700561 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800562 }
563 }
564
565 list_for_each(node, &adev->usecase_list) {
566 usecase = node_to_item(node, struct audio_usecase, list);
567 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700568 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700569 }
570 }
571
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700572 /* Re-route all the usecases on the shared backend other than the
573 specified usecase to new snd devices */
574 list_for_each(node, &adev->usecase_list) {
575 usecase = node_to_item(node, struct audio_usecase, list);
576 /* Update the in_snd_device only before enabling the audio route */
577 if (switch_device[usecase->id] ) {
578 usecase->in_snd_device = snd_device;
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700579 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700580 }
581 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700582 }
583}
584
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800585/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700586static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800587{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700588 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700589 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800590
591 switch (channels) {
592 /*
593 * Do not handle stereo output in Multi-channel cases
594 * Stereo case is handled in normal playback path
595 */
596 case 6:
597 ALOGV("%s: HDMI supports 5.1", __func__);
598 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
599 break;
600 case 8:
601 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
602 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
603 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
604 break;
605 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700606 ALOGE("HDMI does not support multi channel playback");
607 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800608 break;
609 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700610 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800611}
612
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700613static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
614{
615 struct audio_usecase *usecase;
616 struct listnode *node;
617
618 list_for_each(node, &adev->usecase_list) {
619 usecase = node_to_item(node, struct audio_usecase, list);
620 if (usecase->type == VOICE_CALL) {
621 ALOGV("%s: usecase id %d", __func__, usecase->id);
622 return usecase->id;
623 }
624 }
625 return USECASE_INVALID;
626}
627
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700628struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700629 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700630{
631 struct audio_usecase *usecase;
632 struct listnode *node;
633
634 list_for_each(node, &adev->usecase_list) {
635 usecase = node_to_item(node, struct audio_usecase, list);
636 if (usecase->id == uc_id)
637 return usecase;
638 }
639 return NULL;
640}
641
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700642int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800643{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800644 snd_device_t out_snd_device = SND_DEVICE_NONE;
645 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700646 struct audio_usecase *usecase = NULL;
647 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800648 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800649 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800650 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800651 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700652 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800653
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700654 usecase = get_usecase_from_list(adev, uc_id);
655 if (usecase == NULL) {
656 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
657 return -EINVAL;
658 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800659
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800660 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800661 (usecase->type == VOIP_CALL) ||
662 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700663 out_snd_device = platform_get_output_snd_device(adev->platform,
664 usecase->stream.out->devices);
665 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700666 usecase->devices = usecase->stream.out->devices;
667 } else {
668 /*
669 * If the voice call is active, use the sound devices of voice call usecase
670 * so that it would not result any device switch. All the usecases will
671 * be switched to new device when select_devices() is called for voice call
672 * usecase. This is to avoid switching devices for voice call when
673 * check_usecases_codec_backend() is called below.
674 */
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700675 if (adev->voice.in_call && adev->mode == AUDIO_MODE_IN_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700676 vc_usecase = get_usecase_from_list(adev,
677 get_voice_usecase_id_from_list(adev));
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700678 if ((vc_usecase) && ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
679 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700680 in_snd_device = vc_usecase->in_snd_device;
681 out_snd_device = vc_usecase->out_snd_device;
682 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800683 } else if (voice_extn_compress_voip_is_active(adev)) {
684 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700685 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Avinash Vaish4d6167d2014-06-25 12:20:37 +0530686 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700687 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800688 in_snd_device = voip_usecase->in_snd_device;
689 out_snd_device = voip_usecase->out_snd_device;
690 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800691 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800692 hfp_ucid = audio_extn_hfp_get_usecase();
693 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700694 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800695 in_snd_device = hfp_usecase->in_snd_device;
696 out_snd_device = hfp_usecase->out_snd_device;
697 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700698 }
699 if (usecase->type == PCM_PLAYBACK) {
700 usecase->devices = usecase->stream.out->devices;
701 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700702 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700703 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700704 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700705 if (usecase->stream.out == adev->primary_output &&
706 adev->active_input &&
707 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
708 select_devices(adev, adev->active_input->usecase);
709 }
710 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700711 } else if (usecase->type == PCM_CAPTURE) {
712 usecase->devices = usecase->stream.in->device;
713 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700714 if (in_snd_device == SND_DEVICE_NONE) {
715 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
716 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700717 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700718 adev->primary_output->devices);
719 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700720 in_snd_device = platform_get_input_snd_device(adev->platform,
721 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700722 }
723 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700724 }
725 }
726
727 if (out_snd_device == usecase->out_snd_device &&
728 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800729 return 0;
730 }
731
sangwoobc677242013-08-08 16:53:43 +0900732 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700733 out_snd_device, platform_get_snd_device_name(out_snd_device),
734 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800735
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800736 /*
737 * Limitation: While in call, to do a device switch we need to disable
738 * and enable both RX and TX devices though one of them is same as current
739 * device.
740 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700741 if ((usecase->type == VOICE_CALL) &&
742 (usecase->in_snd_device != SND_DEVICE_NONE) &&
743 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700744 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800745 }
746
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700747 /* Disable current sound devices */
748 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700749 disable_audio_route(adev, usecase);
750 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800751 }
752
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700753 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700754 disable_audio_route(adev, usecase);
755 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800756 }
757
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800758 /* Applicable only on the targets that has external modem.
759 * New device information should be sent to modem before enabling
760 * the devices to reduce in-call device switch time.
761 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700762 if ((usecase->type == VOICE_CALL) &&
763 (usecase->in_snd_device != SND_DEVICE_NONE) &&
764 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800765 status = platform_switch_voice_call_enable_device_config(adev->platform,
766 out_snd_device,
767 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700768 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800769
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700770 /* Enable new sound devices */
771 if (out_snd_device != SND_DEVICE_NONE) {
772 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
773 check_usecases_codec_backend(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700774 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800775 }
776
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700777 if (in_snd_device != SND_DEVICE_NONE) {
778 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700779 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700780 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700781
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800782 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700783 status = platform_switch_voice_call_device_post(adev->platform,
784 out_snd_device,
785 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800786
sangwoo170731f2013-06-08 15:36:36 +0900787 usecase->in_snd_device = in_snd_device;
788 usecase->out_snd_device = out_snd_device;
789
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700790 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900791
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800792 /* Applicable only on the targets that has external modem.
793 * Enable device command should be sent to modem only after
794 * enabling voice call mixer controls
795 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -0700796 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800797 status = platform_switch_voice_call_usecase_route_post(adev->platform,
798 out_snd_device,
799 in_snd_device);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530800 ALOGD("%s: done",__func__);
801
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800802 return status;
803}
804
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800805static int stop_input_stream(struct stream_in *in)
806{
807 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800808 struct audio_usecase *uc_info;
809 struct audio_device *adev = in->dev;
810
Eric Laurentc8400632013-02-14 19:04:54 -0800811 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800812
Eric Laurent994a6932013-07-17 11:51:42 -0700813 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700814 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800815 uc_info = get_usecase_from_list(adev, in->usecase);
816 if (uc_info == NULL) {
817 ALOGE("%s: Could not find the usecase (%d) in the list",
818 __func__, in->usecase);
819 return -EINVAL;
820 }
821
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800822 /* Close in-call recording streams */
823 voice_check_and_stop_incall_rec_usecase(adev, in);
824
Eric Laurent150dbfe2013-02-27 14:31:02 -0800825 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700826 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700827
828 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700829 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800830
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800831 list_remove(&uc_info->list);
832 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800833
Eric Laurent994a6932013-07-17 11:51:42 -0700834 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800835 return ret;
836}
837
838int start_input_stream(struct stream_in *in)
839{
840 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800841 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800842 struct audio_usecase *uc_info;
843 struct audio_device *adev = in->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +0530844 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800845
Mingming Yine62d7842013-10-25 16:26:03 -0700846 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530847 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
848 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700849
Naresh Tanniru80659832014-06-04 18:17:56 +0530850
851 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +0530852 ALOGE("%s: sound card is not active/SSR returning error", __func__);
853 ret = -ENETRESET;
Naresh Tanniru4c630392014-05-12 01:05:52 +0530854 goto error_config;
855 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530856
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700857 /* Check if source matches incall recording usecase criteria */
858 ret = voice_check_and_set_incall_rec_usecase(adev, in);
859 if (ret)
860 goto error_config;
861 else
862 ALOGV("%s: usecase(%d)", __func__, in->usecase);
863
Eric Laurentb23d5282013-05-14 15:27:20 -0700864 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800865 if (in->pcm_device_id < 0) {
866 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
867 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800868 ret = -EINVAL;
869 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800870 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700871
872 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800873 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700874
875 if (!uc_info) {
876 ret = -ENOMEM;
877 goto error_config;
878 }
879
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800880 uc_info->id = in->usecase;
881 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800882 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700883 uc_info->devices = in->device;
884 uc_info->in_snd_device = SND_DEVICE_NONE;
885 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800886
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800887 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700888 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800889
Eric Laurentc8400632013-02-14 19:04:54 -0800890 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800891 __func__, adev->snd_card,
892 in->pcm_device_id, in->config.channels);
893 in->pcm = pcm_open(adev->snd_card,
894 in->pcm_device_id, PCM_IN, &in->config);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800895 if (in->pcm && !pcm_is_ready(in->pcm)) {
896 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
897 pcm_close(in->pcm);
898 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800899 ret = -EIO;
900 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800901 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530902
Eric Laurent994a6932013-07-17 11:51:42 -0700903 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800904 return ret;
905
906error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800907 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800908
909error_config:
910 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700911 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800912
913 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800914}
915
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700916/* must be called with out->lock locked */
917static int send_offload_cmd_l(struct stream_out* out, int command)
918{
919 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
920
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700921 if (!cmd) {
922 ALOGE("failed to allocate mem for command 0x%x", command);
923 return -ENOMEM;
924 }
925
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700926 ALOGVV("%s %d", __func__, command);
927
928 cmd->cmd = command;
929 list_add_tail(&out->offload_cmd_list, &cmd->node);
930 pthread_cond_signal(&out->offload_cond);
931 return 0;
932}
933
934/* must be called iwth out->lock locked */
935static void stop_compressed_output_l(struct stream_out *out)
936{
937 out->offload_state = OFFLOAD_STATE_IDLE;
938 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700939 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700940 if (out->compr != NULL) {
941 compress_stop(out->compr);
942 while (out->offload_thread_blocked) {
943 pthread_cond_wait(&out->cond, &out->lock);
944 }
945 }
946}
947
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700948bool is_offload_usecase(audio_usecase_t uc_id)
949{
950 unsigned int i;
951 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
952 if (uc_id == offload_usecases[i])
953 return true;
954 }
955 return false;
956}
957
958static audio_usecase_t get_offload_usecase(struct audio_device *adev)
959{
960 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
961 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
962 char value[PROPERTY_VALUE_MAX] = {0};
963
964 property_get("audio.offload.multiple.enabled", value, NULL);
965 if (!(atoi(value) || !strncmp("true", value, 4)))
966 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
967
968 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
969 for (i = 0; i < num_usecase; i++) {
970 if (!(adev->offload_usecases_state & (0x1<<i))) {
971 adev->offload_usecases_state |= 0x1 << i;
972 ret = offload_usecases[i];
973 break;
974 }
975 }
976 ALOGV("%s: offload usecase is %d", __func__, ret);
977 return ret;
978}
979
980static void free_offload_usecase(struct audio_device *adev,
981 audio_usecase_t uc_id)
982{
983 unsigned int i;
984 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
985 if (offload_usecases[i] == uc_id) {
986 adev->offload_usecases_state &= ~(0x1<<i);
987 break;
988 }
989 }
990 ALOGV("%s: free offload usecase %d", __func__, uc_id);
991}
992
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700993static void *offload_thread_loop(void *context)
994{
995 struct stream_out *out = (struct stream_out *) context;
996 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -0800997 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700998
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700999 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1000 set_sched_policy(0, SP_FOREGROUND);
1001 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1002
1003 ALOGV("%s", __func__);
1004 pthread_mutex_lock(&out->lock);
1005 for (;;) {
1006 struct offload_cmd *cmd = NULL;
1007 stream_callback_event_t event;
1008 bool send_callback = false;
1009
1010 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1011 __func__, list_empty(&out->offload_cmd_list),
1012 out->offload_state);
1013 if (list_empty(&out->offload_cmd_list)) {
1014 ALOGV("%s SLEEPING", __func__);
1015 pthread_cond_wait(&out->offload_cond, &out->lock);
1016 ALOGV("%s RUNNING", __func__);
1017 continue;
1018 }
1019
1020 item = list_head(&out->offload_cmd_list);
1021 cmd = node_to_item(item, struct offload_cmd, node);
1022 list_remove(item);
1023
1024 ALOGVV("%s STATE %d CMD %d out->compr %p",
1025 __func__, out->offload_state, cmd->cmd, out->compr);
1026
1027 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1028 free(cmd);
1029 break;
1030 }
1031
1032 if (out->compr == NULL) {
1033 ALOGE("%s: Compress handle is NULL", __func__);
1034 pthread_cond_signal(&out->cond);
1035 continue;
1036 }
1037 out->offload_thread_blocked = true;
1038 pthread_mutex_unlock(&out->lock);
1039 send_callback = false;
1040 switch(cmd->cmd) {
1041 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001042 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001043 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001044 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001045 send_callback = true;
1046 event = STREAM_CBK_EVENT_WRITE_READY;
1047 break;
1048 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001049 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301050 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001051 ALOGD("copl(%p):calling compress_partial_drain", out);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001052 compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001053 ALOGD("copl(%p):out of compress_partial_drain", out);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301054 }
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001055 else if(ret == -ETIMEDOUT)
1056 compress_drain(out->compr);
1057 else
1058 ALOGE("%s: Next track returned error %d",__func__, ret);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001059 send_callback = true;
1060 event = STREAM_CBK_EVENT_DRAIN_READY;
1061 break;
1062 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001063 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001064 compress_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001065 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001066 send_callback = true;
1067 event = STREAM_CBK_EVENT_DRAIN_READY;
1068 break;
1069 default:
1070 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1071 break;
1072 }
1073 pthread_mutex_lock(&out->lock);
1074 out->offload_thread_blocked = false;
1075 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001076 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001077 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001078 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001079 free(cmd);
1080 }
1081
1082 pthread_cond_signal(&out->cond);
1083 while (!list_empty(&out->offload_cmd_list)) {
1084 item = list_head(&out->offload_cmd_list);
1085 list_remove(item);
1086 free(node_to_item(item, struct offload_cmd, node));
1087 }
1088 pthread_mutex_unlock(&out->lock);
1089
1090 return NULL;
1091}
1092
1093static int create_offload_callback_thread(struct stream_out *out)
1094{
1095 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1096 list_init(&out->offload_cmd_list);
1097 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1098 offload_thread_loop, out);
1099 return 0;
1100}
1101
1102static int destroy_offload_callback_thread(struct stream_out *out)
1103{
1104 pthread_mutex_lock(&out->lock);
1105 stop_compressed_output_l(out);
1106 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1107
1108 pthread_mutex_unlock(&out->lock);
1109 pthread_join(out->offload_thread, (void **) NULL);
1110 pthread_cond_destroy(&out->offload_cond);
1111
1112 return 0;
1113}
1114
Eric Laurent07eeafd2013-10-06 12:52:49 -07001115static bool allow_hdmi_channel_config(struct audio_device *adev)
1116{
1117 struct listnode *node;
1118 struct audio_usecase *usecase;
1119 bool ret = true;
1120
1121 list_for_each(node, &adev->usecase_list) {
1122 usecase = node_to_item(node, struct audio_usecase, list);
1123 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1124 /*
1125 * If voice call is already existing, do not proceed further to avoid
1126 * disabling/enabling both RX and TX devices, CSD calls, etc.
1127 * Once the voice call done, the HDMI channels can be configured to
1128 * max channels of remaining use cases.
1129 */
1130 if (usecase->id == USECASE_VOICE_CALL) {
1131 ALOGD("%s: voice call is active, no change in HDMI channels",
1132 __func__);
1133 ret = false;
1134 break;
1135 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1136 ALOGD("%s: multi channel playback is active, "
1137 "no change in HDMI channels", __func__);
1138 ret = false;
1139 break;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001140 } else if (is_offload_usecase(usecase->id) &&
Mingming Yin139f1072014-02-24 17:56:01 -08001141 popcount(usecase->stream.out->channel_mask) > 2) {
1142 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1143 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1144 ret = false;
1145 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001146 }
1147 }
1148 }
1149 return ret;
1150}
1151
1152static int check_and_set_hdmi_channels(struct audio_device *adev,
1153 unsigned int channels)
1154{
1155 struct listnode *node;
1156 struct audio_usecase *usecase;
1157
1158 /* Check if change in HDMI channel config is allowed */
1159 if (!allow_hdmi_channel_config(adev))
1160 return 0;
1161
1162 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001163 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001164 return 0;
1165 }
1166
1167 platform_set_hdmi_channels(adev->platform, channels);
1168 adev->cur_hdmi_channels = channels;
1169
1170 /*
1171 * Deroute all the playback streams routed to HDMI so that
1172 * the back end is deactivated. Note that backend will not
1173 * be deactivated if any one stream is connected to it.
1174 */
1175 list_for_each(node, &adev->usecase_list) {
1176 usecase = node_to_item(node, struct audio_usecase, list);
1177 if (usecase->type == PCM_PLAYBACK &&
1178 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001179 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001180 }
1181 }
1182
1183 /*
1184 * Enable all the streams disabled above. Now the HDMI backend
1185 * will be activated with new channel configuration
1186 */
1187 list_for_each(node, &adev->usecase_list) {
1188 usecase = node_to_item(node, struct audio_usecase, list);
1189 if (usecase->type == PCM_PLAYBACK &&
1190 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001191 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001192 }
1193 }
1194
1195 return 0;
1196}
1197
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001198static int stop_output_stream(struct stream_out *out)
1199{
1200 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201 struct audio_usecase *uc_info;
1202 struct audio_device *adev = out->dev;
1203
Eric Laurent994a6932013-07-17 11:51:42 -07001204 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001205 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001206 uc_info = get_usecase_from_list(adev, out->usecase);
1207 if (uc_info == NULL) {
1208 ALOGE("%s: Could not find the usecase (%d) in the list",
1209 __func__, out->usecase);
1210 return -EINVAL;
1211 }
1212
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001213 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001214 if (adev->visualizer_stop_output != NULL)
1215 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1216 if (adev->offload_effects_stop_output != NULL)
1217 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1218 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001219
Eric Laurent150dbfe2013-02-27 14:31:02 -08001220 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001221 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001222
1223 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001224 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001225
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001226 list_remove(&uc_info->list);
1227 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001228
Eric Laurent07eeafd2013-10-06 12:52:49 -07001229 /* Must be called after removing the usecase from list */
1230 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1231 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1232
Eric Laurent994a6932013-07-17 11:51:42 -07001233 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001234 return ret;
1235}
1236
1237int start_output_stream(struct stream_out *out)
1238{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001239 int ret = 0;
Mingming Yin9c041392014-05-01 15:37:31 -07001240 int sink_channels = 0;
1241 char prop_value[PROPERTY_VALUE_MAX] = {0};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001242 struct audio_usecase *uc_info;
1243 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301244 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001245
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001246 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
1247 ret = -EINVAL;
1248 goto error_config;
1249 }
1250
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301251 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1252 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1253 out->devices);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301254
Naresh Tanniru80659832014-06-04 18:17:56 +05301255 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301256 ALOGE("%s: sound card is not active/SSR returning error", __func__);
1257 ret = -ENETRESET;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301258 goto error_config;
1259 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05301260
Eric Laurentb23d5282013-05-14 15:27:20 -07001261 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262 if (out->pcm_device_id < 0) {
1263 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1264 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001265 ret = -EINVAL;
1266 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001267 }
1268
1269 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001270
1271 if (!uc_info) {
1272 ret = -ENOMEM;
1273 goto error_config;
1274 }
1275
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001276 uc_info->id = out->usecase;
1277 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001278 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001279 uc_info->devices = out->devices;
1280 uc_info->in_snd_device = SND_DEVICE_NONE;
1281 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001282
Eric Laurent07eeafd2013-10-06 12:52:49 -07001283 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001284 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Mingming Yin9c041392014-05-01 15:37:31 -07001285 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1286 if (!strncmp("true", prop_value, 4)) {
1287 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1288 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1289 check_and_set_hdmi_channels(adev, sink_channels);
1290 } else {
1291 if (is_offload_usecase(out->usecase))
1292 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1293 else
1294 check_and_set_hdmi_channels(adev, out->config.channels);
1295 }
Mingming Yin10fef6a2013-11-26 17:17:01 -08001296 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001297
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001298 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001299
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001300 select_devices(adev, out->usecase);
1301
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001302 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1303 __func__, 0, out->pcm_device_id);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001304 if (!is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001305 out->pcm = pcm_open(adev->snd_card,
1306 out->pcm_device_id,
1307 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001308 if (out->pcm && !pcm_is_ready(out->pcm)) {
1309 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1310 pcm_close(out->pcm);
1311 out->pcm = NULL;
1312 ret = -EIO;
1313 goto error_open;
1314 }
1315 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001316 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001317 out->compr = compress_open(adev->snd_card,
1318 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001319 COMPRESS_IN, &out->compr_config);
1320 if (out->compr && !is_compress_ready(out->compr)) {
1321 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1322 compress_close(out->compr);
1323 out->compr = NULL;
1324 ret = -EIO;
1325 goto error_open;
1326 }
1327 if (out->offload_callback)
1328 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001329
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001330#ifdef DS1_DOLBY_DDP_ENABLED
1331 if (audio_extn_is_dolby_format(out->format))
1332 audio_extn_dolby_send_ddp_endp_params(adev);
1333#endif
1334
Eric Laurentc4aef752013-09-12 17:45:53 -07001335 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001336 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1337 if (adev->offload_effects_start_output != NULL)
1338 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001339 }
Eric Laurent994a6932013-07-17 11:51:42 -07001340 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001341 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001342error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001343 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001344error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001345 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001346}
1347
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001348static int check_input_parameters(uint32_t sample_rate,
1349 audio_format_t format,
1350 int channel_count)
1351{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001352 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001353
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001354 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001355 !voice_extn_compress_voip_is_format_supported(format) &&
1356 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001357
1358 switch (channel_count) {
1359 case 1:
1360 case 2:
1361 case 6:
1362 break;
1363 default:
1364 ret = -EINVAL;
1365 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001366
1367 switch (sample_rate) {
1368 case 8000:
1369 case 11025:
1370 case 12000:
1371 case 16000:
1372 case 22050:
1373 case 24000:
1374 case 32000:
1375 case 44100:
1376 case 48000:
1377 break;
1378 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001379 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001380 }
1381
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001382 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001383}
1384
1385static size_t get_input_buffer_size(uint32_t sample_rate,
1386 audio_format_t format,
1387 int channel_count)
1388{
1389 size_t size = 0;
1390
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001391 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1392 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001393
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001394 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1395 /* ToDo: should use frame_size computed based on the format and
1396 channel_count here. */
1397 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001398
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001399 /* make sure the size is multiple of 64 */
1400 size += 0x3f;
1401 size &= ~0x3f;
1402
1403 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404}
1405
1406static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1407{
1408 struct stream_out *out = (struct stream_out *)stream;
1409
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001410 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001411}
1412
1413static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1414{
1415 return -ENOSYS;
1416}
1417
1418static size_t out_get_buffer_size(const struct audio_stream *stream)
1419{
1420 struct stream_out *out = (struct stream_out *)stream;
1421
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001422 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001423 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001424 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1425 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001426
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001427 return out->config.period_size * audio_stream_frame_size(stream);
1428}
1429
1430static uint32_t out_get_channels(const struct audio_stream *stream)
1431{
1432 struct stream_out *out = (struct stream_out *)stream;
1433
1434 return out->channel_mask;
1435}
1436
1437static audio_format_t out_get_format(const struct audio_stream *stream)
1438{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001439 struct stream_out *out = (struct stream_out *)stream;
1440
1441 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001442}
1443
1444static int out_set_format(struct audio_stream *stream, audio_format_t format)
1445{
1446 return -ENOSYS;
1447}
1448
1449static int out_standby(struct audio_stream *stream)
1450{
1451 struct stream_out *out = (struct stream_out *)stream;
1452 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001453
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301454 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1455 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001456 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1457 /* Ignore standby in case of voip call because the voip output
1458 * stream is closed in adev_close_output_stream()
1459 */
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301460 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001461 return 0;
1462 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001463
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001464 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001465 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001466 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001467 out->standby = true;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001468 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001469 if (out->pcm) {
1470 pcm_close(out->pcm);
1471 out->pcm = NULL;
1472 }
1473 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001474 ALOGD("copl(%p):standby", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001475 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001476 out->gapless_mdata.encoder_delay = 0;
1477 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001478 if (out->compr != NULL) {
1479 compress_close(out->compr);
1480 out->compr = NULL;
1481 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001482 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001483 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001484 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001485 }
1486 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001487 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001488 return 0;
1489}
1490
1491static int out_dump(const struct audio_stream *stream, int fd)
1492{
1493 return 0;
1494}
1495
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001496static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1497{
1498 int ret = 0;
1499 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001500 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001501 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001502 tmp_mdata.encoder_delay = 0;
1503 tmp_mdata.encoder_padding = 0;
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001504
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001505 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001506 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001507 return -EINVAL;
1508 }
1509
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001510 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1511 if (ret >= 0) {
1512 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1513 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1514 ALOGV("ADTS format is set in offload mode");
1515 }
1516 out->send_new_metadata = 1;
1517 }
1518
Mingming Yin3ee55c62014-08-04 14:23:35 -07001519 if (out->format == AUDIO_FORMAT_FLAC) {
1520 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_BLK_SIZE, value, sizeof(value));
1521 if (ret >= 0) {
1522 out->compr_config.codec->options.flac_dec.min_blk_size = atoi(value);
1523 out->send_new_metadata = 1;
1524 }
1525 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_BLK_SIZE, value, sizeof(value));
1526 if (ret >= 0) {
1527 out->compr_config.codec->options.flac_dec.max_blk_size = atoi(value);
1528 out->send_new_metadata = 1;
1529 }
1530 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_FRAME_SIZE, value, sizeof(value));
1531 if (ret >= 0) {
1532 out->compr_config.codec->options.flac_dec.min_frame_size = atoi(value);
1533 out->send_new_metadata = 1;
1534 }
1535 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_FRAME_SIZE, value, sizeof(value));
1536 if (ret >= 0) {
1537 out->compr_config.codec->options.flac_dec.max_frame_size = atoi(value);
1538 out->send_new_metadata = 1;
1539 }
1540 }
1541
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001542 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1543 if(ret >= 0)
1544 is_meta_data_params = true;
1545 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1546 if(ret >= 0 )
1547 is_meta_data_params = true;
1548 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1549 if(ret >= 0 )
1550 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001551 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1552 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001553 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001554 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001555 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001556 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1557 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001558 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001559 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001560 }
1561
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001562 if(!is_meta_data_params) {
1563 ALOGV("%s: Not gapless meta data params", __func__);
1564 return 0;
1565 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001566 out->gapless_mdata = tmp_mdata;
1567 out->send_new_metadata = 1;
1568 ALOGV("%s new encoder delay %u and padding %u", __func__,
1569 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1570
1571 return 0;
1572}
1573
1574
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001575static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1576{
1577 struct stream_out *out = (struct stream_out *)stream;
1578 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001579 struct audio_usecase *usecase;
1580 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001581 struct str_parms *parms;
1582 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001583 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001584 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001585
sangwoobc677242013-08-08 16:53:43 +09001586 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001587 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001588 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001589 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1590 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001591 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001592 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001593 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001594
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001595 /*
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301596 * When HDMI cable is unplugged/usb hs is disconnected the
1597 * music playback is paused and the policy manager sends routing=0
1598 * But the audioflingercontinues to write data until standby time
1599 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001600 * Avoid this by routing audio to speaker until standby.
1601 */
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301602 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1603 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001604 val == AUDIO_DEVICE_NONE) {
1605 val = AUDIO_DEVICE_OUT_SPEAKER;
1606 }
1607
1608 /*
1609 * select_devices() call below switches all the usecases on the same
1610 * backend to the new device. Refer to check_usecases_codec_backend() in
1611 * the select_devices(). But how do we undo this?
1612 *
1613 * For example, music playback is active on headset (deep-buffer usecase)
1614 * and if we go to ringtones and select a ringtone, low-latency usecase
1615 * will be started on headset+speaker. As we can't enable headset+speaker
1616 * and headset devices at the same time, select_devices() switches the music
1617 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1618 * So when the ringtone playback is completed, how do we undo the same?
1619 *
1620 * We are relying on the out_set_parameters() call on deep-buffer output,
1621 * once the ringtone playback is ended.
1622 * NOTE: We should not check if the current devices are same as new devices.
1623 * Because select_devices() must be called to switch back the music
1624 * playback to headset.
1625 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001626 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001627 out->devices = val;
1628
1629 if (!out->standby)
1630 select_devices(adev, out->usecase);
1631
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001632 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001633 !adev->voice.in_call &&
1634 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001635 ret = voice_start_call(adev);
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001636 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1637 adev->voice.in_call &&
1638 (out == adev->primary_output)) {
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -08001639 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001640 }
1641 }
1642
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001643 if ((adev->mode == AUDIO_MODE_NORMAL) &&
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001644 adev->voice.in_call &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001645 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001646 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001647 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001648
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001649 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001650 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001651 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001652
1653 if (out == adev->primary_output) {
1654 pthread_mutex_lock(&adev->lock);
1655 audio_extn_set_parameters(adev, parms);
1656 pthread_mutex_unlock(&adev->lock);
1657 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001658 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001659 pthread_mutex_lock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001660 parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001661 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001662 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001663
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001664 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001665 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001666 return ret;
1667}
1668
1669static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1670{
1671 struct stream_out *out = (struct stream_out *)stream;
1672 struct str_parms *query = str_parms_create_str(keys);
1673 char *str;
1674 char value[256];
1675 struct str_parms *reply = str_parms_create();
1676 size_t i, j;
1677 int ret;
1678 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001679
1680 if (!query || !reply) {
1681 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
1682 return NULL;
1683 }
1684
Eric Laurent994a6932013-07-17 11:51:42 -07001685 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001686 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1687 if (ret >= 0) {
1688 value[0] = '\0';
1689 i = 0;
1690 while (out->supported_channel_masks[i] != 0) {
1691 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1692 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1693 if (!first) {
1694 strcat(value, "|");
1695 }
1696 strcat(value, out_channels_name_to_enum_table[j].name);
1697 first = false;
1698 break;
1699 }
1700 }
1701 i++;
1702 }
1703 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1704 str = str_parms_to_str(reply);
1705 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001706 voice_extn_out_get_parameters(out, query, reply);
1707 str = str_parms_to_str(reply);
1708 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella29b8fc72014-01-29 12:52:19 -08001709 free(str);
1710 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001711 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001712 }
1713 str_parms_destroy(query);
1714 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001715 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001716 return str;
1717}
1718
1719static uint32_t out_get_latency(const struct audio_stream_out *stream)
1720{
1721 struct stream_out *out = (struct stream_out *)stream;
1722
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001723 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001724 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1725
1726 return (out->config.period_count * out->config.period_size * 1000) /
1727 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001728}
1729
1730static int out_set_volume(struct audio_stream_out *stream, float left,
1731 float right)
1732{
Eric Laurenta9024de2013-04-04 09:19:12 -07001733 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001734 int volume[2];
1735
Eric Laurenta9024de2013-04-04 09:19:12 -07001736 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1737 /* only take left channel into account: the API is for stereo anyway */
1738 out->muted = (left == 0.0f);
1739 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001740 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001741 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001742 struct audio_device *adev = out->dev;
1743 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001744 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1745 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001746
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001747 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1748 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001749 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1750 if (!ctl) {
1751 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1752 __func__, mixer_ctl_name);
1753 return -EINVAL;
1754 }
1755 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1756 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1757 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1758 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001759 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001760
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001761 return -ENOSYS;
1762}
1763
1764static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1765 size_t bytes)
1766{
1767 struct stream_out *out = (struct stream_out *)stream;
1768 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301769 int snd_scard_state = get_snd_card_state(adev);
Eric Laurent6e895242013-09-05 16:10:57 -07001770 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001771
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001772 pthread_mutex_lock(&out->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301773
Naresh Tanniru80659832014-06-04 18:17:56 +05301774 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
1775 if (out->pcm) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301776 ALOGD(" %s: sound card is not active/SSR state", __func__);
1777 ret= -ENETRESET;
1778 goto exit;
Naresh Tanniru80659832014-06-04 18:17:56 +05301779 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1780 //during SSR for compress usecase we should return error to flinger
1781 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
1782 pthread_mutex_unlock(&out->lock);
1783 return -ENETRESET;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301784 }
1785 }
1786
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001787 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001788 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001789 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001790 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1791 ret = voice_extn_compress_voip_start_output_stream(out);
1792 else
1793 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001794 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001795 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001796 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001797 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001798 goto exit;
1799 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001800 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001801
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001802 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001803 ALOGD("copl(%p): writing buffer (%d bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001804 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001805 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001806 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1807 out->send_new_metadata = 0;
1808 }
1809
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001810 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001811 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001812 if (ret >= 0 && ret < (ssize_t)bytes) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301813 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001814 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05301815 } else if (-ENETRESET == ret) {
1816 ALOGE("copl %s: received sound card offline state on compress write", __func__);
1817 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1818 pthread_mutex_unlock(&out->lock);
1819 out_standby(&out->stream.common);
1820 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001821 }
Naresh Tanniru80659832014-06-04 18:17:56 +05301822 if (!out->playback_started && ret >= 0) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001823 compress_start(out->compr);
1824 out->playback_started = 1;
1825 out->offload_state = OFFLOAD_STATE_PLAYING;
1826 }
1827 pthread_mutex_unlock(&out->lock);
1828 return ret;
1829 } else {
1830 if (out->pcm) {
1831 if (out->muted)
1832 memset((void *)buffer, 0, bytes);
1833 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1834 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001835 if (ret == 0)
1836 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001837 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001838 }
1839
1840exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05301841 /* ToDo: There may be a corner case when SSR happens back to back during
1842 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05301843 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301844 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301845 }
1846
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001847 pthread_mutex_unlock(&out->lock);
1848
1849 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001850 if (out->pcm)
1851 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301852 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05301853 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301854 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05301855 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301856 out->standby = true;
1857 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001858 out_standby(&out->stream.common);
1859 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05301860 out_get_sample_rate(&out->stream.common));
1861
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001862 }
1863 return bytes;
1864}
1865
1866static int out_get_render_position(const struct audio_stream_out *stream,
1867 uint32_t *dsp_frames)
1868{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001869 struct stream_out *out = (struct stream_out *)stream;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001870 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301871 ssize_t ret = -EINVAL;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001872 *dsp_frames = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001873 pthread_mutex_lock(&out->lock);
1874 if (out->compr != NULL) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301875 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001876 &out->sample_rate);
1877 ALOGVV("%s rendered frames %d sample_rate %d",
1878 __func__, *dsp_frames, out->sample_rate);
1879 }
1880 pthread_mutex_unlock(&out->lock);
Naresh Tanniru80659832014-06-04 18:17:56 +05301881 if (-ENETRESET == ret) {
1882 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
1883 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1884 return -EINVAL;
1885 } else if(ret < 0) {
1886 ALOGE(" ERROR: Unable to get time stamp from compress driver");
1887 return -EINVAL;
1888 } else {
1889 return 0;
1890 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001891 } else
1892 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001893}
1894
1895static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1896{
1897 return 0;
1898}
1899
1900static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1901{
1902 return 0;
1903}
1904
1905static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1906 int64_t *timestamp)
1907{
1908 return -EINVAL;
1909}
1910
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001911static int out_get_presentation_position(const struct audio_stream_out *stream,
1912 uint64_t *frames, struct timespec *timestamp)
1913{
1914 struct stream_out *out = (struct stream_out *)stream;
1915 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001916 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001917
1918 pthread_mutex_lock(&out->lock);
1919
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001920 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07001921 if (out->compr != NULL) {
1922 compress_get_tstamp(out->compr, &dsp_frames,
1923 &out->sample_rate);
1924 ALOGVV("%s rendered frames %ld sample_rate %d",
1925 __func__, dsp_frames, out->sample_rate);
1926 *frames = dsp_frames;
1927 ret = 0;
1928 /* this is the best we can do */
1929 clock_gettime(CLOCK_MONOTONIC, timestamp);
1930 }
1931 } else {
1932 if (out->pcm) {
1933 size_t avail;
1934 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1935 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001936 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001937 // This adjustment accounts for buffering after app processor.
1938 // It is based on estimated DSP latency per use case, rather than exact.
1939 signed_frames -=
1940 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1941
Eric Laurent949a0892013-09-20 09:20:13 -07001942 // It would be unusual for this value to be negative, but check just in case ...
1943 if (signed_frames >= 0) {
1944 *frames = signed_frames;
1945 ret = 0;
1946 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001947 }
1948 }
1949 }
1950
1951 pthread_mutex_unlock(&out->lock);
1952
1953 return ret;
1954}
1955
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001956static int out_set_callback(struct audio_stream_out *stream,
1957 stream_callback_t callback, void *cookie)
1958{
1959 struct stream_out *out = (struct stream_out *)stream;
1960
1961 ALOGV("%s", __func__);
1962 pthread_mutex_lock(&out->lock);
1963 out->offload_callback = callback;
1964 out->offload_cookie = cookie;
1965 pthread_mutex_unlock(&out->lock);
1966 return 0;
1967}
1968
1969static int out_pause(struct audio_stream_out* stream)
1970{
1971 struct stream_out *out = (struct stream_out *)stream;
1972 int status = -ENOSYS;
1973 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001974 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001975 ALOGD("copl(%p):pause compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001976 pthread_mutex_lock(&out->lock);
1977 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301978 struct audio_device *adev = out->dev;
1979 int snd_scard_state = get_snd_card_state(adev);
1980
1981 if (SND_CARD_STATE_ONLINE == snd_scard_state)
1982 status = compress_pause(out->compr);
1983
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001984 out->offload_state = OFFLOAD_STATE_PAUSED;
1985 }
1986 pthread_mutex_unlock(&out->lock);
1987 }
1988 return status;
1989}
1990
1991static int out_resume(struct audio_stream_out* stream)
1992{
1993 struct stream_out *out = (struct stream_out *)stream;
1994 int status = -ENOSYS;
1995 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001996 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001997 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001998 status = 0;
1999 pthread_mutex_lock(&out->lock);
2000 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302001 struct audio_device *adev = out->dev;
2002 int snd_scard_state = get_snd_card_state(adev);
2003
2004 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2005 status = compress_resume(out->compr);
2006
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002007 out->offload_state = OFFLOAD_STATE_PLAYING;
2008 }
2009 pthread_mutex_unlock(&out->lock);
2010 }
2011 return status;
2012}
2013
2014static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2015{
2016 struct stream_out *out = (struct stream_out *)stream;
2017 int status = -ENOSYS;
2018 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002019 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002020 pthread_mutex_lock(&out->lock);
2021 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2022 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2023 else
2024 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2025 pthread_mutex_unlock(&out->lock);
2026 }
2027 return status;
2028}
2029
2030static int out_flush(struct audio_stream_out* stream)
2031{
2032 struct stream_out *out = (struct stream_out *)stream;
2033 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002034 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002035 ALOGD("copl(%p):calling compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002036 pthread_mutex_lock(&out->lock);
2037 stop_compressed_output_l(out);
2038 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002039 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002040 return 0;
2041 }
2042 return -ENOSYS;
2043}
2044
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002045/** audio_stream_in implementation **/
2046static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2047{
2048 struct stream_in *in = (struct stream_in *)stream;
2049
2050 return in->config.rate;
2051}
2052
2053static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
2054{
2055 return -ENOSYS;
2056}
2057
2058static size_t in_get_buffer_size(const struct audio_stream *stream)
2059{
2060 struct stream_in *in = (struct stream_in *)stream;
2061
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002062 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
2063 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07002064 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
2065 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002066
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002067 return in->config.period_size * audio_stream_frame_size(stream);
2068}
2069
2070static uint32_t in_get_channels(const struct audio_stream *stream)
2071{
2072 struct stream_in *in = (struct stream_in *)stream;
2073
2074 return in->channel_mask;
2075}
2076
2077static audio_format_t in_get_format(const struct audio_stream *stream)
2078{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002079 struct stream_in *in = (struct stream_in *)stream;
2080
2081 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002082}
2083
2084static int in_set_format(struct audio_stream *stream, audio_format_t format)
2085{
2086 return -ENOSYS;
2087}
2088
2089static int in_standby(struct audio_stream *stream)
2090{
2091 struct stream_in *in = (struct stream_in *)stream;
2092 struct audio_device *adev = in->dev;
2093 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302094 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2095 stream, in->usecase, use_case_table[in->usecase]);
2096
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002097
2098 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2099 /* Ignore standby in case of voip call because the voip input
2100 * stream is closed in adev_close_input_stream()
2101 */
2102 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2103 return status;
2104 }
2105
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002106 pthread_mutex_lock(&in->lock);
2107 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002108 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002109 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002110 if (in->pcm) {
2111 pcm_close(in->pcm);
2112 in->pcm = NULL;
2113 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002114 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002115 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002116 }
2117 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002118 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002119 return status;
2120}
2121
2122static int in_dump(const struct audio_stream *stream, int fd)
2123{
2124 return 0;
2125}
2126
2127static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2128{
2129 struct stream_in *in = (struct stream_in *)stream;
2130 struct audio_device *adev = in->dev;
2131 struct str_parms *parms;
2132 char *str;
2133 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002134 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002135
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302136 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002137 parms = str_parms_create_str(kvpairs);
2138
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002139 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002140 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002141
2142 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2143 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002144 val = atoi(value);
2145 /* no audio source uses val == 0 */
2146 if ((in->source != val) && (val != 0)) {
2147 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002148 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2149 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2150 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2151 (in->config.rate == 8000 || in->config.rate == 16000) &&
2152 (popcount(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002153 err = voice_extn_compress_voip_open_input_stream(in);
2154 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002155 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002156 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002157 }
2158 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002159 }
2160 }
2161
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002162 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2163 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002164 val = atoi(value);
2165 if ((in->device != val) && (val != 0)) {
2166 in->device = val;
2167 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002168 if (!in->standby)
2169 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002170 }
2171 }
2172
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002173done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002174 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002175 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002176
2177 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002178 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002179 return ret;
2180}
2181
2182static char* in_get_parameters(const struct audio_stream *stream,
2183 const char *keys)
2184{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002185 struct stream_in *in = (struct stream_in *)stream;
2186 struct str_parms *query = str_parms_create_str(keys);
2187 char *str;
2188 char value[256];
2189 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002190
2191 if (!query || !reply) {
2192 ALOGE("in_get_parameters: failed to create query or reply");
2193 return NULL;
2194 }
2195
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002196 ALOGV("%s: enter: keys - %s", __func__, keys);
2197
2198 voice_extn_in_get_parameters(in, query, reply);
2199
2200 str = str_parms_to_str(reply);
2201 str_parms_destroy(query);
2202 str_parms_destroy(reply);
2203
2204 ALOGV("%s: exit: returns - %s", __func__, str);
2205 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002206}
2207
2208static int in_set_gain(struct audio_stream_in *stream, float gain)
2209{
2210 return 0;
2211}
2212
2213static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2214 size_t bytes)
2215{
2216 struct stream_in *in = (struct stream_in *)stream;
2217 struct audio_device *adev = in->dev;
2218 int i, ret = -1;
Naresh Tanniru80659832014-06-04 18:17:56 +05302219 int snd_scard_state = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002220
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002221 pthread_mutex_lock(&in->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302222
2223 if (in->pcm) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302224 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05302225 ALOGD(" %s: sound card is not active/SSR state", __func__);
2226 ret= -ENETRESET;
2227 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302228 }
2229 }
2230
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002231 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002232 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002233 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2234 ret = voice_extn_compress_voip_start_input_stream(in);
2235 else
2236 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002237 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002238 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002239 goto exit;
2240 }
2241 in->standby = 0;
2242 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002243
2244 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002245 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
2246 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002247 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2248 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002249 else
2250 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002251 }
2252
2253 /*
2254 * Instead of writing zeroes here, we could trust the hardware
2255 * to always provide zeroes when muted.
2256 */
kunleizc5a639b2014-04-24 18:46:22 +08002257 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002258 memset(buffer, 0, bytes);
2259
2260exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05302261 /* ToDo: There may be a corner case when SSR happens back to back during
2262 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05302263 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302264 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302265 memset(buffer, 0, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302266 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002267 pthread_mutex_unlock(&in->lock);
2268
2269 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302270 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302271 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302272 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302273 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302274 in->standby = true;
2275 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002276 in_standby(&in->stream.common);
2277 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
2278 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05302279 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002280 }
2281 return bytes;
2282}
2283
2284static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
2285{
2286 return 0;
2287}
2288
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002289static int add_remove_audio_effect(const struct audio_stream *stream,
2290 effect_handle_t effect,
2291 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002292{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002293 struct stream_in *in = (struct stream_in *)stream;
2294 int status = 0;
2295 effect_descriptor_t desc;
2296
2297 status = (*effect)->get_descriptor(effect, &desc);
2298 if (status != 0)
2299 return status;
2300
2301 pthread_mutex_lock(&in->lock);
2302 pthread_mutex_lock(&in->dev->lock);
2303 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2304 in->enable_aec != enable &&
2305 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2306 in->enable_aec = enable;
2307 if (!in->standby)
2308 select_devices(in->dev, in->usecase);
2309 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002310 if (in->enable_ns != enable &&
2311 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2312 in->enable_ns = enable;
2313 if (!in->standby)
2314 select_devices(in->dev, in->usecase);
2315 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002316 pthread_mutex_unlock(&in->dev->lock);
2317 pthread_mutex_unlock(&in->lock);
2318
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002319 return 0;
2320}
2321
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002322static int in_add_audio_effect(const struct audio_stream *stream,
2323 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002324{
Eric Laurent994a6932013-07-17 11:51:42 -07002325 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002326 return add_remove_audio_effect(stream, effect, true);
2327}
2328
2329static int in_remove_audio_effect(const struct audio_stream *stream,
2330 effect_handle_t effect)
2331{
Eric Laurent994a6932013-07-17 11:51:42 -07002332 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002333 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002334}
2335
2336static int adev_open_output_stream(struct audio_hw_device *dev,
2337 audio_io_handle_t handle,
2338 audio_devices_t devices,
2339 audio_output_flags_t flags,
2340 struct audio_config *config,
2341 struct audio_stream_out **stream_out)
2342{
2343 struct audio_device *adev = (struct audio_device *)dev;
2344 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002345 int i, ret = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002346 audio_format_t format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002347
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002348 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05302349
2350 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2351 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2352 ALOGE(" sound card is not active rejecting compress output open request");
2353 return -EINVAL;
2354 }
2355
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002356 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2357
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302358 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2359 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2360 devices, flags, &out->stream);
2361
2362
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002363 if (!out) {
2364 return -ENOMEM;
2365 }
2366
2367 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2368 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2369
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002370 if (devices == AUDIO_DEVICE_NONE)
2371 devices = AUDIO_DEVICE_OUT_SPEAKER;
2372
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002373 out->flags = flags;
2374 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002375 out->dev = adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002376 format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002377 out->sample_rate = config->sample_rate;
2378 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2379 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002380 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002381 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002382
2383 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002384 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2385 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2386 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2387
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002388 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002389 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2390 ret = read_hdmi_channel_masks(out);
2391
2392 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2393 ret = audio_extn_read_afe_proxy_channel_masks(out);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002394 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002395 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002396 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002397
2398 if (config->sample_rate == 0)
2399 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2400 if (config->channel_mask == 0)
2401 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2402
2403 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002404 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002405 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2406 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002407 out->config.rate = config->sample_rate;
2408 out->config.channels = popcount(out->channel_mask);
2409 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002410 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2411 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002412 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002413 ret = voice_extn_compress_voip_open_output_stream(out);
2414 if (ret != 0) {
2415 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2416 __func__, ret);
2417 goto error_open;
2418 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002419 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2420 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2421 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2422 ALOGE("%s: Unsupported Offload information", __func__);
2423 ret = -EINVAL;
2424 goto error_open;
2425 }
Mingming Yin90310102013-11-13 16:57:00 -08002426 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002427 !audio_extn_is_dolby_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002428 ALOGE("%s: Unsupported audio format", __func__);
2429 ret = -EINVAL;
2430 goto error_open;
2431 }
2432
2433 out->compr_config.codec = (struct snd_codec *)
2434 calloc(1, sizeof(struct snd_codec));
2435
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002436 if (!out->compr_config.codec) {
2437 ret = -ENOMEM;
2438 goto error_open;
2439 }
2440
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002441 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002442 if (config->offload_info.channel_mask)
2443 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002444 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002445 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002446 config->offload_info.channel_mask = config->channel_mask;
2447 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002448 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002449 out->sample_rate = config->offload_info.sample_rate;
2450
2451 out->stream.set_callback = out_set_callback;
2452 out->stream.pause = out_pause;
2453 out->stream.resume = out_resume;
2454 out->stream.drain = out_drain;
2455 out->stream.flush = out_flush;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002456 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002457
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002458 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002459 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002460 audio_extn_dolby_get_snd_codec_id(adev, out,
2461 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002462 else
2463 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002464 get_snd_codec_id(config->offload_info.format);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002465 if (audio_is_offload_pcm(config->offload_info.format)) {
2466 out->compr_config.fragment_size =
2467 platform_get_pcm_offload_buffer_size(&config->offload_info);
2468 } else {
2469 out->compr_config.fragment_size =
2470 platform_get_compress_offload_buffer_size(&config->offload_info);
2471 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002472 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2473 out->compr_config.codec->sample_rate =
2474 compress_get_alsa_rate(config->offload_info.sample_rate);
2475 out->compr_config.codec->bit_rate =
2476 config->offload_info.bit_rate;
2477 out->compr_config.codec->ch_in =
2478 popcount(config->channel_mask);
2479 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002480 out->bit_width = PCM_OUTPUT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002481
Mingming Yin3ee55c62014-08-04 14:23:35 -07002482 if (config->offload_info.format == AUDIO_FORMAT_AAC)
2483 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002484 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2485 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002486 if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002487 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2488
Mingming Yin3ee55c62014-08-04 14:23:35 -07002489 if (out->bit_width == 24) {
2490 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2491 }
2492
2493 if (out->bit_width == 24 && !platform_check_24_bit_support()) {
2494 ALOGW("24 bit support is not enabled, using 16 bit backend");
2495 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
2496 }
2497
2498 out->compr_config.codec->options.flac_dec.sample_size = out->bit_width;
2499
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002500 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2501 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002502
2503 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002504 out->offload_state = OFFLOAD_STATE_IDLE;
2505 out->playback_started = 0;
2506
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002507 create_offload_callback_thread(out);
2508 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2509 __func__, config->offload_info.version,
2510 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002511 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -08002512 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002513
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002514 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2515 ret = voice_check_and_set_incall_music_usecase(adev, out);
2516 if (ret != 0) {
2517 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2518 __func__, ret);
2519 goto error_open;
2520 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002521 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002522 format = AUDIO_FORMAT_PCM_16_BIT;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002523 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2524 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002525 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002526 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002527 /* primary path is the default path selected if no other outputs are available/suitable */
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002528 format = AUDIO_FORMAT_PCM_16_BIT;
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002529 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002530 out->config = pcm_config_deep_buffer;
2531 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002532 }
2533
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002534 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
2535 &adev->streams_output_cfg_list,
2536 flags, format, &out->app_type_cfg);
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002537 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2538 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2539 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002540 if(adev->primary_output == NULL)
2541 adev->primary_output = out;
2542 else {
2543 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002544 ret = -EEXIST;
2545 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002546 }
2547 }
2548
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002549 /* Check if this usecase is already existing */
2550 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07002551 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2552 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002553 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002554 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002555 ret = -EEXIST;
2556 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002557 }
2558 pthread_mutex_unlock(&adev->lock);
2559
2560 out->stream.common.get_sample_rate = out_get_sample_rate;
2561 out->stream.common.set_sample_rate = out_set_sample_rate;
2562 out->stream.common.get_buffer_size = out_get_buffer_size;
2563 out->stream.common.get_channels = out_get_channels;
2564 out->stream.common.get_format = out_get_format;
2565 out->stream.common.set_format = out_set_format;
2566 out->stream.common.standby = out_standby;
2567 out->stream.common.dump = out_dump;
2568 out->stream.common.set_parameters = out_set_parameters;
2569 out->stream.common.get_parameters = out_get_parameters;
2570 out->stream.common.add_audio_effect = out_add_audio_effect;
2571 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2572 out->stream.get_latency = out_get_latency;
2573 out->stream.set_volume = out_set_volume;
2574 out->stream.write = out_write;
2575 out->stream.get_render_position = out_get_render_position;
2576 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002577 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002578
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002579 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002580 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002581 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002582
2583 config->format = out->stream.common.get_format(&out->stream.common);
2584 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2585 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2586
2587 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302588 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2589 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002590 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002591 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002592
2593error_open:
2594 free(out);
2595 *stream_out = NULL;
2596 ALOGD("%s: exit: ret %d", __func__, ret);
2597 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002598}
2599
2600static void adev_close_output_stream(struct audio_hw_device *dev,
2601 struct audio_stream_out *stream)
2602{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002603 struct stream_out *out = (struct stream_out *)stream;
2604 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002605 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002606
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302607 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2608
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002609 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302610 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002611 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302612 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002613 if(ret != 0)
2614 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2615 __func__, ret);
2616 }
2617 else
2618 out_standby(&stream->common);
2619
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002620 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002621 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002622 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002623 if (out->compr_config.codec != NULL)
2624 free(out->compr_config.codec);
2625 }
2626 pthread_cond_destroy(&out->cond);
2627 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002628 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002629 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002630}
2631
2632static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2633{
2634 struct audio_device *adev = (struct audio_device *)dev;
2635 struct str_parms *parms;
2636 char *str;
2637 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002638 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002639 int ret = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002640
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002641 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002642 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002643
Naresh Tanniru4c630392014-05-12 01:05:52 +05302644 err = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
2645 if (err >= 0) {
2646 char *snd_card_status = value+2;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302647 if (strstr(snd_card_status, "OFFLINE")) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302648 struct listnode *node;
2649 struct audio_usecase *usecase;
2650
Naresh Tanniru4c630392014-05-12 01:05:52 +05302651 ALOGD("Received sound card OFFLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302652 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2653
2654 pthread_mutex_lock(&adev->lock);
2655 //close compress session on OFFLINE status
2656 usecase = get_usecase_from_list(adev,USECASE_AUDIO_PLAYBACK_OFFLOAD);
2657 if (usecase && usecase->stream.out) {
2658 ALOGD(" %s closing compress session on OFFLINE state", __func__);
2659
2660 struct stream_out *out = usecase->stream.out;
2661
2662 pthread_mutex_unlock(&adev->lock);
2663 out_standby(&out->stream.common);
2664 } else
2665 pthread_mutex_unlock(&adev->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302666 } else if (strstr(snd_card_status, "ONLINE")) {
2667 ALOGD("Received sound card ONLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302668 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302669 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05302670 }
2671
2672 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002673 ret = voice_set_parameters(adev, parms);
2674 if (ret != 0)
2675 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002676
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002677 ret = platform_set_parameters(adev->platform, parms);
2678 if (ret != 0)
2679 goto done;
2680
2681 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2682 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002683 /* When set to false, HAL should disable EC and NS
2684 * But it is currently not supported.
2685 */
2686 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2687 adev->bluetooth_nrec = true;
2688 else
2689 adev->bluetooth_nrec = false;
2690 }
2691
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002692 err = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2693 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002694 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2695 adev->screen_off = false;
2696 else
2697 adev->screen_off = true;
2698 }
2699
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002700 err = str_parms_get_int(parms, "rotation", &val);
2701 if (err >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002702 bool reverse_speakers = false;
2703 switch(val) {
2704 // FIXME: note that the code below assumes that the speakers are in the correct placement
2705 // relative to the user when the device is rotated 90deg from its default rotation. This
2706 // assumption is device-specific, not platform-specific like this code.
2707 case 270:
2708 reverse_speakers = true;
2709 break;
2710 case 0:
2711 case 90:
2712 case 180:
2713 break;
2714 default:
2715 ALOGE("%s: unexpected rotation of %d", __func__, val);
2716 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002717 if (adev->speaker_lr_swap != reverse_speakers) {
2718 adev->speaker_lr_swap = reverse_speakers;
2719 // only update the selected device if there is active pcm playback
2720 struct audio_usecase *usecase;
2721 struct listnode *node;
2722 list_for_each(node, &adev->usecase_list) {
2723 usecase = node_to_item(node, struct audio_usecase, list);
2724 if (usecase->type == PCM_PLAYBACK) {
2725 select_devices(adev, usecase->id);
2726 break;
2727 }
2728 }
2729 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002730 }
2731
Mingming Yin514a8bc2014-07-29 15:22:21 -07002732 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2733 if (ret >= 0) {
2734 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2735 adev->bt_wb_speech_enabled = true;
2736 else
2737 adev->bt_wb_speech_enabled = false;
2738 }
2739
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002740 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002741
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002742done:
2743 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002744 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002745 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002746 return ret;
2747}
2748
2749static char* adev_get_parameters(const struct audio_hw_device *dev,
2750 const char *keys)
2751{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002752 struct audio_device *adev = (struct audio_device *)dev;
2753 struct str_parms *reply = str_parms_create();
2754 struct str_parms *query = str_parms_create_str(keys);
2755 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05302756 char value[256] = {0};
2757 int ret = 0;
2758
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002759 if (!query || !reply) {
2760 ALOGE("adev_get_parameters: failed to create query or reply");
2761 return NULL;
2762 }
2763
Naresh Tannirud7205b62014-06-20 02:54:48 +05302764 ret = str_parms_get_str(query, "SND_CARD_STATUS", value,
2765 sizeof(value));
2766 if (ret >=0) {
2767 int val = 1;
2768 pthread_mutex_lock(&adev->snd_card_status.lock);
2769 if (SND_CARD_STATE_OFFLINE == adev->snd_card_status.state)
2770 val = 0;
2771 pthread_mutex_unlock(&adev->snd_card_status.lock);
2772 str_parms_add_int(reply, "SND_CARD_STATUS", val);
2773 goto exit;
2774 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002775
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002776 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002777 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002778 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002779 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05302780 pthread_mutex_unlock(&adev->lock);
2781
Naresh Tannirud7205b62014-06-20 02:54:48 +05302782exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002783 str = str_parms_to_str(reply);
2784 str_parms_destroy(query);
2785 str_parms_destroy(reply);
2786
2787 ALOGV("%s: exit: returns - %s", __func__, str);
2788 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002789}
2790
2791static int adev_init_check(const struct audio_hw_device *dev)
2792{
2793 return 0;
2794}
2795
2796static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2797{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002798 int ret;
2799 struct audio_device *adev = (struct audio_device *)dev;
2800 pthread_mutex_lock(&adev->lock);
2801 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002802 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002803 pthread_mutex_unlock(&adev->lock);
2804 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002805}
2806
2807static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2808{
2809 return -ENOSYS;
2810}
2811
2812static int adev_get_master_volume(struct audio_hw_device *dev,
2813 float *volume)
2814{
2815 return -ENOSYS;
2816}
2817
2818static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2819{
2820 return -ENOSYS;
2821}
2822
2823static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2824{
2825 return -ENOSYS;
2826}
2827
2828static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2829{
2830 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002831 pthread_mutex_lock(&adev->lock);
2832 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002833 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002834 adev->mode = mode;
2835 }
2836 pthread_mutex_unlock(&adev->lock);
2837 return 0;
2838}
2839
2840static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2841{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002842 int ret;
2843
2844 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002845 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002846 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2847 pthread_mutex_unlock(&adev->lock);
2848
2849 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002850}
2851
2852static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2853{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002854 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002855 return 0;
2856}
2857
2858static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2859 const struct audio_config *config)
2860{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002861 int channel_count = popcount(config->channel_mask);
2862
2863 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2864}
2865
2866static int adev_open_input_stream(struct audio_hw_device *dev,
2867 audio_io_handle_t handle,
2868 audio_devices_t devices,
2869 struct audio_config *config,
2870 struct audio_stream_in **stream_in)
2871{
2872 struct audio_device *adev = (struct audio_device *)dev;
2873 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002874 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002875 int channel_count = popcount(config->channel_mask);
2876
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302877
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002878 *stream_in = NULL;
2879 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2880 return -EINVAL;
2881
2882 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002883
2884 if (!in) {
2885 ALOGE("failed to allocate input stream");
2886 return -ENOMEM;
2887 }
2888
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302889 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
2890 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2891 devices, &in->stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002892
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07002893 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2894
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002895 in->stream.common.get_sample_rate = in_get_sample_rate;
2896 in->stream.common.set_sample_rate = in_set_sample_rate;
2897 in->stream.common.get_buffer_size = in_get_buffer_size;
2898 in->stream.common.get_channels = in_get_channels;
2899 in->stream.common.get_format = in_get_format;
2900 in->stream.common.set_format = in_set_format;
2901 in->stream.common.standby = in_standby;
2902 in->stream.common.dump = in_dump;
2903 in->stream.common.set_parameters = in_set_parameters;
2904 in->stream.common.get_parameters = in_get_parameters;
2905 in->stream.common.add_audio_effect = in_add_audio_effect;
2906 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2907 in->stream.set_gain = in_set_gain;
2908 in->stream.read = in_read;
2909 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2910
2911 in->device = devices;
2912 in->source = AUDIO_SOURCE_DEFAULT;
2913 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002914 in->standby = 1;
2915 in->channel_mask = config->channel_mask;
2916
2917 /* Update config params with the requested sample rate and channels */
2918 in->usecase = USECASE_AUDIO_RECORD;
2919 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002920 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002921 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002922
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002923 if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002924 if(audio_extn_ssr_get_enabled()) {
2925 if(audio_extn_ssr_init(adev, in)) {
2926 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2927 ret = -EINVAL;
2928 goto err_open;
2929 }
2930 } else {
2931 ret = -EINVAL;
2932 goto err_open;
2933 }
Mingming Yine62d7842013-10-25 16:26:03 -07002934 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002935 audio_extn_compr_cap_format_supported(config->format) &&
2936 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Mingming Yine62d7842013-10-25 16:26:03 -07002937 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002938 } else {
2939 in->config.channels = channel_count;
2940 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2941 buffer_size = get_input_buffer_size(config->sample_rate,
2942 config->format,
2943 channel_count);
2944 in->config.period_size = buffer_size / frame_size;
2945 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002946
2947 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002948 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002949 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002950
2951err_open:
2952 free(in);
2953 *stream_in = NULL;
2954 return ret;
2955}
2956
2957static void adev_close_input_stream(struct audio_hw_device *dev,
2958 struct audio_stream_in *stream)
2959{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002960 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002961 struct stream_in *in = (struct stream_in *)stream;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302962 struct audio_device *adev = in->dev;
2963
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302964 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002965
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002966 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302967 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002968 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302969 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002970 if (ret != 0)
2971 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2972 __func__, ret);
2973 } else
2974 in_standby(&stream->common);
2975
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002976 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2977 audio_extn_ssr_deinit();
2978 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002979 free(stream);
2980
Mingming Yine62d7842013-10-25 16:26:03 -07002981 if(audio_extn_compr_cap_enabled() &&
2982 audio_extn_compr_cap_format_supported(in->config.format))
2983 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002984 return;
2985}
2986
2987static int adev_dump(const audio_hw_device_t *device, int fd)
2988{
2989 return 0;
2990}
2991
2992static int adev_close(hw_device_t *device)
2993{
2994 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002995
2996 if (!adev)
2997 return 0;
2998
2999 pthread_mutex_lock(&adev_init_lock);
3000
3001 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08003002 audio_extn_listen_deinit(adev);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003003 audio_extn_utils_release_streams_output_cfg_list(&adev->streams_output_cfg_list);
Kiran Kandi910e1862013-10-29 13:29:42 -07003004 audio_route_free(adev->audio_route);
3005 free(adev->snd_dev_ref_cnt);
3006 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07003007 free(device);
3008 adev = NULL;
3009 }
3010 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003011 return 0;
3012}
3013
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003014static int adev_open(const hw_module_t *module, const char *name,
3015 hw_device_t **device)
3016{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003017 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003018
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003019 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003020 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3021
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003022 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07003023 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003024 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07003025 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003026 ALOGD("%s: returning existing instance of adev", __func__);
3027 ALOGD("%s: exit", __func__);
3028 pthread_mutex_unlock(&adev_init_lock);
3029 return 0;
3030 }
3031
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003032 adev = calloc(1, sizeof(struct audio_device));
3033
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003034 if (!adev) {
3035 pthread_mutex_unlock(&adev_init_lock);
3036 return -ENOMEM;
3037 }
3038
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07003039 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3040
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003041 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3042 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3043 adev->device.common.module = (struct hw_module_t *)module;
3044 adev->device.common.close = adev_close;
3045
3046 adev->device.init_check = adev_init_check;
3047 adev->device.set_voice_volume = adev_set_voice_volume;
3048 adev->device.set_master_volume = adev_set_master_volume;
3049 adev->device.get_master_volume = adev_get_master_volume;
3050 adev->device.set_master_mute = adev_set_master_mute;
3051 adev->device.get_master_mute = adev_get_master_mute;
3052 adev->device.set_mode = adev_set_mode;
3053 adev->device.set_mic_mute = adev_set_mic_mute;
3054 adev->device.get_mic_mute = adev_get_mic_mute;
3055 adev->device.set_parameters = adev_set_parameters;
3056 adev->device.get_parameters = adev_get_parameters;
3057 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3058 adev->device.open_output_stream = adev_open_output_stream;
3059 adev->device.close_output_stream = adev_close_output_stream;
3060 adev->device.open_input_stream = adev_open_input_stream;
3061 adev->device.close_input_stream = adev_close_input_stream;
3062 adev->device.dump = adev_dump;
3063
3064 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003065 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003066 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003067 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003068 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003069 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003070 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003071 /* adev->cur_hdmi_channels = 0; by calloc() */
Mingming Yin3ee55c62014-08-04 14:23:35 -07003072 adev->cur_codec_backend_samplerate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
3073 adev->cur_codec_backend_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Eric Laurentb23d5282013-05-14 15:27:20 -07003074 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003075 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003076 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08003077 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003078 adev->offload_usecases_state = 0;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303079
3080 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
3081 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003082 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003083 adev->platform = platform_init(adev);
3084 if (!adev->platform) {
3085 free(adev->snd_dev_ref_cnt);
3086 free(adev);
3087 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3088 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08003089 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003090 return -EINVAL;
3091 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003092
Naresh Tanniru4c630392014-05-12 01:05:52 +05303093 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
3094
Eric Laurentc4aef752013-09-12 17:45:53 -07003095 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
3096 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3097 if (adev->visualizer_lib == NULL) {
3098 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3099 } else {
3100 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3101 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003102 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003103 "visualizer_hal_start_output");
3104 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003105 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003106 "visualizer_hal_stop_output");
3107 }
3108 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003109 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07003110
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003111 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
3112 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3113 if (adev->offload_effects_lib == NULL) {
3114 ALOGE("%s: DLOPEN failed for %s", __func__,
3115 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3116 } else {
3117 ALOGV("%s: DLOPEN successful for %s", __func__,
3118 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3119 adev->offload_effects_start_output =
3120 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3121 "offload_effects_bundle_hal_start_output");
3122 adev->offload_effects_stop_output =
3123 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3124 "offload_effects_bundle_hal_stop_output");
3125 }
3126 }
3127
Mingming Yin514a8bc2014-07-29 15:22:21 -07003128 adev->bt_wb_speech_enabled = false;
3129
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003130 *device = &adev->device.common;
3131
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003132 audio_extn_utils_update_streams_output_cfg_list(adev->platform, adev->mixer,
3133 &adev->streams_output_cfg_list);
3134
Kiran Kandi910e1862013-10-29 13:29:42 -07003135 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003136 pthread_mutex_unlock(&adev_init_lock);
3137
Eric Laurent994a6932013-07-17 11:51:42 -07003138 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003139 return 0;
3140}
3141
Mingming Yin07972cc2014-06-06 17:11:23 -07003142int pcm_ioctl(struct pcm *pcm, int request, ...)
3143{
3144 va_list ap;
3145 void * arg;
3146 int pcm_fd = *(int*)pcm;
3147
3148 va_start(ap, request);
3149 arg = va_arg(ap, void *);
3150 va_end(ap);
3151
3152 return ioctl(pcm_fd, request, arg);
3153}
3154
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003155static struct hw_module_methods_t hal_module_methods = {
3156 .open = adev_open,
3157};
3158
3159struct audio_module HAL_MODULE_INFO_SYM = {
3160 .common = {
3161 .tag = HARDWARE_MODULE_TAG,
3162 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3163 .hal_api_version = HARDWARE_HAL_API_VERSION,
3164 .id = AUDIO_HARDWARE_MODULE_ID,
3165 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08003166 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003167 .methods = &hal_module_methods,
3168 },
3169};