blob: 69f3b20c70d2405a29eda5351fa4ec84e354f2fc [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003 * Not a Contribution.
4 *
Shiv Maliyappanahalli8911f282014-01-10 15:56:19 -08005 * Copyright (C) 2013 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_primary"
21/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070022/*#define VERY_VERY_VERBOSE_LOGGING*/
23#ifdef VERY_VERY_VERBOSE_LOGGING
24#define ALOGVV ALOGV
25#else
26#define ALOGVV(a...) do { } while(0)
27#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080028
29#include <errno.h>
30#include <pthread.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080034#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070035#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070036#include <sys/resource.h>
37#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038
39#include <cutils/log.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070042#include <cutils/atomic.h>
43#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080044
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <hardware/audio_effect.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070050#include "platform_api.h"
51#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070052#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080053#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include "sound/compress_params.h"
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080056#include "sound/asound.h"
ApurupaPattapu2e084df2013-12-18 15:47:59 -080057
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070058#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
59/* ToDo: Check and update a proper value in msec */
60#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
61#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
62
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080063
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080064#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
65
Eric Laurentb23d5282013-05-14 15:27:20 -070066struct pcm_config pcm_config_deep_buffer = {
67 .channels = 2,
68 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
69 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
70 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
71 .format = PCM_FORMAT_S16_LE,
72 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
73 .stop_threshold = INT_MAX,
74 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
75};
76
77struct pcm_config pcm_config_low_latency = {
78 .channels = 2,
79 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
80 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
81 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
82 .format = PCM_FORMAT_S16_LE,
83 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
84 .stop_threshold = INT_MAX,
85 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
86};
87
88struct pcm_config pcm_config_hdmi_multi = {
89 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
90 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
91 .period_size = HDMI_MULTI_PERIOD_SIZE,
92 .period_count = HDMI_MULTI_PERIOD_COUNT,
93 .format = PCM_FORMAT_S16_LE,
94 .start_threshold = 0,
95 .stop_threshold = INT_MAX,
96 .avail_min = 0,
97};
98
99struct pcm_config pcm_config_audio_capture = {
100 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700101 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
102 .format = PCM_FORMAT_S16_LE,
103};
104
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800105const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700106 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
107 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
108 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700109 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700110#ifdef MULTIPLE_OFFLOAD_ENABLED
111 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
112 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
113 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
114 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
115 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
116 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
117 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
118 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
119#endif
Eric Laurentb23d5282013-05-14 15:27:20 -0700120 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700121 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700122 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700123 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700124 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800125 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800126 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700127 [USECASE_VOICE_CALL] = "voice-call",
Mingming Yin3ee55c62014-08-04 14:23:35 -0700128
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700129 [USECASE_VOICE2_CALL] = "voice2-call",
130 [USECASE_VOLTE_CALL] = "volte-call",
131 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800132 [USECASE_VOWLAN_CALL] = "vowlan-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800133 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700134 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
135 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
136 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800137 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
138 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
139 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
140
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700141 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
142 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700143 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
144 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700145};
146
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700147static const audio_usecase_t offload_usecases[] = {
148 USECASE_AUDIO_PLAYBACK_OFFLOAD,
149#ifdef MULTIPLE_OFFLOAD_ENABLED
150 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
151 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
152 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
153 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
154 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
155 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
156 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
157 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
158#endif
159};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800160
161#define STRING_TO_ENUM(string) { #string, string }
162
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163struct string_to_enum {
164 const char *name;
165 uint32_t value;
166};
167
168static const struct string_to_enum out_channels_name_to_enum_table[] = {
169 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
170 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
171 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
172};
173
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700174static struct audio_device *adev = NULL;
175static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700176static unsigned int audio_device_ref_count;
177
Haynes Mathew George5191a852013-09-11 14:19:36 -0700178static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800179
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800180static int check_and_set_gapless_mode(struct audio_device *adev) {
181
182
183 char value[PROPERTY_VALUE_MAX] = {0};
184 bool gapless_enabled = false;
185 const char *mixer_ctl_name = "Compress Gapless Playback";
186 struct mixer_ctl *ctl;
187
188 ALOGV("%s:", __func__);
189 property_get("audio.offload.gapless.enabled", value, NULL);
190 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
191
192 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
193 if (!ctl) {
194 ALOGE("%s: Could not get ctl for mixer cmd - %s",
195 __func__, mixer_ctl_name);
196 return -EINVAL;
197 }
198
199 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
200 ALOGE("%s: Could not set gapless mode %d",
201 __func__, gapless_enabled);
202 return -EINVAL;
203 }
204 return 0;
205}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700206
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700207static bool is_supported_format(audio_format_t format)
208{
Eric Laurent86e17132013-09-12 17:49:30 -0700209 if (format == AUDIO_FORMAT_MP3 ||
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800210 format == AUDIO_FORMAT_AAC ||
211 format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD ||
Mingming Yin3ee55c62014-08-04 14:23:35 -0700212 format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD ||
213 format == AUDIO_FORMAT_FLAC)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800214 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700215
216 return false;
217}
218
219static int get_snd_codec_id(audio_format_t format)
220{
221 int id = 0;
222
223 switch (format) {
224 case AUDIO_FORMAT_MP3:
225 id = SND_AUDIOCODEC_MP3;
226 break;
227 case AUDIO_FORMAT_AAC:
228 id = SND_AUDIOCODEC_AAC;
229 break;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800230 case AUDIO_FORMAT_PCM_16_BIT_OFFLOAD:
231 case AUDIO_FORMAT_PCM_24_BIT_OFFLOAD:
232 id = SND_AUDIOCODEC_PCM;
233 break;
Mingming Yin3ee55c62014-08-04 14:23:35 -0700234 case AUDIO_FORMAT_FLAC:
235 id = SND_AUDIOCODEC_FLAC;
236 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700237 default:
Mingming Yin90310102013-11-13 16:57:00 -0800238 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700239 }
240
241 return id;
242}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800243
Venkata Narendra Kumar Guttaed0f94f2014-07-09 16:29:28 +0530244int get_snd_card_state(struct audio_device *adev)
Naresh Tanniru80659832014-06-04 18:17:56 +0530245{
246 int snd_scard_state;
247
248 if (!adev)
249 return SND_CARD_STATE_OFFLINE;
250
251 pthread_mutex_lock(&adev->snd_card_status.lock);
252 snd_scard_state = adev->snd_card_status.state;
253 pthread_mutex_unlock(&adev->snd_card_status.lock);
254
255 return snd_scard_state;
256}
257
258static int set_snd_card_state(struct audio_device *adev, int snd_scard_state)
259{
260 if (!adev)
261 return -ENOSYS;
262
263 pthread_mutex_lock(&adev->snd_card_status.lock);
264 adev->snd_card_status.state = snd_scard_state;
265 pthread_mutex_unlock(&adev->snd_card_status.lock);
266
267 return 0;
268}
269
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700270int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700271 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800272{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700273 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700274 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800275
276 if (usecase == NULL)
277 return -EINVAL;
278
279 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
280
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800281 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700282 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800283 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700284 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800285
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -0800286#ifdef DS1_DOLBY_DAP_ENABLED
287 audio_extn_dolby_set_dmid(adev);
288 audio_extn_dolby_set_endpoint(adev);
289#endif
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530290 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700291 audio_extn_utils_send_audio_calibration(adev, usecase);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700292 audio_extn_utils_send_app_type_cfg(usecase);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800293 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700294 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700295 ALOGV("%s: apply mixer and update path: %s", __func__, mixer_path);
296 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800297 ALOGV("%s: exit", __func__);
298 return 0;
299}
300
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700301int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700302 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800303{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700304 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700305 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800306
307 if (usecase == NULL)
308 return -EINVAL;
309
310 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700311 if (usecase->type == PCM_CAPTURE)
312 snd_device = usecase->in_snd_device;
313 else
314 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800315 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700316 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700317 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
318 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530319 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800320 ALOGV("%s: exit", __func__);
321 return 0;
322}
323
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700324int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700325 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800326{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700327 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
328
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800329 if (snd_device < SND_DEVICE_MIN ||
330 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800331 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800332 return -EINVAL;
333 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700334
335 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700336
337 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
338 ALOGE("%s: Invalid sound device returned", __func__);
339 return -EINVAL;
340 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700341 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700342 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700343 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700344 return 0;
345 }
346
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -0700347 if (audio_extn_spkr_prot_is_enabled())
348 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700349 /* start usb playback thread */
350 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
351 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
352 audio_extn_usb_start_playback(adev);
353
354 /* start usb capture thread */
355 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
356 audio_extn_usb_start_capture(adev);
357
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800358 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
359 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700360 audio_extn_spkr_prot_is_enabled()) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700361 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
362 adev->snd_dev_ref_cnt[snd_device]--;
363 return -EINVAL;
364 }
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800365 if (audio_extn_spkr_prot_start_processing(snd_device)) {
366 ALOGE("%s: spkr_start_processing failed", __func__);
367 return -EINVAL;
368 }
369 } else {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700370 ALOGV("%s: snd_device(%d: %s)", __func__,
371 snd_device, device_name);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -0700372 /* due to the possibility of calibration overwrite between listen
373 and audio, notify listen hal before audio calibration is sent */
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530374 audio_extn_listen_update_device_status(snd_device,
375 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700376 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700377 adev->snd_dev_ref_cnt[snd_device]--;
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530378 audio_extn_listen_update_device_status(snd_device,
379 LISTEN_EVENT_SND_DEVICE_FREE);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700380 return -EINVAL;
381 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300382 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700383 audio_route_apply_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800384 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800385 return 0;
386}
387
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700388int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700389 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800390{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700391 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
392
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800393 if (snd_device < SND_DEVICE_MIN ||
394 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800395 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800396 return -EINVAL;
397 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700398 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
399 ALOGE("%s: device ref cnt is already 0", __func__);
400 return -EINVAL;
401 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700402
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700403 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700404
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700405 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
406 ALOGE("%s: Invalid sound device returned", __func__);
407 return -EINVAL;
408 }
409
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700410 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700411 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700412 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800413 /* exit usb play back thread */
414 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
415 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
416 audio_extn_usb_stop_playback();
417
418 /* exit usb capture thread */
419 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -0700420 audio_extn_usb_stop_capture();
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800421
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800422 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
423 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700424 audio_extn_spkr_prot_is_enabled()) {
425 audio_extn_spkr_prot_stop_processing();
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300426 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700427 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300428 audio_extn_dev_arbi_release(snd_device);
429 }
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530430 audio_extn_listen_update_device_status(snd_device,
Kiran Kandide144c82013-11-20 15:58:32 -0800431 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700432 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700433
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800434 return 0;
435}
436
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700437static void check_usecases_codec_backend(struct audio_device *adev,
438 struct audio_usecase *uc_info,
439 snd_device_t snd_device)
440{
441 struct listnode *node;
442 struct audio_usecase *usecase;
443 bool switch_device[AUDIO_USECASE_MAX];
444 int i, num_uc_to_switch = 0;
445
446 /*
447 * This function is to make sure that all the usecases that are active on
448 * the hardware codec backend are always routed to any one device that is
449 * handled by the hardware codec.
450 * For example, if low-latency and deep-buffer usecases are currently active
451 * on speaker and out_set_parameters(headset) is received on low-latency
452 * output, then we have to make sure deep-buffer is also switched to headset,
453 * because of the limitation that both the devices cannot be enabled
454 * at the same time as they share the same backend.
455 */
Mingming Yin3ee55c62014-08-04 14:23:35 -0700456 /*
457 * This call is to check if we need to force routing for a particular stream
458 * If there is a backend configuration change for the device when a
459 * new stream starts, then ADM needs to be closed and re-opened with the new
460 * configuraion. This call check if we need to re-route all the streams
461 * associated with the backend. Touch tone + 24 bit playback.
462 */
463 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info);
464
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700465 /* Disable all the usecases on the shared backend other than the
466 specified usecase */
467 for (i = 0; i < AUDIO_USECASE_MAX; i++)
468 switch_device[i] = false;
469
470 list_for_each(node, &adev->usecase_list) {
471 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800472 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700473 usecase != uc_info &&
Mingming Yin3ee55c62014-08-04 14:23:35 -0700474 (usecase->out_snd_device != snd_device || force_routing) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700475 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
476 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
477 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700478 platform_get_snd_device_name(usecase->out_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700479 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700480 switch_device[usecase->id] = true;
481 num_uc_to_switch++;
482 }
483 }
484
485 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700486 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700487
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530488 /* Make sure the previous devices to be disabled first and then enable the
489 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700490 list_for_each(node, &adev->usecase_list) {
491 usecase = node_to_item(node, struct audio_usecase, list);
492 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700493 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700494 }
495 }
496
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700497 list_for_each(node, &adev->usecase_list) {
498 usecase = node_to_item(node, struct audio_usecase, list);
499 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700500 enable_snd_device(adev, snd_device);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700501 }
502 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700503
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700504 /* Re-route all the usecases on the shared backend other than the
505 specified usecase to new snd devices */
506 list_for_each(node, &adev->usecase_list) {
507 usecase = node_to_item(node, struct audio_usecase, list);
508 /* Update the out_snd_device only before enabling the audio route */
509 if (switch_device[usecase->id] ) {
510 usecase->out_snd_device = snd_device;
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700511 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700512 }
513 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700514 }
515}
516
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700517static void check_and_route_capture_usecases(struct audio_device *adev,
518 struct audio_usecase *uc_info,
519 snd_device_t snd_device)
520{
521 struct listnode *node;
522 struct audio_usecase *usecase;
523 bool switch_device[AUDIO_USECASE_MAX];
524 int i, num_uc_to_switch = 0;
525
526 /*
527 * This function is to make sure that all the active capture usecases
528 * are always routed to the same input sound device.
529 * For example, if audio-record and voice-call usecases are currently
530 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
531 * is received for voice call then we have to make sure that audio-record
532 * usecase is also switched to earpiece i.e. voice-dmic-ef,
533 * because of the limitation that two devices cannot be enabled
534 * at the same time if they share the same backend.
535 */
536 for (i = 0; i < AUDIO_USECASE_MAX; i++)
537 switch_device[i] = false;
538
539 list_for_each(node, &adev->usecase_list) {
540 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800541 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700542 usecase != uc_info &&
543 usecase->in_snd_device != snd_device) {
544 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
545 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700546 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700547 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700548 switch_device[usecase->id] = true;
549 num_uc_to_switch++;
550 }
551 }
552
553 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700554 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700555
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530556 /* Make sure the previous devices to be disabled first and then enable the
557 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700558 list_for_each(node, &adev->usecase_list) {
559 usecase = node_to_item(node, struct audio_usecase, list);
560 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700561 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800562 }
563 }
564
565 list_for_each(node, &adev->usecase_list) {
566 usecase = node_to_item(node, struct audio_usecase, list);
567 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700568 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700569 }
570 }
571
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700572 /* Re-route all the usecases on the shared backend other than the
573 specified usecase to new snd devices */
574 list_for_each(node, &adev->usecase_list) {
575 usecase = node_to_item(node, struct audio_usecase, list);
576 /* Update the in_snd_device only before enabling the audio route */
577 if (switch_device[usecase->id] ) {
578 usecase->in_snd_device = snd_device;
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700579 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700580 }
581 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700582 }
583}
584
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800585/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700586static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800587{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700588 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700589 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800590
591 switch (channels) {
592 /*
593 * Do not handle stereo output in Multi-channel cases
594 * Stereo case is handled in normal playback path
595 */
596 case 6:
597 ALOGV("%s: HDMI supports 5.1", __func__);
598 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
599 break;
600 case 8:
601 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
602 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
603 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
604 break;
605 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700606 ALOGE("HDMI does not support multi channel playback");
607 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800608 break;
609 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700610 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800611}
612
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700613static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
614{
615 struct audio_usecase *usecase;
616 struct listnode *node;
617
618 list_for_each(node, &adev->usecase_list) {
619 usecase = node_to_item(node, struct audio_usecase, list);
620 if (usecase->type == VOICE_CALL) {
621 ALOGV("%s: usecase id %d", __func__, usecase->id);
622 return usecase->id;
623 }
624 }
625 return USECASE_INVALID;
626}
627
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700628struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700629 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700630{
631 struct audio_usecase *usecase;
632 struct listnode *node;
633
634 list_for_each(node, &adev->usecase_list) {
635 usecase = node_to_item(node, struct audio_usecase, list);
636 if (usecase->id == uc_id)
637 return usecase;
638 }
639 return NULL;
640}
641
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700642int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800643{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800644 snd_device_t out_snd_device = SND_DEVICE_NONE;
645 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700646 struct audio_usecase *usecase = NULL;
647 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800648 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800649 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800650 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800651 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700652 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800653
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700654 usecase = get_usecase_from_list(adev, uc_id);
655 if (usecase == NULL) {
656 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
657 return -EINVAL;
658 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800659
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800660 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800661 (usecase->type == VOIP_CALL) ||
662 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700663 out_snd_device = platform_get_output_snd_device(adev->platform,
664 usecase->stream.out->devices);
665 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700666 usecase->devices = usecase->stream.out->devices;
667 } else {
668 /*
669 * If the voice call is active, use the sound devices of voice call usecase
670 * so that it would not result any device switch. All the usecases will
671 * be switched to new device when select_devices() is called for voice call
672 * usecase. This is to avoid switching devices for voice call when
673 * check_usecases_codec_backend() is called below.
674 */
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700675 if (adev->voice.in_call && adev->mode == AUDIO_MODE_IN_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700676 vc_usecase = get_usecase_from_list(adev,
677 get_voice_usecase_id_from_list(adev));
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700678 if ((vc_usecase) && ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
679 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700680 in_snd_device = vc_usecase->in_snd_device;
681 out_snd_device = vc_usecase->out_snd_device;
682 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800683 } else if (voice_extn_compress_voip_is_active(adev)) {
684 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700685 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Avinash Vaish4d6167d2014-06-25 12:20:37 +0530686 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700687 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800688 in_snd_device = voip_usecase->in_snd_device;
689 out_snd_device = voip_usecase->out_snd_device;
690 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800691 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800692 hfp_ucid = audio_extn_hfp_get_usecase();
693 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700694 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800695 in_snd_device = hfp_usecase->in_snd_device;
696 out_snd_device = hfp_usecase->out_snd_device;
697 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700698 }
699 if (usecase->type == PCM_PLAYBACK) {
700 usecase->devices = usecase->stream.out->devices;
701 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700702 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700703 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700704 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700705 if (usecase->stream.out == adev->primary_output &&
706 adev->active_input &&
707 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
708 select_devices(adev, adev->active_input->usecase);
709 }
710 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700711 } else if (usecase->type == PCM_CAPTURE) {
712 usecase->devices = usecase->stream.in->device;
713 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700714 if (in_snd_device == SND_DEVICE_NONE) {
715 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
716 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700717 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700718 adev->primary_output->devices);
719 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700720 in_snd_device = platform_get_input_snd_device(adev->platform,
721 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700722 }
723 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700724 }
725 }
726
727 if (out_snd_device == usecase->out_snd_device &&
728 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800729 return 0;
730 }
731
sangwoobc677242013-08-08 16:53:43 +0900732 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700733 out_snd_device, platform_get_snd_device_name(out_snd_device),
734 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800735
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800736 /*
737 * Limitation: While in call, to do a device switch we need to disable
738 * and enable both RX and TX devices though one of them is same as current
739 * device.
740 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700741 if ((usecase->type == VOICE_CALL) &&
742 (usecase->in_snd_device != SND_DEVICE_NONE) &&
743 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700744 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800745 }
746
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700747 /* Disable current sound devices */
748 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700749 disable_audio_route(adev, usecase);
750 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800751 }
752
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700753 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700754 disable_audio_route(adev, usecase);
755 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800756 }
757
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800758 /* Applicable only on the targets that has external modem.
759 * New device information should be sent to modem before enabling
760 * the devices to reduce in-call device switch time.
761 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700762 if ((usecase->type == VOICE_CALL) &&
763 (usecase->in_snd_device != SND_DEVICE_NONE) &&
764 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800765 status = platform_switch_voice_call_enable_device_config(adev->platform,
766 out_snd_device,
767 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700768 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800769
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700770 /* Enable new sound devices */
771 if (out_snd_device != SND_DEVICE_NONE) {
772 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
773 check_usecases_codec_backend(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700774 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800775 }
776
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700777 if (in_snd_device != SND_DEVICE_NONE) {
778 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700779 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700780 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700781
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800782 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700783 status = platform_switch_voice_call_device_post(adev->platform,
784 out_snd_device,
785 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800786
sangwoo170731f2013-06-08 15:36:36 +0900787 usecase->in_snd_device = in_snd_device;
788 usecase->out_snd_device = out_snd_device;
789
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700790 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900791
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800792 /* Applicable only on the targets that has external modem.
793 * Enable device command should be sent to modem only after
794 * enabling voice call mixer controls
795 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -0700796 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800797 status = platform_switch_voice_call_usecase_route_post(adev->platform,
798 out_snd_device,
799 in_snd_device);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530800 ALOGD("%s: done",__func__);
801
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800802 return status;
803}
804
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800805static int stop_input_stream(struct stream_in *in)
806{
807 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800808 struct audio_usecase *uc_info;
809 struct audio_device *adev = in->dev;
810
Eric Laurentc8400632013-02-14 19:04:54 -0800811 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800812
Eric Laurent994a6932013-07-17 11:51:42 -0700813 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700814 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800815 uc_info = get_usecase_from_list(adev, in->usecase);
816 if (uc_info == NULL) {
817 ALOGE("%s: Could not find the usecase (%d) in the list",
818 __func__, in->usecase);
819 return -EINVAL;
820 }
821
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800822 /* Close in-call recording streams */
823 voice_check_and_stop_incall_rec_usecase(adev, in);
824
Eric Laurent150dbfe2013-02-27 14:31:02 -0800825 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700826 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700827
828 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700829 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800830
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800831 list_remove(&uc_info->list);
832 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800833
Eric Laurent994a6932013-07-17 11:51:42 -0700834 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800835 return ret;
836}
837
838int start_input_stream(struct stream_in *in)
839{
840 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800841 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800842 struct audio_usecase *uc_info;
843 struct audio_device *adev = in->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +0530844 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800845
Mingming Yine62d7842013-10-25 16:26:03 -0700846 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530847 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
848 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700849
Naresh Tanniru80659832014-06-04 18:17:56 +0530850
851 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +0530852 ALOGE("%s: sound card is not active/SSR returning error", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +0530853 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +0530854 goto error_config;
855 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530856
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700857 /* Check if source matches incall recording usecase criteria */
858 ret = voice_check_and_set_incall_rec_usecase(adev, in);
859 if (ret)
860 goto error_config;
861 else
862 ALOGV("%s: usecase(%d)", __func__, in->usecase);
863
Eric Laurentb23d5282013-05-14 15:27:20 -0700864 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800865 if (in->pcm_device_id < 0) {
866 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
867 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800868 ret = -EINVAL;
869 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800870 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700871
872 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800873 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700874
875 if (!uc_info) {
876 ret = -ENOMEM;
877 goto error_config;
878 }
879
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800880 uc_info->id = in->usecase;
881 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800882 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700883 uc_info->devices = in->device;
884 uc_info->in_snd_device = SND_DEVICE_NONE;
885 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800886
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800887 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700888 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800889
Eric Laurentc8400632013-02-14 19:04:54 -0800890 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800891 __func__, adev->snd_card,
892 in->pcm_device_id, in->config.channels);
893 in->pcm = pcm_open(adev->snd_card,
894 in->pcm_device_id, PCM_IN, &in->config);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800895 if (in->pcm && !pcm_is_ready(in->pcm)) {
896 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
897 pcm_close(in->pcm);
898 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800899 ret = -EIO;
900 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800901 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530902
Eric Laurent994a6932013-07-17 11:51:42 -0700903 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800904 return ret;
905
906error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800907 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800908
909error_config:
910 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700911 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800912
913 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800914}
915
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700916/* must be called with out->lock locked */
917static int send_offload_cmd_l(struct stream_out* out, int command)
918{
919 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
920
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700921 if (!cmd) {
922 ALOGE("failed to allocate mem for command 0x%x", command);
923 return -ENOMEM;
924 }
925
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700926 ALOGVV("%s %d", __func__, command);
927
928 cmd->cmd = command;
929 list_add_tail(&out->offload_cmd_list, &cmd->node);
930 pthread_cond_signal(&out->offload_cond);
931 return 0;
932}
933
934/* must be called iwth out->lock locked */
935static void stop_compressed_output_l(struct stream_out *out)
936{
937 out->offload_state = OFFLOAD_STATE_IDLE;
938 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700939 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700940 if (out->compr != NULL) {
941 compress_stop(out->compr);
942 while (out->offload_thread_blocked) {
943 pthread_cond_wait(&out->cond, &out->lock);
944 }
945 }
946}
947
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700948bool is_offload_usecase(audio_usecase_t uc_id)
949{
950 unsigned int i;
951 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
952 if (uc_id == offload_usecases[i])
953 return true;
954 }
955 return false;
956}
957
958static audio_usecase_t get_offload_usecase(struct audio_device *adev)
959{
960 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
961 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
962 char value[PROPERTY_VALUE_MAX] = {0};
963
964 property_get("audio.offload.multiple.enabled", value, NULL);
965 if (!(atoi(value) || !strncmp("true", value, 4)))
966 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
967
968 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
969 for (i = 0; i < num_usecase; i++) {
970 if (!(adev->offload_usecases_state & (0x1<<i))) {
971 adev->offload_usecases_state |= 0x1 << i;
972 ret = offload_usecases[i];
973 break;
974 }
975 }
976 ALOGV("%s: offload usecase is %d", __func__, ret);
977 return ret;
978}
979
980static void free_offload_usecase(struct audio_device *adev,
981 audio_usecase_t uc_id)
982{
983 unsigned int i;
984 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
985 if (offload_usecases[i] == uc_id) {
986 adev->offload_usecases_state &= ~(0x1<<i);
987 break;
988 }
989 }
990 ALOGV("%s: free offload usecase %d", __func__, uc_id);
991}
992
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700993static void *offload_thread_loop(void *context)
994{
995 struct stream_out *out = (struct stream_out *) context;
996 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -0800997 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700998
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700999 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1000 set_sched_policy(0, SP_FOREGROUND);
1001 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1002
1003 ALOGV("%s", __func__);
1004 pthread_mutex_lock(&out->lock);
1005 for (;;) {
1006 struct offload_cmd *cmd = NULL;
1007 stream_callback_event_t event;
1008 bool send_callback = false;
1009
1010 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1011 __func__, list_empty(&out->offload_cmd_list),
1012 out->offload_state);
1013 if (list_empty(&out->offload_cmd_list)) {
1014 ALOGV("%s SLEEPING", __func__);
1015 pthread_cond_wait(&out->offload_cond, &out->lock);
1016 ALOGV("%s RUNNING", __func__);
1017 continue;
1018 }
1019
1020 item = list_head(&out->offload_cmd_list);
1021 cmd = node_to_item(item, struct offload_cmd, node);
1022 list_remove(item);
1023
1024 ALOGVV("%s STATE %d CMD %d out->compr %p",
1025 __func__, out->offload_state, cmd->cmd, out->compr);
1026
1027 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1028 free(cmd);
1029 break;
1030 }
1031
1032 if (out->compr == NULL) {
1033 ALOGE("%s: Compress handle is NULL", __func__);
1034 pthread_cond_signal(&out->cond);
1035 continue;
1036 }
1037 out->offload_thread_blocked = true;
1038 pthread_mutex_unlock(&out->lock);
1039 send_callback = false;
1040 switch(cmd->cmd) {
1041 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001042 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001043 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001044 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001045 send_callback = true;
1046 event = STREAM_CBK_EVENT_WRITE_READY;
1047 break;
1048 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001049 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301050 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001051 ALOGD("copl(%p):calling compress_partial_drain", out);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001052 compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001053 ALOGD("copl(%p):out of compress_partial_drain", out);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301054 }
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001055 else if(ret == -ETIMEDOUT)
1056 compress_drain(out->compr);
1057 else
1058 ALOGE("%s: Next track returned error %d",__func__, ret);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001059 send_callback = true;
1060 event = STREAM_CBK_EVENT_DRAIN_READY;
1061 break;
1062 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001063 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001064 compress_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001065 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001066 send_callback = true;
1067 event = STREAM_CBK_EVENT_DRAIN_READY;
1068 break;
1069 default:
1070 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1071 break;
1072 }
1073 pthread_mutex_lock(&out->lock);
1074 out->offload_thread_blocked = false;
1075 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001076 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001077 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001078 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001079 free(cmd);
1080 }
1081
1082 pthread_cond_signal(&out->cond);
1083 while (!list_empty(&out->offload_cmd_list)) {
1084 item = list_head(&out->offload_cmd_list);
1085 list_remove(item);
1086 free(node_to_item(item, struct offload_cmd, node));
1087 }
1088 pthread_mutex_unlock(&out->lock);
1089
1090 return NULL;
1091}
1092
1093static int create_offload_callback_thread(struct stream_out *out)
1094{
1095 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1096 list_init(&out->offload_cmd_list);
1097 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1098 offload_thread_loop, out);
1099 return 0;
1100}
1101
1102static int destroy_offload_callback_thread(struct stream_out *out)
1103{
1104 pthread_mutex_lock(&out->lock);
1105 stop_compressed_output_l(out);
1106 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1107
1108 pthread_mutex_unlock(&out->lock);
1109 pthread_join(out->offload_thread, (void **) NULL);
1110 pthread_cond_destroy(&out->offload_cond);
1111
1112 return 0;
1113}
1114
Eric Laurent07eeafd2013-10-06 12:52:49 -07001115static bool allow_hdmi_channel_config(struct audio_device *adev)
1116{
1117 struct listnode *node;
1118 struct audio_usecase *usecase;
1119 bool ret = true;
1120
1121 list_for_each(node, &adev->usecase_list) {
1122 usecase = node_to_item(node, struct audio_usecase, list);
1123 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1124 /*
1125 * If voice call is already existing, do not proceed further to avoid
1126 * disabling/enabling both RX and TX devices, CSD calls, etc.
1127 * Once the voice call done, the HDMI channels can be configured to
1128 * max channels of remaining use cases.
1129 */
1130 if (usecase->id == USECASE_VOICE_CALL) {
1131 ALOGD("%s: voice call is active, no change in HDMI channels",
1132 __func__);
1133 ret = false;
1134 break;
1135 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1136 ALOGD("%s: multi channel playback is active, "
1137 "no change in HDMI channels", __func__);
1138 ret = false;
1139 break;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001140 } else if (is_offload_usecase(usecase->id) &&
Mingming Yin139f1072014-02-24 17:56:01 -08001141 popcount(usecase->stream.out->channel_mask) > 2) {
1142 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1143 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1144 ret = false;
1145 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001146 }
1147 }
1148 }
1149 return ret;
1150}
1151
1152static int check_and_set_hdmi_channels(struct audio_device *adev,
1153 unsigned int channels)
1154{
1155 struct listnode *node;
1156 struct audio_usecase *usecase;
1157
1158 /* Check if change in HDMI channel config is allowed */
1159 if (!allow_hdmi_channel_config(adev))
1160 return 0;
1161
1162 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001163 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001164 return 0;
1165 }
1166
1167 platform_set_hdmi_channels(adev->platform, channels);
1168 adev->cur_hdmi_channels = channels;
1169
1170 /*
1171 * Deroute all the playback streams routed to HDMI so that
1172 * the back end is deactivated. Note that backend will not
1173 * be deactivated if any one stream is connected to it.
1174 */
1175 list_for_each(node, &adev->usecase_list) {
1176 usecase = node_to_item(node, struct audio_usecase, list);
1177 if (usecase->type == PCM_PLAYBACK &&
1178 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001179 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001180 }
1181 }
1182
1183 /*
1184 * Enable all the streams disabled above. Now the HDMI backend
1185 * will be activated with new channel configuration
1186 */
1187 list_for_each(node, &adev->usecase_list) {
1188 usecase = node_to_item(node, struct audio_usecase, list);
1189 if (usecase->type == PCM_PLAYBACK &&
1190 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001191 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001192 }
1193 }
1194
1195 return 0;
1196}
1197
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001198static int stop_output_stream(struct stream_out *out)
1199{
1200 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201 struct audio_usecase *uc_info;
1202 struct audio_device *adev = out->dev;
1203
Eric Laurent994a6932013-07-17 11:51:42 -07001204 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001205 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001206 uc_info = get_usecase_from_list(adev, out->usecase);
1207 if (uc_info == NULL) {
1208 ALOGE("%s: Could not find the usecase (%d) in the list",
1209 __func__, out->usecase);
1210 return -EINVAL;
1211 }
1212
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001213 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001214 if (adev->visualizer_stop_output != NULL)
1215 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1216 if (adev->offload_effects_stop_output != NULL)
1217 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1218 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001219
Eric Laurent150dbfe2013-02-27 14:31:02 -08001220 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001221 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001222
1223 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001224 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001225
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001226 list_remove(&uc_info->list);
1227 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001228
Eric Laurent07eeafd2013-10-06 12:52:49 -07001229 /* Must be called after removing the usecase from list */
1230 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1231 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1232
Eric Laurent994a6932013-07-17 11:51:42 -07001233 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001234 return ret;
1235}
1236
1237int start_output_stream(struct stream_out *out)
1238{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001239 int ret = 0;
Mingming Yin9c041392014-05-01 15:37:31 -07001240 int sink_channels = 0;
1241 char prop_value[PROPERTY_VALUE_MAX] = {0};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001242 struct audio_usecase *uc_info;
1243 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301244 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001245
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001246 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
1247 ret = -EINVAL;
1248 goto error_config;
1249 }
1250
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301251 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1252 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1253 out->devices);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301254
Naresh Tanniru80659832014-06-04 18:17:56 +05301255 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301256 ALOGE("%s: sound card is not active/SSR returning error", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05301257 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301258 goto error_config;
1259 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05301260
Eric Laurentb23d5282013-05-14 15:27:20 -07001261 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262 if (out->pcm_device_id < 0) {
1263 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1264 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001265 ret = -EINVAL;
1266 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001267 }
1268
1269 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001270
1271 if (!uc_info) {
1272 ret = -ENOMEM;
1273 goto error_config;
1274 }
1275
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001276 uc_info->id = out->usecase;
1277 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001278 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001279 uc_info->devices = out->devices;
1280 uc_info->in_snd_device = SND_DEVICE_NONE;
1281 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001282
Eric Laurent07eeafd2013-10-06 12:52:49 -07001283 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001284 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Mingming Yin9c041392014-05-01 15:37:31 -07001285 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1286 if (!strncmp("true", prop_value, 4)) {
1287 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1288 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1289 check_and_set_hdmi_channels(adev, sink_channels);
1290 } else {
1291 if (is_offload_usecase(out->usecase))
1292 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1293 else
1294 check_and_set_hdmi_channels(adev, out->config.channels);
1295 }
Mingming Yin10fef6a2013-11-26 17:17:01 -08001296 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001297
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001298 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001299
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001300 select_devices(adev, out->usecase);
1301
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001302 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1303 __func__, 0, out->pcm_device_id);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001304 if (!is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001305 out->pcm = pcm_open(adev->snd_card,
1306 out->pcm_device_id,
1307 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001308 if (out->pcm && !pcm_is_ready(out->pcm)) {
1309 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1310 pcm_close(out->pcm);
1311 out->pcm = NULL;
1312 ret = -EIO;
1313 goto error_open;
1314 }
1315 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001316 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001317 out->compr = compress_open(adev->snd_card,
1318 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001319 COMPRESS_IN, &out->compr_config);
1320 if (out->compr && !is_compress_ready(out->compr)) {
1321 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1322 compress_close(out->compr);
1323 out->compr = NULL;
1324 ret = -EIO;
1325 goto error_open;
1326 }
1327 if (out->offload_callback)
1328 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001329
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001330#ifdef DS1_DOLBY_DDP_ENABLED
1331 if (audio_extn_is_dolby_format(out->format))
1332 audio_extn_dolby_send_ddp_endp_params(adev);
1333#endif
1334
Eric Laurentc4aef752013-09-12 17:45:53 -07001335 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001336 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1337 if (adev->offload_effects_start_output != NULL)
1338 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001339 }
Eric Laurent994a6932013-07-17 11:51:42 -07001340 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001341 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001342error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001343 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001344error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001345 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001346}
1347
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001348static int check_input_parameters(uint32_t sample_rate,
1349 audio_format_t format,
1350 int channel_count)
1351{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001352 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001353
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001354 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001355 !voice_extn_compress_voip_is_format_supported(format) &&
1356 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001357
1358 switch (channel_count) {
1359 case 1:
1360 case 2:
1361 case 6:
1362 break;
1363 default:
1364 ret = -EINVAL;
1365 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001366
1367 switch (sample_rate) {
1368 case 8000:
1369 case 11025:
1370 case 12000:
1371 case 16000:
1372 case 22050:
1373 case 24000:
1374 case 32000:
1375 case 44100:
1376 case 48000:
1377 break;
1378 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001379 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001380 }
1381
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001382 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001383}
1384
1385static size_t get_input_buffer_size(uint32_t sample_rate,
1386 audio_format_t format,
1387 int channel_count)
1388{
1389 size_t size = 0;
1390
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001391 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1392 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001393
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001394 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1395 /* ToDo: should use frame_size computed based on the format and
1396 channel_count here. */
1397 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001398
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001399 /* make sure the size is multiple of 64 */
1400 size += 0x3f;
1401 size &= ~0x3f;
1402
1403 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404}
1405
1406static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1407{
1408 struct stream_out *out = (struct stream_out *)stream;
1409
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001410 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001411}
1412
1413static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1414{
1415 return -ENOSYS;
1416}
1417
1418static size_t out_get_buffer_size(const struct audio_stream *stream)
1419{
1420 struct stream_out *out = (struct stream_out *)stream;
1421
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001422 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001423 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001424 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1425 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001426
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001427 return out->config.period_size * audio_stream_frame_size(stream);
1428}
1429
1430static uint32_t out_get_channels(const struct audio_stream *stream)
1431{
1432 struct stream_out *out = (struct stream_out *)stream;
1433
1434 return out->channel_mask;
1435}
1436
1437static audio_format_t out_get_format(const struct audio_stream *stream)
1438{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001439 struct stream_out *out = (struct stream_out *)stream;
1440
1441 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001442}
1443
1444static int out_set_format(struct audio_stream *stream, audio_format_t format)
1445{
1446 return -ENOSYS;
1447}
1448
1449static int out_standby(struct audio_stream *stream)
1450{
1451 struct stream_out *out = (struct stream_out *)stream;
1452 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001453
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301454 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1455 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001456 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1457 /* Ignore standby in case of voip call because the voip output
1458 * stream is closed in adev_close_output_stream()
1459 */
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301460 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001461 return 0;
1462 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001463
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001464 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001465 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001466 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001467 out->standby = true;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001468 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001469 if (out->pcm) {
1470 pcm_close(out->pcm);
1471 out->pcm = NULL;
1472 }
1473 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001474 ALOGD("copl(%p):standby", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001475 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001476 out->gapless_mdata.encoder_delay = 0;
1477 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001478 if (out->compr != NULL) {
1479 compress_close(out->compr);
1480 out->compr = NULL;
1481 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001482 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001483 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001484 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001485 }
1486 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001487 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001488 return 0;
1489}
1490
1491static int out_dump(const struct audio_stream *stream, int fd)
1492{
1493 return 0;
1494}
1495
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001496static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1497{
1498 int ret = 0;
1499 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001500 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001501 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001502 tmp_mdata.encoder_delay = 0;
1503 tmp_mdata.encoder_padding = 0;
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001504
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001505 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001506 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001507 return -EINVAL;
1508 }
1509
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001510 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1511 if (ret >= 0) {
1512 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1513 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1514 ALOGV("ADTS format is set in offload mode");
1515 }
1516 out->send_new_metadata = 1;
1517 }
1518
Mingming Yin3ee55c62014-08-04 14:23:35 -07001519 if (out->format == AUDIO_FORMAT_FLAC) {
1520 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_BLK_SIZE, value, sizeof(value));
1521 if (ret >= 0) {
1522 out->compr_config.codec->options.flac_dec.min_blk_size = atoi(value);
1523 out->send_new_metadata = 1;
1524 }
1525 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_BLK_SIZE, value, sizeof(value));
1526 if (ret >= 0) {
1527 out->compr_config.codec->options.flac_dec.max_blk_size = atoi(value);
1528 out->send_new_metadata = 1;
1529 }
1530 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_FRAME_SIZE, value, sizeof(value));
1531 if (ret >= 0) {
1532 out->compr_config.codec->options.flac_dec.min_frame_size = atoi(value);
1533 out->send_new_metadata = 1;
1534 }
1535 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_FRAME_SIZE, value, sizeof(value));
1536 if (ret >= 0) {
1537 out->compr_config.codec->options.flac_dec.max_frame_size = atoi(value);
1538 out->send_new_metadata = 1;
1539 }
1540 }
1541
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001542 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1543 if(ret >= 0)
1544 is_meta_data_params = true;
1545 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1546 if(ret >= 0 )
1547 is_meta_data_params = true;
1548 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1549 if(ret >= 0 )
1550 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001551 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1552 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001553 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001554 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001555 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001556 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1557 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001558 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001559 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001560 }
1561
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001562 if(!is_meta_data_params) {
1563 ALOGV("%s: Not gapless meta data params", __func__);
1564 return 0;
1565 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001566 out->gapless_mdata = tmp_mdata;
1567 out->send_new_metadata = 1;
1568 ALOGV("%s new encoder delay %u and padding %u", __func__,
1569 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1570
1571 return 0;
1572}
1573
1574
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001575static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1576{
1577 struct stream_out *out = (struct stream_out *)stream;
1578 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001579 struct audio_usecase *usecase;
1580 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001581 struct str_parms *parms;
1582 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001583 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001584 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001585
sangwoobc677242013-08-08 16:53:43 +09001586 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001587 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001588 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001589 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1590 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001591 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001592 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001593 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001594
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001595 /*
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301596 * When HDMI cable is unplugged/usb hs is disconnected the
1597 * music playback is paused and the policy manager sends routing=0
1598 * But the audioflingercontinues to write data until standby time
1599 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001600 * Avoid this by routing audio to speaker until standby.
1601 */
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301602 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1603 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001604 val == AUDIO_DEVICE_NONE) {
1605 val = AUDIO_DEVICE_OUT_SPEAKER;
1606 }
1607
1608 /*
1609 * select_devices() call below switches all the usecases on the same
1610 * backend to the new device. Refer to check_usecases_codec_backend() in
1611 * the select_devices(). But how do we undo this?
1612 *
1613 * For example, music playback is active on headset (deep-buffer usecase)
1614 * and if we go to ringtones and select a ringtone, low-latency usecase
1615 * will be started on headset+speaker. As we can't enable headset+speaker
1616 * and headset devices at the same time, select_devices() switches the music
1617 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1618 * So when the ringtone playback is completed, how do we undo the same?
1619 *
1620 * We are relying on the out_set_parameters() call on deep-buffer output,
1621 * once the ringtone playback is ended.
1622 * NOTE: We should not check if the current devices are same as new devices.
1623 * Because select_devices() must be called to switch back the music
1624 * playback to headset.
1625 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001626 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001627 out->devices = val;
1628
1629 if (!out->standby)
1630 select_devices(adev, out->usecase);
1631
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001632 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001633 !adev->voice.in_call &&
1634 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001635 ret = voice_start_call(adev);
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001636 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1637 adev->voice.in_call &&
1638 (out == adev->primary_output)) {
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -08001639 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001640 }
1641 }
1642
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001643 if ((adev->mode == AUDIO_MODE_NORMAL) &&
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001644 adev->voice.in_call &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001645 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001646 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001647 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001648
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001649 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001650 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001651 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001652
1653 if (out == adev->primary_output) {
1654 pthread_mutex_lock(&adev->lock);
1655 audio_extn_set_parameters(adev, parms);
1656 pthread_mutex_unlock(&adev->lock);
1657 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001658 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001659 pthread_mutex_lock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001660 parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001661 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001662 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001663
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001664 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001665 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001666 return ret;
1667}
1668
1669static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1670{
1671 struct stream_out *out = (struct stream_out *)stream;
1672 struct str_parms *query = str_parms_create_str(keys);
1673 char *str;
1674 char value[256];
1675 struct str_parms *reply = str_parms_create();
1676 size_t i, j;
1677 int ret;
1678 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001679
1680 if (!query || !reply) {
1681 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
1682 return NULL;
1683 }
1684
Eric Laurent994a6932013-07-17 11:51:42 -07001685 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001686 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1687 if (ret >= 0) {
1688 value[0] = '\0';
1689 i = 0;
1690 while (out->supported_channel_masks[i] != 0) {
1691 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1692 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1693 if (!first) {
1694 strcat(value, "|");
1695 }
1696 strcat(value, out_channels_name_to_enum_table[j].name);
1697 first = false;
1698 break;
1699 }
1700 }
1701 i++;
1702 }
1703 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1704 str = str_parms_to_str(reply);
1705 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001706 voice_extn_out_get_parameters(out, query, reply);
1707 str = str_parms_to_str(reply);
1708 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella29b8fc72014-01-29 12:52:19 -08001709 free(str);
1710 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001711 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001712 }
1713 str_parms_destroy(query);
1714 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001715 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001716 return str;
1717}
1718
1719static uint32_t out_get_latency(const struct audio_stream_out *stream)
1720{
1721 struct stream_out *out = (struct stream_out *)stream;
1722
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001723 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001724 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1725
1726 return (out->config.period_count * out->config.period_size * 1000) /
1727 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001728}
1729
1730static int out_set_volume(struct audio_stream_out *stream, float left,
1731 float right)
1732{
Eric Laurenta9024de2013-04-04 09:19:12 -07001733 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001734 int volume[2];
1735
Eric Laurenta9024de2013-04-04 09:19:12 -07001736 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1737 /* only take left channel into account: the API is for stereo anyway */
1738 out->muted = (left == 0.0f);
1739 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001740 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001741 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001742 struct audio_device *adev = out->dev;
1743 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001744 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1745 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001746
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001747 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1748 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001749 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1750 if (!ctl) {
1751 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1752 __func__, mixer_ctl_name);
1753 return -EINVAL;
1754 }
1755 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1756 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1757 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1758 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001759 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001760
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001761 return -ENOSYS;
1762}
1763
1764static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1765 size_t bytes)
1766{
1767 struct stream_out *out = (struct stream_out *)stream;
1768 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301769 int snd_scard_state = get_snd_card_state(adev);
Eric Laurent6e895242013-09-05 16:10:57 -07001770 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001771
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001772 pthread_mutex_lock(&out->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301773
Naresh Tanniru80659832014-06-04 18:17:56 +05301774 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
1775 if (out->pcm) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301776 ALOGD(" %s: sound card is not active/SSR state", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05301777 ret= -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301778 goto exit;
Naresh Tanniru80659832014-06-04 18:17:56 +05301779 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1780 //during SSR for compress usecase we should return error to flinger
1781 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
1782 pthread_mutex_unlock(&out->lock);
1783 return -ENETRESET;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301784 }
1785 }
1786
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001787 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001788 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001789 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001790 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1791 ret = voice_extn_compress_voip_start_output_stream(out);
1792 else
1793 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001794 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001795 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001796 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001797 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001798 goto exit;
1799 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001800 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001801
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001802 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001803 ALOGD("copl(%p): writing buffer (%d bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001804 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001805 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001806 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1807 out->send_new_metadata = 0;
1808 }
1809
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001810 ret = compress_write(out->compr, buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301811 if (ret < 0)
1812 ret = -errno;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001813 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001814 if (ret >= 0 && ret < (ssize_t)bytes) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301815 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001816 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05301817 } else if (-ENETRESET == ret) {
1818 ALOGE("copl %s: received sound card offline state on compress write", __func__);
1819 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1820 pthread_mutex_unlock(&out->lock);
1821 out_standby(&out->stream.common);
1822 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001823 }
Naresh Tanniru80659832014-06-04 18:17:56 +05301824 if (!out->playback_started && ret >= 0) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001825 compress_start(out->compr);
1826 out->playback_started = 1;
1827 out->offload_state = OFFLOAD_STATE_PLAYING;
1828 }
1829 pthread_mutex_unlock(&out->lock);
1830 return ret;
1831 } else {
1832 if (out->pcm) {
1833 if (out->muted)
1834 memset((void *)buffer, 0, bytes);
1835 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1836 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301837 if (ret < 0)
1838 ret = -errno;
1839 else if (ret == 0)
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001840 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001841 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001842 }
1843
1844exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05301845 /* ToDo: There may be a corner case when SSR happens back to back during
1846 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05301847 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301848 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301849 }
1850
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001851 pthread_mutex_unlock(&out->lock);
1852
1853 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001854 if (out->pcm)
1855 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301856 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05301857 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301858 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05301859 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301860 out->standby = true;
1861 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001862 out_standby(&out->stream.common);
1863 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05301864 out_get_sample_rate(&out->stream.common));
1865
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001866 }
1867 return bytes;
1868}
1869
1870static int out_get_render_position(const struct audio_stream_out *stream,
1871 uint32_t *dsp_frames)
1872{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001873 struct stream_out *out = (struct stream_out *)stream;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001874 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301875 ssize_t ret = -EINVAL;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001876 *dsp_frames = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001877 pthread_mutex_lock(&out->lock);
1878 if (out->compr != NULL) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301879 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001880 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301881 if (ret < 0)
1882 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001883 ALOGVV("%s rendered frames %d sample_rate %d",
1884 __func__, *dsp_frames, out->sample_rate);
1885 }
1886 pthread_mutex_unlock(&out->lock);
Naresh Tanniru80659832014-06-04 18:17:56 +05301887 if (-ENETRESET == ret) {
1888 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
1889 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1890 return -EINVAL;
1891 } else if(ret < 0) {
1892 ALOGE(" ERROR: Unable to get time stamp from compress driver");
1893 return -EINVAL;
1894 } else {
1895 return 0;
1896 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001897 } else
1898 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001899}
1900
1901static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1902{
1903 return 0;
1904}
1905
1906static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1907{
1908 return 0;
1909}
1910
1911static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1912 int64_t *timestamp)
1913{
1914 return -EINVAL;
1915}
1916
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001917static int out_get_presentation_position(const struct audio_stream_out *stream,
1918 uint64_t *frames, struct timespec *timestamp)
1919{
1920 struct stream_out *out = (struct stream_out *)stream;
1921 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001922 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001923
1924 pthread_mutex_lock(&out->lock);
1925
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001926 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07001927 if (out->compr != NULL) {
1928 compress_get_tstamp(out->compr, &dsp_frames,
1929 &out->sample_rate);
1930 ALOGVV("%s rendered frames %ld sample_rate %d",
1931 __func__, dsp_frames, out->sample_rate);
1932 *frames = dsp_frames;
1933 ret = 0;
1934 /* this is the best we can do */
1935 clock_gettime(CLOCK_MONOTONIC, timestamp);
1936 }
1937 } else {
1938 if (out->pcm) {
1939 size_t avail;
1940 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1941 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001942 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001943 // This adjustment accounts for buffering after app processor.
1944 // It is based on estimated DSP latency per use case, rather than exact.
1945 signed_frames -=
1946 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1947
Eric Laurent949a0892013-09-20 09:20:13 -07001948 // It would be unusual for this value to be negative, but check just in case ...
1949 if (signed_frames >= 0) {
1950 *frames = signed_frames;
1951 ret = 0;
1952 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001953 }
1954 }
1955 }
1956
1957 pthread_mutex_unlock(&out->lock);
1958
1959 return ret;
1960}
1961
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001962static int out_set_callback(struct audio_stream_out *stream,
1963 stream_callback_t callback, void *cookie)
1964{
1965 struct stream_out *out = (struct stream_out *)stream;
1966
1967 ALOGV("%s", __func__);
1968 pthread_mutex_lock(&out->lock);
1969 out->offload_callback = callback;
1970 out->offload_cookie = cookie;
1971 pthread_mutex_unlock(&out->lock);
1972 return 0;
1973}
1974
1975static int out_pause(struct audio_stream_out* stream)
1976{
1977 struct stream_out *out = (struct stream_out *)stream;
1978 int status = -ENOSYS;
1979 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001980 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001981 ALOGD("copl(%p):pause compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001982 pthread_mutex_lock(&out->lock);
1983 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301984 struct audio_device *adev = out->dev;
1985 int snd_scard_state = get_snd_card_state(adev);
1986
1987 if (SND_CARD_STATE_ONLINE == snd_scard_state)
1988 status = compress_pause(out->compr);
1989
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001990 out->offload_state = OFFLOAD_STATE_PAUSED;
1991 }
1992 pthread_mutex_unlock(&out->lock);
1993 }
1994 return status;
1995}
1996
1997static int out_resume(struct audio_stream_out* stream)
1998{
1999 struct stream_out *out = (struct stream_out *)stream;
2000 int status = -ENOSYS;
2001 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002002 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002003 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002004 status = 0;
2005 pthread_mutex_lock(&out->lock);
2006 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302007 struct audio_device *adev = out->dev;
2008 int snd_scard_state = get_snd_card_state(adev);
2009
2010 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2011 status = compress_resume(out->compr);
2012
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002013 out->offload_state = OFFLOAD_STATE_PLAYING;
2014 }
2015 pthread_mutex_unlock(&out->lock);
2016 }
2017 return status;
2018}
2019
2020static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2021{
2022 struct stream_out *out = (struct stream_out *)stream;
2023 int status = -ENOSYS;
2024 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002025 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002026 pthread_mutex_lock(&out->lock);
2027 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2028 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2029 else
2030 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2031 pthread_mutex_unlock(&out->lock);
2032 }
2033 return status;
2034}
2035
2036static int out_flush(struct audio_stream_out* stream)
2037{
2038 struct stream_out *out = (struct stream_out *)stream;
2039 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002040 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002041 ALOGD("copl(%p):calling compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002042 pthread_mutex_lock(&out->lock);
2043 stop_compressed_output_l(out);
2044 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002045 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002046 return 0;
2047 }
2048 return -ENOSYS;
2049}
2050
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002051/** audio_stream_in implementation **/
2052static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2053{
2054 struct stream_in *in = (struct stream_in *)stream;
2055
2056 return in->config.rate;
2057}
2058
2059static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
2060{
2061 return -ENOSYS;
2062}
2063
2064static size_t in_get_buffer_size(const struct audio_stream *stream)
2065{
2066 struct stream_in *in = (struct stream_in *)stream;
2067
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002068 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
2069 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07002070 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
2071 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002072
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002073 return in->config.period_size * audio_stream_frame_size(stream);
2074}
2075
2076static uint32_t in_get_channels(const struct audio_stream *stream)
2077{
2078 struct stream_in *in = (struct stream_in *)stream;
2079
2080 return in->channel_mask;
2081}
2082
2083static audio_format_t in_get_format(const struct audio_stream *stream)
2084{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002085 struct stream_in *in = (struct stream_in *)stream;
2086
2087 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002088}
2089
2090static int in_set_format(struct audio_stream *stream, audio_format_t format)
2091{
2092 return -ENOSYS;
2093}
2094
2095static int in_standby(struct audio_stream *stream)
2096{
2097 struct stream_in *in = (struct stream_in *)stream;
2098 struct audio_device *adev = in->dev;
2099 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302100 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2101 stream, in->usecase, use_case_table[in->usecase]);
2102
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002103
2104 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2105 /* Ignore standby in case of voip call because the voip input
2106 * stream is closed in adev_close_input_stream()
2107 */
2108 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2109 return status;
2110 }
2111
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002112 pthread_mutex_lock(&in->lock);
2113 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002114 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002115 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002116 if (in->pcm) {
2117 pcm_close(in->pcm);
2118 in->pcm = NULL;
2119 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002120 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002121 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002122 }
2123 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002124 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002125 return status;
2126}
2127
2128static int in_dump(const struct audio_stream *stream, int fd)
2129{
2130 return 0;
2131}
2132
2133static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2134{
2135 struct stream_in *in = (struct stream_in *)stream;
2136 struct audio_device *adev = in->dev;
2137 struct str_parms *parms;
2138 char *str;
2139 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002140 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002141
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302142 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002143 parms = str_parms_create_str(kvpairs);
2144
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002145 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002146 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002147
2148 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2149 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002150 val = atoi(value);
2151 /* no audio source uses val == 0 */
2152 if ((in->source != val) && (val != 0)) {
2153 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002154 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2155 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2156 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2157 (in->config.rate == 8000 || in->config.rate == 16000) &&
2158 (popcount(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002159 err = voice_extn_compress_voip_open_input_stream(in);
2160 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002161 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002162 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002163 }
2164 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002165 }
2166 }
2167
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002168 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2169 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002170 val = atoi(value);
2171 if ((in->device != val) && (val != 0)) {
2172 in->device = val;
2173 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002174 if (!in->standby)
2175 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002176 }
2177 }
2178
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002179done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002180 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002181 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002182
2183 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002184 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002185 return ret;
2186}
2187
2188static char* in_get_parameters(const struct audio_stream *stream,
2189 const char *keys)
2190{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002191 struct stream_in *in = (struct stream_in *)stream;
2192 struct str_parms *query = str_parms_create_str(keys);
2193 char *str;
2194 char value[256];
2195 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002196
2197 if (!query || !reply) {
2198 ALOGE("in_get_parameters: failed to create query or reply");
2199 return NULL;
2200 }
2201
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002202 ALOGV("%s: enter: keys - %s", __func__, keys);
2203
2204 voice_extn_in_get_parameters(in, query, reply);
2205
2206 str = str_parms_to_str(reply);
2207 str_parms_destroy(query);
2208 str_parms_destroy(reply);
2209
2210 ALOGV("%s: exit: returns - %s", __func__, str);
2211 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002212}
2213
2214static int in_set_gain(struct audio_stream_in *stream, float gain)
2215{
2216 return 0;
2217}
2218
2219static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2220 size_t bytes)
2221{
2222 struct stream_in *in = (struct stream_in *)stream;
2223 struct audio_device *adev = in->dev;
2224 int i, ret = -1;
Naresh Tanniru80659832014-06-04 18:17:56 +05302225 int snd_scard_state = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002226
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002227 pthread_mutex_lock(&in->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302228
2229 if (in->pcm) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302230 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05302231 ALOGD(" %s: sound card is not active/SSR state", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302232 ret= -EIO;;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302233 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302234 }
2235 }
2236
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002237 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002238 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002239 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2240 ret = voice_extn_compress_voip_start_input_stream(in);
2241 else
2242 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002243 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002244 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002245 goto exit;
2246 }
2247 in->standby = 0;
2248 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002249
2250 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002251 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
2252 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002253 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2254 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002255 else
2256 ret = pcm_read(in->pcm, buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05302257 if (ret < 0)
2258 ret = -errno;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002259 }
2260
2261 /*
2262 * Instead of writing zeroes here, we could trust the hardware
2263 * to always provide zeroes when muted.
2264 */
kunleizc5a639b2014-04-24 18:46:22 +08002265 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002266 memset(buffer, 0, bytes);
2267
2268exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05302269 /* ToDo: There may be a corner case when SSR happens back to back during
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302270 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05302271 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302272 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302273 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002274 pthread_mutex_unlock(&in->lock);
2275
2276 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302277 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302278 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302279 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302280 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302281 in->standby = true;
2282 }
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302283 memset(buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002284 in_standby(&in->stream.common);
2285 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
2286 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05302287 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002288 }
2289 return bytes;
2290}
2291
2292static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
2293{
2294 return 0;
2295}
2296
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002297static int add_remove_audio_effect(const struct audio_stream *stream,
2298 effect_handle_t effect,
2299 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002300{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002301 struct stream_in *in = (struct stream_in *)stream;
2302 int status = 0;
2303 effect_descriptor_t desc;
2304
2305 status = (*effect)->get_descriptor(effect, &desc);
2306 if (status != 0)
2307 return status;
2308
2309 pthread_mutex_lock(&in->lock);
2310 pthread_mutex_lock(&in->dev->lock);
2311 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2312 in->enable_aec != enable &&
2313 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2314 in->enable_aec = enable;
2315 if (!in->standby)
2316 select_devices(in->dev, in->usecase);
2317 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002318 if (in->enable_ns != enable &&
2319 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2320 in->enable_ns = enable;
2321 if (!in->standby)
2322 select_devices(in->dev, in->usecase);
2323 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002324 pthread_mutex_unlock(&in->dev->lock);
2325 pthread_mutex_unlock(&in->lock);
2326
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002327 return 0;
2328}
2329
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002330static int in_add_audio_effect(const struct audio_stream *stream,
2331 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002332{
Eric Laurent994a6932013-07-17 11:51:42 -07002333 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002334 return add_remove_audio_effect(stream, effect, true);
2335}
2336
2337static int in_remove_audio_effect(const struct audio_stream *stream,
2338 effect_handle_t effect)
2339{
Eric Laurent994a6932013-07-17 11:51:42 -07002340 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002341 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002342}
2343
2344static int adev_open_output_stream(struct audio_hw_device *dev,
2345 audio_io_handle_t handle,
2346 audio_devices_t devices,
2347 audio_output_flags_t flags,
2348 struct audio_config *config,
2349 struct audio_stream_out **stream_out)
2350{
2351 struct audio_device *adev = (struct audio_device *)dev;
2352 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002353 int i, ret = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002354 audio_format_t format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002355
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002356 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05302357
2358 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2359 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2360 ALOGE(" sound card is not active rejecting compress output open request");
2361 return -EINVAL;
2362 }
2363
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002364 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2365
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302366 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2367 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2368 devices, flags, &out->stream);
2369
2370
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002371 if (!out) {
2372 return -ENOMEM;
2373 }
2374
2375 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2376 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2377
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002378 if (devices == AUDIO_DEVICE_NONE)
2379 devices = AUDIO_DEVICE_OUT_SPEAKER;
2380
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002381 out->flags = flags;
2382 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002383 out->dev = adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002384 format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002385 out->sample_rate = config->sample_rate;
2386 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2387 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002388 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002389 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002390
2391 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002392 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2393 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2394 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2395
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002396 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002397 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2398 ret = read_hdmi_channel_masks(out);
2399
2400 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2401 ret = audio_extn_read_afe_proxy_channel_masks(out);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002402 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002403 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002404 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002405
2406 if (config->sample_rate == 0)
2407 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2408 if (config->channel_mask == 0)
2409 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2410
2411 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002412 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002413 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2414 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002415 out->config.rate = config->sample_rate;
2416 out->config.channels = popcount(out->channel_mask);
2417 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002418 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2419 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002420 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002421 ret = voice_extn_compress_voip_open_output_stream(out);
2422 if (ret != 0) {
2423 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2424 __func__, ret);
2425 goto error_open;
2426 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002427 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2428 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2429 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2430 ALOGE("%s: Unsupported Offload information", __func__);
2431 ret = -EINVAL;
2432 goto error_open;
2433 }
Mingming Yin90310102013-11-13 16:57:00 -08002434 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002435 !audio_extn_is_dolby_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002436 ALOGE("%s: Unsupported audio format", __func__);
2437 ret = -EINVAL;
2438 goto error_open;
2439 }
2440
2441 out->compr_config.codec = (struct snd_codec *)
2442 calloc(1, sizeof(struct snd_codec));
2443
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002444 if (!out->compr_config.codec) {
2445 ret = -ENOMEM;
2446 goto error_open;
2447 }
2448
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002449 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002450 if (config->offload_info.channel_mask)
2451 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002452 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002453 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002454 config->offload_info.channel_mask = config->channel_mask;
2455 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002456 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002457 out->sample_rate = config->offload_info.sample_rate;
2458
2459 out->stream.set_callback = out_set_callback;
2460 out->stream.pause = out_pause;
2461 out->stream.resume = out_resume;
2462 out->stream.drain = out_drain;
2463 out->stream.flush = out_flush;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002464 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002465
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002466 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002467 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002468 audio_extn_dolby_get_snd_codec_id(adev, out,
2469 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002470 else
2471 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002472 get_snd_codec_id(config->offload_info.format);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002473 if (audio_is_offload_pcm(config->offload_info.format)) {
2474 out->compr_config.fragment_size =
2475 platform_get_pcm_offload_buffer_size(&config->offload_info);
2476 } else {
2477 out->compr_config.fragment_size =
2478 platform_get_compress_offload_buffer_size(&config->offload_info);
2479 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002480 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2481 out->compr_config.codec->sample_rate =
2482 compress_get_alsa_rate(config->offload_info.sample_rate);
2483 out->compr_config.codec->bit_rate =
2484 config->offload_info.bit_rate;
2485 out->compr_config.codec->ch_in =
2486 popcount(config->channel_mask);
2487 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002488 out->bit_width = PCM_OUTPUT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002489
Mingming Yin3ee55c62014-08-04 14:23:35 -07002490 if (config->offload_info.format == AUDIO_FORMAT_AAC)
2491 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002492 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2493 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002494 if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002495 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2496
Mingming Yin3ee55c62014-08-04 14:23:35 -07002497 if (out->bit_width == 24) {
2498 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2499 }
2500
2501 if (out->bit_width == 24 && !platform_check_24_bit_support()) {
2502 ALOGW("24 bit support is not enabled, using 16 bit backend");
2503 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
2504 }
2505
2506 out->compr_config.codec->options.flac_dec.sample_size = out->bit_width;
2507
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002508 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2509 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002510
2511 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002512 out->offload_state = OFFLOAD_STATE_IDLE;
2513 out->playback_started = 0;
2514
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002515 create_offload_callback_thread(out);
2516 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2517 __func__, config->offload_info.version,
2518 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002519 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -08002520 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002521
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002522 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2523 ret = voice_check_and_set_incall_music_usecase(adev, out);
2524 if (ret != 0) {
2525 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2526 __func__, ret);
2527 goto error_open;
2528 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002529 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002530 format = AUDIO_FORMAT_PCM_16_BIT;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002531 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2532 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002533 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002534 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002535 /* primary path is the default path selected if no other outputs are available/suitable */
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002536 format = AUDIO_FORMAT_PCM_16_BIT;
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002537 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002538 out->config = pcm_config_deep_buffer;
2539 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002540 }
2541
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002542 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
2543 &adev->streams_output_cfg_list,
2544 flags, format, &out->app_type_cfg);
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002545 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2546 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2547 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002548 if(adev->primary_output == NULL)
2549 adev->primary_output = out;
2550 else {
2551 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002552 ret = -EEXIST;
2553 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002554 }
2555 }
2556
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002557 /* Check if this usecase is already existing */
2558 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07002559 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2560 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002561 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002562 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002563 ret = -EEXIST;
2564 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002565 }
2566 pthread_mutex_unlock(&adev->lock);
2567
2568 out->stream.common.get_sample_rate = out_get_sample_rate;
2569 out->stream.common.set_sample_rate = out_set_sample_rate;
2570 out->stream.common.get_buffer_size = out_get_buffer_size;
2571 out->stream.common.get_channels = out_get_channels;
2572 out->stream.common.get_format = out_get_format;
2573 out->stream.common.set_format = out_set_format;
2574 out->stream.common.standby = out_standby;
2575 out->stream.common.dump = out_dump;
2576 out->stream.common.set_parameters = out_set_parameters;
2577 out->stream.common.get_parameters = out_get_parameters;
2578 out->stream.common.add_audio_effect = out_add_audio_effect;
2579 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2580 out->stream.get_latency = out_get_latency;
2581 out->stream.set_volume = out_set_volume;
2582 out->stream.write = out_write;
2583 out->stream.get_render_position = out_get_render_position;
2584 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002585 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002586
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002587 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002588 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002589 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002590
2591 config->format = out->stream.common.get_format(&out->stream.common);
2592 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2593 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2594
2595 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302596 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2597 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002598 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002599 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002600
2601error_open:
2602 free(out);
2603 *stream_out = NULL;
2604 ALOGD("%s: exit: ret %d", __func__, ret);
2605 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002606}
2607
2608static void adev_close_output_stream(struct audio_hw_device *dev,
2609 struct audio_stream_out *stream)
2610{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002611 struct stream_out *out = (struct stream_out *)stream;
2612 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002613 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002614
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302615 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2616
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002617 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302618 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002619 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302620 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002621 if(ret != 0)
2622 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2623 __func__, ret);
2624 }
2625 else
2626 out_standby(&stream->common);
2627
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002628 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002629 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002630 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002631 if (out->compr_config.codec != NULL)
2632 free(out->compr_config.codec);
2633 }
2634 pthread_cond_destroy(&out->cond);
2635 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002636 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002637 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002638}
2639
2640static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2641{
2642 struct audio_device *adev = (struct audio_device *)dev;
2643 struct str_parms *parms;
2644 char *str;
2645 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002646 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002647 int ret = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002648
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002649 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002650 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002651
Naresh Tanniru4c630392014-05-12 01:05:52 +05302652 err = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
2653 if (err >= 0) {
2654 char *snd_card_status = value+2;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302655 if (strstr(snd_card_status, "OFFLINE")) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302656 struct listnode *node;
2657 struct audio_usecase *usecase;
2658
Naresh Tanniru4c630392014-05-12 01:05:52 +05302659 ALOGD("Received sound card OFFLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302660 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2661
2662 pthread_mutex_lock(&adev->lock);
2663 //close compress session on OFFLINE status
2664 usecase = get_usecase_from_list(adev,USECASE_AUDIO_PLAYBACK_OFFLOAD);
2665 if (usecase && usecase->stream.out) {
2666 ALOGD(" %s closing compress session on OFFLINE state", __func__);
2667
2668 struct stream_out *out = usecase->stream.out;
2669
2670 pthread_mutex_unlock(&adev->lock);
2671 out_standby(&out->stream.common);
2672 } else
2673 pthread_mutex_unlock(&adev->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302674 } else if (strstr(snd_card_status, "ONLINE")) {
2675 ALOGD("Received sound card ONLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302676 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302677 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05302678 }
2679
2680 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002681 ret = voice_set_parameters(adev, parms);
2682 if (ret != 0)
2683 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002684
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002685 ret = platform_set_parameters(adev->platform, parms);
2686 if (ret != 0)
2687 goto done;
2688
2689 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2690 if (err >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07002691 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002692 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2693 adev->bluetooth_nrec = true;
2694 else
2695 adev->bluetooth_nrec = false;
2696 }
2697
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002698 err = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2699 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002700 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2701 adev->screen_off = false;
2702 else
2703 adev->screen_off = true;
2704 }
2705
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002706 err = str_parms_get_int(parms, "rotation", &val);
2707 if (err >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002708 bool reverse_speakers = false;
2709 switch(val) {
2710 // FIXME: note that the code below assumes that the speakers are in the correct placement
2711 // relative to the user when the device is rotated 90deg from its default rotation. This
2712 // assumption is device-specific, not platform-specific like this code.
2713 case 270:
2714 reverse_speakers = true;
2715 break;
2716 case 0:
2717 case 90:
2718 case 180:
2719 break;
2720 default:
2721 ALOGE("%s: unexpected rotation of %d", __func__, val);
2722 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002723 if (adev->speaker_lr_swap != reverse_speakers) {
2724 adev->speaker_lr_swap = reverse_speakers;
2725 // only update the selected device if there is active pcm playback
2726 struct audio_usecase *usecase;
2727 struct listnode *node;
2728 list_for_each(node, &adev->usecase_list) {
2729 usecase = node_to_item(node, struct audio_usecase, list);
2730 if (usecase->type == PCM_PLAYBACK) {
2731 select_devices(adev, usecase->id);
2732 break;
2733 }
2734 }
2735 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002736 }
2737
Mingming Yin514a8bc2014-07-29 15:22:21 -07002738 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2739 if (ret >= 0) {
2740 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2741 adev->bt_wb_speech_enabled = true;
2742 else
2743 adev->bt_wb_speech_enabled = false;
2744 }
2745
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002746 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002747
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002748done:
2749 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002750 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002751 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002752 return ret;
2753}
2754
2755static char* adev_get_parameters(const struct audio_hw_device *dev,
2756 const char *keys)
2757{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002758 struct audio_device *adev = (struct audio_device *)dev;
2759 struct str_parms *reply = str_parms_create();
2760 struct str_parms *query = str_parms_create_str(keys);
2761 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05302762 char value[256] = {0};
2763 int ret = 0;
2764
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002765 if (!query || !reply) {
2766 ALOGE("adev_get_parameters: failed to create query or reply");
2767 return NULL;
2768 }
2769
Naresh Tannirud7205b62014-06-20 02:54:48 +05302770 ret = str_parms_get_str(query, "SND_CARD_STATUS", value,
2771 sizeof(value));
2772 if (ret >=0) {
2773 int val = 1;
2774 pthread_mutex_lock(&adev->snd_card_status.lock);
2775 if (SND_CARD_STATE_OFFLINE == adev->snd_card_status.state)
2776 val = 0;
2777 pthread_mutex_unlock(&adev->snd_card_status.lock);
2778 str_parms_add_int(reply, "SND_CARD_STATUS", val);
2779 goto exit;
2780 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002781
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002782 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002783 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002784 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002785 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05302786 pthread_mutex_unlock(&adev->lock);
2787
Naresh Tannirud7205b62014-06-20 02:54:48 +05302788exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002789 str = str_parms_to_str(reply);
2790 str_parms_destroy(query);
2791 str_parms_destroy(reply);
2792
2793 ALOGV("%s: exit: returns - %s", __func__, str);
2794 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002795}
2796
2797static int adev_init_check(const struct audio_hw_device *dev)
2798{
2799 return 0;
2800}
2801
2802static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2803{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002804 int ret;
2805 struct audio_device *adev = (struct audio_device *)dev;
2806 pthread_mutex_lock(&adev->lock);
2807 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002808 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002809 pthread_mutex_unlock(&adev->lock);
2810 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002811}
2812
2813static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2814{
2815 return -ENOSYS;
2816}
2817
2818static int adev_get_master_volume(struct audio_hw_device *dev,
2819 float *volume)
2820{
2821 return -ENOSYS;
2822}
2823
2824static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2825{
2826 return -ENOSYS;
2827}
2828
2829static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2830{
2831 return -ENOSYS;
2832}
2833
2834static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2835{
2836 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002837 pthread_mutex_lock(&adev->lock);
2838 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002839 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002840 adev->mode = mode;
2841 }
2842 pthread_mutex_unlock(&adev->lock);
2843 return 0;
2844}
2845
2846static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2847{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002848 int ret;
2849
2850 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002851 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002852 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2853 pthread_mutex_unlock(&adev->lock);
2854
2855 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002856}
2857
2858static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2859{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002860 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002861 return 0;
2862}
2863
2864static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2865 const struct audio_config *config)
2866{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002867 int channel_count = popcount(config->channel_mask);
2868
2869 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2870}
2871
2872static int adev_open_input_stream(struct audio_hw_device *dev,
2873 audio_io_handle_t handle,
2874 audio_devices_t devices,
2875 struct audio_config *config,
2876 struct audio_stream_in **stream_in)
2877{
2878 struct audio_device *adev = (struct audio_device *)dev;
2879 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002880 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002881 int channel_count = popcount(config->channel_mask);
2882
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302883
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002884 *stream_in = NULL;
2885 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2886 return -EINVAL;
2887
2888 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002889
2890 if (!in) {
2891 ALOGE("failed to allocate input stream");
2892 return -ENOMEM;
2893 }
2894
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302895 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
2896 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2897 devices, &in->stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002898
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07002899 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2900
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002901 in->stream.common.get_sample_rate = in_get_sample_rate;
2902 in->stream.common.set_sample_rate = in_set_sample_rate;
2903 in->stream.common.get_buffer_size = in_get_buffer_size;
2904 in->stream.common.get_channels = in_get_channels;
2905 in->stream.common.get_format = in_get_format;
2906 in->stream.common.set_format = in_set_format;
2907 in->stream.common.standby = in_standby;
2908 in->stream.common.dump = in_dump;
2909 in->stream.common.set_parameters = in_set_parameters;
2910 in->stream.common.get_parameters = in_get_parameters;
2911 in->stream.common.add_audio_effect = in_add_audio_effect;
2912 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2913 in->stream.set_gain = in_set_gain;
2914 in->stream.read = in_read;
2915 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2916
2917 in->device = devices;
2918 in->source = AUDIO_SOURCE_DEFAULT;
2919 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002920 in->standby = 1;
2921 in->channel_mask = config->channel_mask;
2922
2923 /* Update config params with the requested sample rate and channels */
2924 in->usecase = USECASE_AUDIO_RECORD;
2925 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002926 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002927 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002928
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002929 if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002930 if(audio_extn_ssr_get_enabled()) {
2931 if(audio_extn_ssr_init(adev, in)) {
2932 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2933 ret = -EINVAL;
2934 goto err_open;
2935 }
2936 } else {
2937 ret = -EINVAL;
2938 goto err_open;
2939 }
Mingming Yine62d7842013-10-25 16:26:03 -07002940 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002941 audio_extn_compr_cap_format_supported(config->format) &&
2942 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Mingming Yine62d7842013-10-25 16:26:03 -07002943 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002944 } else {
2945 in->config.channels = channel_count;
2946 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2947 buffer_size = get_input_buffer_size(config->sample_rate,
2948 config->format,
2949 channel_count);
2950 in->config.period_size = buffer_size / frame_size;
2951 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002952
2953 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002954 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002955 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002956
2957err_open:
2958 free(in);
2959 *stream_in = NULL;
2960 return ret;
2961}
2962
2963static void adev_close_input_stream(struct audio_hw_device *dev,
2964 struct audio_stream_in *stream)
2965{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002966 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002967 struct stream_in *in = (struct stream_in *)stream;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302968 struct audio_device *adev = in->dev;
2969
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302970 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002971
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002972 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302973 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002974 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302975 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002976 if (ret != 0)
2977 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2978 __func__, ret);
2979 } else
2980 in_standby(&stream->common);
2981
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002982 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2983 audio_extn_ssr_deinit();
2984 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002985 free(stream);
2986
Mingming Yine62d7842013-10-25 16:26:03 -07002987 if(audio_extn_compr_cap_enabled() &&
2988 audio_extn_compr_cap_format_supported(in->config.format))
2989 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002990 return;
2991}
2992
2993static int adev_dump(const audio_hw_device_t *device, int fd)
2994{
2995 return 0;
2996}
2997
2998static int adev_close(hw_device_t *device)
2999{
3000 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07003001
3002 if (!adev)
3003 return 0;
3004
3005 pthread_mutex_lock(&adev_init_lock);
3006
3007 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08003008 audio_extn_listen_deinit(adev);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003009 audio_extn_utils_release_streams_output_cfg_list(&adev->streams_output_cfg_list);
Kiran Kandi910e1862013-10-29 13:29:42 -07003010 audio_route_free(adev->audio_route);
3011 free(adev->snd_dev_ref_cnt);
3012 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07003013 free(device);
3014 adev = NULL;
3015 }
3016 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003017 return 0;
3018}
3019
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003020static int adev_open(const hw_module_t *module, const char *name,
3021 hw_device_t **device)
3022{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003023 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003024
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003025 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003026 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3027
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003028 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07003029 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003030 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07003031 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003032 ALOGD("%s: returning existing instance of adev", __func__);
3033 ALOGD("%s: exit", __func__);
3034 pthread_mutex_unlock(&adev_init_lock);
3035 return 0;
3036 }
3037
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003038 adev = calloc(1, sizeof(struct audio_device));
3039
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003040 if (!adev) {
3041 pthread_mutex_unlock(&adev_init_lock);
3042 return -ENOMEM;
3043 }
3044
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07003045 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3046
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003047 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3048 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3049 adev->device.common.module = (struct hw_module_t *)module;
3050 adev->device.common.close = adev_close;
3051
3052 adev->device.init_check = adev_init_check;
3053 adev->device.set_voice_volume = adev_set_voice_volume;
3054 adev->device.set_master_volume = adev_set_master_volume;
3055 adev->device.get_master_volume = adev_get_master_volume;
3056 adev->device.set_master_mute = adev_set_master_mute;
3057 adev->device.get_master_mute = adev_get_master_mute;
3058 adev->device.set_mode = adev_set_mode;
3059 adev->device.set_mic_mute = adev_set_mic_mute;
3060 adev->device.get_mic_mute = adev_get_mic_mute;
3061 adev->device.set_parameters = adev_set_parameters;
3062 adev->device.get_parameters = adev_get_parameters;
3063 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3064 adev->device.open_output_stream = adev_open_output_stream;
3065 adev->device.close_output_stream = adev_close_output_stream;
3066 adev->device.open_input_stream = adev_open_input_stream;
3067 adev->device.close_input_stream = adev_close_input_stream;
3068 adev->device.dump = adev_dump;
3069
3070 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003071 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003072 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003073 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003074 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003075 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003076 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003077 /* adev->cur_hdmi_channels = 0; by calloc() */
Mingming Yin3ee55c62014-08-04 14:23:35 -07003078 adev->cur_codec_backend_samplerate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
3079 adev->cur_codec_backend_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Eric Laurentb23d5282013-05-14 15:27:20 -07003080 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003081 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003082 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08003083 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003084 adev->offload_usecases_state = 0;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303085
3086 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
3087 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003088 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003089 adev->platform = platform_init(adev);
3090 if (!adev->platform) {
3091 free(adev->snd_dev_ref_cnt);
3092 free(adev);
3093 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3094 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08003095 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003096 return -EINVAL;
3097 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003098
Naresh Tanniru4c630392014-05-12 01:05:52 +05303099 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
3100
Eric Laurentc4aef752013-09-12 17:45:53 -07003101 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
3102 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3103 if (adev->visualizer_lib == NULL) {
3104 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3105 } else {
3106 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3107 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003108 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003109 "visualizer_hal_start_output");
3110 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003111 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003112 "visualizer_hal_stop_output");
3113 }
3114 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003115 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07003116
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003117 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
3118 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3119 if (adev->offload_effects_lib == NULL) {
3120 ALOGE("%s: DLOPEN failed for %s", __func__,
3121 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3122 } else {
3123 ALOGV("%s: DLOPEN successful for %s", __func__,
3124 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3125 adev->offload_effects_start_output =
3126 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3127 "offload_effects_bundle_hal_start_output");
3128 adev->offload_effects_stop_output =
3129 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3130 "offload_effects_bundle_hal_stop_output");
3131 }
3132 }
3133
Mingming Yin514a8bc2014-07-29 15:22:21 -07003134 adev->bt_wb_speech_enabled = false;
3135
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003136 *device = &adev->device.common;
3137
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003138 audio_extn_utils_update_streams_output_cfg_list(adev->platform, adev->mixer,
3139 &adev->streams_output_cfg_list);
3140
Kiran Kandi910e1862013-10-29 13:29:42 -07003141 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003142 pthread_mutex_unlock(&adev_init_lock);
3143
Eric Laurent994a6932013-07-17 11:51:42 -07003144 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003145 return 0;
3146}
3147
Mingming Yin07972cc2014-06-06 17:11:23 -07003148int pcm_ioctl(struct pcm *pcm, int request, ...)
3149{
3150 va_list ap;
3151 void * arg;
3152 int pcm_fd = *(int*)pcm;
3153
3154 va_start(ap, request);
3155 arg = va_arg(ap, void *);
3156 va_end(ap);
3157
3158 return ioctl(pcm_fd, request, arg);
3159}
3160
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003161static struct hw_module_methods_t hal_module_methods = {
3162 .open = adev_open,
3163};
3164
3165struct audio_module HAL_MODULE_INFO_SYM = {
3166 .common = {
3167 .tag = HARDWARE_MODULE_TAG,
3168 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3169 .hal_api_version = HARDWARE_HAL_API_VERSION,
3170 .id = AUDIO_HARDWARE_MODULE_ID,
3171 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08003172 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003173 .methods = &hal_module_methods,
3174 },
3175};