blob: 627900bb6cde272ad49bcd05ff53b851e62fb075 [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 ||
Ashish Jainf9b78162014-08-25 20:36:25 +0530210 format == AUDIO_FORMAT_AAC_LC ||
211 format == AUDIO_FORMAT_AAC_HE_V1 ||
212 format == AUDIO_FORMAT_AAC_HE_V2 ||
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800213 format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD ||
Mingming Yin3ee55c62014-08-04 14:23:35 -0700214 format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD ||
215 format == AUDIO_FORMAT_FLAC)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800216 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700217
218 return false;
219}
220
221static int get_snd_codec_id(audio_format_t format)
222{
223 int id = 0;
224
Ashish Jainf9b78162014-08-25 20:36:25 +0530225 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700226 case AUDIO_FORMAT_MP3:
227 id = SND_AUDIOCODEC_MP3;
228 break;
229 case AUDIO_FORMAT_AAC:
230 id = SND_AUDIOCODEC_AAC;
231 break;
Ashish Jainf9b78162014-08-25 20:36:25 +0530232 case AUDIO_FORMAT_PCM_OFFLOAD:
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800233 id = SND_AUDIOCODEC_PCM;
234 break;
Mingming Yin3ee55c62014-08-04 14:23:35 -0700235 case AUDIO_FORMAT_FLAC:
236 id = SND_AUDIOCODEC_FLAC;
237 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700238 default:
Mingming Yin90310102013-11-13 16:57:00 -0800239 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700240 }
241
242 return id;
243}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800244
Venkata Narendra Kumar Guttaed0f94f2014-07-09 16:29:28 +0530245int get_snd_card_state(struct audio_device *adev)
Naresh Tanniru80659832014-06-04 18:17:56 +0530246{
247 int snd_scard_state;
248
249 if (!adev)
250 return SND_CARD_STATE_OFFLINE;
251
252 pthread_mutex_lock(&adev->snd_card_status.lock);
253 snd_scard_state = adev->snd_card_status.state;
254 pthread_mutex_unlock(&adev->snd_card_status.lock);
255
256 return snd_scard_state;
257}
258
259static int set_snd_card_state(struct audio_device *adev, int snd_scard_state)
260{
261 if (!adev)
262 return -ENOSYS;
263
264 pthread_mutex_lock(&adev->snd_card_status.lock);
265 adev->snd_card_status.state = snd_scard_state;
266 pthread_mutex_unlock(&adev->snd_card_status.lock);
267
268 return 0;
269}
270
Avinash Vaish71a8b972014-07-24 15:36:33 +0530271static int enable_audio_route_for_voice_usecases(struct audio_device *adev,
272 struct audio_usecase *uc_info)
273{
274 struct listnode *node;
275 struct audio_usecase *usecase;
276
277 if (uc_info == NULL)
278 return -EINVAL;
279
280 /* Re-route all voice usecases on the shared backend other than the
281 specified usecase to new snd devices */
282 list_for_each(node, &adev->usecase_list) {
283 usecase = node_to_item(node, struct audio_usecase, list);
284 if ((usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) &&
285 (usecase != uc_info))
286 enable_audio_route(adev, usecase);
287 }
288 return 0;
289}
290
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700291int enable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700292 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800293{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700294 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700295 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800296
297 if (usecase == NULL)
298 return -EINVAL;
299
300 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
301
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800302 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700303 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800304 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700305 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800306
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -0800307#ifdef DS1_DOLBY_DAP_ENABLED
308 audio_extn_dolby_set_dmid(adev);
309 audio_extn_dolby_set_endpoint(adev);
310#endif
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700311 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530312 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700313 audio_extn_utils_send_audio_calibration(adev, usecase);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -0700314 audio_extn_utils_send_app_type_cfg(usecase);
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: apply mixer and update path: %s", __func__, mixer_path);
318 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800319 ALOGV("%s: exit", __func__);
320 return 0;
321}
322
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700323int disable_audio_route(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700324 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800325{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700326 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700327 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800328
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +0530329 if (usecase == NULL || usecase->id == USECASE_INVALID)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800330 return -EINVAL;
331
332 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700333 if (usecase->type == PCM_CAPTURE)
334 snd_device = usecase->in_snd_device;
335 else
336 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800337 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700338 platform_add_backend_name(mixer_path, snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700339 ALOGV("%s: reset and update mixer path: %s", __func__, mixer_path);
340 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700341 audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530342 audio_extn_listen_update_stream_status(usecase, LISTEN_EVENT_STREAM_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800343 ALOGV("%s: exit", __func__);
344 return 0;
345}
346
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700347int enable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700348 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800349{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700350 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
351
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800352 if (snd_device < SND_DEVICE_MIN ||
353 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800354 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800355 return -EINVAL;
356 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700357
358 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700359
360 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
361 ALOGE("%s: Invalid sound device returned", __func__);
362 return -EINVAL;
363 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700364 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700365 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700366 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700367 return 0;
368 }
369
Gopikrishnaiah Anandane85d0462014-06-30 21:41:20 -0700370 if (audio_extn_spkr_prot_is_enabled())
371 audio_extn_spkr_prot_calib_cancel(adev);
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700372 /* start usb playback thread */
373 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
374 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
375 audio_extn_usb_start_playback(adev);
376
377 /* start usb capture thread */
378 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
379 audio_extn_usb_start_capture(adev);
380
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800381 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
382 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700383 audio_extn_spkr_prot_is_enabled()) {
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700384 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
385 adev->snd_dev_ref_cnt[snd_device]--;
386 return -EINVAL;
387 }
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800388 if (audio_extn_spkr_prot_start_processing(snd_device)) {
389 ALOGE("%s: spkr_start_processing failed", __func__);
390 return -EINVAL;
391 }
392 } else {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700393 ALOGV("%s: snd_device(%d: %s)", __func__,
394 snd_device, device_name);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -0700395 /* due to the possibility of calibration overwrite between listen
396 and audio, notify listen hal before audio calibration is sent */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700397 audio_extn_sound_trigger_update_device_status(snd_device,
398 ST_EVENT_SND_DEVICE_BUSY);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530399 audio_extn_listen_update_device_status(snd_device,
400 LISTEN_EVENT_SND_DEVICE_BUSY);
Subhash Chandra Bose Naripeddy54274672014-03-10 14:51:02 -0700401 if (platform_get_snd_device_acdb_id(snd_device) < 0) {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700402 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700403 audio_extn_sound_trigger_update_device_status(snd_device,
404 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530405 audio_extn_listen_update_device_status(snd_device,
406 LISTEN_EVENT_SND_DEVICE_FREE);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700407 return -EINVAL;
408 }
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300409 audio_extn_dev_arbi_acquire(snd_device);
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700410 audio_route_apply_and_update_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800411 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800412 return 0;
413}
414
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700415int disable_snd_device(struct audio_device *adev,
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700416 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800417{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700418 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
419
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800420 if (snd_device < SND_DEVICE_MIN ||
421 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800422 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800423 return -EINVAL;
424 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700425 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
426 ALOGE("%s: device ref cnt is already 0", __func__);
427 return -EINVAL;
428 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700429
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700430 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700431
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700432 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
433 ALOGE("%s: Invalid sound device returned", __func__);
434 return -EINVAL;
435 }
436
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700437 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700438 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700439 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800440 /* exit usb play back thread */
441 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
442 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
443 audio_extn_usb_stop_playback();
444
445 /* exit usb capture thread */
446 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -0700447 audio_extn_usb_stop_capture();
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800448
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800449 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
450 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700451 audio_extn_spkr_prot_is_enabled()) {
Anish Kumar46c7b872014-09-09 01:49:44 -0700452 audio_extn_spkr_prot_stop_processing(snd_device);
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300453 } else {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700454 audio_route_reset_and_update_path(adev->audio_route, device_name);
Lior Barenboim0b61bc72014-05-13 13:01:37 +0300455 audio_extn_dev_arbi_release(snd_device);
456 }
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -0700457
458 audio_extn_sound_trigger_update_device_status(snd_device,
459 ST_EVENT_SND_DEVICE_FREE);
Dhananjay Kumar45b71742014-05-29 21:47:27 +0530460 audio_extn_listen_update_device_status(snd_device,
Kiran Kandide144c82013-11-20 15:58:32 -0800461 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700462 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700463
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800464 return 0;
465}
466
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700467static void check_usecases_codec_backend(struct audio_device *adev,
468 struct audio_usecase *uc_info,
469 snd_device_t snd_device)
470{
471 struct listnode *node;
472 struct audio_usecase *usecase;
473 bool switch_device[AUDIO_USECASE_MAX];
474 int i, num_uc_to_switch = 0;
475
476 /*
477 * This function is to make sure that all the usecases that are active on
478 * the hardware codec backend are always routed to any one device that is
479 * handled by the hardware codec.
480 * For example, if low-latency and deep-buffer usecases are currently active
481 * on speaker and out_set_parameters(headset) is received on low-latency
482 * output, then we have to make sure deep-buffer is also switched to headset,
483 * because of the limitation that both the devices cannot be enabled
484 * at the same time as they share the same backend.
485 */
Mingming Yin3ee55c62014-08-04 14:23:35 -0700486 /*
487 * This call is to check if we need to force routing for a particular stream
488 * If there is a backend configuration change for the device when a
489 * new stream starts, then ADM needs to be closed and re-opened with the new
490 * configuraion. This call check if we need to re-route all the streams
491 * associated with the backend. Touch tone + 24 bit playback.
492 */
493 bool force_routing = platform_check_and_set_codec_backend_cfg(adev, uc_info);
494
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700495 /* Disable all the usecases on the shared backend other than the
496 specified usecase */
497 for (i = 0; i < AUDIO_USECASE_MAX; i++)
498 switch_device[i] = false;
499
500 list_for_each(node, &adev->usecase_list) {
501 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800502 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700503 usecase != uc_info &&
Mingming Yin3ee55c62014-08-04 14:23:35 -0700504 (usecase->out_snd_device != snd_device || force_routing) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700505 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
506 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
507 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700508 platform_get_snd_device_name(usecase->out_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700509 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700510 switch_device[usecase->id] = true;
511 num_uc_to_switch++;
512 }
513 }
514
515 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700516 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700517
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530518 /* Make sure the previous devices to be disabled first and then enable the
519 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700520 list_for_each(node, &adev->usecase_list) {
521 usecase = node_to_item(node, struct audio_usecase, list);
522 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700523 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700524 }
525 }
526
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700527 list_for_each(node, &adev->usecase_list) {
528 usecase = node_to_item(node, struct audio_usecase, list);
529 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700530 enable_snd_device(adev, snd_device);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700531 }
532 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700533
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700534 /* Re-route all the usecases on the shared backend other than the
535 specified usecase to new snd devices */
536 list_for_each(node, &adev->usecase_list) {
537 usecase = node_to_item(node, struct audio_usecase, list);
538 /* Update the out_snd_device only before enabling the audio route */
539 if (switch_device[usecase->id] ) {
540 usecase->out_snd_device = snd_device;
Avinash Vaish71a8b972014-07-24 15:36:33 +0530541 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
542 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700543 }
544 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700545 }
546}
547
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700548static void check_and_route_capture_usecases(struct audio_device *adev,
549 struct audio_usecase *uc_info,
550 snd_device_t snd_device)
551{
552 struct listnode *node;
553 struct audio_usecase *usecase;
554 bool switch_device[AUDIO_USECASE_MAX];
555 int i, num_uc_to_switch = 0;
556
557 /*
558 * This function is to make sure that all the active capture usecases
559 * are always routed to the same input sound device.
560 * For example, if audio-record and voice-call usecases are currently
561 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
562 * is received for voice call then we have to make sure that audio-record
563 * usecase is also switched to earpiece i.e. voice-dmic-ef,
564 * because of the limitation that two devices cannot be enabled
565 * at the same time if they share the same backend.
566 */
567 for (i = 0; i < AUDIO_USECASE_MAX; i++)
568 switch_device[i] = false;
569
570 list_for_each(node, &adev->usecase_list) {
571 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800572 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700573 usecase != uc_info &&
574 usecase->in_snd_device != snd_device) {
575 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
576 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700577 platform_get_snd_device_name(usecase->in_snd_device));
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700578 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700579 switch_device[usecase->id] = true;
580 num_uc_to_switch++;
581 }
582 }
583
584 if (num_uc_to_switch) {
Haynes Mathew Georgeef1e3d32014-04-24 11:53:44 -0700585 /* All streams have been de-routed. Disable the device */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700586
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530587 /* Make sure the previous devices to be disabled first and then enable the
588 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700589 list_for_each(node, &adev->usecase_list) {
590 usecase = node_to_item(node, struct audio_usecase, list);
591 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700592 disable_snd_device(adev, usecase->in_snd_device);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800593 }
594 }
595
596 list_for_each(node, &adev->usecase_list) {
597 usecase = node_to_item(node, struct audio_usecase, list);
598 if (switch_device[usecase->id]) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700599 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700600 }
601 }
602
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700603 /* Re-route all the usecases on the shared backend other than the
604 specified usecase to new snd devices */
605 list_for_each(node, &adev->usecase_list) {
606 usecase = node_to_item(node, struct audio_usecase, list);
607 /* Update the in_snd_device only before enabling the audio route */
608 if (switch_device[usecase->id] ) {
609 usecase->in_snd_device = snd_device;
Avinash Vaish71a8b972014-07-24 15:36:33 +0530610 if (usecase->type != VOICE_CALL && usecase->type != VOIP_CALL)
611 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700612 }
613 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700614 }
615}
616
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800617/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700618static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800619{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700620 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700621 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800622
623 switch (channels) {
624 /*
625 * Do not handle stereo output in Multi-channel cases
626 * Stereo case is handled in normal playback path
627 */
628 case 6:
629 ALOGV("%s: HDMI supports 5.1", __func__);
630 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
631 break;
632 case 8:
633 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
634 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
635 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
636 break;
637 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700638 ALOGE("HDMI does not support multi channel playback");
639 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800640 break;
641 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700642 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800643}
644
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700645static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
646{
647 struct audio_usecase *usecase;
648 struct listnode *node;
649
650 list_for_each(node, &adev->usecase_list) {
651 usecase = node_to_item(node, struct audio_usecase, list);
652 if (usecase->type == VOICE_CALL) {
653 ALOGV("%s: usecase id %d", __func__, usecase->id);
654 return usecase->id;
655 }
656 }
657 return USECASE_INVALID;
658}
659
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700660struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700661 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700662{
663 struct audio_usecase *usecase;
664 struct listnode *node;
665
666 list_for_each(node, &adev->usecase_list) {
667 usecase = node_to_item(node, struct audio_usecase, list);
668 if (usecase->id == uc_id)
669 return usecase;
670 }
671 return NULL;
672}
673
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700674int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800675{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800676 snd_device_t out_snd_device = SND_DEVICE_NONE;
677 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700678 struct audio_usecase *usecase = NULL;
679 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800680 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800681 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800682 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800683 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700684 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800685
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700686 usecase = get_usecase_from_list(adev, uc_id);
687 if (usecase == NULL) {
688 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
689 return -EINVAL;
690 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800692 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800693 (usecase->type == VOIP_CALL) ||
694 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700695 out_snd_device = platform_get_output_snd_device(adev->platform,
696 usecase->stream.out->devices);
697 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700698 usecase->devices = usecase->stream.out->devices;
699 } else {
700 /*
701 * If the voice call is active, use the sound devices of voice call usecase
702 * so that it would not result any device switch. All the usecases will
703 * be switched to new device when select_devices() is called for voice call
704 * usecase. This is to avoid switching devices for voice call when
705 * check_usecases_codec_backend() is called below.
706 */
Vidyakumar Athotaad34d572014-08-05 18:20:42 -0700707 if (adev->voice.in_call && adev->mode == AUDIO_MODE_IN_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700708 vc_usecase = get_usecase_from_list(adev,
709 get_voice_usecase_id_from_list(adev));
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700710 if ((vc_usecase) && ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
711 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700712 in_snd_device = vc_usecase->in_snd_device;
713 out_snd_device = vc_usecase->out_snd_device;
714 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800715 } else if (voice_extn_compress_voip_is_active(adev)) {
716 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700717 if ((voip_usecase) && ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Avinash Vaish4d6167d2014-06-25 12:20:37 +0530718 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700719 (voip_usecase->stream.out != adev->primary_output))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800720 in_snd_device = voip_usecase->in_snd_device;
721 out_snd_device = voip_usecase->out_snd_device;
722 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800723 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800724 hfp_ucid = audio_extn_hfp_get_usecase();
725 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Mingming Yin2d8aa2e2014-08-14 00:00:51 -0700726 if ((hfp_usecase) && (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800727 in_snd_device = hfp_usecase->in_snd_device;
728 out_snd_device = hfp_usecase->out_snd_device;
729 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700730 }
731 if (usecase->type == PCM_PLAYBACK) {
732 usecase->devices = usecase->stream.out->devices;
733 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700734 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700735 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700736 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700737 if (usecase->stream.out == adev->primary_output &&
738 adev->active_input &&
739 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
740 select_devices(adev, adev->active_input->usecase);
741 }
742 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700743 } else if (usecase->type == PCM_CAPTURE) {
744 usecase->devices = usecase->stream.in->device;
745 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700746 if (in_snd_device == SND_DEVICE_NONE) {
747 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
748 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700749 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700750 adev->primary_output->devices);
751 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700752 in_snd_device = platform_get_input_snd_device(adev->platform,
753 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700754 }
755 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700756 }
757 }
758
759 if (out_snd_device == usecase->out_snd_device &&
760 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800761 return 0;
762 }
763
sangwoobc677242013-08-08 16:53:43 +0900764 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700765 out_snd_device, platform_get_snd_device_name(out_snd_device),
766 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800767
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800768 /*
769 * Limitation: While in call, to do a device switch we need to disable
770 * and enable both RX and TX devices though one of them is same as current
771 * device.
772 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700773 if ((usecase->type == VOICE_CALL) &&
774 (usecase->in_snd_device != SND_DEVICE_NONE) &&
775 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700776 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800777 }
778
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700779 /* Disable current sound devices */
780 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700781 disable_audio_route(adev, usecase);
782 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800783 }
784
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700785 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700786 disable_audio_route(adev, usecase);
787 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800788 }
789
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800790 /* Applicable only on the targets that has external modem.
791 * New device information should be sent to modem before enabling
792 * the devices to reduce in-call device switch time.
793 */
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700794 if ((usecase->type == VOICE_CALL) &&
795 (usecase->in_snd_device != SND_DEVICE_NONE) &&
796 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800797 status = platform_switch_voice_call_enable_device_config(adev->platform,
798 out_snd_device,
799 in_snd_device);
Vidyakumar Athota21b3bb92014-04-25 11:08:08 -0700800 }
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800801
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700802 /* Enable new sound devices */
803 if (out_snd_device != SND_DEVICE_NONE) {
804 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
805 check_usecases_codec_backend(adev, usecase, out_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700806 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800807 }
808
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700809 if (in_snd_device != SND_DEVICE_NONE) {
810 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700811 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700812 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700813
Avinash Vaish71a8b972014-07-24 15:36:33 +0530814 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700815 status = platform_switch_voice_call_device_post(adev->platform,
816 out_snd_device,
817 in_snd_device);
Avinash Vaish71a8b972014-07-24 15:36:33 +0530818 enable_audio_route_for_voice_usecases(adev, usecase);
819 }
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800820
sangwoo170731f2013-06-08 15:36:36 +0900821 usecase->in_snd_device = in_snd_device;
822 usecase->out_snd_device = out_snd_device;
823
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530824 if (usecase->type == PCM_PLAYBACK) {
825 if ((24 == usecase->stream.out->bit_width) &&
826 (AUDIO_DEVICE_OUT_SPEAKER == usecase->stream.out->devices)) {
827 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
828 &adev->streams_output_cfg_list,
829 usecase->stream.out->flags,
830 usecase->stream.out->format,
831 DEFAULT_OUTPUT_SAMPLING_RATE,
832 usecase->stream.out->bit_width,
833 &usecase->stream.out->app_type_cfg);
834 } else {
835 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
836 &adev->streams_output_cfg_list,
837 usecase->stream.out->flags,
838 usecase->stream.out->format,
839 usecase->stream.out->sample_rate,
840 usecase->stream.out->bit_width,
841 &usecase->stream.out->app_type_cfg);
842 }
843 ALOGI("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
844 }
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700845 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900846
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800847 /* Applicable only on the targets that has external modem.
848 * Enable device command should be sent to modem only after
849 * enabling voice call mixer controls
850 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -0700851 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800852 status = platform_switch_voice_call_usecase_route_post(adev->platform,
853 out_snd_device,
854 in_snd_device);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530855 ALOGD("%s: done",__func__);
856
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800857 return status;
858}
859
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800860static int stop_input_stream(struct stream_in *in)
861{
862 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800863 struct audio_usecase *uc_info;
864 struct audio_device *adev = in->dev;
865
Eric Laurentc8400632013-02-14 19:04:54 -0800866 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800867
Eric Laurent994a6932013-07-17 11:51:42 -0700868 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700869 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800870 uc_info = get_usecase_from_list(adev, in->usecase);
871 if (uc_info == NULL) {
872 ALOGE("%s: Could not find the usecase (%d) in the list",
873 __func__, in->usecase);
874 return -EINVAL;
875 }
876
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800877 /* Close in-call recording streams */
878 voice_check_and_stop_incall_rec_usecase(adev, in);
879
Eric Laurent150dbfe2013-02-27 14:31:02 -0800880 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700881 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700882
883 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700884 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800885
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800886 list_remove(&uc_info->list);
887 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800888
Eric Laurent994a6932013-07-17 11:51:42 -0700889 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800890 return ret;
891}
892
893int start_input_stream(struct stream_in *in)
894{
895 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800896 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800897 struct audio_usecase *uc_info;
898 struct audio_device *adev = in->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +0530899 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800900
Mingming Yine62d7842013-10-25 16:26:03 -0700901 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530902 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
903 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700904
Naresh Tanniru80659832014-06-04 18:17:56 +0530905
906 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +0530907 ALOGE("%s: sound card is not active/SSR returning error", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +0530908 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +0530909 goto error_config;
910 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530911
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700912 /* Check if source matches incall recording usecase criteria */
913 ret = voice_check_and_set_incall_rec_usecase(adev, in);
914 if (ret)
915 goto error_config;
916 else
917 ALOGV("%s: usecase(%d)", __func__, in->usecase);
918
Eric Laurentb23d5282013-05-14 15:27:20 -0700919 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800920 if (in->pcm_device_id < 0) {
921 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
922 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800923 ret = -EINVAL;
924 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800925 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700926
927 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800928 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700929
930 if (!uc_info) {
931 ret = -ENOMEM;
932 goto error_config;
933 }
934
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800935 uc_info->id = in->usecase;
936 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800937 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700938 uc_info->devices = in->device;
939 uc_info->in_snd_device = SND_DEVICE_NONE;
940 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800941
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800942 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700943 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800944
Eric Laurentc8400632013-02-14 19:04:54 -0800945 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800946 __func__, adev->snd_card,
947 in->pcm_device_id, in->config.channels);
948 in->pcm = pcm_open(adev->snd_card,
949 in->pcm_device_id, PCM_IN, &in->config);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800950 if (in->pcm && !pcm_is_ready(in->pcm)) {
951 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
952 pcm_close(in->pcm);
953 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800954 ret = -EIO;
955 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800956 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530957
Eric Laurent994a6932013-07-17 11:51:42 -0700958 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800959 return ret;
960
961error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800962 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800963
964error_config:
965 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700966 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800967
968 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800969}
970
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700971/* must be called with out->lock locked */
972static int send_offload_cmd_l(struct stream_out* out, int command)
973{
974 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
975
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700976 if (!cmd) {
977 ALOGE("failed to allocate mem for command 0x%x", command);
978 return -ENOMEM;
979 }
980
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700981 ALOGVV("%s %d", __func__, command);
982
983 cmd->cmd = command;
984 list_add_tail(&out->offload_cmd_list, &cmd->node);
985 pthread_cond_signal(&out->offload_cond);
986 return 0;
987}
988
989/* must be called iwth out->lock locked */
990static void stop_compressed_output_l(struct stream_out *out)
991{
992 out->offload_state = OFFLOAD_STATE_IDLE;
993 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700994 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700995 if (out->compr != NULL) {
996 compress_stop(out->compr);
997 while (out->offload_thread_blocked) {
998 pthread_cond_wait(&out->cond, &out->lock);
999 }
1000 }
1001}
1002
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001003bool is_offload_usecase(audio_usecase_t uc_id)
1004{
1005 unsigned int i;
1006 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1007 if (uc_id == offload_usecases[i])
1008 return true;
1009 }
1010 return false;
1011}
1012
1013static audio_usecase_t get_offload_usecase(struct audio_device *adev)
1014{
1015 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1016 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
1017 char value[PROPERTY_VALUE_MAX] = {0};
1018
1019 property_get("audio.offload.multiple.enabled", value, NULL);
1020 if (!(atoi(value) || !strncmp("true", value, 4)))
1021 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
1022
1023 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
1024 for (i = 0; i < num_usecase; i++) {
1025 if (!(adev->offload_usecases_state & (0x1<<i))) {
1026 adev->offload_usecases_state |= 0x1 << i;
1027 ret = offload_usecases[i];
1028 break;
1029 }
1030 }
1031 ALOGV("%s: offload usecase is %d", __func__, ret);
1032 return ret;
1033}
1034
1035static void free_offload_usecase(struct audio_device *adev,
1036 audio_usecase_t uc_id)
1037{
1038 unsigned int i;
1039 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1040 if (offload_usecases[i] == uc_id) {
1041 adev->offload_usecases_state &= ~(0x1<<i);
1042 break;
1043 }
1044 }
1045 ALOGV("%s: free offload usecase %d", __func__, uc_id);
1046}
1047
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001048static void *offload_thread_loop(void *context)
1049{
1050 struct stream_out *out = (struct stream_out *) context;
1051 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001052 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001053
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001054 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1055 set_sched_policy(0, SP_FOREGROUND);
1056 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1057
1058 ALOGV("%s", __func__);
1059 pthread_mutex_lock(&out->lock);
1060 for (;;) {
1061 struct offload_cmd *cmd = NULL;
1062 stream_callback_event_t event;
1063 bool send_callback = false;
1064
1065 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1066 __func__, list_empty(&out->offload_cmd_list),
1067 out->offload_state);
1068 if (list_empty(&out->offload_cmd_list)) {
1069 ALOGV("%s SLEEPING", __func__);
1070 pthread_cond_wait(&out->offload_cond, &out->lock);
1071 ALOGV("%s RUNNING", __func__);
1072 continue;
1073 }
1074
1075 item = list_head(&out->offload_cmd_list);
1076 cmd = node_to_item(item, struct offload_cmd, node);
1077 list_remove(item);
1078
1079 ALOGVV("%s STATE %d CMD %d out->compr %p",
1080 __func__, out->offload_state, cmd->cmd, out->compr);
1081
1082 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1083 free(cmd);
1084 break;
1085 }
1086
1087 if (out->compr == NULL) {
1088 ALOGE("%s: Compress handle is NULL", __func__);
1089 pthread_cond_signal(&out->cond);
1090 continue;
1091 }
1092 out->offload_thread_blocked = true;
1093 pthread_mutex_unlock(&out->lock);
1094 send_callback = false;
1095 switch(cmd->cmd) {
1096 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001097 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001098 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001099 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001100 send_callback = true;
1101 event = STREAM_CBK_EVENT_WRITE_READY;
1102 break;
1103 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001104 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301105 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001106 ALOGD("copl(%p):calling compress_partial_drain", out);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001107 compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001108 ALOGD("copl(%p):out of compress_partial_drain", out);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301109 }
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001110 else if(ret == -ETIMEDOUT)
1111 compress_drain(out->compr);
1112 else
1113 ALOGE("%s: Next track returned error %d",__func__, ret);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001114 send_callback = true;
1115 event = STREAM_CBK_EVENT_DRAIN_READY;
1116 break;
1117 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001118 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001119 compress_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001120 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001121 send_callback = true;
1122 event = STREAM_CBK_EVENT_DRAIN_READY;
1123 break;
1124 default:
1125 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1126 break;
1127 }
1128 pthread_mutex_lock(&out->lock);
1129 out->offload_thread_blocked = false;
1130 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001131 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001132 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001133 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001134 free(cmd);
1135 }
1136
1137 pthread_cond_signal(&out->cond);
1138 while (!list_empty(&out->offload_cmd_list)) {
1139 item = list_head(&out->offload_cmd_list);
1140 list_remove(item);
1141 free(node_to_item(item, struct offload_cmd, node));
1142 }
1143 pthread_mutex_unlock(&out->lock);
1144
1145 return NULL;
1146}
1147
1148static int create_offload_callback_thread(struct stream_out *out)
1149{
1150 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1151 list_init(&out->offload_cmd_list);
1152 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1153 offload_thread_loop, out);
1154 return 0;
1155}
1156
1157static int destroy_offload_callback_thread(struct stream_out *out)
1158{
1159 pthread_mutex_lock(&out->lock);
1160 stop_compressed_output_l(out);
1161 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1162
1163 pthread_mutex_unlock(&out->lock);
1164 pthread_join(out->offload_thread, (void **) NULL);
1165 pthread_cond_destroy(&out->offload_cond);
1166
1167 return 0;
1168}
1169
Eric Laurent07eeafd2013-10-06 12:52:49 -07001170static bool allow_hdmi_channel_config(struct audio_device *adev)
1171{
1172 struct listnode *node;
1173 struct audio_usecase *usecase;
1174 bool ret = true;
1175
1176 list_for_each(node, &adev->usecase_list) {
1177 usecase = node_to_item(node, struct audio_usecase, list);
1178 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1179 /*
1180 * If voice call is already existing, do not proceed further to avoid
1181 * disabling/enabling both RX and TX devices, CSD calls, etc.
1182 * Once the voice call done, the HDMI channels can be configured to
1183 * max channels of remaining use cases.
1184 */
1185 if (usecase->id == USECASE_VOICE_CALL) {
1186 ALOGD("%s: voice call is active, no change in HDMI channels",
1187 __func__);
1188 ret = false;
1189 break;
1190 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1191 ALOGD("%s: multi channel playback is active, "
1192 "no change in HDMI channels", __func__);
1193 ret = false;
1194 break;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001195 } else if (is_offload_usecase(usecase->id) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07001196 audio_channel_count_from_out_mask(usecase->stream.out->channel_mask) > 2) {
Mingming Yin139f1072014-02-24 17:56:01 -08001197 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1198 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1199 ret = false;
1200 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001201 }
1202 }
1203 }
1204 return ret;
1205}
1206
1207static int check_and_set_hdmi_channels(struct audio_device *adev,
1208 unsigned int channels)
1209{
1210 struct listnode *node;
1211 struct audio_usecase *usecase;
1212
1213 /* Check if change in HDMI channel config is allowed */
1214 if (!allow_hdmi_channel_config(adev))
1215 return 0;
1216
1217 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001218 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001219 return 0;
1220 }
1221
1222 platform_set_hdmi_channels(adev->platform, channels);
1223 adev->cur_hdmi_channels = channels;
1224
1225 /*
1226 * Deroute all the playback streams routed to HDMI so that
1227 * the back end is deactivated. Note that backend will not
1228 * be deactivated if any one stream is connected to it.
1229 */
1230 list_for_each(node, &adev->usecase_list) {
1231 usecase = node_to_item(node, struct audio_usecase, list);
1232 if (usecase->type == PCM_PLAYBACK &&
1233 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001234 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001235 }
1236 }
1237
1238 /*
1239 * Enable all the streams disabled above. Now the HDMI backend
1240 * will be activated with new channel configuration
1241 */
1242 list_for_each(node, &adev->usecase_list) {
1243 usecase = node_to_item(node, struct audio_usecase, list);
1244 if (usecase->type == PCM_PLAYBACK &&
1245 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001246 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001247 }
1248 }
1249
1250 return 0;
1251}
1252
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001253static int stop_output_stream(struct stream_out *out)
1254{
1255 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001256 struct audio_usecase *uc_info;
1257 struct audio_device *adev = out->dev;
1258
Eric Laurent994a6932013-07-17 11:51:42 -07001259 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001260 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001261 uc_info = get_usecase_from_list(adev, out->usecase);
1262 if (uc_info == NULL) {
1263 ALOGE("%s: Could not find the usecase (%d) in the list",
1264 __func__, out->usecase);
1265 return -EINVAL;
1266 }
1267
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001268 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001269 if (adev->visualizer_stop_output != NULL)
1270 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1271 if (adev->offload_effects_stop_output != NULL)
1272 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1273 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001274
Eric Laurent150dbfe2013-02-27 14:31:02 -08001275 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001276 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001277
1278 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001279 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001280
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001281 list_remove(&uc_info->list);
1282 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001283
Eric Laurent07eeafd2013-10-06 12:52:49 -07001284 /* Must be called after removing the usecase from list */
1285 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1286 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1287
Eric Laurent994a6932013-07-17 11:51:42 -07001288 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001289 return ret;
1290}
1291
1292int start_output_stream(struct stream_out *out)
1293{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001294 int ret = 0;
Mingming Yin9c041392014-05-01 15:37:31 -07001295 int sink_channels = 0;
1296 char prop_value[PROPERTY_VALUE_MAX] = {0};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001297 struct audio_usecase *uc_info;
1298 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301299 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001300
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001301 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
1302 ret = -EINVAL;
1303 goto error_config;
1304 }
1305
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301306 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1307 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1308 out->devices);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301309
Naresh Tanniru80659832014-06-04 18:17:56 +05301310 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301311 ALOGE("%s: sound card is not active/SSR returning error", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05301312 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301313 goto error_config;
1314 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05301315
Eric Laurentb23d5282013-05-14 15:27:20 -07001316 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001317 if (out->pcm_device_id < 0) {
1318 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1319 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001320 ret = -EINVAL;
1321 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001322 }
1323
1324 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001325
1326 if (!uc_info) {
1327 ret = -ENOMEM;
1328 goto error_config;
1329 }
1330
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001331 uc_info->id = out->usecase;
1332 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001333 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001334 uc_info->devices = out->devices;
1335 uc_info->in_snd_device = SND_DEVICE_NONE;
1336 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337
Eric Laurent07eeafd2013-10-06 12:52:49 -07001338 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001339 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Mingming Yin9c041392014-05-01 15:37:31 -07001340 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1341 if (!strncmp("true", prop_value, 4)) {
1342 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1343 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1344 check_and_set_hdmi_channels(adev, sink_channels);
1345 } else {
1346 if (is_offload_usecase(out->usecase))
1347 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1348 else
1349 check_and_set_hdmi_channels(adev, out->config.channels);
1350 }
Mingming Yin10fef6a2013-11-26 17:17:01 -08001351 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001352
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001353 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001354
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001355 select_devices(adev, out->usecase);
1356
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001357 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1358 __func__, 0, out->pcm_device_id);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001359 if (!is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001360 out->pcm = pcm_open(adev->snd_card,
1361 out->pcm_device_id,
1362 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001363 if (out->pcm && !pcm_is_ready(out->pcm)) {
1364 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1365 pcm_close(out->pcm);
1366 out->pcm = NULL;
1367 ret = -EIO;
1368 goto error_open;
1369 }
1370 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001371 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001372 out->compr = compress_open(adev->snd_card,
1373 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001374 COMPRESS_IN, &out->compr_config);
1375 if (out->compr && !is_compress_ready(out->compr)) {
1376 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1377 compress_close(out->compr);
1378 out->compr = NULL;
1379 ret = -EIO;
1380 goto error_open;
1381 }
1382 if (out->offload_callback)
1383 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001384
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001385#ifdef DS1_DOLBY_DDP_ENABLED
1386 if (audio_extn_is_dolby_format(out->format))
1387 audio_extn_dolby_send_ddp_endp_params(adev);
1388#endif
1389
Eric Laurentc4aef752013-09-12 17:45:53 -07001390 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001391 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1392 if (adev->offload_effects_start_output != NULL)
1393 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001394 }
Eric Laurent994a6932013-07-17 11:51:42 -07001395 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001396 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001397error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001398 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001399error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001400 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001401}
1402
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001403static int check_input_parameters(uint32_t sample_rate,
1404 audio_format_t format,
1405 int channel_count)
1406{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001407 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001408
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001409 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001410 !voice_extn_compress_voip_is_format_supported(format) &&
1411 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001412
1413 switch (channel_count) {
1414 case 1:
1415 case 2:
1416 case 6:
1417 break;
1418 default:
1419 ret = -EINVAL;
1420 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001421
1422 switch (sample_rate) {
1423 case 8000:
1424 case 11025:
1425 case 12000:
1426 case 16000:
1427 case 22050:
1428 case 24000:
1429 case 32000:
1430 case 44100:
1431 case 48000:
1432 break;
1433 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001434 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001435 }
1436
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001437 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001438}
1439
1440static size_t get_input_buffer_size(uint32_t sample_rate,
1441 audio_format_t format,
1442 int channel_count)
1443{
1444 size_t size = 0;
1445
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001446 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1447 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001448
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001449 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1450 /* ToDo: should use frame_size computed based on the format and
1451 channel_count here. */
1452 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001453
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001454 /* make sure the size is multiple of 64 */
1455 size += 0x3f;
1456 size &= ~0x3f;
1457
1458 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001459}
1460
1461static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1462{
1463 struct stream_out *out = (struct stream_out *)stream;
1464
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001465 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001466}
1467
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001468static int out_set_sample_rate(struct audio_stream *stream __unused,
1469 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001470{
1471 return -ENOSYS;
1472}
1473
1474static size_t out_get_buffer_size(const struct audio_stream *stream)
1475{
1476 struct stream_out *out = (struct stream_out *)stream;
1477
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001478 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001479 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001480 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1481 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001482
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07001483 return out->config.period_size *
1484 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001485}
1486
1487static uint32_t out_get_channels(const struct audio_stream *stream)
1488{
1489 struct stream_out *out = (struct stream_out *)stream;
1490
1491 return out->channel_mask;
1492}
1493
1494static audio_format_t out_get_format(const struct audio_stream *stream)
1495{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001496 struct stream_out *out = (struct stream_out *)stream;
1497
1498 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001499}
1500
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001501static int out_set_format(struct audio_stream *stream __unused,
1502 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001503{
1504 return -ENOSYS;
1505}
1506
1507static int out_standby(struct audio_stream *stream)
1508{
1509 struct stream_out *out = (struct stream_out *)stream;
1510 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001511
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301512 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1513 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001514 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1515 /* Ignore standby in case of voip call because the voip output
1516 * stream is closed in adev_close_output_stream()
1517 */
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301518 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001519 return 0;
1520 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001521
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001522 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001523 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001524 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001525 out->standby = true;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001526 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001527 if (out->pcm) {
1528 pcm_close(out->pcm);
1529 out->pcm = NULL;
1530 }
1531 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001532 ALOGD("copl(%p):standby", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001533 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001534 out->gapless_mdata.encoder_delay = 0;
1535 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001536 if (out->compr != NULL) {
1537 compress_close(out->compr);
1538 out->compr = NULL;
1539 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001540 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001541 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001542 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001543 }
1544 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001545 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001546 return 0;
1547}
1548
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001549static int out_dump(const struct audio_stream *stream __unused,
1550 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001551{
1552 return 0;
1553}
1554
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001555static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1556{
1557 int ret = 0;
1558 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001559 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001560 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001561 tmp_mdata.encoder_delay = 0;
1562 tmp_mdata.encoder_padding = 0;
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001563
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001564 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001565 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001566 return -EINVAL;
1567 }
1568
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001569 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1570 if (ret >= 0) {
1571 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1572 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1573 ALOGV("ADTS format is set in offload mode");
1574 }
1575 out->send_new_metadata = 1;
1576 }
1577
Mingming Yin3ee55c62014-08-04 14:23:35 -07001578 if (out->format == AUDIO_FORMAT_FLAC) {
1579 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_BLK_SIZE, value, sizeof(value));
1580 if (ret >= 0) {
1581 out->compr_config.codec->options.flac_dec.min_blk_size = atoi(value);
1582 out->send_new_metadata = 1;
1583 }
1584 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_BLK_SIZE, value, sizeof(value));
1585 if (ret >= 0) {
1586 out->compr_config.codec->options.flac_dec.max_blk_size = atoi(value);
1587 out->send_new_metadata = 1;
1588 }
1589 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_FRAME_SIZE, value, sizeof(value));
1590 if (ret >= 0) {
1591 out->compr_config.codec->options.flac_dec.min_frame_size = atoi(value);
1592 out->send_new_metadata = 1;
1593 }
1594 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_FRAME_SIZE, value, sizeof(value));
1595 if (ret >= 0) {
1596 out->compr_config.codec->options.flac_dec.max_frame_size = atoi(value);
1597 out->send_new_metadata = 1;
1598 }
1599 }
1600
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001601 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1602 if(ret >= 0)
1603 is_meta_data_params = true;
1604 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1605 if(ret >= 0 )
1606 is_meta_data_params = true;
1607 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1608 if(ret >= 0 )
1609 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001610 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1611 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001612 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001613 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001614 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001615 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1616 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001617 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001618 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001619 }
1620
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001621 if(!is_meta_data_params) {
1622 ALOGV("%s: Not gapless meta data params", __func__);
1623 return 0;
1624 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001625 out->gapless_mdata = tmp_mdata;
1626 out->send_new_metadata = 1;
1627 ALOGV("%s new encoder delay %u and padding %u", __func__,
1628 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1629
1630 return 0;
1631}
1632
1633
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001634static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1635{
1636 struct stream_out *out = (struct stream_out *)stream;
1637 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001638 struct audio_usecase *usecase;
1639 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001640 struct str_parms *parms;
1641 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001642 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001643 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001644
sangwoobc677242013-08-08 16:53:43 +09001645 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001646 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001647 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301648 if (!parms)
1649 goto error;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001650 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1651 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001652 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001653 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001654 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001655
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001656 /*
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301657 * When HDMI cable is unplugged/usb hs is disconnected the
1658 * music playback is paused and the policy manager sends routing=0
1659 * But the audioflingercontinues to write data until standby time
1660 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001661 * Avoid this by routing audio to speaker until standby.
1662 */
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301663 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1664 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001665 val == AUDIO_DEVICE_NONE) {
1666 val = AUDIO_DEVICE_OUT_SPEAKER;
1667 }
1668
1669 /*
1670 * select_devices() call below switches all the usecases on the same
1671 * backend to the new device. Refer to check_usecases_codec_backend() in
1672 * the select_devices(). But how do we undo this?
1673 *
1674 * For example, music playback is active on headset (deep-buffer usecase)
1675 * and if we go to ringtones and select a ringtone, low-latency usecase
1676 * will be started on headset+speaker. As we can't enable headset+speaker
1677 * and headset devices at the same time, select_devices() switches the music
1678 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1679 * So when the ringtone playback is completed, how do we undo the same?
1680 *
1681 * We are relying on the out_set_parameters() call on deep-buffer output,
1682 * once the ringtone playback is ended.
1683 * NOTE: We should not check if the current devices are same as new devices.
1684 * Because select_devices() must be called to switch back the music
1685 * playback to headset.
1686 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001687 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001688 out->devices = val;
1689
1690 if (!out->standby)
1691 select_devices(adev, out->usecase);
1692
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001693 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001694 !adev->voice.in_call &&
1695 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001696 ret = voice_start_call(adev);
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001697 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1698 adev->voice.in_call &&
1699 (out == adev->primary_output)) {
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -08001700 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001701 }
1702 }
1703
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001704 if ((adev->mode == AUDIO_MODE_NORMAL) &&
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001705 adev->voice.in_call &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001706 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001707 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001708 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001709
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001710 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001711 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001712 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001713
1714 if (out == adev->primary_output) {
1715 pthread_mutex_lock(&adev->lock);
1716 audio_extn_set_parameters(adev, parms);
1717 pthread_mutex_unlock(&adev->lock);
1718 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001719 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001720 pthread_mutex_lock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001721 parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001722 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001723 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001724
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001725 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301726error:
Eric Laurent994a6932013-07-17 11:51:42 -07001727 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001728 return ret;
1729}
1730
1731static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1732{
1733 struct stream_out *out = (struct stream_out *)stream;
1734 struct str_parms *query = str_parms_create_str(keys);
1735 char *str;
1736 char value[256];
1737 struct str_parms *reply = str_parms_create();
1738 size_t i, j;
1739 int ret;
1740 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001741
1742 if (!query || !reply) {
1743 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
1744 return NULL;
1745 }
1746
Eric Laurent994a6932013-07-17 11:51:42 -07001747 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001748 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1749 if (ret >= 0) {
1750 value[0] = '\0';
1751 i = 0;
1752 while (out->supported_channel_masks[i] != 0) {
1753 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1754 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1755 if (!first) {
1756 strcat(value, "|");
1757 }
1758 strcat(value, out_channels_name_to_enum_table[j].name);
1759 first = false;
1760 break;
1761 }
1762 }
1763 i++;
1764 }
1765 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1766 str = str_parms_to_str(reply);
1767 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001768 voice_extn_out_get_parameters(out, query, reply);
1769 str = str_parms_to_str(reply);
1770 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella29b8fc72014-01-29 12:52:19 -08001771 free(str);
1772 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001773 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001774 }
1775 str_parms_destroy(query);
1776 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001777 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001778 return str;
1779}
1780
1781static uint32_t out_get_latency(const struct audio_stream_out *stream)
1782{
1783 struct stream_out *out = (struct stream_out *)stream;
1784
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001785 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001786 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1787
1788 return (out->config.period_count * out->config.period_size * 1000) /
1789 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790}
1791
1792static int out_set_volume(struct audio_stream_out *stream, float left,
1793 float right)
1794{
Eric Laurenta9024de2013-04-04 09:19:12 -07001795 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001796 int volume[2];
1797
Eric Laurenta9024de2013-04-04 09:19:12 -07001798 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1799 /* only take left channel into account: the API is for stereo anyway */
1800 out->muted = (left == 0.0f);
1801 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001802 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001803 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001804 struct audio_device *adev = out->dev;
1805 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001806 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1807 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001808
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001809 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1810 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001811 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1812 if (!ctl) {
1813 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1814 __func__, mixer_ctl_name);
1815 return -EINVAL;
1816 }
1817 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1818 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1819 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1820 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001821 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001822
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001823 return -ENOSYS;
1824}
1825
1826static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1827 size_t bytes)
1828{
1829 struct stream_out *out = (struct stream_out *)stream;
1830 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301831 int snd_scard_state = get_snd_card_state(adev);
Eric Laurent6e895242013-09-05 16:10:57 -07001832 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001833
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001834 pthread_mutex_lock(&out->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301835
Naresh Tanniru80659832014-06-04 18:17:56 +05301836 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
1837 if (out->pcm) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301838 ALOGD(" %s: sound card is not active/SSR state", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05301839 ret= -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301840 goto exit;
Naresh Tanniru80659832014-06-04 18:17:56 +05301841 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1842 //during SSR for compress usecase we should return error to flinger
1843 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
1844 pthread_mutex_unlock(&out->lock);
1845 return -ENETRESET;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301846 }
1847 }
1848
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001849 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001850 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001851 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001852 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1853 ret = voice_extn_compress_voip_start_output_stream(out);
1854 else
1855 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001856 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001857 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001858 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001859 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001860 goto exit;
1861 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001862 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001863
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001864 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001865 ALOGD("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001866 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001867 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001868 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1869 out->send_new_metadata = 0;
1870 }
1871
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001872 ret = compress_write(out->compr, buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301873 if (ret < 0)
1874 ret = -errno;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001875 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001876 if (ret >= 0 && ret < (ssize_t)bytes) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301877 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001878 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05301879 } else if (-ENETRESET == ret) {
1880 ALOGE("copl %s: received sound card offline state on compress write", __func__);
1881 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1882 pthread_mutex_unlock(&out->lock);
1883 out_standby(&out->stream.common);
1884 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001885 }
Naresh Tanniru80659832014-06-04 18:17:56 +05301886 if (!out->playback_started && ret >= 0) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001887 compress_start(out->compr);
1888 out->playback_started = 1;
1889 out->offload_state = OFFLOAD_STATE_PLAYING;
1890 }
1891 pthread_mutex_unlock(&out->lock);
1892 return ret;
1893 } else {
1894 if (out->pcm) {
1895 if (out->muted)
1896 memset((void *)buffer, 0, bytes);
1897 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1898 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301899 if (ret < 0)
1900 ret = -errno;
1901 else if (ret == 0)
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001902 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001903 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001904 }
1905
1906exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05301907 /* ToDo: There may be a corner case when SSR happens back to back during
1908 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05301909 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301910 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301911 }
1912
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001913 pthread_mutex_unlock(&out->lock);
1914
1915 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001916 if (out->pcm)
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001917 ALOGE("%s: error %ld - %s", __func__, ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301918 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05301919 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301920 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05301921 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301922 out->standby = true;
1923 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001924 out_standby(&out->stream.common);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07001925 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05301926 out_get_sample_rate(&out->stream.common));
1927
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001928 }
1929 return bytes;
1930}
1931
1932static int out_get_render_position(const struct audio_stream_out *stream,
1933 uint32_t *dsp_frames)
1934{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001935 struct stream_out *out = (struct stream_out *)stream;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001936 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301937 ssize_t ret = -EINVAL;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001938 *dsp_frames = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001939 pthread_mutex_lock(&out->lock);
1940 if (out->compr != NULL) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301941 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001942 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301943 if (ret < 0)
1944 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001945 ALOGVV("%s rendered frames %d sample_rate %d",
1946 __func__, *dsp_frames, out->sample_rate);
1947 }
1948 pthread_mutex_unlock(&out->lock);
Naresh Tanniru80659832014-06-04 18:17:56 +05301949 if (-ENETRESET == ret) {
1950 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
1951 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1952 return -EINVAL;
1953 } else if(ret < 0) {
1954 ALOGE(" ERROR: Unable to get time stamp from compress driver");
1955 return -EINVAL;
1956 } else {
1957 return 0;
1958 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001959 } else
1960 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001961}
1962
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001963static int out_add_audio_effect(const struct audio_stream *stream __unused,
1964 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001965{
1966 return 0;
1967}
1968
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001969static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1970 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001971{
1972 return 0;
1973}
1974
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001975static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1976 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001977{
1978 return -EINVAL;
1979}
1980
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001981static int out_get_presentation_position(const struct audio_stream_out *stream,
1982 uint64_t *frames, struct timespec *timestamp)
1983{
1984 struct stream_out *out = (struct stream_out *)stream;
1985 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001986 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001987
1988 pthread_mutex_lock(&out->lock);
1989
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001990 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07001991 if (out->compr != NULL) {
1992 compress_get_tstamp(out->compr, &dsp_frames,
1993 &out->sample_rate);
1994 ALOGVV("%s rendered frames %ld sample_rate %d",
1995 __func__, dsp_frames, out->sample_rate);
1996 *frames = dsp_frames;
1997 ret = 0;
1998 /* this is the best we can do */
1999 clock_gettime(CLOCK_MONOTONIC, timestamp);
2000 }
2001 } else {
2002 if (out->pcm) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002003 unsigned int avail;
Eric Laurent949a0892013-09-20 09:20:13 -07002004 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
2005 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07002006 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07002007 // This adjustment accounts for buffering after app processor.
2008 // It is based on estimated DSP latency per use case, rather than exact.
2009 signed_frames -=
2010 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
2011
Eric Laurent949a0892013-09-20 09:20:13 -07002012 // It would be unusual for this value to be negative, but check just in case ...
2013 if (signed_frames >= 0) {
2014 *frames = signed_frames;
2015 ret = 0;
2016 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002017 }
2018 }
2019 }
2020
2021 pthread_mutex_unlock(&out->lock);
2022
2023 return ret;
2024}
2025
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002026static int out_set_callback(struct audio_stream_out *stream,
2027 stream_callback_t callback, void *cookie)
2028{
2029 struct stream_out *out = (struct stream_out *)stream;
2030
2031 ALOGV("%s", __func__);
2032 pthread_mutex_lock(&out->lock);
2033 out->offload_callback = callback;
2034 out->offload_cookie = cookie;
2035 pthread_mutex_unlock(&out->lock);
2036 return 0;
2037}
2038
2039static int out_pause(struct audio_stream_out* stream)
2040{
2041 struct stream_out *out = (struct stream_out *)stream;
2042 int status = -ENOSYS;
2043 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002044 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002045 ALOGD("copl(%p):pause compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002046 pthread_mutex_lock(&out->lock);
2047 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302048 struct audio_device *adev = out->dev;
2049 int snd_scard_state = get_snd_card_state(adev);
2050
2051 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2052 status = compress_pause(out->compr);
2053
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002054 out->offload_state = OFFLOAD_STATE_PAUSED;
2055 }
2056 pthread_mutex_unlock(&out->lock);
2057 }
2058 return status;
2059}
2060
2061static int out_resume(struct audio_stream_out* stream)
2062{
2063 struct stream_out *out = (struct stream_out *)stream;
2064 int status = -ENOSYS;
2065 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002066 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002067 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002068 status = 0;
2069 pthread_mutex_lock(&out->lock);
2070 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302071 struct audio_device *adev = out->dev;
2072 int snd_scard_state = get_snd_card_state(adev);
2073
2074 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2075 status = compress_resume(out->compr);
2076
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002077 out->offload_state = OFFLOAD_STATE_PLAYING;
2078 }
2079 pthread_mutex_unlock(&out->lock);
2080 }
2081 return status;
2082}
2083
2084static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2085{
2086 struct stream_out *out = (struct stream_out *)stream;
2087 int status = -ENOSYS;
2088 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002089 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002090 pthread_mutex_lock(&out->lock);
2091 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2092 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2093 else
2094 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2095 pthread_mutex_unlock(&out->lock);
2096 }
2097 return status;
2098}
2099
2100static int out_flush(struct audio_stream_out* stream)
2101{
2102 struct stream_out *out = (struct stream_out *)stream;
2103 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002104 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002105 ALOGD("copl(%p):calling compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002106 pthread_mutex_lock(&out->lock);
2107 stop_compressed_output_l(out);
2108 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002109 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002110 return 0;
2111 }
2112 return -ENOSYS;
2113}
2114
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002115/** audio_stream_in implementation **/
2116static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2117{
2118 struct stream_in *in = (struct stream_in *)stream;
2119
2120 return in->config.rate;
2121}
2122
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002123static int in_set_sample_rate(struct audio_stream *stream __unused,
2124 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002125{
2126 return -ENOSYS;
2127}
2128
2129static size_t in_get_buffer_size(const struct audio_stream *stream)
2130{
2131 struct stream_in *in = (struct stream_in *)stream;
2132
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002133 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
2134 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07002135 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
2136 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002137
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002138 return in->config.period_size *
2139 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002140}
2141
2142static uint32_t in_get_channels(const struct audio_stream *stream)
2143{
2144 struct stream_in *in = (struct stream_in *)stream;
2145
2146 return in->channel_mask;
2147}
2148
2149static audio_format_t in_get_format(const struct audio_stream *stream)
2150{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002151 struct stream_in *in = (struct stream_in *)stream;
2152
2153 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002154}
2155
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002156static int in_set_format(struct audio_stream *stream __unused,
2157 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002158{
2159 return -ENOSYS;
2160}
2161
2162static int in_standby(struct audio_stream *stream)
2163{
2164 struct stream_in *in = (struct stream_in *)stream;
2165 struct audio_device *adev = in->dev;
2166 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302167 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2168 stream, in->usecase, use_case_table[in->usecase]);
2169
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002170 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2171 /* Ignore standby in case of voip call because the voip input
2172 * stream is closed in adev_close_input_stream()
2173 */
2174 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2175 return status;
2176 }
2177
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002178 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002179 if (!in->standby && in->is_st_session) {
2180 ALOGD("%s: sound trigger pcm stop lab", __func__);
2181 audio_extn_sound_trigger_stop_lab(in);
2182 in->standby = 1;
2183 }
2184
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002185 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002186 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002187 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002188 if (in->pcm) {
2189 pcm_close(in->pcm);
2190 in->pcm = NULL;
2191 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002192 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002193 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002194 }
2195 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002196 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002197 return status;
2198}
2199
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002200static int in_dump(const struct audio_stream *stream __unused,
2201 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002202{
2203 return 0;
2204}
2205
2206static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2207{
2208 struct stream_in *in = (struct stream_in *)stream;
2209 struct audio_device *adev = in->dev;
2210 struct str_parms *parms;
2211 char *str;
2212 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002213 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002214
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302215 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002216 parms = str_parms_create_str(kvpairs);
2217
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302218 if (!parms)
2219 goto error;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002220 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002221 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002222
2223 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2224 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002225 val = atoi(value);
2226 /* no audio source uses val == 0 */
2227 if ((in->source != val) && (val != 0)) {
2228 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002229 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2230 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2231 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2232 (in->config.rate == 8000 || in->config.rate == 16000) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002233 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002234 err = voice_extn_compress_voip_open_input_stream(in);
2235 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002236 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002237 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002238 }
2239 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002240 }
2241 }
2242
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002243 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2244 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002245 val = atoi(value);
2246 if ((in->device != val) && (val != 0)) {
2247 in->device = val;
2248 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002249 if (!in->standby && !in->is_st_session)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002250 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002251 }
2252 }
2253
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002254done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002255 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002256 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002257
2258 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302259error:
Eric Laurent994a6932013-07-17 11:51:42 -07002260 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002261 return ret;
2262}
2263
2264static char* in_get_parameters(const struct audio_stream *stream,
2265 const char *keys)
2266{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002267 struct stream_in *in = (struct stream_in *)stream;
2268 struct str_parms *query = str_parms_create_str(keys);
2269 char *str;
2270 char value[256];
2271 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002272
2273 if (!query || !reply) {
2274 ALOGE("in_get_parameters: failed to create query or reply");
2275 return NULL;
2276 }
2277
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002278 ALOGV("%s: enter: keys - %s", __func__, keys);
2279
2280 voice_extn_in_get_parameters(in, query, reply);
2281
2282 str = str_parms_to_str(reply);
2283 str_parms_destroy(query);
2284 str_parms_destroy(reply);
2285
2286 ALOGV("%s: exit: returns - %s", __func__, str);
2287 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002288}
2289
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002290static int in_set_gain(struct audio_stream_in *stream __unused,
2291 float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002292{
2293 return 0;
2294}
2295
2296static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2297 size_t bytes)
2298{
2299 struct stream_in *in = (struct stream_in *)stream;
2300 struct audio_device *adev = in->dev;
2301 int i, ret = -1;
Naresh Tanniru80659832014-06-04 18:17:56 +05302302 int snd_scard_state = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002303
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002304 pthread_mutex_lock(&in->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302305
2306 if (in->pcm) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302307 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05302308 ALOGD(" %s: sound card is not active/SSR state", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302309 ret= -EIO;;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302310 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302311 }
2312 }
2313
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002314 if (in->standby) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002315 if (!in->is_st_session) {
2316 pthread_mutex_lock(&adev->lock);
2317 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2318 ret = voice_extn_compress_voip_start_input_stream(in);
2319 else
2320 ret = start_input_stream(in);
2321 pthread_mutex_unlock(&adev->lock);
2322 if (ret != 0) {
2323 goto exit;
2324 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002325 }
2326 in->standby = 0;
2327 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002328
2329 if (in->pcm) {
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002330 if (audio_extn_ssr_get_enabled() &&
2331 audio_channel_count_from_in_mask(in->channel_mask) == 6)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002332 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002333 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2334 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002335 else
2336 ret = pcm_read(in->pcm, buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05302337 if (ret < 0)
2338 ret = -errno;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002339 }
2340
2341 /*
2342 * Instead of writing zeroes here, we could trust the hardware
2343 * to always provide zeroes when muted.
2344 */
kunleizc5a639b2014-04-24 18:46:22 +08002345 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002346 memset(buffer, 0, bytes);
2347
2348exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05302349 /* ToDo: There may be a corner case when SSR happens back to back during
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302350 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05302351 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302352 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302353 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002354 pthread_mutex_unlock(&in->lock);
2355
2356 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302357 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302358 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302359 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302360 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302361 in->standby = true;
2362 }
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302363 memset(buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002364 in_standby(&in->stream.common);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002365 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002366 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05302367 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002368 }
2369 return bytes;
2370}
2371
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002372static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002373{
2374 return 0;
2375}
2376
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002377static int add_remove_audio_effect(const struct audio_stream *stream,
2378 effect_handle_t effect,
2379 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002380{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002381 struct stream_in *in = (struct stream_in *)stream;
2382 int status = 0;
2383 effect_descriptor_t desc;
2384
2385 status = (*effect)->get_descriptor(effect, &desc);
2386 if (status != 0)
2387 return status;
2388
2389 pthread_mutex_lock(&in->lock);
2390 pthread_mutex_lock(&in->dev->lock);
2391 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2392 in->enable_aec != enable &&
2393 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2394 in->enable_aec = enable;
2395 if (!in->standby)
2396 select_devices(in->dev, in->usecase);
2397 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002398 if (in->enable_ns != enable &&
2399 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2400 in->enable_ns = enable;
2401 if (!in->standby)
2402 select_devices(in->dev, in->usecase);
2403 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002404 pthread_mutex_unlock(&in->dev->lock);
2405 pthread_mutex_unlock(&in->lock);
2406
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002407 return 0;
2408}
2409
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002410static int in_add_audio_effect(const struct audio_stream *stream,
2411 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002412{
Eric Laurent994a6932013-07-17 11:51:42 -07002413 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002414 return add_remove_audio_effect(stream, effect, true);
2415}
2416
2417static int in_remove_audio_effect(const struct audio_stream *stream,
2418 effect_handle_t effect)
2419{
Eric Laurent994a6932013-07-17 11:51:42 -07002420 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002421 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002422}
2423
2424static int adev_open_output_stream(struct audio_hw_device *dev,
2425 audio_io_handle_t handle,
2426 audio_devices_t devices,
2427 audio_output_flags_t flags,
2428 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002429 struct audio_stream_out **stream_out,
2430 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002431{
2432 struct audio_device *adev = (struct audio_device *)dev;
2433 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002434 int i, ret = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002435 audio_format_t format;
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302436 int32_t sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002437
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002438 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05302439
2440 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2441 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2442 ALOGE(" sound card is not active rejecting compress output open request");
2443 return -EINVAL;
2444 }
2445
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002446 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2447
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302448 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2449 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2450 devices, flags, &out->stream);
2451
2452
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002453 if (!out) {
2454 return -ENOMEM;
2455 }
2456
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002457 if (devices == AUDIO_DEVICE_NONE)
2458 devices = AUDIO_DEVICE_OUT_SPEAKER;
2459
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002460 out->flags = flags;
2461 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002462 out->dev = adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002463 format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002464 out->sample_rate = config->sample_rate;
2465 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2466 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002467 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002468 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002469
2470 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002471 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2472 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2473 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2474
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002475 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002476 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2477 ret = read_hdmi_channel_masks(out);
2478
2479 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2480 ret = audio_extn_read_afe_proxy_channel_masks(out);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002481 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002482 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002483 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002484
2485 if (config->sample_rate == 0)
2486 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2487 if (config->channel_mask == 0)
2488 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2489
2490 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002491 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002492 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2493 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002494 out->config.rate = config->sample_rate;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002495 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002496 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002497 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2498 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002499 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002500 ret = voice_extn_compress_voip_open_output_stream(out);
2501 if (ret != 0) {
2502 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2503 __func__, ret);
2504 goto error_open;
2505 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002506 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2507 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2508 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2509 ALOGE("%s: Unsupported Offload information", __func__);
2510 ret = -EINVAL;
2511 goto error_open;
2512 }
Mingming Yin90310102013-11-13 16:57:00 -08002513 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002514 !audio_extn_is_dolby_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002515 ALOGE("%s: Unsupported audio format", __func__);
2516 ret = -EINVAL;
2517 goto error_open;
2518 }
2519
2520 out->compr_config.codec = (struct snd_codec *)
2521 calloc(1, sizeof(struct snd_codec));
2522
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002523 if (!out->compr_config.codec) {
2524 ret = -ENOMEM;
2525 goto error_open;
2526 }
2527
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002528 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002529 if (config->offload_info.channel_mask)
2530 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002531 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002532 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002533 config->offload_info.channel_mask = config->channel_mask;
2534 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002535 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002536 out->sample_rate = config->offload_info.sample_rate;
2537
2538 out->stream.set_callback = out_set_callback;
2539 out->stream.pause = out_pause;
2540 out->stream.resume = out_resume;
2541 out->stream.drain = out_drain;
2542 out->stream.flush = out_flush;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002543 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002544
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002545 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002546 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002547 audio_extn_dolby_get_snd_codec_id(adev, out,
2548 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002549 else
2550 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002551 get_snd_codec_id(config->offload_info.format);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002552 if (audio_is_offload_pcm(config->offload_info.format)) {
2553 out->compr_config.fragment_size =
2554 platform_get_pcm_offload_buffer_size(&config->offload_info);
2555 } else {
2556 out->compr_config.fragment_size =
2557 platform_get_compress_offload_buffer_size(&config->offload_info);
2558 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002559 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2560 out->compr_config.codec->sample_rate =
2561 compress_get_alsa_rate(config->offload_info.sample_rate);
2562 out->compr_config.codec->bit_rate =
2563 config->offload_info.bit_rate;
2564 out->compr_config.codec->ch_in =
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002565 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002566 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002567 out->bit_width = PCM_OUTPUT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002568
Mingming Yin3ee55c62014-08-04 14:23:35 -07002569 if (config->offload_info.format == AUDIO_FORMAT_AAC)
2570 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002571 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2572 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002573 if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002574 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2575
Mingming Yin3ee55c62014-08-04 14:23:35 -07002576 if (out->bit_width == 24) {
2577 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2578 }
2579
2580 if (out->bit_width == 24 && !platform_check_24_bit_support()) {
2581 ALOGW("24 bit support is not enabled, using 16 bit backend");
2582 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
2583 }
2584
Amit Shekhar6f461b12014-08-01 14:52:58 -07002585 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
Mingming Yinc9efb4f2014-09-19 12:16:36 -07002586 out->compr_config.codec->options.flac_dec.sample_size = PCM_OUTPUT_BIT_WIDTH;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002587
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002588 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2589 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002590
2591 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002592 out->offload_state = OFFLOAD_STATE_IDLE;
2593 out->playback_started = 0;
2594
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002595 create_offload_callback_thread(out);
2596 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2597 __func__, config->offload_info.version,
2598 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002599 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -08002600 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002601
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002602 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2603 ret = voice_check_and_set_incall_music_usecase(adev, out);
2604 if (ret != 0) {
2605 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2606 __func__, ret);
2607 goto error_open;
2608 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002609 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002610 format = AUDIO_FORMAT_PCM_16_BIT;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002611 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2612 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002613 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002614 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002615 /* primary path is the default path selected if no other outputs are available/suitable */
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002616 format = AUDIO_FORMAT_PCM_16_BIT;
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002617 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002618 out->config = pcm_config_deep_buffer;
2619 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002620 }
2621
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302622 if ((24 == out->bit_width) &&
2623 (devices == AUDIO_DEVICE_OUT_SPEAKER)) {
2624 sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2625 ALOGI("%s 24-bit playback on Speaker is allowed ONLY at 48khz. Hence changing sample rate to: %d",
2626 __func__, sample_rate);
2627 } else {
2628 sample_rate = out->sample_rate;
2629 }
2630
2631 ALOGV("%s flags %x, format %x, sample_rate %d, out->bit_width %d",
2632 __func__, flags, format, sample_rate, out->bit_width);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002633 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
2634 &adev->streams_output_cfg_list,
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +05302635 flags, format, sample_rate,
Amit Shekhar6f461b12014-08-01 14:52:58 -07002636 out->bit_width, &out->app_type_cfg);
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002637 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2638 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2639 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002640 if(adev->primary_output == NULL)
2641 adev->primary_output = out;
2642 else {
2643 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002644 ret = -EEXIST;
2645 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002646 }
2647 }
2648
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002649 /* Check if this usecase is already existing */
2650 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07002651 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2652 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002653 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002654 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002655 ret = -EEXIST;
2656 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002657 }
2658 pthread_mutex_unlock(&adev->lock);
2659
2660 out->stream.common.get_sample_rate = out_get_sample_rate;
2661 out->stream.common.set_sample_rate = out_set_sample_rate;
2662 out->stream.common.get_buffer_size = out_get_buffer_size;
2663 out->stream.common.get_channels = out_get_channels;
2664 out->stream.common.get_format = out_get_format;
2665 out->stream.common.set_format = out_set_format;
2666 out->stream.common.standby = out_standby;
2667 out->stream.common.dump = out_dump;
2668 out->stream.common.set_parameters = out_set_parameters;
2669 out->stream.common.get_parameters = out_get_parameters;
2670 out->stream.common.add_audio_effect = out_add_audio_effect;
2671 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2672 out->stream.get_latency = out_get_latency;
2673 out->stream.set_volume = out_set_volume;
2674 out->stream.write = out_write;
2675 out->stream.get_render_position = out_get_render_position;
2676 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002677 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002678
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002679 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002680 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002681 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002682
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002683 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2684 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2685
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002686 config->format = out->stream.common.get_format(&out->stream.common);
2687 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2688 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2689
2690 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302691 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2692 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002693 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002694 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002695
2696error_open:
2697 free(out);
2698 *stream_out = NULL;
2699 ALOGD("%s: exit: ret %d", __func__, ret);
2700 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002701}
2702
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002703static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002704 struct audio_stream_out *stream)
2705{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002706 struct stream_out *out = (struct stream_out *)stream;
2707 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002708 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002709
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302710 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2711
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002712 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302713 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002714 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302715 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002716 if(ret != 0)
2717 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2718 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002719 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002720 out_standby(&stream->common);
2721
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002722 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002723 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002724 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002725 if (out->compr_config.codec != NULL)
2726 free(out->compr_config.codec);
2727 }
2728 pthread_cond_destroy(&out->cond);
2729 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002730 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002731 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002732}
2733
2734static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2735{
2736 struct audio_device *adev = (struct audio_device *)dev;
2737 struct str_parms *parms;
2738 char *str;
2739 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002740 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002741 int ret;
2742 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002743
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002744 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002745 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002746
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302747 if (!parms)
2748 goto error;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002749 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
2750 if (ret >= 0) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05302751 char *snd_card_status = value+2;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302752 if (strstr(snd_card_status, "OFFLINE")) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302753 struct listnode *node;
2754 struct audio_usecase *usecase;
2755
Naresh Tanniru4c630392014-05-12 01:05:52 +05302756 ALOGD("Received sound card OFFLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302757 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2758
2759 pthread_mutex_lock(&adev->lock);
2760 //close compress session on OFFLINE status
2761 usecase = get_usecase_from_list(adev,USECASE_AUDIO_PLAYBACK_OFFLOAD);
2762 if (usecase && usecase->stream.out) {
2763 ALOGD(" %s closing compress session on OFFLINE state", __func__);
2764
2765 struct stream_out *out = usecase->stream.out;
2766
2767 pthread_mutex_unlock(&adev->lock);
2768 out_standby(&out->stream.common);
2769 } else
2770 pthread_mutex_unlock(&adev->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302771 } else if (strstr(snd_card_status, "ONLINE")) {
2772 ALOGD("Received sound card ONLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302773 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302774 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05302775 }
2776
2777 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002778 status = voice_set_parameters(adev, parms);
2779 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002780 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002781
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002782 status = platform_set_parameters(adev->platform, parms);
2783 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002784 goto done;
2785
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002786 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2787 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07002788 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002789 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2790 adev->bluetooth_nrec = true;
2791 else
2792 adev->bluetooth_nrec = false;
2793 }
2794
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002795 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2796 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002797 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2798 adev->screen_off = false;
2799 else
2800 adev->screen_off = true;
2801 }
2802
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002803 ret = str_parms_get_int(parms, "rotation", &val);
2804 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002805 bool reverse_speakers = false;
2806 switch(val) {
2807 // FIXME: note that the code below assumes that the speakers are in the correct placement
2808 // relative to the user when the device is rotated 90deg from its default rotation. This
2809 // assumption is device-specific, not platform-specific like this code.
2810 case 270:
2811 reverse_speakers = true;
2812 break;
2813 case 0:
2814 case 90:
2815 case 180:
2816 break;
2817 default:
2818 ALOGE("%s: unexpected rotation of %d", __func__, val);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002819 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002820 }
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002821 if (status == 0) {
2822 if (adev->speaker_lr_swap != reverse_speakers) {
2823 adev->speaker_lr_swap = reverse_speakers;
2824 // only update the selected device if there is active pcm playback
2825 struct audio_usecase *usecase;
2826 struct listnode *node;
2827 list_for_each(node, &adev->usecase_list) {
2828 usecase = node_to_item(node, struct audio_usecase, list);
2829 if (usecase->type == PCM_PLAYBACK) {
2830 select_devices(adev, usecase->id);
2831 break;
2832 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002833 }
2834 }
2835 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002836 }
2837
Mingming Yin514a8bc2014-07-29 15:22:21 -07002838 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2839 if (ret >= 0) {
2840 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2841 adev->bt_wb_speech_enabled = true;
2842 else
2843 adev->bt_wb_speech_enabled = false;
2844 }
2845
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002846 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002847
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002848done:
2849 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002850 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302851error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002852 ALOGV("%s: exit with code(%d)", __func__, status);
2853 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002854}
2855
2856static char* adev_get_parameters(const struct audio_hw_device *dev,
2857 const char *keys)
2858{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002859 struct audio_device *adev = (struct audio_device *)dev;
2860 struct str_parms *reply = str_parms_create();
2861 struct str_parms *query = str_parms_create_str(keys);
2862 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05302863 char value[256] = {0};
2864 int ret = 0;
2865
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002866 if (!query || !reply) {
2867 ALOGE("adev_get_parameters: failed to create query or reply");
2868 return NULL;
2869 }
2870
Naresh Tannirud7205b62014-06-20 02:54:48 +05302871 ret = str_parms_get_str(query, "SND_CARD_STATUS", value,
2872 sizeof(value));
2873 if (ret >=0) {
2874 int val = 1;
2875 pthread_mutex_lock(&adev->snd_card_status.lock);
2876 if (SND_CARD_STATE_OFFLINE == adev->snd_card_status.state)
2877 val = 0;
2878 pthread_mutex_unlock(&adev->snd_card_status.lock);
2879 str_parms_add_int(reply, "SND_CARD_STATUS", val);
2880 goto exit;
2881 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002882
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002883 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002884 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002885 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002886 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05302887 pthread_mutex_unlock(&adev->lock);
2888
Naresh Tannirud7205b62014-06-20 02:54:48 +05302889exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002890 str = str_parms_to_str(reply);
2891 str_parms_destroy(query);
2892 str_parms_destroy(reply);
2893
2894 ALOGV("%s: exit: returns - %s", __func__, str);
2895 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002896}
2897
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002898static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002899{
2900 return 0;
2901}
2902
2903static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2904{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002905 int ret;
2906 struct audio_device *adev = (struct audio_device *)dev;
2907 pthread_mutex_lock(&adev->lock);
2908 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002909 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002910 pthread_mutex_unlock(&adev->lock);
2911 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002912}
2913
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002914static int adev_set_master_volume(struct audio_hw_device *dev __unused,
2915 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002916{
2917 return -ENOSYS;
2918}
2919
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002920static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2921 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002922{
2923 return -ENOSYS;
2924}
2925
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002926static int adev_set_master_mute(struct audio_hw_device *dev __unused,
2927 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002928{
2929 return -ENOSYS;
2930}
2931
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002932static int adev_get_master_mute(struct audio_hw_device *dev __unused,
2933 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002934{
2935 return -ENOSYS;
2936}
2937
2938static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2939{
2940 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002941 pthread_mutex_lock(&adev->lock);
2942 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002943 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002944 adev->mode = mode;
2945 }
2946 pthread_mutex_unlock(&adev->lock);
2947 return 0;
2948}
2949
2950static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2951{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002952 int ret;
2953
2954 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002955 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002956 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2957 pthread_mutex_unlock(&adev->lock);
2958
2959 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002960}
2961
2962static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2963{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002964 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002965 return 0;
2966}
2967
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002968static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002969 const struct audio_config *config)
2970{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002971 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002972
2973 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2974}
2975
2976static int adev_open_input_stream(struct audio_hw_device *dev,
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002977 audio_io_handle_t handle __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002978 audio_devices_t devices,
2979 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002980 struct audio_stream_in **stream_in,
2981 audio_input_flags_t flags __unused,
2982 const char *address __unused,
2983 audio_source_t source __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002984{
2985 struct audio_device *adev = (struct audio_device *)dev;
2986 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002987 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002988 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002989
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302990
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002991 *stream_in = NULL;
2992 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2993 return -EINVAL;
2994
2995 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002996
2997 if (!in) {
2998 ALOGE("failed to allocate input stream");
2999 return -ENOMEM;
3000 }
3001
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303002 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003003 stream_handle(%p) io_handle(%d)",__func__, config->sample_rate, config->channel_mask,
3004 devices, &in->stream, handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003005
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07003006 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
3007
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003008 in->stream.common.get_sample_rate = in_get_sample_rate;
3009 in->stream.common.set_sample_rate = in_set_sample_rate;
3010 in->stream.common.get_buffer_size = in_get_buffer_size;
3011 in->stream.common.get_channels = in_get_channels;
3012 in->stream.common.get_format = in_get_format;
3013 in->stream.common.set_format = in_set_format;
3014 in->stream.common.standby = in_standby;
3015 in->stream.common.dump = in_dump;
3016 in->stream.common.set_parameters = in_set_parameters;
3017 in->stream.common.get_parameters = in_get_parameters;
3018 in->stream.common.add_audio_effect = in_add_audio_effect;
3019 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3020 in->stream.set_gain = in_set_gain;
3021 in->stream.read = in_read;
3022 in->stream.get_input_frames_lost = in_get_input_frames_lost;
3023
3024 in->device = devices;
3025 in->source = AUDIO_SOURCE_DEFAULT;
3026 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003027 in->standby = 1;
3028 in->channel_mask = config->channel_mask;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003029 in->capture_handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003030
3031 /* Update config params with the requested sample rate and channels */
3032 in->usecase = USECASE_AUDIO_RECORD;
3033 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003034 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003035 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003036
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08003037 if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003038 if(audio_extn_ssr_get_enabled()) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003039 if(audio_extn_ssr_init(in)) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003040 ALOGE("%s: audio_extn_ssr_init failed", __func__);
3041 ret = -EINVAL;
3042 goto err_open;
3043 }
3044 } else {
Mingming Yin3cf99da2014-09-16 17:41:33 -07003045 ALOGW("%s: surround sound recording is not supported", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003046 }
Mingming Yine62d7842013-10-25 16:26:03 -07003047 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08003048 audio_extn_compr_cap_format_supported(config->format) &&
3049 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003050 audio_extn_compr_cap_init(in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003051 } else {
3052 in->config.channels = channel_count;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003053 frame_size = audio_stream_in_frame_size(&in->stream);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003054 buffer_size = get_input_buffer_size(config->sample_rate,
3055 config->format,
3056 channel_count);
3057 in->config.period_size = buffer_size / frame_size;
3058 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003059
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003060 /* This stream could be for sound trigger lab,
3061 get sound trigger pcm if present */
3062 audio_extn_sound_trigger_check_and_get_session(in);
3063
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003064 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07003065 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003066 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003067
3068err_open:
3069 free(in);
3070 *stream_in = NULL;
3071 return ret;
3072}
3073
3074static void adev_close_input_stream(struct audio_hw_device *dev,
3075 struct audio_stream_in *stream)
3076{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003077 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003078 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003079 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05303080
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303081 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003082
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05303083 /* Disable echo reference while closing input stream */
3084 platform_set_echo_reference(adev->platform, false);
3085
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003086 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05303087 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003088 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05303089 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003090 if (ret != 0)
3091 ALOGE("%s: Compress voip input cannot be closed, error:%d",
3092 __func__, ret);
3093 } else
3094 in_standby(&stream->common);
3095
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003096 if (audio_extn_ssr_get_enabled() &&
3097 (audio_channel_count_from_in_mask(in->channel_mask) == 6)) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003098 audio_extn_ssr_deinit();
3099 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003100
Mingming Yine62d7842013-10-25 16:26:03 -07003101 if(audio_extn_compr_cap_enabled() &&
3102 audio_extn_compr_cap_format_supported(in->config.format))
3103 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003104
3105 free(stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003106 return;
3107}
3108
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003109static int adev_dump(const audio_hw_device_t *device __unused,
3110 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003111{
3112 return 0;
3113}
3114
3115static int adev_close(hw_device_t *device)
3116{
3117 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07003118
3119 if (!adev)
3120 return 0;
3121
3122 pthread_mutex_lock(&adev_init_lock);
3123
3124 if ((--audio_device_ref_count) == 0) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003125 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -08003126 audio_extn_listen_deinit(adev);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003127 audio_extn_utils_release_streams_output_cfg_list(&adev->streams_output_cfg_list);
Kiran Kandi910e1862013-10-29 13:29:42 -07003128 audio_route_free(adev->audio_route);
3129 free(adev->snd_dev_ref_cnt);
3130 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07003131 free(device);
3132 adev = NULL;
3133 }
3134 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003135 return 0;
3136}
3137
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003138static int adev_open(const hw_module_t *module, const char *name,
3139 hw_device_t **device)
3140{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003141 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003142
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003143 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003144 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3145
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003146 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07003147 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003148 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07003149 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003150 ALOGD("%s: returning existing instance of adev", __func__);
3151 ALOGD("%s: exit", __func__);
3152 pthread_mutex_unlock(&adev_init_lock);
3153 return 0;
3154 }
3155
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003156 adev = calloc(1, sizeof(struct audio_device));
3157
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003158 if (!adev) {
3159 pthread_mutex_unlock(&adev_init_lock);
3160 return -ENOMEM;
3161 }
3162
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07003163 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3164
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003165 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3166 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3167 adev->device.common.module = (struct hw_module_t *)module;
3168 adev->device.common.close = adev_close;
3169
3170 adev->device.init_check = adev_init_check;
3171 adev->device.set_voice_volume = adev_set_voice_volume;
3172 adev->device.set_master_volume = adev_set_master_volume;
3173 adev->device.get_master_volume = adev_get_master_volume;
3174 adev->device.set_master_mute = adev_set_master_mute;
3175 adev->device.get_master_mute = adev_get_master_mute;
3176 adev->device.set_mode = adev_set_mode;
3177 adev->device.set_mic_mute = adev_set_mic_mute;
3178 adev->device.get_mic_mute = adev_get_mic_mute;
3179 adev->device.set_parameters = adev_set_parameters;
3180 adev->device.get_parameters = adev_get_parameters;
3181 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3182 adev->device.open_output_stream = adev_open_output_stream;
3183 adev->device.close_output_stream = adev_close_output_stream;
3184 adev->device.open_input_stream = adev_open_input_stream;
3185 adev->device.close_input_stream = adev_close_input_stream;
3186 adev->device.dump = adev_dump;
3187
3188 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003189 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003190 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003191 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003192 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003193 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003194 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003195 /* adev->cur_hdmi_channels = 0; by calloc() */
Mingming Yin3ee55c62014-08-04 14:23:35 -07003196 adev->cur_codec_backend_samplerate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
3197 adev->cur_codec_backend_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Eric Laurentb23d5282013-05-14 15:27:20 -07003198 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003199 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003200 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08003201 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003202 adev->offload_usecases_state = 0;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303203
3204 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
3205 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003206 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003207 adev->platform = platform_init(adev);
3208 if (!adev->platform) {
3209 free(adev->snd_dev_ref_cnt);
3210 free(adev);
3211 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3212 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08003213 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003214 return -EINVAL;
3215 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003216
Naresh Tanniru4c630392014-05-12 01:05:52 +05303217 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
3218
Eric Laurentc4aef752013-09-12 17:45:53 -07003219 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
3220 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3221 if (adev->visualizer_lib == NULL) {
3222 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3223 } else {
3224 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3225 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003226 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003227 "visualizer_hal_start_output");
3228 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003229 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003230 "visualizer_hal_stop_output");
3231 }
3232 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003233 audio_extn_listen_init(adev, adev->snd_card);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003234 audio_extn_sound_trigger_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07003235
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003236 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
3237 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3238 if (adev->offload_effects_lib == NULL) {
3239 ALOGE("%s: DLOPEN failed for %s", __func__,
3240 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3241 } else {
3242 ALOGV("%s: DLOPEN successful for %s", __func__,
3243 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3244 adev->offload_effects_start_output =
3245 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3246 "offload_effects_bundle_hal_start_output");
3247 adev->offload_effects_stop_output =
3248 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3249 "offload_effects_bundle_hal_stop_output");
3250 }
3251 }
3252
Mingming Yin514a8bc2014-07-29 15:22:21 -07003253 adev->bt_wb_speech_enabled = false;
3254
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003255 *device = &adev->device.common;
3256
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003257 audio_extn_utils_update_streams_output_cfg_list(adev->platform, adev->mixer,
3258 &adev->streams_output_cfg_list);
3259
Kiran Kandi910e1862013-10-29 13:29:42 -07003260 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003261 pthread_mutex_unlock(&adev_init_lock);
3262
Eric Laurent994a6932013-07-17 11:51:42 -07003263 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003264 return 0;
3265}
3266
Mingming Yin07972cc2014-06-06 17:11:23 -07003267int pcm_ioctl(struct pcm *pcm, int request, ...)
3268{
3269 va_list ap;
3270 void * arg;
3271 int pcm_fd = *(int*)pcm;
3272
3273 va_start(ap, request);
3274 arg = va_arg(ap, void *);
3275 va_end(ap);
3276
3277 return ioctl(pcm_fd, request, arg);
3278}
3279
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003280static struct hw_module_methods_t hal_module_methods = {
3281 .open = adev_open,
3282};
3283
3284struct audio_module HAL_MODULE_INFO_SYM = {
3285 .common = {
3286 .tag = HARDWARE_MODULE_TAG,
3287 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3288 .hal_api_version = HARDWARE_HAL_API_VERSION,
3289 .id = AUDIO_HARDWARE_MODULE_ID,
3290 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08003291 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003292 .methods = &hal_module_methods,
3293 },
3294};