blob: d5c61f8acb941eea6bf771f264df63a41351227e [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003 * Not a Contribution.
4 *
Shiv Maliyappanahalli8911f282014-01-10 15:56:19 -08005 * Copyright (C) 2013 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_primary"
21/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070022/*#define VERY_VERY_VERBOSE_LOGGING*/
23#ifdef VERY_VERY_VERBOSE_LOGGING
24#define ALOGVV ALOGV
25#else
26#define ALOGVV(a...) do { } while(0)
27#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080028
29#include <errno.h>
30#include <pthread.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080034#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070035#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070036#include <sys/resource.h>
37#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038
39#include <cutils/log.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070042#include <cutils/atomic.h>
43#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080044
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <hardware/audio_effect.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070050#include "platform_api.h"
51#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070052#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080053#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include "sound/compress_params.h"
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080056#include "sound/asound.h"
ApurupaPattapu2e084df2013-12-18 15:47:59 -080057
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070058#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
59/* ToDo: Check and update a proper value in msec */
60#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
61#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
62
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080063
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080064#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
65
Eric Laurentb23d5282013-05-14 15:27:20 -070066struct pcm_config pcm_config_deep_buffer = {
67 .channels = 2,
68 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
69 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
70 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
71 .format = PCM_FORMAT_S16_LE,
72 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
73 .stop_threshold = INT_MAX,
74 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
75};
76
77struct pcm_config pcm_config_low_latency = {
78 .channels = 2,
79 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
80 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
81 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
82 .format = PCM_FORMAT_S16_LE,
83 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
84 .stop_threshold = INT_MAX,
85 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
86};
87
88struct pcm_config pcm_config_hdmi_multi = {
89 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
90 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
91 .period_size = HDMI_MULTI_PERIOD_SIZE,
92 .period_count = HDMI_MULTI_PERIOD_COUNT,
93 .format = PCM_FORMAT_S16_LE,
94 .start_threshold = 0,
95 .stop_threshold = INT_MAX,
96 .avail_min = 0,
97};
98
99struct pcm_config pcm_config_audio_capture = {
100 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700101 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
102 .format = PCM_FORMAT_S16_LE,
103};
104
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800105const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700106 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
107 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
108 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700109 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700110#ifdef MULTIPLE_OFFLOAD_ENABLED
111 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
112 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
113 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
114 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
115 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
116 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
117 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
118 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
119#endif
Eric Laurentb23d5282013-05-14 15:27:20 -0700120 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700121 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700122 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700123 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700124 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800125 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800126 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700127 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700128
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700129 [USECASE_VOICE2_CALL] = "voice2-call",
130 [USECASE_VOLTE_CALL] = "volte-call",
131 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800132 [USECASE_VOWLAN_CALL] = "vowlan-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800133 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700134 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
135 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
136 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800137 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
138 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
139 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
140
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700141 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
142 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700143 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
144 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700145};
146
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700147static const audio_usecase_t offload_usecases[] = {
148 USECASE_AUDIO_PLAYBACK_OFFLOAD,
149#ifdef MULTIPLE_OFFLOAD_ENABLED
150 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
151 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
152 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
153 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
154 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
155 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
156 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
157 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
158#endif
159};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800160
161#define STRING_TO_ENUM(string) { #string, string }
162
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163struct string_to_enum {
164 const char *name;
165 uint32_t value;
166};
167
168static const struct string_to_enum out_channels_name_to_enum_table[] = {
169 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
170 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
171 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
172};
173
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700174static struct audio_device *adev = NULL;
175static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700176static unsigned int audio_device_ref_count;
177
Haynes Mathew George5191a852013-09-11 14:19:36 -0700178static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800179
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800180static int check_and_set_gapless_mode(struct audio_device *adev) {
181
182
183 char value[PROPERTY_VALUE_MAX] = {0};
184 bool gapless_enabled = false;
185 const char *mixer_ctl_name = "Compress Gapless Playback";
186 struct mixer_ctl *ctl;
187
188 ALOGV("%s:", __func__);
189 property_get("audio.offload.gapless.enabled", value, NULL);
190 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
191
192 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
193 if (!ctl) {
194 ALOGE("%s: Could not get ctl for mixer cmd - %s",
195 __func__, mixer_ctl_name);
196 return -EINVAL;
197 }
198
199 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
200 ALOGE("%s: Could not set gapless mode %d",
201 __func__, gapless_enabled);
202 return -EINVAL;
203 }
204 return 0;
205}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700206
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700207static bool is_supported_format(audio_format_t format)
208{
Eric Laurent86e17132013-09-12 17:49:30 -0700209 if (format == AUDIO_FORMAT_MP3 ||
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800210 format == AUDIO_FORMAT_AAC ||
211 format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD ||
212 format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
213 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700214
215 return false;
216}
217
218static int get_snd_codec_id(audio_format_t format)
219{
220 int id = 0;
221
222 switch (format) {
223 case AUDIO_FORMAT_MP3:
224 id = SND_AUDIOCODEC_MP3;
225 break;
226 case AUDIO_FORMAT_AAC:
227 id = SND_AUDIOCODEC_AAC;
228 break;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800229 case AUDIO_FORMAT_PCM_16_BIT_OFFLOAD:
230 case AUDIO_FORMAT_PCM_24_BIT_OFFLOAD:
231 id = SND_AUDIOCODEC_PCM;
232 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700233 default:
Mingming Yin90310102013-11-13 16:57:00 -0800234 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700235 }
236
237 return id;
238}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800239
Naresh Tanniru80659832014-06-04 18:17:56 +0530240static int get_snd_card_state(struct audio_device *adev)
241{
242 int snd_scard_state;
243
244 if (!adev)
245 return SND_CARD_STATE_OFFLINE;
246
247 pthread_mutex_lock(&adev->snd_card_status.lock);
248 snd_scard_state = adev->snd_card_status.state;
249 pthread_mutex_unlock(&adev->snd_card_status.lock);
250
251 return snd_scard_state;
252}
253
254static int set_snd_card_state(struct audio_device *adev, int snd_scard_state)
255{
256 if (!adev)
257 return -ENOSYS;
258
259 pthread_mutex_lock(&adev->snd_card_status.lock);
260 adev->snd_card_status.state = snd_scard_state;
261 pthread_mutex_unlock(&adev->snd_card_status.lock);
262
263 return 0;
264}
265
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700266int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700267 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800268{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700269 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700270 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800271
272 if (usecase == NULL)
273 return -EINVAL;
274
275 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
276
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800277 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700278 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800279 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700280 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800281
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -0800282#ifdef DS1_DOLBY_DAP_ENABLED
283 audio_extn_dolby_set_dmid(adev);
284 audio_extn_dolby_set_endpoint(adev);
285#endif
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530286 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700287 audio_extn_utils_send_audio_calibration(adev, usecase);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700288 audio_extn_utils_send_app_type_cfg(usecase);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800289 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700290 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700291 ALOGV("%s: apply mixer and update path: %s", __func__, mixer_path);
292 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800293 ALOGV("%s: exit", __func__);
294 return 0;
295}
296
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700297int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700298 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800299{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700300 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700301 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800302
303 if (usecase == NULL)
304 return -EINVAL;
305
306 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700307 if (usecase->type == PCM_CAPTURE)
308 snd_device = usecase->in_snd_device;
309 else
310 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800311 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700312 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700313 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
314 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530315 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800316 ALOGV("%s: exit", __func__);
317 return 0;
318}
319
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700320int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700321 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800322{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700323 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
324
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800325 if (snd_device < SND_DEVICE_MIN ||
326 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800327 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800328 return -EINVAL;
329 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700330
331 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700332
333 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
334 ALOGE("%s: Invalid sound device returned", __func__);
335 return -EINVAL;
336 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700337 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700338 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700339 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700340 return 0;
341 }
342
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700343 /* start usb playback thread */
344 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
345 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
346 audio_extn_usb_start_playback(adev);
347
348 /* start usb capture thread */
349 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
350 audio_extn_usb_start_capture(adev);
351
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800352 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
353 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700354 audio_extn_spkr_prot_is_enabled()) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700355 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
356 adev->snd_dev_ref_cnt[snd_device]--;
357 return -EINVAL;
358 }
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800359 if (audio_extn_spkr_prot_start_processing(snd_device)) {
360 ALOGE("%s: spkr_start_processing failed", __func__);
361 return -EINVAL;
362 }
363 } else {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700364 ALOGV("%s: snd_device(%d: %s)", __func__,
365 snd_device, device_name);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -0700366 /* due to the possibility of calibration overwrite between listen
367 and audio, notify listen hal before audio calibration is sent */
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530368 audio_extn_listen_update_device_status(snd_device,
369 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700370 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700371 adev->snd_dev_ref_cnt[snd_device]--;
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530372 audio_extn_listen_update_device_status(snd_device,
373 LISTEN_EVENT_SND_DEVICE_FREE);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700374 return -EINVAL;
375 }
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700376 audio_route_apply_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800377 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800378 return 0;
379}
380
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700381int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700382 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800383{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700384 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
385
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800386 if (snd_device < SND_DEVICE_MIN ||
387 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800388 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800389 return -EINVAL;
390 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700391 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
392 ALOGE("%s: device ref cnt is already 0", __func__);
393 return -EINVAL;
394 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700395
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700396 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700397
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700398 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
399 ALOGE("%s: Invalid sound device returned", __func__);
400 return -EINVAL;
401 }
402
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700403 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700404 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700405 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800406 /* exit usb play back thread */
407 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
408 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
409 audio_extn_usb_stop_playback();
410
411 /* exit usb capture thread */
412 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -0700413 audio_extn_usb_stop_capture();
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800414
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800415 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
416 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700417 audio_extn_spkr_prot_is_enabled()) {
418 audio_extn_spkr_prot_stop_processing();
419 } else
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700420 audio_route_reset_and_update_path(adev->audio_route, device_name);
Kiran Kandide144c82013-11-20 15:58:32 -0800421
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530422 audio_extn_listen_update_device_status(snd_device,
Kiran Kandide144c82013-11-20 15:58:32 -0800423 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700424 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700425
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800426 return 0;
427}
428
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700429static void check_usecases_codec_backend(struct audio_device *adev,
430 struct audio_usecase *uc_info,
431 snd_device_t snd_device)
432{
433 struct listnode *node;
434 struct audio_usecase *usecase;
435 bool switch_device[AUDIO_USECASE_MAX];
436 int i, num_uc_to_switch = 0;
437
438 /*
439 * This function is to make sure that all the usecases that are active on
440 * the hardware codec backend are always routed to any one device that is
441 * handled by the hardware codec.
442 * For example, if low-latency and deep-buffer usecases are currently active
443 * on speaker and out_set_parameters(headset) is received on low-latency
444 * output, then we have to make sure deep-buffer is also switched to headset,
445 * because of the limitation that both the devices cannot be enabled
446 * at the same time as they share the same backend.
447 */
448 /* Disable all the usecases on the shared backend other than the
449 specified usecase */
450 for (i = 0; i < AUDIO_USECASE_MAX; i++)
451 switch_device[i] = false;
452
453 list_for_each(node, &adev->usecase_list) {
454 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800455 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700456 usecase != uc_info &&
457 usecase->out_snd_device != snd_device &&
458 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
459 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
460 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700461 platform_get_snd_device_name(usecase->out_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700462 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700463 switch_device[usecase->id] = true;
464 num_uc_to_switch++;
465 }
466 }
467
468 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700469 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700470
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530471 /* Make sure the previous devices to be disabled first and then enable the
472 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700473 list_for_each(node, &adev->usecase_list) {
474 usecase = node_to_item(node, struct audio_usecase, list);
475 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700476 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700477 }
478 }
479
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700480 list_for_each(node, &adev->usecase_list) {
481 usecase = node_to_item(node, struct audio_usecase, list);
482 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700483 enable_snd_device(adev, snd_device);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700484 }
485 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700486
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700487 /* Re-route all the usecases on the shared backend other than the
488 specified usecase to new snd devices */
489 list_for_each(node, &adev->usecase_list) {
490 usecase = node_to_item(node, struct audio_usecase, list);
491 /* Update the out_snd_device only before enabling the audio route */
492 if (switch_device[usecase->id] ) {
493 usecase->out_snd_device = snd_device;
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700494 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700495 }
496 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700497 }
498}
499
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700500static void check_and_route_capture_usecases(struct audio_device *adev,
501 struct audio_usecase *uc_info,
502 snd_device_t snd_device)
503{
504 struct listnode *node;
505 struct audio_usecase *usecase;
506 bool switch_device[AUDIO_USECASE_MAX];
507 int i, num_uc_to_switch = 0;
508
509 /*
510 * This function is to make sure that all the active capture usecases
511 * are always routed to the same input sound device.
512 * For example, if audio-record and voice-call usecases are currently
513 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
514 * is received for voice call then we have to make sure that audio-record
515 * usecase is also switched to earpiece i.e. voice-dmic-ef,
516 * because of the limitation that two devices cannot be enabled
517 * at the same time if they share the same backend.
518 */
519 for (i = 0; i < AUDIO_USECASE_MAX; i++)
520 switch_device[i] = false;
521
522 list_for_each(node, &adev->usecase_list) {
523 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800524 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700525 usecase != uc_info &&
526 usecase->in_snd_device != snd_device) {
527 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
528 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700529 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700530 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700531 switch_device[usecase->id] = true;
532 num_uc_to_switch++;
533 }
534 }
535
536 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700537 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700538
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530539 /* Make sure the previous devices to be disabled first and then enable the
540 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700541 list_for_each(node, &adev->usecase_list) {
542 usecase = node_to_item(node, struct audio_usecase, list);
543 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700544 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800545 }
546 }
547
548 list_for_each(node, &adev->usecase_list) {
549 usecase = node_to_item(node, struct audio_usecase, list);
550 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700551 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700552 }
553 }
554
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700555 /* Re-route all the usecases on the shared backend other than the
556 specified usecase to new snd devices */
557 list_for_each(node, &adev->usecase_list) {
558 usecase = node_to_item(node, struct audio_usecase, list);
559 /* Update the in_snd_device only before enabling the audio route */
560 if (switch_device[usecase->id] ) {
561 usecase->in_snd_device = snd_device;
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700562 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700563 }
564 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700565 }
566}
567
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800568/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700569static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800570{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700571 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700572 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800573
574 switch (channels) {
575 /*
576 * Do not handle stereo output in Multi-channel cases
577 * Stereo case is handled in normal playback path
578 */
579 case 6:
580 ALOGV("%s: HDMI supports 5.1", __func__);
581 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
582 break;
583 case 8:
584 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
585 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
586 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
587 break;
588 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700589 ALOGE("HDMI does not support multi channel playback");
590 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800591 break;
592 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700593 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800594}
595
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700596static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
597{
598 struct audio_usecase *usecase;
599 struct listnode *node;
600
601 list_for_each(node, &adev->usecase_list) {
602 usecase = node_to_item(node, struct audio_usecase, list);
603 if (usecase->type == VOICE_CALL) {
604 ALOGV("%s: usecase id %d", __func__, usecase->id);
605 return usecase->id;
606 }
607 }
608 return USECASE_INVALID;
609}
610
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700611struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700612 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700613{
614 struct audio_usecase *usecase;
615 struct listnode *node;
616
617 list_for_each(node, &adev->usecase_list) {
618 usecase = node_to_item(node, struct audio_usecase, list);
619 if (usecase->id == uc_id)
620 return usecase;
621 }
622 return NULL;
623}
624
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700625int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800626{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800627 snd_device_t out_snd_device = SND_DEVICE_NONE;
628 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700629 struct audio_usecase *usecase = NULL;
630 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800631 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800632 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800633 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800634 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700635 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800636
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700637 usecase = get_usecase_from_list(adev, uc_id);
638 if (usecase == NULL) {
639 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
640 return -EINVAL;
641 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800642
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800643 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800644 (usecase->type == VOIP_CALL) ||
645 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700646 out_snd_device = platform_get_output_snd_device(adev->platform,
647 usecase->stream.out->devices);
648 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700649 usecase->devices = usecase->stream.out->devices;
650 } else {
651 /*
652 * If the voice call is active, use the sound devices of voice call usecase
653 * so that it would not result any device switch. All the usecases will
654 * be switched to new device when select_devices() is called for voice call
655 * usecase. This is to avoid switching devices for voice call when
656 * check_usecases_codec_backend() is called below.
657 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700658 if (voice_is_in_call(adev)) {
659 vc_usecase = get_usecase_from_list(adev,
660 get_voice_usecase_id_from_list(adev));
Helen Zeng067b96b2013-11-26 12:10:29 -0800661 if ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
662 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700663 in_snd_device = vc_usecase->in_snd_device;
664 out_snd_device = vc_usecase->out_snd_device;
665 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800666 } else if (voice_extn_compress_voip_is_active(adev)) {
667 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Narsinga Rao Chellac17cab62014-04-11 17:20:46 -0700668 if ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Avinash Vaish4d6167d2014-06-25 12:20:37 +0530669 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
670 (voip_usecase->stream.out != adev->primary_output)) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800671 in_snd_device = voip_usecase->in_snd_device;
672 out_snd_device = voip_usecase->out_snd_device;
673 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800674 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800675 hfp_ucid = audio_extn_hfp_get_usecase();
676 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800677 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
678 in_snd_device = hfp_usecase->in_snd_device;
679 out_snd_device = hfp_usecase->out_snd_device;
680 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700681 }
682 if (usecase->type == PCM_PLAYBACK) {
683 usecase->devices = usecase->stream.out->devices;
684 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700685 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700686 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700687 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700688 if (usecase->stream.out == adev->primary_output &&
689 adev->active_input &&
690 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
691 select_devices(adev, adev->active_input->usecase);
692 }
693 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700694 } else if (usecase->type == PCM_CAPTURE) {
695 usecase->devices = usecase->stream.in->device;
696 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700697 if (in_snd_device == SND_DEVICE_NONE) {
698 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
699 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700700 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700701 adev->primary_output->devices);
702 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700703 in_snd_device = platform_get_input_snd_device(adev->platform,
704 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700705 }
706 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700707 }
708 }
709
710 if (out_snd_device == usecase->out_snd_device &&
711 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800712 return 0;
713 }
714
sangwoobc677242013-08-08 16:53:43 +0900715 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700716 out_snd_device, platform_get_snd_device_name(out_snd_device),
717 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800718
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800719 /*
720 * Limitation: While in call, to do a device switch we need to disable
721 * and enable both RX and TX devices though one of them is same as current
722 * device.
723 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800724 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700725 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800726 }
727
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700728 /* Disable current sound devices */
729 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700730 disable_audio_route(adev, usecase);
731 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732 }
733
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700734 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700735 disable_audio_route(adev, usecase);
736 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800737 }
738
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800739 /* Applicable only on the targets that has external modem.
740 * New device information should be sent to modem before enabling
741 * the devices to reduce in-call device switch time.
742 */
743 if (usecase->type == VOICE_CALL)
744 status = platform_switch_voice_call_enable_device_config(adev->platform,
745 out_snd_device,
746 in_snd_device);
747
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700748 /* Enable new sound devices */
749 if (out_snd_device != SND_DEVICE_NONE) {
750 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
751 check_usecases_codec_backend(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700752 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800753 }
754
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700755 if (in_snd_device != SND_DEVICE_NONE) {
756 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700757 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700758 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700759
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800760 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700761 status = platform_switch_voice_call_device_post(adev->platform,
762 out_snd_device,
763 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800764
sangwoo170731f2013-06-08 15:36:36 +0900765 usecase->in_snd_device = in_snd_device;
766 usecase->out_snd_device = out_snd_device;
767
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700768 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900769
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800770 /* Applicable only on the targets that has external modem.
771 * Enable device command should be sent to modem only after
772 * enabling voice call mixer controls
773 */
774 if (usecase->type == VOICE_CALL)
775 status = platform_switch_voice_call_usecase_route_post(adev->platform,
776 out_snd_device,
777 in_snd_device);
778
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530779 ALOGD("%s: done",__func__);
780
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800781 return status;
782}
783
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800784static int stop_input_stream(struct stream_in *in)
785{
786 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800787 struct audio_usecase *uc_info;
788 struct audio_device *adev = in->dev;
789
Eric Laurentc8400632013-02-14 19:04:54 -0800790 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800791
Eric Laurent994a6932013-07-17 11:51:42 -0700792 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700793 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800794 uc_info = get_usecase_from_list(adev, in->usecase);
795 if (uc_info == NULL) {
796 ALOGE("%s: Could not find the usecase (%d) in the list",
797 __func__, in->usecase);
798 return -EINVAL;
799 }
800
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800801 /* Close in-call recording streams */
802 voice_check_and_stop_incall_rec_usecase(adev, in);
803
Eric Laurent150dbfe2013-02-27 14:31:02 -0800804 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700805 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700806
807 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700808 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800809
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800810 list_remove(&uc_info->list);
811 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800812
Eric Laurent994a6932013-07-17 11:51:42 -0700813 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800814 return ret;
815}
816
817int start_input_stream(struct stream_in *in)
818{
819 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800820 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800821 struct audio_usecase *uc_info;
822 struct audio_device *adev = in->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +0530823 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800824
Mingming Yine62d7842013-10-25 16:26:03 -0700825 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530826 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
827 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700828
Naresh Tanniru80659832014-06-04 18:17:56 +0530829
830 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +0530831 ALOGE("%s: sound card is not active/SSR returning error", __func__);
832 ret = -ENETRESET;
Naresh Tanniru4c630392014-05-12 01:05:52 +0530833 goto error_config;
834 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530835
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700836 /* Check if source matches incall recording usecase criteria */
837 ret = voice_check_and_set_incall_rec_usecase(adev, in);
838 if (ret)
839 goto error_config;
840 else
841 ALOGV("%s: usecase(%d)", __func__, in->usecase);
842
Eric Laurentb23d5282013-05-14 15:27:20 -0700843 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800844 if (in->pcm_device_id < 0) {
845 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
846 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800847 ret = -EINVAL;
848 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800849 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700850
851 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800852 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
853 uc_info->id = in->usecase;
854 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800855 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700856 uc_info->devices = in->device;
857 uc_info->in_snd_device = SND_DEVICE_NONE;
858 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800859
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800860 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700861 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800862
Eric Laurentc8400632013-02-14 19:04:54 -0800863 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800864 __func__, adev->snd_card,
865 in->pcm_device_id, in->config.channels);
866 in->pcm = pcm_open(adev->snd_card,
867 in->pcm_device_id, PCM_IN, &in->config);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800868 if (in->pcm && !pcm_is_ready(in->pcm)) {
869 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
870 pcm_close(in->pcm);
871 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800872 ret = -EIO;
873 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800874 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530875
Eric Laurent994a6932013-07-17 11:51:42 -0700876 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800877 return ret;
878
879error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800880 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800881
882error_config:
883 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700884 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800885
886 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800887}
888
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700889/* must be called with out->lock locked */
890static int send_offload_cmd_l(struct stream_out* out, int command)
891{
892 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
893
894 ALOGVV("%s %d", __func__, command);
895
896 cmd->cmd = command;
897 list_add_tail(&out->offload_cmd_list, &cmd->node);
898 pthread_cond_signal(&out->offload_cond);
899 return 0;
900}
901
902/* must be called iwth out->lock locked */
903static void stop_compressed_output_l(struct stream_out *out)
904{
905 out->offload_state = OFFLOAD_STATE_IDLE;
906 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700907 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700908 if (out->compr != NULL) {
909 compress_stop(out->compr);
910 while (out->offload_thread_blocked) {
911 pthread_cond_wait(&out->cond, &out->lock);
912 }
913 }
914}
915
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700916bool is_offload_usecase(audio_usecase_t uc_id)
917{
918 unsigned int i;
919 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
920 if (uc_id == offload_usecases[i])
921 return true;
922 }
923 return false;
924}
925
926static audio_usecase_t get_offload_usecase(struct audio_device *adev)
927{
928 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
929 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
930 char value[PROPERTY_VALUE_MAX] = {0};
931
932 property_get("audio.offload.multiple.enabled", value, NULL);
933 if (!(atoi(value) || !strncmp("true", value, 4)))
934 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
935
936 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
937 for (i = 0; i < num_usecase; i++) {
938 if (!(adev->offload_usecases_state & (0x1<<i))) {
939 adev->offload_usecases_state |= 0x1 << i;
940 ret = offload_usecases[i];
941 break;
942 }
943 }
944 ALOGV("%s: offload usecase is %d", __func__, ret);
945 return ret;
946}
947
948static void free_offload_usecase(struct audio_device *adev,
949 audio_usecase_t uc_id)
950{
951 unsigned int i;
952 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
953 if (offload_usecases[i] == uc_id) {
954 adev->offload_usecases_state &= ~(0x1<<i);
955 break;
956 }
957 }
958 ALOGV("%s: free offload usecase %d", __func__, uc_id);
959}
960
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700961static void *offload_thread_loop(void *context)
962{
963 struct stream_out *out = (struct stream_out *) context;
964 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -0800965 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700966
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700967 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
968 set_sched_policy(0, SP_FOREGROUND);
969 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
970
971 ALOGV("%s", __func__);
972 pthread_mutex_lock(&out->lock);
973 for (;;) {
974 struct offload_cmd *cmd = NULL;
975 stream_callback_event_t event;
976 bool send_callback = false;
977
978 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
979 __func__, list_empty(&out->offload_cmd_list),
980 out->offload_state);
981 if (list_empty(&out->offload_cmd_list)) {
982 ALOGV("%s SLEEPING", __func__);
983 pthread_cond_wait(&out->offload_cond, &out->lock);
984 ALOGV("%s RUNNING", __func__);
985 continue;
986 }
987
988 item = list_head(&out->offload_cmd_list);
989 cmd = node_to_item(item, struct offload_cmd, node);
990 list_remove(item);
991
992 ALOGVV("%s STATE %d CMD %d out->compr %p",
993 __func__, out->offload_state, cmd->cmd, out->compr);
994
995 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
996 free(cmd);
997 break;
998 }
999
1000 if (out->compr == NULL) {
1001 ALOGE("%s: Compress handle is NULL", __func__);
1002 pthread_cond_signal(&out->cond);
1003 continue;
1004 }
1005 out->offload_thread_blocked = true;
1006 pthread_mutex_unlock(&out->lock);
1007 send_callback = false;
1008 switch(cmd->cmd) {
1009 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001010 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001011 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001012 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001013 send_callback = true;
1014 event = STREAM_CBK_EVENT_WRITE_READY;
1015 break;
1016 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001017 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301018 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001019 ALOGD("copl(%p):calling compress_partial_drain", out);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001020 compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001021 ALOGD("copl(%p):out of compress_partial_drain", out);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301022 }
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001023 else if(ret == -ETIMEDOUT)
1024 compress_drain(out->compr);
1025 else
1026 ALOGE("%s: Next track returned error %d",__func__, ret);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001027 send_callback = true;
1028 event = STREAM_CBK_EVENT_DRAIN_READY;
1029 break;
1030 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001031 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001032 compress_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001033 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001034 send_callback = true;
1035 event = STREAM_CBK_EVENT_DRAIN_READY;
1036 break;
1037 default:
1038 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1039 break;
1040 }
1041 pthread_mutex_lock(&out->lock);
1042 out->offload_thread_blocked = false;
1043 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001044 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001045 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001046 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001047 free(cmd);
1048 }
1049
1050 pthread_cond_signal(&out->cond);
1051 while (!list_empty(&out->offload_cmd_list)) {
1052 item = list_head(&out->offload_cmd_list);
1053 list_remove(item);
1054 free(node_to_item(item, struct offload_cmd, node));
1055 }
1056 pthread_mutex_unlock(&out->lock);
1057
1058 return NULL;
1059}
1060
1061static int create_offload_callback_thread(struct stream_out *out)
1062{
1063 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1064 list_init(&out->offload_cmd_list);
1065 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1066 offload_thread_loop, out);
1067 return 0;
1068}
1069
1070static int destroy_offload_callback_thread(struct stream_out *out)
1071{
1072 pthread_mutex_lock(&out->lock);
1073 stop_compressed_output_l(out);
1074 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1075
1076 pthread_mutex_unlock(&out->lock);
1077 pthread_join(out->offload_thread, (void **) NULL);
1078 pthread_cond_destroy(&out->offload_cond);
1079
1080 return 0;
1081}
1082
Eric Laurent07eeafd2013-10-06 12:52:49 -07001083static bool allow_hdmi_channel_config(struct audio_device *adev)
1084{
1085 struct listnode *node;
1086 struct audio_usecase *usecase;
1087 bool ret = true;
1088
1089 list_for_each(node, &adev->usecase_list) {
1090 usecase = node_to_item(node, struct audio_usecase, list);
1091 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1092 /*
1093 * If voice call is already existing, do not proceed further to avoid
1094 * disabling/enabling both RX and TX devices, CSD calls, etc.
1095 * Once the voice call done, the HDMI channels can be configured to
1096 * max channels of remaining use cases.
1097 */
1098 if (usecase->id == USECASE_VOICE_CALL) {
1099 ALOGD("%s: voice call is active, no change in HDMI channels",
1100 __func__);
1101 ret = false;
1102 break;
1103 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1104 ALOGD("%s: multi channel playback is active, "
1105 "no change in HDMI channels", __func__);
1106 ret = false;
1107 break;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001108 } else if (is_offload_usecase(usecase->id) &&
Mingming Yin139f1072014-02-24 17:56:01 -08001109 popcount(usecase->stream.out->channel_mask) > 2) {
1110 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1111 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1112 ret = false;
1113 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001114 }
1115 }
1116 }
1117 return ret;
1118}
1119
1120static int check_and_set_hdmi_channels(struct audio_device *adev,
1121 unsigned int channels)
1122{
1123 struct listnode *node;
1124 struct audio_usecase *usecase;
1125
1126 /* Check if change in HDMI channel config is allowed */
1127 if (!allow_hdmi_channel_config(adev))
1128 return 0;
1129
1130 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001131 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001132 return 0;
1133 }
1134
1135 platform_set_hdmi_channels(adev->platform, channels);
1136 adev->cur_hdmi_channels = channels;
1137
1138 /*
1139 * Deroute all the playback streams routed to HDMI so that
1140 * the back end is deactivated. Note that backend will not
1141 * be deactivated if any one stream is connected to it.
1142 */
1143 list_for_each(node, &adev->usecase_list) {
1144 usecase = node_to_item(node, struct audio_usecase, list);
1145 if (usecase->type == PCM_PLAYBACK &&
1146 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001147 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001148 }
1149 }
1150
1151 /*
1152 * Enable all the streams disabled above. Now the HDMI backend
1153 * will be activated with new channel configuration
1154 */
1155 list_for_each(node, &adev->usecase_list) {
1156 usecase = node_to_item(node, struct audio_usecase, list);
1157 if (usecase->type == PCM_PLAYBACK &&
1158 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001159 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001160 }
1161 }
1162
1163 return 0;
1164}
1165
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001166static int stop_output_stream(struct stream_out *out)
1167{
1168 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001169 struct audio_usecase *uc_info;
1170 struct audio_device *adev = out->dev;
1171
Eric Laurent994a6932013-07-17 11:51:42 -07001172 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001173 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001174 uc_info = get_usecase_from_list(adev, out->usecase);
1175 if (uc_info == NULL) {
1176 ALOGE("%s: Could not find the usecase (%d) in the list",
1177 __func__, out->usecase);
1178 return -EINVAL;
1179 }
1180
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001181 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001182 if (adev->visualizer_stop_output != NULL)
1183 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1184 if (adev->offload_effects_stop_output != NULL)
1185 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1186 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001187
Eric Laurent150dbfe2013-02-27 14:31:02 -08001188 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001189 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001190
1191 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001192 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001193
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001194 list_remove(&uc_info->list);
1195 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001196
Eric Laurent07eeafd2013-10-06 12:52:49 -07001197 /* Must be called after removing the usecase from list */
1198 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1199 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1200
Eric Laurent994a6932013-07-17 11:51:42 -07001201 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001202 return ret;
1203}
1204
1205int start_output_stream(struct stream_out *out)
1206{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001207 int ret = 0;
Mingming Yin9c041392014-05-01 15:37:31 -07001208 int sink_channels = 0;
1209 char prop_value[PROPERTY_VALUE_MAX] = {0};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001210 struct audio_usecase *uc_info;
1211 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301212 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001213
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301214 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1215 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1216 out->devices);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301217
Naresh Tanniru80659832014-06-04 18:17:56 +05301218 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301219 ALOGE("%s: sound card is not active/SSR returning error", __func__);
1220 ret = -ENETRESET;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301221 goto error_config;
1222 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05301223
Eric Laurentb23d5282013-05-14 15:27:20 -07001224 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001225 if (out->pcm_device_id < 0) {
1226 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1227 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001228 ret = -EINVAL;
1229 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001230 }
1231
1232 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1233 uc_info->id = out->usecase;
1234 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001235 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001236 uc_info->devices = out->devices;
1237 uc_info->in_snd_device = SND_DEVICE_NONE;
1238 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001239
Eric Laurent07eeafd2013-10-06 12:52:49 -07001240 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001241 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Mingming Yin9c041392014-05-01 15:37:31 -07001242 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1243 if (!strncmp("true", prop_value, 4)) {
1244 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1245 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1246 check_and_set_hdmi_channels(adev, sink_channels);
1247 } else {
1248 if (is_offload_usecase(out->usecase))
1249 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1250 else
1251 check_and_set_hdmi_channels(adev, out->config.channels);
1252 }
Mingming Yin10fef6a2013-11-26 17:17:01 -08001253 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001254
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001255 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001256
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001257 select_devices(adev, out->usecase);
1258
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001259 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1260 __func__, 0, out->pcm_device_id);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001261 if (!is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001262 out->pcm = pcm_open(adev->snd_card,
1263 out->pcm_device_id,
1264 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001265 if (out->pcm && !pcm_is_ready(out->pcm)) {
1266 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1267 pcm_close(out->pcm);
1268 out->pcm = NULL;
1269 ret = -EIO;
1270 goto error_open;
1271 }
1272 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001274 out->compr = compress_open(adev->snd_card,
1275 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001276 COMPRESS_IN, &out->compr_config);
1277 if (out->compr && !is_compress_ready(out->compr)) {
1278 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1279 compress_close(out->compr);
1280 out->compr = NULL;
1281 ret = -EIO;
1282 goto error_open;
1283 }
1284 if (out->offload_callback)
1285 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001286
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001287#ifdef DS1_DOLBY_DDP_ENABLED
1288 if (audio_extn_is_dolby_format(out->format))
1289 audio_extn_dolby_send_ddp_endp_params(adev);
1290#endif
1291
Eric Laurentc4aef752013-09-12 17:45:53 -07001292 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001293 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1294 if (adev->offload_effects_start_output != NULL)
1295 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001296 }
Eric Laurent994a6932013-07-17 11:51:42 -07001297 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001298 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001299error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001300 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001301error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001302 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001303}
1304
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001305static int check_input_parameters(uint32_t sample_rate,
1306 audio_format_t format,
1307 int channel_count)
1308{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001309 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001310
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001311 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001312 !voice_extn_compress_voip_is_format_supported(format) &&
1313 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001314
1315 switch (channel_count) {
1316 case 1:
1317 case 2:
1318 case 6:
1319 break;
1320 default:
1321 ret = -EINVAL;
1322 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323
1324 switch (sample_rate) {
1325 case 8000:
1326 case 11025:
1327 case 12000:
1328 case 16000:
1329 case 22050:
1330 case 24000:
1331 case 32000:
1332 case 44100:
1333 case 48000:
1334 break;
1335 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001336 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337 }
1338
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001339 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001340}
1341
1342static size_t get_input_buffer_size(uint32_t sample_rate,
1343 audio_format_t format,
1344 int channel_count)
1345{
1346 size_t size = 0;
1347
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001348 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1349 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001350
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001351 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1352 /* ToDo: should use frame_size computed based on the format and
1353 channel_count here. */
1354 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001355
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001356 /* make sure the size is multiple of 64 */
1357 size += 0x3f;
1358 size &= ~0x3f;
1359
1360 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001361}
1362
1363static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1364{
1365 struct stream_out *out = (struct stream_out *)stream;
1366
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001367 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001368}
1369
1370static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1371{
1372 return -ENOSYS;
1373}
1374
1375static size_t out_get_buffer_size(const struct audio_stream *stream)
1376{
1377 struct stream_out *out = (struct stream_out *)stream;
1378
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001379 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001380 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001381 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1382 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001383
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001384 return out->config.period_size * audio_stream_frame_size(stream);
1385}
1386
1387static uint32_t out_get_channels(const struct audio_stream *stream)
1388{
1389 struct stream_out *out = (struct stream_out *)stream;
1390
1391 return out->channel_mask;
1392}
1393
1394static audio_format_t out_get_format(const struct audio_stream *stream)
1395{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001396 struct stream_out *out = (struct stream_out *)stream;
1397
1398 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001399}
1400
1401static int out_set_format(struct audio_stream *stream, audio_format_t format)
1402{
1403 return -ENOSYS;
1404}
1405
1406static int out_standby(struct audio_stream *stream)
1407{
1408 struct stream_out *out = (struct stream_out *)stream;
1409 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001410
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301411 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1412 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001413 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1414 /* Ignore standby in case of voip call because the voip output
1415 * stream is closed in adev_close_output_stream()
1416 */
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301417 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001418 return 0;
1419 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001420
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001421 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001422 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001423 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001424 out->standby = true;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001425 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001426 if (out->pcm) {
1427 pcm_close(out->pcm);
1428 out->pcm = NULL;
1429 }
1430 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001431 ALOGD("copl(%p):standby", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001432 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001433 out->gapless_mdata.encoder_delay = 0;
1434 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001435 if (out->compr != NULL) {
1436 compress_close(out->compr);
1437 out->compr = NULL;
1438 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001439 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001440 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001441 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001442 }
1443 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001444 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001445 return 0;
1446}
1447
1448static int out_dump(const struct audio_stream *stream, int fd)
1449{
1450 return 0;
1451}
1452
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001453static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1454{
1455 int ret = 0;
1456 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001457 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001458 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001459 tmp_mdata.encoder_delay = 0;
1460 tmp_mdata.encoder_padding = 0;
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001461
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001462 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001463 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001464 return -EINVAL;
1465 }
1466
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001467 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1468 if (ret >= 0) {
1469 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1470 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1471 ALOGV("ADTS format is set in offload mode");
1472 }
1473 out->send_new_metadata = 1;
1474 }
1475
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001476 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1477 if(ret >= 0)
1478 is_meta_data_params = true;
1479 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1480 if(ret >= 0 )
1481 is_meta_data_params = true;
1482 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1483 if(ret >= 0 )
1484 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001485 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1486 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001487 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001488 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001489 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001490 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1491 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001492 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001493 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001494 }
1495
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001496 if(!is_meta_data_params) {
1497 ALOGV("%s: Not gapless meta data params", __func__);
1498 return 0;
1499 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001500 out->gapless_mdata = tmp_mdata;
1501 out->send_new_metadata = 1;
1502 ALOGV("%s new encoder delay %u and padding %u", __func__,
1503 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1504
1505 return 0;
1506}
1507
1508
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001509static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1510{
1511 struct stream_out *out = (struct stream_out *)stream;
1512 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001513 struct audio_usecase *usecase;
1514 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001515 struct str_parms *parms;
1516 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001517 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001518 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001519
sangwoobc677242013-08-08 16:53:43 +09001520 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001521 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001522 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001523 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1524 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001525 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001526 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001527 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001528
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001529 /*
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301530 * When HDMI cable is unplugged/usb hs is disconnected the
1531 * music playback is paused and the policy manager sends routing=0
1532 * But the audioflingercontinues to write data until standby time
1533 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001534 * Avoid this by routing audio to speaker until standby.
1535 */
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301536 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1537 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001538 val == AUDIO_DEVICE_NONE) {
1539 val = AUDIO_DEVICE_OUT_SPEAKER;
1540 }
1541
1542 /*
1543 * select_devices() call below switches all the usecases on the same
1544 * backend to the new device. Refer to check_usecases_codec_backend() in
1545 * the select_devices(). But how do we undo this?
1546 *
1547 * For example, music playback is active on headset (deep-buffer usecase)
1548 * and if we go to ringtones and select a ringtone, low-latency usecase
1549 * will be started on headset+speaker. As we can't enable headset+speaker
1550 * and headset devices at the same time, select_devices() switches the music
1551 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1552 * So when the ringtone playback is completed, how do we undo the same?
1553 *
1554 * We are relying on the out_set_parameters() call on deep-buffer output,
1555 * once the ringtone playback is ended.
1556 * NOTE: We should not check if the current devices are same as new devices.
1557 * Because select_devices() must be called to switch back the music
1558 * playback to headset.
1559 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001560 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001561 out->devices = val;
1562
1563 if (!out->standby)
1564 select_devices(adev, out->usecase);
1565
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001566 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1567 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001568 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001569 ret = voice_start_call(adev);
Karthik Reddy Katta060a6c42014-05-20 15:21:28 +05301570 } else if (voice_is_in_call(adev) &&
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001571 (out == adev->primary_output)) {
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -08001572 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001573 }
1574 }
1575
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001576 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1577 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001578 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001579 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001580 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001581
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001582 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001583 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001584 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001585
1586 if (out == adev->primary_output) {
1587 pthread_mutex_lock(&adev->lock);
1588 audio_extn_set_parameters(adev, parms);
1589 pthread_mutex_unlock(&adev->lock);
1590 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001591 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001592 pthread_mutex_lock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001593 parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001594 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001595 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001596
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001597 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001598 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001599 return ret;
1600}
1601
1602static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1603{
1604 struct stream_out *out = (struct stream_out *)stream;
1605 struct str_parms *query = str_parms_create_str(keys);
1606 char *str;
1607 char value[256];
1608 struct str_parms *reply = str_parms_create();
1609 size_t i, j;
1610 int ret;
1611 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001612 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001613 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1614 if (ret >= 0) {
1615 value[0] = '\0';
1616 i = 0;
1617 while (out->supported_channel_masks[i] != 0) {
1618 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1619 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1620 if (!first) {
1621 strcat(value, "|");
1622 }
1623 strcat(value, out_channels_name_to_enum_table[j].name);
1624 first = false;
1625 break;
1626 }
1627 }
1628 i++;
1629 }
1630 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1631 str = str_parms_to_str(reply);
1632 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001633 voice_extn_out_get_parameters(out, query, reply);
1634 str = str_parms_to_str(reply);
1635 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella29b8fc72014-01-29 12:52:19 -08001636 free(str);
1637 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001638 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001639 }
1640 str_parms_destroy(query);
1641 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001642 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001643 return str;
1644}
1645
1646static uint32_t out_get_latency(const struct audio_stream_out *stream)
1647{
1648 struct stream_out *out = (struct stream_out *)stream;
1649
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001650 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001651 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1652
1653 return (out->config.period_count * out->config.period_size * 1000) /
1654 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001655}
1656
1657static int out_set_volume(struct audio_stream_out *stream, float left,
1658 float right)
1659{
Eric Laurenta9024de2013-04-04 09:19:12 -07001660 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001661 int volume[2];
1662
Eric Laurenta9024de2013-04-04 09:19:12 -07001663 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1664 /* only take left channel into account: the API is for stereo anyway */
1665 out->muted = (left == 0.0f);
1666 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001667 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001668 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001669 struct audio_device *adev = out->dev;
1670 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001671 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1672 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001673
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001674 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1675 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001676 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1677 if (!ctl) {
1678 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1679 __func__, mixer_ctl_name);
1680 return -EINVAL;
1681 }
1682 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1683 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1684 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1685 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001686 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001687
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001688 return -ENOSYS;
1689}
1690
1691static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1692 size_t bytes)
1693{
1694 struct stream_out *out = (struct stream_out *)stream;
1695 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301696 int snd_scard_state = get_snd_card_state(adev);
Eric Laurent6e895242013-09-05 16:10:57 -07001697 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001698
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001699 pthread_mutex_lock(&out->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301700
Naresh Tanniru80659832014-06-04 18:17:56 +05301701 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
1702 if (out->pcm) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301703 ALOGD(" %s: sound card is not active/SSR state", __func__);
1704 ret= -ENETRESET;
1705 goto exit;
Naresh Tanniru80659832014-06-04 18:17:56 +05301706 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1707 //during SSR for compress usecase we should return error to flinger
1708 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
1709 pthread_mutex_unlock(&out->lock);
1710 return -ENETRESET;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301711 }
1712 }
1713
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001714 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001715 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001716 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001717 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1718 ret = voice_extn_compress_voip_start_output_stream(out);
1719 else
1720 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001721 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001722 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001723 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001724 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001725 goto exit;
1726 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001727 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001728
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001729 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001730 ALOGD("copl(%p): writing buffer (%d bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001731 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001732 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001733 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1734 out->send_new_metadata = 0;
1735 }
1736
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001737 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001738 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001739 if (ret >= 0 && ret < (ssize_t)bytes) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301740 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001741 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05301742 } else if (-ENETRESET == ret) {
1743 ALOGE("copl %s: received sound card offline state on compress write", __func__);
1744 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1745 pthread_mutex_unlock(&out->lock);
1746 out_standby(&out->stream.common);
1747 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001748 }
Naresh Tanniru80659832014-06-04 18:17:56 +05301749 if (!out->playback_started && ret >= 0) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001750 compress_start(out->compr);
1751 out->playback_started = 1;
1752 out->offload_state = OFFLOAD_STATE_PLAYING;
1753 }
1754 pthread_mutex_unlock(&out->lock);
1755 return ret;
1756 } else {
1757 if (out->pcm) {
1758 if (out->muted)
1759 memset((void *)buffer, 0, bytes);
1760 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1761 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001762 if (ret == 0)
1763 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001764 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001765 }
1766
1767exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05301768 /* ToDo: There may be a corner case when SSR happens back to back during
1769 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05301770 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301771 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301772 }
1773
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001774 pthread_mutex_unlock(&out->lock);
1775
1776 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001777 if (out->pcm)
1778 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001779 out_standby(&out->stream.common);
1780 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05301781 out_get_sample_rate(&out->stream.common));
1782
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001783 }
1784 return bytes;
1785}
1786
1787static int out_get_render_position(const struct audio_stream_out *stream,
1788 uint32_t *dsp_frames)
1789{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001790 struct stream_out *out = (struct stream_out *)stream;
1791 *dsp_frames = 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001792 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301793 ssize_t ret = -EINVAL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001794 pthread_mutex_lock(&out->lock);
1795 if (out->compr != NULL) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301796 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001797 &out->sample_rate);
1798 ALOGVV("%s rendered frames %d sample_rate %d",
1799 __func__, *dsp_frames, out->sample_rate);
1800 }
1801 pthread_mutex_unlock(&out->lock);
Naresh Tanniru80659832014-06-04 18:17:56 +05301802 if (-ENETRESET == ret) {
1803 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
1804 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1805 return -EINVAL;
1806 } else if(ret < 0) {
1807 ALOGE(" ERROR: Unable to get time stamp from compress driver");
1808 return -EINVAL;
1809 } else {
1810 return 0;
1811 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001812 } else
1813 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001814}
1815
1816static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1817{
1818 return 0;
1819}
1820
1821static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1822{
1823 return 0;
1824}
1825
1826static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1827 int64_t *timestamp)
1828{
1829 return -EINVAL;
1830}
1831
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001832static int out_get_presentation_position(const struct audio_stream_out *stream,
1833 uint64_t *frames, struct timespec *timestamp)
1834{
1835 struct stream_out *out = (struct stream_out *)stream;
1836 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001837 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001838
1839 pthread_mutex_lock(&out->lock);
1840
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001841 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07001842 if (out->compr != NULL) {
1843 compress_get_tstamp(out->compr, &dsp_frames,
1844 &out->sample_rate);
1845 ALOGVV("%s rendered frames %ld sample_rate %d",
1846 __func__, dsp_frames, out->sample_rate);
1847 *frames = dsp_frames;
1848 ret = 0;
1849 /* this is the best we can do */
1850 clock_gettime(CLOCK_MONOTONIC, timestamp);
1851 }
1852 } else {
1853 if (out->pcm) {
1854 size_t avail;
1855 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1856 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001857 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001858 // This adjustment accounts for buffering after app processor.
1859 // It is based on estimated DSP latency per use case, rather than exact.
1860 signed_frames -=
1861 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1862
Eric Laurent949a0892013-09-20 09:20:13 -07001863 // It would be unusual for this value to be negative, but check just in case ...
1864 if (signed_frames >= 0) {
1865 *frames = signed_frames;
1866 ret = 0;
1867 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001868 }
1869 }
1870 }
1871
1872 pthread_mutex_unlock(&out->lock);
1873
1874 return ret;
1875}
1876
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001877static int out_set_callback(struct audio_stream_out *stream,
1878 stream_callback_t callback, void *cookie)
1879{
1880 struct stream_out *out = (struct stream_out *)stream;
1881
1882 ALOGV("%s", __func__);
1883 pthread_mutex_lock(&out->lock);
1884 out->offload_callback = callback;
1885 out->offload_cookie = cookie;
1886 pthread_mutex_unlock(&out->lock);
1887 return 0;
1888}
1889
1890static int out_pause(struct audio_stream_out* stream)
1891{
1892 struct stream_out *out = (struct stream_out *)stream;
1893 int status = -ENOSYS;
1894 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001895 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001896 ALOGD("copl(%p):pause compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001897 pthread_mutex_lock(&out->lock);
1898 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301899 struct audio_device *adev = out->dev;
1900 int snd_scard_state = get_snd_card_state(adev);
1901
1902 if (SND_CARD_STATE_ONLINE == snd_scard_state)
1903 status = compress_pause(out->compr);
1904
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001905 out->offload_state = OFFLOAD_STATE_PAUSED;
1906 }
1907 pthread_mutex_unlock(&out->lock);
1908 }
1909 return status;
1910}
1911
1912static int out_resume(struct audio_stream_out* stream)
1913{
1914 struct stream_out *out = (struct stream_out *)stream;
1915 int status = -ENOSYS;
1916 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001917 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001918 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001919 status = 0;
1920 pthread_mutex_lock(&out->lock);
1921 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301922 struct audio_device *adev = out->dev;
1923 int snd_scard_state = get_snd_card_state(adev);
1924
1925 if (SND_CARD_STATE_ONLINE == snd_scard_state)
1926 status = compress_resume(out->compr);
1927
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001928 out->offload_state = OFFLOAD_STATE_PLAYING;
1929 }
1930 pthread_mutex_unlock(&out->lock);
1931 }
1932 return status;
1933}
1934
1935static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1936{
1937 struct stream_out *out = (struct stream_out *)stream;
1938 int status = -ENOSYS;
1939 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001940 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001941 pthread_mutex_lock(&out->lock);
1942 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1943 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1944 else
1945 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1946 pthread_mutex_unlock(&out->lock);
1947 }
1948 return status;
1949}
1950
1951static int out_flush(struct audio_stream_out* stream)
1952{
1953 struct stream_out *out = (struct stream_out *)stream;
1954 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001955 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001956 ALOGD("copl(%p):calling compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001957 pthread_mutex_lock(&out->lock);
1958 stop_compressed_output_l(out);
1959 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001960 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001961 return 0;
1962 }
1963 return -ENOSYS;
1964}
1965
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001966/** audio_stream_in implementation **/
1967static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1968{
1969 struct stream_in *in = (struct stream_in *)stream;
1970
1971 return in->config.rate;
1972}
1973
1974static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1975{
1976 return -ENOSYS;
1977}
1978
1979static size_t in_get_buffer_size(const struct audio_stream *stream)
1980{
1981 struct stream_in *in = (struct stream_in *)stream;
1982
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001983 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1984 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001985 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1986 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001987
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001988 return in->config.period_size * audio_stream_frame_size(stream);
1989}
1990
1991static uint32_t in_get_channels(const struct audio_stream *stream)
1992{
1993 struct stream_in *in = (struct stream_in *)stream;
1994
1995 return in->channel_mask;
1996}
1997
1998static audio_format_t in_get_format(const struct audio_stream *stream)
1999{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002000 struct stream_in *in = (struct stream_in *)stream;
2001
2002 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002003}
2004
2005static int in_set_format(struct audio_stream *stream, audio_format_t format)
2006{
2007 return -ENOSYS;
2008}
2009
2010static int in_standby(struct audio_stream *stream)
2011{
2012 struct stream_in *in = (struct stream_in *)stream;
2013 struct audio_device *adev = in->dev;
2014 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302015 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2016 stream, in->usecase, use_case_table[in->usecase]);
2017
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002018
2019 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2020 /* Ignore standby in case of voip call because the voip input
2021 * stream is closed in adev_close_input_stream()
2022 */
2023 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2024 return status;
2025 }
2026
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002027 pthread_mutex_lock(&in->lock);
2028 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002029 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002030 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002031 if (in->pcm) {
2032 pcm_close(in->pcm);
2033 in->pcm = NULL;
2034 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002035 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002036 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002037 }
2038 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002039 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002040 return status;
2041}
2042
2043static int in_dump(const struct audio_stream *stream, int fd)
2044{
2045 return 0;
2046}
2047
2048static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2049{
2050 struct stream_in *in = (struct stream_in *)stream;
2051 struct audio_device *adev = in->dev;
2052 struct str_parms *parms;
2053 char *str;
2054 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002055 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002056
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302057 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002058 parms = str_parms_create_str(kvpairs);
2059
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002060 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002061 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002062
2063 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2064 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002065 val = atoi(value);
2066 /* no audio source uses val == 0 */
2067 if ((in->source != val) && (val != 0)) {
2068 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002069 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2070 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2071 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2072 (in->config.rate == 8000 || in->config.rate == 16000) &&
2073 (popcount(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002074 err = voice_extn_compress_voip_open_input_stream(in);
2075 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002076 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002077 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002078 }
2079 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002080 }
2081 }
2082
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002083 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2084 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002085 val = atoi(value);
2086 if ((in->device != val) && (val != 0)) {
2087 in->device = val;
2088 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002089 if (!in->standby)
2090 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002091 }
2092 }
2093
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002094done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002095 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002096 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002097
2098 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002099 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002100 return ret;
2101}
2102
2103static char* in_get_parameters(const struct audio_stream *stream,
2104 const char *keys)
2105{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002106 struct stream_in *in = (struct stream_in *)stream;
2107 struct str_parms *query = str_parms_create_str(keys);
2108 char *str;
2109 char value[256];
2110 struct str_parms *reply = str_parms_create();
2111 ALOGV("%s: enter: keys - %s", __func__, keys);
2112
2113 voice_extn_in_get_parameters(in, query, reply);
2114
2115 str = str_parms_to_str(reply);
2116 str_parms_destroy(query);
2117 str_parms_destroy(reply);
2118
2119 ALOGV("%s: exit: returns - %s", __func__, str);
2120 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002121}
2122
2123static int in_set_gain(struct audio_stream_in *stream, float gain)
2124{
2125 return 0;
2126}
2127
2128static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2129 size_t bytes)
2130{
2131 struct stream_in *in = (struct stream_in *)stream;
2132 struct audio_device *adev = in->dev;
2133 int i, ret = -1;
Naresh Tanniru80659832014-06-04 18:17:56 +05302134 int snd_scard_state = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002135
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002136 pthread_mutex_lock(&in->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302137
2138 if (in->pcm) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302139 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05302140 ALOGD(" %s: sound card is not active/SSR state", __func__);
2141 ret= -ENETRESET;
2142 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302143 }
2144 }
2145
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002146 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002147 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002148 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2149 ret = voice_extn_compress_voip_start_input_stream(in);
2150 else
2151 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002152 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002153 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002154 goto exit;
2155 }
2156 in->standby = 0;
2157 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002158
2159 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002160 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
2161 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002162 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2163 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002164 else
2165 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002166 }
2167
2168 /*
2169 * Instead of writing zeroes here, we could trust the hardware
2170 * to always provide zeroes when muted.
2171 */
kunleizc5a639b2014-04-24 18:46:22 +08002172 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002173 memset(buffer, 0, bytes);
2174
2175exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05302176 /* ToDo: There may be a corner case when SSR happens back to back during
2177 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05302178 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302179 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302180 memset(buffer, 0, bytes);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302181 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002182 pthread_mutex_unlock(&in->lock);
2183
2184 if (ret != 0) {
2185 in_standby(&in->stream.common);
2186 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
2187 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05302188 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002189 }
2190 return bytes;
2191}
2192
2193static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
2194{
2195 return 0;
2196}
2197
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002198static int add_remove_audio_effect(const struct audio_stream *stream,
2199 effect_handle_t effect,
2200 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002201{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002202 struct stream_in *in = (struct stream_in *)stream;
2203 int status = 0;
2204 effect_descriptor_t desc;
2205
2206 status = (*effect)->get_descriptor(effect, &desc);
2207 if (status != 0)
2208 return status;
2209
2210 pthread_mutex_lock(&in->lock);
2211 pthread_mutex_lock(&in->dev->lock);
2212 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2213 in->enable_aec != enable &&
2214 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2215 in->enable_aec = enable;
2216 if (!in->standby)
2217 select_devices(in->dev, in->usecase);
2218 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002219 if (in->enable_ns != enable &&
2220 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2221 in->enable_ns = enable;
2222 if (!in->standby)
2223 select_devices(in->dev, in->usecase);
2224 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002225 pthread_mutex_unlock(&in->dev->lock);
2226 pthread_mutex_unlock(&in->lock);
2227
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002228 return 0;
2229}
2230
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002231static int in_add_audio_effect(const struct audio_stream *stream,
2232 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002233{
Eric Laurent994a6932013-07-17 11:51:42 -07002234 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002235 return add_remove_audio_effect(stream, effect, true);
2236}
2237
2238static int in_remove_audio_effect(const struct audio_stream *stream,
2239 effect_handle_t effect)
2240{
Eric Laurent994a6932013-07-17 11:51:42 -07002241 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002242 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002243}
2244
2245static int adev_open_output_stream(struct audio_hw_device *dev,
2246 audio_io_handle_t handle,
2247 audio_devices_t devices,
2248 audio_output_flags_t flags,
2249 struct audio_config *config,
2250 struct audio_stream_out **stream_out)
2251{
2252 struct audio_device *adev = (struct audio_device *)dev;
2253 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002254 int i, ret = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002255 audio_format_t format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002256
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002257 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05302258
2259 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2260 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2261 ALOGE(" sound card is not active rejecting compress output open request");
2262 return -EINVAL;
2263 }
2264
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002265 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2266
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302267 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2268 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2269 devices, flags, &out->stream);
2270
2271
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002272 if (!out) {
2273 return -ENOMEM;
2274 }
2275
2276 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2277 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2278
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002279 if (devices == AUDIO_DEVICE_NONE)
2280 devices = AUDIO_DEVICE_OUT_SPEAKER;
2281
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002282 out->flags = flags;
2283 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002284 out->dev = adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002285 format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002286 out->sample_rate = config->sample_rate;
2287 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2288 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002289 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002290
2291 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002292 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2293 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2294 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2295
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002296 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002297 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2298 ret = read_hdmi_channel_masks(out);
2299
2300 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2301 ret = audio_extn_read_afe_proxy_channel_masks(out);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002302 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002303 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002304 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002305
2306 if (config->sample_rate == 0)
2307 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2308 if (config->channel_mask == 0)
2309 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2310
2311 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002312 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002313 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2314 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002315 out->config.rate = config->sample_rate;
2316 out->config.channels = popcount(out->channel_mask);
2317 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002318 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2319 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002320 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002321 ret = voice_extn_compress_voip_open_output_stream(out);
2322 if (ret != 0) {
2323 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2324 __func__, ret);
2325 goto error_open;
2326 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002327 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2328 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2329 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2330 ALOGE("%s: Unsupported Offload information", __func__);
2331 ret = -EINVAL;
2332 goto error_open;
2333 }
Mingming Yin90310102013-11-13 16:57:00 -08002334 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002335 !audio_extn_is_dolby_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002336 ALOGE("%s: Unsupported audio format", __func__);
2337 ret = -EINVAL;
2338 goto error_open;
2339 }
2340
2341 out->compr_config.codec = (struct snd_codec *)
2342 calloc(1, sizeof(struct snd_codec));
2343
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002344 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002345 if (config->offload_info.channel_mask)
2346 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002347 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002348 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002349 config->offload_info.channel_mask = config->channel_mask;
2350 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002351 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002352 out->sample_rate = config->offload_info.sample_rate;
2353
2354 out->stream.set_callback = out_set_callback;
2355 out->stream.pause = out_pause;
2356 out->stream.resume = out_resume;
2357 out->stream.drain = out_drain;
2358 out->stream.flush = out_flush;
2359
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002360 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002361 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002362 audio_extn_dolby_get_snd_codec_id(adev, out,
2363 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002364 else
2365 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002366 get_snd_codec_id(config->offload_info.format);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002367 if (audio_is_offload_pcm(config->offload_info.format)) {
2368 out->compr_config.fragment_size =
2369 platform_get_pcm_offload_buffer_size(&config->offload_info);
2370 } else {
2371 out->compr_config.fragment_size =
2372 platform_get_compress_offload_buffer_size(&config->offload_info);
2373 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002374 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2375 out->compr_config.codec->sample_rate =
2376 compress_get_alsa_rate(config->offload_info.sample_rate);
2377 out->compr_config.codec->bit_rate =
2378 config->offload_info.bit_rate;
2379 out->compr_config.codec->ch_in =
2380 popcount(config->channel_mask);
2381 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
ApurupaPattapu2e084df2013-12-18 15:47:59 -08002382 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002383
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002384 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2385 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
2386 else if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
2387 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2388
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002389 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2390 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002391
2392 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002393 out->offload_state = OFFLOAD_STATE_IDLE;
2394 out->playback_started = 0;
2395
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002396 create_offload_callback_thread(out);
2397 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2398 __func__, config->offload_info.version,
2399 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002400 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -08002401 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002402
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002403 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2404 ret = voice_check_and_set_incall_music_usecase(adev, out);
2405 if (ret != 0) {
2406 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2407 __func__, ret);
2408 goto error_open;
2409 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002410 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002411 format = AUDIO_FORMAT_PCM_16_BIT;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002412 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2413 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002414 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002415 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002416 /* primary path is the default path selected if no other outputs are available/suitable */
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002417 format = AUDIO_FORMAT_PCM_16_BIT;
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002418 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002419 out->config = pcm_config_deep_buffer;
2420 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002421 }
2422
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002423 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
2424 &adev->streams_output_cfg_list,
2425 flags, format, &out->app_type_cfg);
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002426 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2427 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2428 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002429 if(adev->primary_output == NULL)
2430 adev->primary_output = out;
2431 else {
2432 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002433 ret = -EEXIST;
2434 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002435 }
2436 }
2437
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002438 /* Check if this usecase is already existing */
2439 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07002440 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2441 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002442 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002443 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002444 ret = -EEXIST;
2445 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002446 }
2447 pthread_mutex_unlock(&adev->lock);
2448
2449 out->stream.common.get_sample_rate = out_get_sample_rate;
2450 out->stream.common.set_sample_rate = out_set_sample_rate;
2451 out->stream.common.get_buffer_size = out_get_buffer_size;
2452 out->stream.common.get_channels = out_get_channels;
2453 out->stream.common.get_format = out_get_format;
2454 out->stream.common.set_format = out_set_format;
2455 out->stream.common.standby = out_standby;
2456 out->stream.common.dump = out_dump;
2457 out->stream.common.set_parameters = out_set_parameters;
2458 out->stream.common.get_parameters = out_get_parameters;
2459 out->stream.common.add_audio_effect = out_add_audio_effect;
2460 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2461 out->stream.get_latency = out_get_latency;
2462 out->stream.set_volume = out_set_volume;
2463 out->stream.write = out_write;
2464 out->stream.get_render_position = out_get_render_position;
2465 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002466 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002467
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002468 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002469 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002470 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002471
2472 config->format = out->stream.common.get_format(&out->stream.common);
2473 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2474 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2475
2476 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302477 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2478 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002479 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002480 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002481
2482error_open:
2483 free(out);
2484 *stream_out = NULL;
2485 ALOGD("%s: exit: ret %d", __func__, ret);
2486 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002487}
2488
2489static void adev_close_output_stream(struct audio_hw_device *dev,
2490 struct audio_stream_out *stream)
2491{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002492 struct stream_out *out = (struct stream_out *)stream;
2493 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002494 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002495
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302496 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2497
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002498 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2499 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2500 if(ret != 0)
2501 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2502 __func__, ret);
2503 }
2504 else
2505 out_standby(&stream->common);
2506
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002507 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002508 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002509 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002510 if (out->compr_config.codec != NULL)
2511 free(out->compr_config.codec);
2512 }
2513 pthread_cond_destroy(&out->cond);
2514 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002515 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002516 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002517}
2518
2519static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2520{
2521 struct audio_device *adev = (struct audio_device *)dev;
2522 struct str_parms *parms;
2523 char *str;
2524 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002525 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002526 int ret = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002527
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002528 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002529 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002530
Naresh Tanniru4c630392014-05-12 01:05:52 +05302531 err = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
2532 if (err >= 0) {
2533 char *snd_card_status = value+2;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302534 if (strstr(snd_card_status, "OFFLINE")) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302535 struct listnode *node;
2536 struct audio_usecase *usecase;
2537
Naresh Tanniru4c630392014-05-12 01:05:52 +05302538 ALOGD("Received sound card OFFLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302539 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2540
2541 pthread_mutex_lock(&adev->lock);
2542 //close compress session on OFFLINE status
2543 usecase = get_usecase_from_list(adev,USECASE_AUDIO_PLAYBACK_OFFLOAD);
2544 if (usecase && usecase->stream.out) {
2545 ALOGD(" %s closing compress session on OFFLINE state", __func__);
2546
2547 struct stream_out *out = usecase->stream.out;
2548
2549 pthread_mutex_unlock(&adev->lock);
2550 out_standby(&out->stream.common);
2551 } else
2552 pthread_mutex_unlock(&adev->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302553 } else if (strstr(snd_card_status, "ONLINE")) {
2554 ALOGD("Received sound card ONLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302555 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302556 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05302557 }
2558
2559 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002560 ret = voice_set_parameters(adev, parms);
2561 if (ret != 0)
2562 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002563
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002564 ret = platform_set_parameters(adev->platform, parms);
2565 if (ret != 0)
2566 goto done;
2567
2568 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2569 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002570 /* When set to false, HAL should disable EC and NS
2571 * But it is currently not supported.
2572 */
2573 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2574 adev->bluetooth_nrec = true;
2575 else
2576 adev->bluetooth_nrec = false;
2577 }
2578
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002579 err = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2580 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002581 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2582 adev->screen_off = false;
2583 else
2584 adev->screen_off = true;
2585 }
2586
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002587 err = str_parms_get_int(parms, "rotation", &val);
2588 if (err >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002589 bool reverse_speakers = false;
2590 switch(val) {
2591 // FIXME: note that the code below assumes that the speakers are in the correct placement
2592 // relative to the user when the device is rotated 90deg from its default rotation. This
2593 // assumption is device-specific, not platform-specific like this code.
2594 case 270:
2595 reverse_speakers = true;
2596 break;
2597 case 0:
2598 case 90:
2599 case 180:
2600 break;
2601 default:
2602 ALOGE("%s: unexpected rotation of %d", __func__, val);
2603 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002604 if (adev->speaker_lr_swap != reverse_speakers) {
2605 adev->speaker_lr_swap = reverse_speakers;
2606 // only update the selected device if there is active pcm playback
2607 struct audio_usecase *usecase;
2608 struct listnode *node;
2609 list_for_each(node, &adev->usecase_list) {
2610 usecase = node_to_item(node, struct audio_usecase, list);
2611 if (usecase->type == PCM_PLAYBACK) {
2612 select_devices(adev, usecase->id);
2613 break;
2614 }
2615 }
2616 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002617 }
2618
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002619 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002620
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002621done:
2622 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002623 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002624 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002625 return ret;
2626}
2627
2628static char* adev_get_parameters(const struct audio_hw_device *dev,
2629 const char *keys)
2630{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002631 struct audio_device *adev = (struct audio_device *)dev;
2632 struct str_parms *reply = str_parms_create();
2633 struct str_parms *query = str_parms_create_str(keys);
2634 char *str;
2635
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002636 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002637 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002638 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002639 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05302640 pthread_mutex_unlock(&adev->lock);
2641
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002642 str = str_parms_to_str(reply);
2643 str_parms_destroy(query);
2644 str_parms_destroy(reply);
2645
2646 ALOGV("%s: exit: returns - %s", __func__, str);
2647 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002648}
2649
2650static int adev_init_check(const struct audio_hw_device *dev)
2651{
2652 return 0;
2653}
2654
2655static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2656{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002657 int ret;
2658 struct audio_device *adev = (struct audio_device *)dev;
2659 pthread_mutex_lock(&adev->lock);
2660 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002661 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002662 pthread_mutex_unlock(&adev->lock);
2663 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002664}
2665
2666static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2667{
2668 return -ENOSYS;
2669}
2670
2671static int adev_get_master_volume(struct audio_hw_device *dev,
2672 float *volume)
2673{
2674 return -ENOSYS;
2675}
2676
2677static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2678{
2679 return -ENOSYS;
2680}
2681
2682static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2683{
2684 return -ENOSYS;
2685}
2686
2687static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2688{
2689 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002690 pthread_mutex_lock(&adev->lock);
2691 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002692 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002693 adev->mode = mode;
2694 }
2695 pthread_mutex_unlock(&adev->lock);
2696 return 0;
2697}
2698
2699static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2700{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002701 int ret;
2702
2703 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002704 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002705 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2706 pthread_mutex_unlock(&adev->lock);
2707
2708 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002709}
2710
2711static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2712{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002713 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002714 return 0;
2715}
2716
2717static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2718 const struct audio_config *config)
2719{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002720 int channel_count = popcount(config->channel_mask);
2721
2722 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2723}
2724
2725static int adev_open_input_stream(struct audio_hw_device *dev,
2726 audio_io_handle_t handle,
2727 audio_devices_t devices,
2728 struct audio_config *config,
2729 struct audio_stream_in **stream_in)
2730{
2731 struct audio_device *adev = (struct audio_device *)dev;
2732 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002733 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002734 int channel_count = popcount(config->channel_mask);
2735
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302736
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002737 *stream_in = NULL;
2738 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2739 return -EINVAL;
2740
2741 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302742 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
2743 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2744 devices, &in->stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002745
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07002746 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2747
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002748 in->stream.common.get_sample_rate = in_get_sample_rate;
2749 in->stream.common.set_sample_rate = in_set_sample_rate;
2750 in->stream.common.get_buffer_size = in_get_buffer_size;
2751 in->stream.common.get_channels = in_get_channels;
2752 in->stream.common.get_format = in_get_format;
2753 in->stream.common.set_format = in_set_format;
2754 in->stream.common.standby = in_standby;
2755 in->stream.common.dump = in_dump;
2756 in->stream.common.set_parameters = in_set_parameters;
2757 in->stream.common.get_parameters = in_get_parameters;
2758 in->stream.common.add_audio_effect = in_add_audio_effect;
2759 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2760 in->stream.set_gain = in_set_gain;
2761 in->stream.read = in_read;
2762 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2763
2764 in->device = devices;
2765 in->source = AUDIO_SOURCE_DEFAULT;
2766 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002767 in->standby = 1;
2768 in->channel_mask = config->channel_mask;
2769
2770 /* Update config params with the requested sample rate and channels */
2771 in->usecase = USECASE_AUDIO_RECORD;
2772 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002773 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002774 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002775
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002776 if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002777 if(audio_extn_ssr_get_enabled()) {
2778 if(audio_extn_ssr_init(adev, in)) {
2779 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2780 ret = -EINVAL;
2781 goto err_open;
2782 }
2783 } else {
2784 ret = -EINVAL;
2785 goto err_open;
2786 }
Mingming Yine62d7842013-10-25 16:26:03 -07002787 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002788 audio_extn_compr_cap_format_supported(config->format) &&
2789 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Mingming Yine62d7842013-10-25 16:26:03 -07002790 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002791 } else {
2792 in->config.channels = channel_count;
2793 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2794 buffer_size = get_input_buffer_size(config->sample_rate,
2795 config->format,
2796 channel_count);
2797 in->config.period_size = buffer_size / frame_size;
2798 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002799
2800 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002801 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002802 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002803
2804err_open:
2805 free(in);
2806 *stream_in = NULL;
2807 return ret;
2808}
2809
2810static void adev_close_input_stream(struct audio_hw_device *dev,
2811 struct audio_stream_in *stream)
2812{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002813 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002814 struct stream_in *in = (struct stream_in *)stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302815 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002816
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002817 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2818 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2819 if (ret != 0)
2820 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2821 __func__, ret);
2822 } else
2823 in_standby(&stream->common);
2824
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002825 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2826 audio_extn_ssr_deinit();
2827 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002828 free(stream);
2829
Mingming Yine62d7842013-10-25 16:26:03 -07002830 if(audio_extn_compr_cap_enabled() &&
2831 audio_extn_compr_cap_format_supported(in->config.format))
2832 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002833 return;
2834}
2835
2836static int adev_dump(const audio_hw_device_t *device, int fd)
2837{
2838 return 0;
2839}
2840
2841static int adev_close(hw_device_t *device)
2842{
2843 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002844
2845 if (!adev)
2846 return 0;
2847
2848 pthread_mutex_lock(&adev_init_lock);
2849
2850 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002851 audio_extn_listen_deinit(adev);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002852 audio_extn_utils_release_streams_output_cfg_list(&adev->streams_output_cfg_list);
Kiran Kandi910e1862013-10-29 13:29:42 -07002853 audio_route_free(adev->audio_route);
2854 free(adev->snd_dev_ref_cnt);
2855 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002856 free(device);
2857 adev = NULL;
2858 }
2859 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002860 return 0;
2861}
2862
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002863static int adev_open(const hw_module_t *module, const char *name,
2864 hw_device_t **device)
2865{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002866 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002867
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002868 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002869 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2870
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002871 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002872 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002873 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002874 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002875 ALOGD("%s: returning existing instance of adev", __func__);
2876 ALOGD("%s: exit", __func__);
2877 pthread_mutex_unlock(&adev_init_lock);
2878 return 0;
2879 }
2880
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002881 adev = calloc(1, sizeof(struct audio_device));
2882
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07002883 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2884
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002885 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2886 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2887 adev->device.common.module = (struct hw_module_t *)module;
2888 adev->device.common.close = adev_close;
2889
2890 adev->device.init_check = adev_init_check;
2891 adev->device.set_voice_volume = adev_set_voice_volume;
2892 adev->device.set_master_volume = adev_set_master_volume;
2893 adev->device.get_master_volume = adev_get_master_volume;
2894 adev->device.set_master_mute = adev_set_master_mute;
2895 adev->device.get_master_mute = adev_get_master_mute;
2896 adev->device.set_mode = adev_set_mode;
2897 adev->device.set_mic_mute = adev_set_mic_mute;
2898 adev->device.get_mic_mute = adev_get_mic_mute;
2899 adev->device.set_parameters = adev_set_parameters;
2900 adev->device.get_parameters = adev_get_parameters;
2901 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2902 adev->device.open_output_stream = adev_open_output_stream;
2903 adev->device.close_output_stream = adev_close_output_stream;
2904 adev->device.open_input_stream = adev_open_input_stream;
2905 adev->device.close_input_stream = adev_close_input_stream;
2906 adev->device.dump = adev_dump;
2907
2908 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002909 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002910 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002911 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002912 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002913 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002914 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002915 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002916 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002917 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002918 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08002919 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002920 adev->offload_usecases_state = 0;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302921
2922 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
2923 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002924 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002925 adev->platform = platform_init(adev);
2926 if (!adev->platform) {
2927 free(adev->snd_dev_ref_cnt);
2928 free(adev);
2929 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2930 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08002931 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07002932 return -EINVAL;
2933 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002934
Naresh Tanniru4c630392014-05-12 01:05:52 +05302935 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
2936
Eric Laurentc4aef752013-09-12 17:45:53 -07002937 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2938 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2939 if (adev->visualizer_lib == NULL) {
2940 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2941 } else {
2942 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2943 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002944 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002945 "visualizer_hal_start_output");
2946 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002947 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002948 "visualizer_hal_stop_output");
2949 }
2950 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08002951 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07002952
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002953 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2954 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2955 if (adev->offload_effects_lib == NULL) {
2956 ALOGE("%s: DLOPEN failed for %s", __func__,
2957 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2958 } else {
2959 ALOGV("%s: DLOPEN successful for %s", __func__,
2960 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2961 adev->offload_effects_start_output =
2962 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2963 "offload_effects_bundle_hal_start_output");
2964 adev->offload_effects_stop_output =
2965 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2966 "offload_effects_bundle_hal_stop_output");
2967 }
2968 }
2969
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002970 *device = &adev->device.common;
2971
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002972 audio_extn_utils_update_streams_output_cfg_list(adev->platform, adev->mixer,
2973 &adev->streams_output_cfg_list);
2974
Kiran Kandi910e1862013-10-29 13:29:42 -07002975 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002976 pthread_mutex_unlock(&adev_init_lock);
2977
Eric Laurent994a6932013-07-17 11:51:42 -07002978 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002979 return 0;
2980}
2981
Mingming Yin07972cc2014-06-06 17:11:23 -07002982int pcm_ioctl(struct pcm *pcm, int request, ...)
2983{
2984 va_list ap;
2985 void * arg;
2986 int pcm_fd = *(int*)pcm;
2987
2988 va_start(ap, request);
2989 arg = va_arg(ap, void *);
2990 va_end(ap);
2991
2992 return ioctl(pcm_fd, request, arg);
2993}
2994
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002995static struct hw_module_methods_t hal_module_methods = {
2996 .open = adev_open,
2997};
2998
2999struct audio_module HAL_MODULE_INFO_SYM = {
3000 .common = {
3001 .tag = HARDWARE_MODULE_TAG,
3002 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3003 .hal_api_version = HARDWARE_HAL_API_VERSION,
3004 .id = AUDIO_HARDWARE_MODULE_ID,
3005 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08003006 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003007 .methods = &hal_module_methods,
3008 },
3009};