blob: 05bf1329928d692aaae24e445c101e310a286308 [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) {
Amit Shekhar1d896042014-10-03 13:16:09 -0700825 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530826 &adev->streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -0700827 usecase->stream.out->devices,
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530828 usecase->stream.out->flags,
829 usecase->stream.out->format,
830 usecase->stream.out->sample_rate,
831 usecase->stream.out->bit_width,
832 &usecase->stream.out->app_type_cfg);
Amit Shekhar1d896042014-10-03 13:16:09 -0700833 ALOGI("%s Selected apptype: %d", __func__, usecase->stream.out->app_type_cfg.app_type);
Preetam Singh Ranawata4a37d82014-09-25 16:56:38 +0530834 }
Amit Shekhar1d896042014-10-03 13:16:09 -0700835
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700836 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900837
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800838 /* Applicable only on the targets that has external modem.
839 * Enable device command should be sent to modem only after
840 * enabling voice call mixer controls
841 */
Vidyakumar Athota339342f2014-07-01 15:30:57 -0700842 if (usecase->type == VOICE_CALL)
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800843 status = platform_switch_voice_call_usecase_route_post(adev->platform,
844 out_snd_device,
845 in_snd_device);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530846 ALOGD("%s: done",__func__);
847
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800848 return status;
849}
850
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800851static int stop_input_stream(struct stream_in *in)
852{
853 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800854 struct audio_usecase *uc_info;
855 struct audio_device *adev = in->dev;
856
Eric Laurentc8400632013-02-14 19:04:54 -0800857 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800858
Eric Laurent994a6932013-07-17 11:51:42 -0700859 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700860 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800861 uc_info = get_usecase_from_list(adev, in->usecase);
862 if (uc_info == NULL) {
863 ALOGE("%s: Could not find the usecase (%d) in the list",
864 __func__, in->usecase);
865 return -EINVAL;
866 }
867
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800868 /* Close in-call recording streams */
869 voice_check_and_stop_incall_rec_usecase(adev, in);
870
Eric Laurent150dbfe2013-02-27 14:31:02 -0800871 /* 1. Disable stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700872 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700873
874 /* 2. Disable the tx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -0700875 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800876
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800877 list_remove(&uc_info->list);
878 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800879
Eric Laurent994a6932013-07-17 11:51:42 -0700880 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800881 return ret;
882}
883
884int start_input_stream(struct stream_in *in)
885{
886 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800887 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800888 struct audio_usecase *uc_info;
889 struct audio_device *adev = in->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +0530890 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800891
Mingming Yine62d7842013-10-25 16:26:03 -0700892 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Sidipotu Ashokf43018c2014-05-02 16:21:50 +0530893 ALOGD("%s: enter: stream(%p)usecase(%d: %s)",
894 __func__, &in->stream, in->usecase, use_case_table[in->usecase]);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700895
Naresh Tanniru80659832014-06-04 18:17:56 +0530896
897 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +0530898 ALOGE("%s: sound card is not active/SSR returning error", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +0530899 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +0530900 goto error_config;
901 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530902
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700903 /* Check if source matches incall recording usecase criteria */
904 ret = voice_check_and_set_incall_rec_usecase(adev, in);
905 if (ret)
906 goto error_config;
907 else
908 ALOGV("%s: usecase(%d)", __func__, in->usecase);
909
Eric Laurentb23d5282013-05-14 15:27:20 -0700910 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800911 if (in->pcm_device_id < 0) {
912 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
913 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800914 ret = -EINVAL;
915 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800916 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700917
918 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800919 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700920
921 if (!uc_info) {
922 ret = -ENOMEM;
923 goto error_config;
924 }
925
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800926 uc_info->id = in->usecase;
927 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800928 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700929 uc_info->devices = in->device;
930 uc_info->in_snd_device = SND_DEVICE_NONE;
931 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800932
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800933 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700934 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800935
Eric Laurentc8400632013-02-14 19:04:54 -0800936 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800937 __func__, adev->snd_card,
938 in->pcm_device_id, in->config.channels);
939 in->pcm = pcm_open(adev->snd_card,
940 in->pcm_device_id, PCM_IN, &in->config);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800941 if (in->pcm && !pcm_is_ready(in->pcm)) {
942 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
943 pcm_close(in->pcm);
944 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800945 ret = -EIO;
946 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800947 }
Naresh Tanniru4c630392014-05-12 01:05:52 +0530948
Eric Laurent994a6932013-07-17 11:51:42 -0700949 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800950 return ret;
951
952error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800953 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800954
955error_config:
956 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700957 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800958
959 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800960}
961
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700962/* must be called with out->lock locked */
963static int send_offload_cmd_l(struct stream_out* out, int command)
964{
965 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
966
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -0700967 if (!cmd) {
968 ALOGE("failed to allocate mem for command 0x%x", command);
969 return -ENOMEM;
970 }
971
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700972 ALOGVV("%s %d", __func__, command);
973
974 cmd->cmd = command;
975 list_add_tail(&out->offload_cmd_list, &cmd->node);
976 pthread_cond_signal(&out->offload_cond);
977 return 0;
978}
979
980/* must be called iwth out->lock locked */
981static void stop_compressed_output_l(struct stream_out *out)
982{
983 out->offload_state = OFFLOAD_STATE_IDLE;
984 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700985 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700986 if (out->compr != NULL) {
987 compress_stop(out->compr);
988 while (out->offload_thread_blocked) {
989 pthread_cond_wait(&out->cond, &out->lock);
990 }
991 }
992}
993
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700994bool is_offload_usecase(audio_usecase_t uc_id)
995{
996 unsigned int i;
997 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
998 if (uc_id == offload_usecases[i])
999 return true;
1000 }
1001 return false;
1002}
1003
1004static audio_usecase_t get_offload_usecase(struct audio_device *adev)
1005{
1006 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1007 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
1008 char value[PROPERTY_VALUE_MAX] = {0};
1009
1010 property_get("audio.offload.multiple.enabled", value, NULL);
1011 if (!(atoi(value) || !strncmp("true", value, 4)))
1012 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
1013
1014 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
1015 for (i = 0; i < num_usecase; i++) {
1016 if (!(adev->offload_usecases_state & (0x1<<i))) {
1017 adev->offload_usecases_state |= 0x1 << i;
1018 ret = offload_usecases[i];
1019 break;
1020 }
1021 }
1022 ALOGV("%s: offload usecase is %d", __func__, ret);
1023 return ret;
1024}
1025
1026static void free_offload_usecase(struct audio_device *adev,
1027 audio_usecase_t uc_id)
1028{
1029 unsigned int i;
1030 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
1031 if (offload_usecases[i] == uc_id) {
1032 adev->offload_usecases_state &= ~(0x1<<i);
1033 break;
1034 }
1035 }
1036 ALOGV("%s: free offload usecase %d", __func__, uc_id);
1037}
1038
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001039static void *offload_thread_loop(void *context)
1040{
1041 struct stream_out *out = (struct stream_out *) context;
1042 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001043 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001044
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001045 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
1046 set_sched_policy(0, SP_FOREGROUND);
1047 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
1048
1049 ALOGV("%s", __func__);
1050 pthread_mutex_lock(&out->lock);
1051 for (;;) {
1052 struct offload_cmd *cmd = NULL;
1053 stream_callback_event_t event;
1054 bool send_callback = false;
1055
1056 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
1057 __func__, list_empty(&out->offload_cmd_list),
1058 out->offload_state);
1059 if (list_empty(&out->offload_cmd_list)) {
1060 ALOGV("%s SLEEPING", __func__);
1061 pthread_cond_wait(&out->offload_cond, &out->lock);
1062 ALOGV("%s RUNNING", __func__);
1063 continue;
1064 }
1065
1066 item = list_head(&out->offload_cmd_list);
1067 cmd = node_to_item(item, struct offload_cmd, node);
1068 list_remove(item);
1069
1070 ALOGVV("%s STATE %d CMD %d out->compr %p",
1071 __func__, out->offload_state, cmd->cmd, out->compr);
1072
1073 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
1074 free(cmd);
1075 break;
1076 }
1077
1078 if (out->compr == NULL) {
1079 ALOGE("%s: Compress handle is NULL", __func__);
1080 pthread_cond_signal(&out->cond);
1081 continue;
1082 }
1083 out->offload_thread_blocked = true;
1084 pthread_mutex_unlock(&out->lock);
1085 send_callback = false;
1086 switch(cmd->cmd) {
1087 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001088 ALOGD("copl(%p):calling compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001089 compress_wait(out->compr, -1);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001090 ALOGD("copl(%p):out of compress_wait", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001091 send_callback = true;
1092 event = STREAM_CBK_EVENT_WRITE_READY;
1093 break;
1094 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001095 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301096 if(ret == 0) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001097 ALOGD("copl(%p):calling compress_partial_drain", out);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001098 compress_partial_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001099 ALOGD("copl(%p):out of compress_partial_drain", out);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301100 }
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001101 else if(ret == -ETIMEDOUT)
1102 compress_drain(out->compr);
1103 else
1104 ALOGE("%s: Next track returned error %d",__func__, ret);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001105 send_callback = true;
1106 event = STREAM_CBK_EVENT_DRAIN_READY;
1107 break;
1108 case OFFLOAD_CMD_DRAIN:
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001109 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001110 compress_drain(out->compr);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001111 ALOGD("copl(%p):calling compress_drain", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001112 send_callback = true;
1113 event = STREAM_CBK_EVENT_DRAIN_READY;
1114 break;
1115 default:
1116 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1117 break;
1118 }
1119 pthread_mutex_lock(&out->lock);
1120 out->offload_thread_blocked = false;
1121 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001122 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001123 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001124 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001125 free(cmd);
1126 }
1127
1128 pthread_cond_signal(&out->cond);
1129 while (!list_empty(&out->offload_cmd_list)) {
1130 item = list_head(&out->offload_cmd_list);
1131 list_remove(item);
1132 free(node_to_item(item, struct offload_cmd, node));
1133 }
1134 pthread_mutex_unlock(&out->lock);
1135
1136 return NULL;
1137}
1138
1139static int create_offload_callback_thread(struct stream_out *out)
1140{
1141 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1142 list_init(&out->offload_cmd_list);
1143 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1144 offload_thread_loop, out);
1145 return 0;
1146}
1147
1148static int destroy_offload_callback_thread(struct stream_out *out)
1149{
1150 pthread_mutex_lock(&out->lock);
1151 stop_compressed_output_l(out);
1152 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1153
1154 pthread_mutex_unlock(&out->lock);
1155 pthread_join(out->offload_thread, (void **) NULL);
1156 pthread_cond_destroy(&out->offload_cond);
1157
1158 return 0;
1159}
1160
Eric Laurent07eeafd2013-10-06 12:52:49 -07001161static bool allow_hdmi_channel_config(struct audio_device *adev)
1162{
1163 struct listnode *node;
1164 struct audio_usecase *usecase;
1165 bool ret = true;
1166
1167 list_for_each(node, &adev->usecase_list) {
1168 usecase = node_to_item(node, struct audio_usecase, list);
1169 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1170 /*
1171 * If voice call is already existing, do not proceed further to avoid
1172 * disabling/enabling both RX and TX devices, CSD calls, etc.
1173 * Once the voice call done, the HDMI channels can be configured to
1174 * max channels of remaining use cases.
1175 */
1176 if (usecase->id == USECASE_VOICE_CALL) {
1177 ALOGD("%s: voice call is active, no change in HDMI channels",
1178 __func__);
1179 ret = false;
1180 break;
1181 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1182 ALOGD("%s: multi channel playback is active, "
1183 "no change in HDMI channels", __func__);
1184 ret = false;
1185 break;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001186 } else if (is_offload_usecase(usecase->id) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07001187 audio_channel_count_from_out_mask(usecase->stream.out->channel_mask) > 2) {
Mingming Yin139f1072014-02-24 17:56:01 -08001188 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1189 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1190 ret = false;
1191 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001192 }
1193 }
1194 }
1195 return ret;
1196}
1197
1198static int check_and_set_hdmi_channels(struct audio_device *adev,
1199 unsigned int channels)
1200{
1201 struct listnode *node;
1202 struct audio_usecase *usecase;
1203
1204 /* Check if change in HDMI channel config is allowed */
1205 if (!allow_hdmi_channel_config(adev))
1206 return 0;
1207
1208 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001209 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001210 return 0;
1211 }
1212
1213 platform_set_hdmi_channels(adev->platform, channels);
1214 adev->cur_hdmi_channels = channels;
1215
1216 /*
1217 * Deroute all the playback streams routed to HDMI so that
1218 * the back end is deactivated. Note that backend will not
1219 * be deactivated if any one stream is connected to it.
1220 */
1221 list_for_each(node, &adev->usecase_list) {
1222 usecase = node_to_item(node, struct audio_usecase, list);
1223 if (usecase->type == PCM_PLAYBACK &&
1224 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001225 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001226 }
1227 }
1228
1229 /*
1230 * Enable all the streams disabled above. Now the HDMI backend
1231 * will be activated with new channel configuration
1232 */
1233 list_for_each(node, &adev->usecase_list) {
1234 usecase = node_to_item(node, struct audio_usecase, list);
1235 if (usecase->type == PCM_PLAYBACK &&
1236 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001237 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001238 }
1239 }
1240
1241 return 0;
1242}
1243
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001244static int stop_output_stream(struct stream_out *out)
1245{
1246 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001247 struct audio_usecase *uc_info;
1248 struct audio_device *adev = out->dev;
1249
Eric Laurent994a6932013-07-17 11:51:42 -07001250 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001251 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001252 uc_info = get_usecase_from_list(adev, out->usecase);
1253 if (uc_info == NULL) {
1254 ALOGE("%s: Could not find the usecase (%d) in the list",
1255 __func__, out->usecase);
1256 return -EINVAL;
1257 }
1258
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001259 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001260 if (adev->visualizer_stop_output != NULL)
1261 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1262 if (adev->offload_effects_stop_output != NULL)
1263 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1264 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001265
Eric Laurent150dbfe2013-02-27 14:31:02 -08001266 /* 1. Get and set stream specific mixer controls */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001267 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001268
1269 /* 2. Disable the rx device */
Haynes Mathew George1376ca62014-04-24 11:55:48 -07001270 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001271
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001272 list_remove(&uc_info->list);
1273 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001274
Eric Laurent07eeafd2013-10-06 12:52:49 -07001275 /* Must be called after removing the usecase from list */
1276 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1277 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1278
Eric Laurent994a6932013-07-17 11:51:42 -07001279 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001280 return ret;
1281}
1282
1283int start_output_stream(struct stream_out *out)
1284{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001285 int ret = 0;
Mingming Yin9c041392014-05-01 15:37:31 -07001286 int sink_channels = 0;
1287 char prop_value[PROPERTY_VALUE_MAX] = {0};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001288 struct audio_usecase *uc_info;
1289 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301290 int snd_card_status = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001291
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001292 if ((out->usecase < 0) || (out->usecase >= AUDIO_USECASE_MAX)) {
1293 ret = -EINVAL;
1294 goto error_config;
1295 }
1296
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301297 ALOGD("%s: enter: stream(%p)usecase(%d: %s) devices(%#x)",
1298 __func__, &out->stream, out->usecase, use_case_table[out->usecase],
1299 out->devices);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301300
Naresh Tanniru80659832014-06-04 18:17:56 +05301301 if (SND_CARD_STATE_OFFLINE == snd_card_status) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301302 ALOGE("%s: sound card is not active/SSR returning error", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05301303 ret = -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301304 goto error_config;
1305 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05301306
Eric Laurentb23d5282013-05-14 15:27:20 -07001307 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001308 if (out->pcm_device_id < 0) {
1309 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1310 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001311 ret = -EINVAL;
1312 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001313 }
1314
1315 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001316
1317 if (!uc_info) {
1318 ret = -ENOMEM;
1319 goto error_config;
1320 }
1321
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001322 uc_info->id = out->usecase;
1323 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001324 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001325 uc_info->devices = out->devices;
1326 uc_info->in_snd_device = SND_DEVICE_NONE;
1327 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001328
Eric Laurent07eeafd2013-10-06 12:52:49 -07001329 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001330 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Mingming Yin9c041392014-05-01 15:37:31 -07001331 property_get("audio.use.hdmi.sink.cap", prop_value, NULL);
1332 if (!strncmp("true", prop_value, 4)) {
1333 sink_channels = platform_edid_get_max_channels(out->dev->platform);
1334 ALOGD("%s: set HDMI channel count[%d] based on sink capability", __func__, sink_channels);
1335 check_and_set_hdmi_channels(adev, sink_channels);
1336 } else {
1337 if (is_offload_usecase(out->usecase))
1338 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1339 else
1340 check_and_set_hdmi_channels(adev, out->config.channels);
1341 }
Mingming Yin10fef6a2013-11-26 17:17:01 -08001342 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001343
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001344 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001345
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001346 select_devices(adev, out->usecase);
1347
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001348 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1349 __func__, 0, out->pcm_device_id);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001350 if (!is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001351 out->pcm = pcm_open(adev->snd_card,
1352 out->pcm_device_id,
1353 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001354 if (out->pcm && !pcm_is_ready(out->pcm)) {
1355 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1356 pcm_close(out->pcm);
1357 out->pcm = NULL;
1358 ret = -EIO;
1359 goto error_open;
1360 }
1361 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001362 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001363 out->compr = compress_open(adev->snd_card,
1364 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001365 COMPRESS_IN, &out->compr_config);
1366 if (out->compr && !is_compress_ready(out->compr)) {
1367 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1368 compress_close(out->compr);
1369 out->compr = NULL;
1370 ret = -EIO;
1371 goto error_open;
1372 }
1373 if (out->offload_callback)
1374 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001375
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001376#ifdef DS1_DOLBY_DDP_ENABLED
1377 if (audio_extn_is_dolby_format(out->format))
1378 audio_extn_dolby_send_ddp_endp_params(adev);
1379#endif
1380
Eric Laurentc4aef752013-09-12 17:45:53 -07001381 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001382 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1383 if (adev->offload_effects_start_output != NULL)
1384 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001385 }
Eric Laurent994a6932013-07-17 11:51:42 -07001386 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001387 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001388error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001389 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001390error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001391 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001392}
1393
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001394static int check_input_parameters(uint32_t sample_rate,
1395 audio_format_t format,
1396 int channel_count)
1397{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001398 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001399
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001400 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001401 !voice_extn_compress_voip_is_format_supported(format) &&
1402 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001403
1404 switch (channel_count) {
1405 case 1:
1406 case 2:
1407 case 6:
1408 break;
1409 default:
1410 ret = -EINVAL;
1411 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001412
1413 switch (sample_rate) {
1414 case 8000:
1415 case 11025:
1416 case 12000:
1417 case 16000:
1418 case 22050:
1419 case 24000:
1420 case 32000:
1421 case 44100:
1422 case 48000:
1423 break;
1424 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001425 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001426 }
1427
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001428 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001429}
1430
1431static size_t get_input_buffer_size(uint32_t sample_rate,
1432 audio_format_t format,
1433 int channel_count)
1434{
1435 size_t size = 0;
1436
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001437 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1438 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001439
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001440 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1441 /* ToDo: should use frame_size computed based on the format and
1442 channel_count here. */
1443 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001444
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001445 /* make sure the size is multiple of 64 */
1446 size += 0x3f;
1447 size &= ~0x3f;
1448
1449 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001450}
1451
1452static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1453{
1454 struct stream_out *out = (struct stream_out *)stream;
1455
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001456 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001457}
1458
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001459static int out_set_sample_rate(struct audio_stream *stream __unused,
1460 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001461{
1462 return -ENOSYS;
1463}
1464
1465static size_t out_get_buffer_size(const struct audio_stream *stream)
1466{
1467 struct stream_out *out = (struct stream_out *)stream;
1468
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001469 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001470 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001471 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1472 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001473
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07001474 return out->config.period_size *
1475 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001476}
1477
1478static uint32_t out_get_channels(const struct audio_stream *stream)
1479{
1480 struct stream_out *out = (struct stream_out *)stream;
1481
1482 return out->channel_mask;
1483}
1484
1485static audio_format_t out_get_format(const struct audio_stream *stream)
1486{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001487 struct stream_out *out = (struct stream_out *)stream;
1488
1489 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001490}
1491
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001492static int out_set_format(struct audio_stream *stream __unused,
1493 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001494{
1495 return -ENOSYS;
1496}
1497
1498static int out_standby(struct audio_stream *stream)
1499{
1500 struct stream_out *out = (struct stream_out *)stream;
1501 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001502
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301503 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
1504 stream, out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001505 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1506 /* Ignore standby in case of voip call because the voip output
1507 * stream is closed in adev_close_output_stream()
1508 */
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05301509 ALOGD("%s: Ignore Standby in VOIP call", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001510 return 0;
1511 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001512
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001513 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001514 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001515 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001516 out->standby = true;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001517 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001518 if (out->pcm) {
1519 pcm_close(out->pcm);
1520 out->pcm = NULL;
1521 }
1522 } else {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001523 ALOGD("copl(%p):standby", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001524 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001525 out->gapless_mdata.encoder_delay = 0;
1526 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001527 if (out->compr != NULL) {
1528 compress_close(out->compr);
1529 out->compr = NULL;
1530 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001531 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001532 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001533 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001534 }
1535 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001536 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001537 return 0;
1538}
1539
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001540static int out_dump(const struct audio_stream *stream __unused,
1541 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001542{
1543 return 0;
1544}
1545
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001546static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1547{
1548 int ret = 0;
1549 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001550 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001551 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001552 tmp_mdata.encoder_delay = 0;
1553 tmp_mdata.encoder_padding = 0;
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001554
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001555 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001556 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001557 return -EINVAL;
1558 }
1559
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001560 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1561 if (ret >= 0) {
1562 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1563 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1564 ALOGV("ADTS format is set in offload mode");
1565 }
1566 out->send_new_metadata = 1;
1567 }
1568
Mingming Yin3ee55c62014-08-04 14:23:35 -07001569 if (out->format == AUDIO_FORMAT_FLAC) {
1570 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_BLK_SIZE, value, sizeof(value));
1571 if (ret >= 0) {
1572 out->compr_config.codec->options.flac_dec.min_blk_size = atoi(value);
1573 out->send_new_metadata = 1;
1574 }
1575 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_BLK_SIZE, value, sizeof(value));
1576 if (ret >= 0) {
1577 out->compr_config.codec->options.flac_dec.max_blk_size = atoi(value);
1578 out->send_new_metadata = 1;
1579 }
1580 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MIN_FRAME_SIZE, value, sizeof(value));
1581 if (ret >= 0) {
1582 out->compr_config.codec->options.flac_dec.min_frame_size = atoi(value);
1583 out->send_new_metadata = 1;
1584 }
1585 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FLAC_MAX_FRAME_SIZE, value, sizeof(value));
1586 if (ret >= 0) {
1587 out->compr_config.codec->options.flac_dec.max_frame_size = atoi(value);
1588 out->send_new_metadata = 1;
1589 }
1590 }
1591
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001592 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1593 if(ret >= 0)
1594 is_meta_data_params = true;
1595 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1596 if(ret >= 0 )
1597 is_meta_data_params = true;
1598 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1599 if(ret >= 0 )
1600 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001601 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1602 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001603 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001604 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001605 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001606 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1607 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001608 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001609 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001610 }
1611
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001612 if(!is_meta_data_params) {
1613 ALOGV("%s: Not gapless meta data params", __func__);
1614 return 0;
1615 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001616 out->gapless_mdata = tmp_mdata;
1617 out->send_new_metadata = 1;
1618 ALOGV("%s new encoder delay %u and padding %u", __func__,
1619 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1620
1621 return 0;
1622}
1623
1624
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001625static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1626{
1627 struct stream_out *out = (struct stream_out *)stream;
1628 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001629 struct audio_usecase *usecase;
1630 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001631 struct str_parms *parms;
1632 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001633 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001634 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001635
sangwoobc677242013-08-08 16:53:43 +09001636 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001637 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001638 parms = str_parms_create_str(kvpairs);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301639 if (!parms)
1640 goto error;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001641 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1642 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001643 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001644 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001645 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001646
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001647 /*
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301648 * When HDMI cable is unplugged/usb hs is disconnected the
1649 * music playback is paused and the policy manager sends routing=0
1650 * But the audioflingercontinues to write data until standby time
1651 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001652 * Avoid this by routing audio to speaker until standby.
1653 */
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301654 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1655 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001656 val == AUDIO_DEVICE_NONE) {
1657 val = AUDIO_DEVICE_OUT_SPEAKER;
1658 }
1659
1660 /*
1661 * select_devices() call below switches all the usecases on the same
1662 * backend to the new device. Refer to check_usecases_codec_backend() in
1663 * the select_devices(). But how do we undo this?
1664 *
1665 * For example, music playback is active on headset (deep-buffer usecase)
1666 * and if we go to ringtones and select a ringtone, low-latency usecase
1667 * will be started on headset+speaker. As we can't enable headset+speaker
1668 * and headset devices at the same time, select_devices() switches the music
1669 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1670 * So when the ringtone playback is completed, how do we undo the same?
1671 *
1672 * We are relying on the out_set_parameters() call on deep-buffer output,
1673 * once the ringtone playback is ended.
1674 * NOTE: We should not check if the current devices are same as new devices.
1675 * Because select_devices() must be called to switch back the music
1676 * playback to headset.
1677 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001678 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001679 out->devices = val;
1680
1681 if (!out->standby)
1682 select_devices(adev, out->usecase);
1683
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001684 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001685 !adev->voice.in_call &&
1686 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001687 ret = voice_start_call(adev);
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001688 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1689 adev->voice.in_call &&
1690 (out == adev->primary_output)) {
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -08001691 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001692 }
1693 }
1694
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001695 if ((adev->mode == AUDIO_MODE_NORMAL) &&
Vidyakumar Athotaad34d572014-08-05 18:20:42 -07001696 adev->voice.in_call &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001697 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001698 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001699 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001700
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001701 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001702 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001703 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001704
1705 if (out == adev->primary_output) {
1706 pthread_mutex_lock(&adev->lock);
1707 audio_extn_set_parameters(adev, parms);
1708 pthread_mutex_unlock(&adev->lock);
1709 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001710 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001711 pthread_mutex_lock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001712 parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001713 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001714 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001715
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001716 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05301717error:
Eric Laurent994a6932013-07-17 11:51:42 -07001718 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001719 return ret;
1720}
1721
1722static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1723{
1724 struct stream_out *out = (struct stream_out *)stream;
1725 struct str_parms *query = str_parms_create_str(keys);
1726 char *str;
1727 char value[256];
1728 struct str_parms *reply = str_parms_create();
1729 size_t i, j;
1730 int ret;
1731 bool first = true;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001732
1733 if (!query || !reply) {
1734 ALOGE("out_get_parameters: failed to allocate mem for query or reply");
1735 return NULL;
1736 }
1737
Eric Laurent994a6932013-07-17 11:51:42 -07001738 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001739 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1740 if (ret >= 0) {
1741 value[0] = '\0';
1742 i = 0;
1743 while (out->supported_channel_masks[i] != 0) {
1744 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1745 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1746 if (!first) {
1747 strcat(value, "|");
1748 }
1749 strcat(value, out_channels_name_to_enum_table[j].name);
1750 first = false;
1751 break;
1752 }
1753 }
1754 i++;
1755 }
1756 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1757 str = str_parms_to_str(reply);
1758 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001759 voice_extn_out_get_parameters(out, query, reply);
1760 str = str_parms_to_str(reply);
1761 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella29b8fc72014-01-29 12:52:19 -08001762 free(str);
1763 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001764 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001765 }
1766 str_parms_destroy(query);
1767 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001768 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001769 return str;
1770}
1771
1772static uint32_t out_get_latency(const struct audio_stream_out *stream)
1773{
1774 struct stream_out *out = (struct stream_out *)stream;
1775
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001776 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001777 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1778
1779 return (out->config.period_count * out->config.period_size * 1000) /
1780 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001781}
1782
1783static int out_set_volume(struct audio_stream_out *stream, float left,
1784 float right)
1785{
Eric Laurenta9024de2013-04-04 09:19:12 -07001786 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001787 int volume[2];
1788
Eric Laurenta9024de2013-04-04 09:19:12 -07001789 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1790 /* only take left channel into account: the API is for stereo anyway */
1791 out->muted = (left == 0.0f);
1792 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001793 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001794 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001795 struct audio_device *adev = out->dev;
1796 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001797 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1798 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001799
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001800 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1801 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001802 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1803 if (!ctl) {
1804 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1805 __func__, mixer_ctl_name);
1806 return -EINVAL;
1807 }
1808 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1809 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1810 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1811 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001812 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001813
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001814 return -ENOSYS;
1815}
1816
1817static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1818 size_t bytes)
1819{
1820 struct stream_out *out = (struct stream_out *)stream;
1821 struct audio_device *adev = out->dev;
Naresh Tanniru80659832014-06-04 18:17:56 +05301822 int snd_scard_state = get_snd_card_state(adev);
Eric Laurent6e895242013-09-05 16:10:57 -07001823 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001824
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001825 pthread_mutex_lock(&out->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301826
Naresh Tanniru80659832014-06-04 18:17:56 +05301827 if (SND_CARD_STATE_OFFLINE == snd_scard_state) {
1828 if (out->pcm) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05301829 ALOGD(" %s: sound card is not active/SSR state", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05301830 ret= -EIO;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301831 goto exit;
Naresh Tanniru80659832014-06-04 18:17:56 +05301832 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1833 //during SSR for compress usecase we should return error to flinger
1834 ALOGD(" copl %s: sound card is not active/SSR state", __func__);
1835 pthread_mutex_unlock(&out->lock);
1836 return -ENETRESET;
Naresh Tanniru4c630392014-05-12 01:05:52 +05301837 }
1838 }
1839
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001840 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001841 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001842 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001843 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1844 ret = voice_extn_compress_voip_start_output_stream(out);
1845 else
1846 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001847 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001848 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001849 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001850 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001851 goto exit;
1852 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001853 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001854
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001855 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001856 ALOGD("copl(%p): writing buffer (%zu bytes) to compress device", out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001857 if (out->send_new_metadata) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07001858 ALOGD("copl(%p):send new gapless metadata", out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001859 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1860 out->send_new_metadata = 0;
1861 }
1862
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001863 ret = compress_write(out->compr, buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301864 if (ret < 0)
1865 ret = -errno;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001866 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001867 if (ret >= 0 && ret < (ssize_t)bytes) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301868 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001869 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
Naresh Tanniru80659832014-06-04 18:17:56 +05301870 } else if (-ENETRESET == ret) {
1871 ALOGE("copl %s: received sound card offline state on compress write", __func__);
1872 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1873 pthread_mutex_unlock(&out->lock);
1874 out_standby(&out->stream.common);
1875 return ret;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001876 }
Naresh Tanniru80659832014-06-04 18:17:56 +05301877 if (!out->playback_started && ret >= 0) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001878 compress_start(out->compr);
1879 out->playback_started = 1;
1880 out->offload_state = OFFLOAD_STATE_PLAYING;
1881 }
1882 pthread_mutex_unlock(&out->lock);
1883 return ret;
1884 } else {
1885 if (out->pcm) {
1886 if (out->muted)
1887 memset((void *)buffer, 0, bytes);
1888 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1889 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301890 if (ret < 0)
1891 ret = -errno;
1892 else if (ret == 0)
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001893 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001894 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001895 }
1896
1897exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05301898 /* ToDo: There may be a corner case when SSR happens back to back during
1899 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05301900 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301901 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05301902 }
1903
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001904 pthread_mutex_unlock(&out->lock);
1905
1906 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001907 if (out->pcm)
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001908 ALOGE("%s: error %ld - %s", __func__, ret, pcm_get_error(out->pcm));
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301909 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05301910 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301911 voice_extn_compress_voip_close_output_stream(&out->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05301912 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05301913 out->standby = true;
1914 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001915 out_standby(&out->stream.common);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07001916 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05301917 out_get_sample_rate(&out->stream.common));
1918
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001919 }
1920 return bytes;
1921}
1922
1923static int out_get_render_position(const struct audio_stream_out *stream,
1924 uint32_t *dsp_frames)
1925{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001926 struct stream_out *out = (struct stream_out *)stream;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001927 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301928 ssize_t ret = -EINVAL;
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07001929 *dsp_frames = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001930 pthread_mutex_lock(&out->lock);
1931 if (out->compr != NULL) {
Naresh Tanniru80659832014-06-04 18:17:56 +05301932 ret = compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001933 &out->sample_rate);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05301934 if (ret < 0)
1935 ret = -errno;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001936 ALOGVV("%s rendered frames %d sample_rate %d",
1937 __func__, *dsp_frames, out->sample_rate);
1938 }
1939 pthread_mutex_unlock(&out->lock);
Naresh Tanniru80659832014-06-04 18:17:56 +05301940 if (-ENETRESET == ret) {
1941 ALOGE(" ERROR: sound card not active Unable to get time stamp from compress driver");
1942 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
1943 return -EINVAL;
1944 } else if(ret < 0) {
1945 ALOGE(" ERROR: Unable to get time stamp from compress driver");
1946 return -EINVAL;
1947 } else {
1948 return 0;
1949 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001950 } else
1951 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001952}
1953
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001954static int out_add_audio_effect(const struct audio_stream *stream __unused,
1955 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001956{
1957 return 0;
1958}
1959
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001960static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1961 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001962{
1963 return 0;
1964}
1965
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001966static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1967 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001968{
1969 return -EINVAL;
1970}
1971
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001972static int out_get_presentation_position(const struct audio_stream_out *stream,
1973 uint64_t *frames, struct timespec *timestamp)
1974{
1975 struct stream_out *out = (struct stream_out *)stream;
1976 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001977 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001978
1979 pthread_mutex_lock(&out->lock);
1980
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001981 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07001982 if (out->compr != NULL) {
1983 compress_get_tstamp(out->compr, &dsp_frames,
1984 &out->sample_rate);
1985 ALOGVV("%s rendered frames %ld sample_rate %d",
1986 __func__, dsp_frames, out->sample_rate);
1987 *frames = dsp_frames;
1988 ret = 0;
1989 /* this is the best we can do */
1990 clock_gettime(CLOCK_MONOTONIC, timestamp);
1991 }
1992 } else {
1993 if (out->pcm) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07001994 unsigned int avail;
Eric Laurent949a0892013-09-20 09:20:13 -07001995 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1996 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001997 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001998 // This adjustment accounts for buffering after app processor.
1999 // It is based on estimated DSP latency per use case, rather than exact.
2000 signed_frames -=
2001 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
2002
Eric Laurent949a0892013-09-20 09:20:13 -07002003 // It would be unusual for this value to be negative, but check just in case ...
2004 if (signed_frames >= 0) {
2005 *frames = signed_frames;
2006 ret = 0;
2007 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002008 }
2009 }
2010 }
2011
2012 pthread_mutex_unlock(&out->lock);
2013
2014 return ret;
2015}
2016
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002017static int out_set_callback(struct audio_stream_out *stream,
2018 stream_callback_t callback, void *cookie)
2019{
2020 struct stream_out *out = (struct stream_out *)stream;
2021
2022 ALOGV("%s", __func__);
2023 pthread_mutex_lock(&out->lock);
2024 out->offload_callback = callback;
2025 out->offload_cookie = cookie;
2026 pthread_mutex_unlock(&out->lock);
2027 return 0;
2028}
2029
2030static int out_pause(struct audio_stream_out* stream)
2031{
2032 struct stream_out *out = (struct stream_out *)stream;
2033 int status = -ENOSYS;
2034 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002035 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002036 ALOGD("copl(%p):pause compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002037 pthread_mutex_lock(&out->lock);
2038 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302039 struct audio_device *adev = out->dev;
2040 int snd_scard_state = get_snd_card_state(adev);
2041
2042 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2043 status = compress_pause(out->compr);
2044
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002045 out->offload_state = OFFLOAD_STATE_PAUSED;
2046 }
2047 pthread_mutex_unlock(&out->lock);
2048 }
2049 return status;
2050}
2051
2052static int out_resume(struct audio_stream_out* stream)
2053{
2054 struct stream_out *out = (struct stream_out *)stream;
2055 int status = -ENOSYS;
2056 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002057 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002058 ALOGD("copl(%p):resume compress driver", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002059 status = 0;
2060 pthread_mutex_lock(&out->lock);
2061 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302062 struct audio_device *adev = out->dev;
2063 int snd_scard_state = get_snd_card_state(adev);
2064
2065 if (SND_CARD_STATE_ONLINE == snd_scard_state)
2066 status = compress_resume(out->compr);
2067
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002068 out->offload_state = OFFLOAD_STATE_PLAYING;
2069 }
2070 pthread_mutex_unlock(&out->lock);
2071 }
2072 return status;
2073}
2074
2075static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
2076{
2077 struct stream_out *out = (struct stream_out *)stream;
2078 int status = -ENOSYS;
2079 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002080 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002081 pthread_mutex_lock(&out->lock);
2082 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
2083 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
2084 else
2085 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
2086 pthread_mutex_unlock(&out->lock);
2087 }
2088 return status;
2089}
2090
2091static int out_flush(struct audio_stream_out* stream)
2092{
2093 struct stream_out *out = (struct stream_out *)stream;
2094 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002095 if (is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002096 ALOGD("copl(%p):calling compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002097 pthread_mutex_lock(&out->lock);
2098 stop_compressed_output_l(out);
2099 pthread_mutex_unlock(&out->lock);
Apoorv Raghuvanshi44bd9172014-05-28 14:50:07 -07002100 ALOGD("copl(%p):out of compress flush", out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002101 return 0;
2102 }
2103 return -ENOSYS;
2104}
2105
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002106/** audio_stream_in implementation **/
2107static uint32_t in_get_sample_rate(const struct audio_stream *stream)
2108{
2109 struct stream_in *in = (struct stream_in *)stream;
2110
2111 return in->config.rate;
2112}
2113
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002114static int in_set_sample_rate(struct audio_stream *stream __unused,
2115 uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002116{
2117 return -ENOSYS;
2118}
2119
2120static size_t in_get_buffer_size(const struct audio_stream *stream)
2121{
2122 struct stream_in *in = (struct stream_in *)stream;
2123
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002124 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
2125 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07002126 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
2127 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002128
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002129 return in->config.period_size *
2130 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002131}
2132
2133static uint32_t in_get_channels(const struct audio_stream *stream)
2134{
2135 struct stream_in *in = (struct stream_in *)stream;
2136
2137 return in->channel_mask;
2138}
2139
2140static audio_format_t in_get_format(const struct audio_stream *stream)
2141{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002142 struct stream_in *in = (struct stream_in *)stream;
2143
2144 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002145}
2146
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002147static int in_set_format(struct audio_stream *stream __unused,
2148 audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002149{
2150 return -ENOSYS;
2151}
2152
2153static int in_standby(struct audio_stream *stream)
2154{
2155 struct stream_in *in = (struct stream_in *)stream;
2156 struct audio_device *adev = in->dev;
2157 int status = 0;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302158 ALOGD("%s: enter: stream (%p) usecase(%d: %s)", __func__,
2159 stream, in->usecase, use_case_table[in->usecase]);
2160
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002161 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2162 /* Ignore standby in case of voip call because the voip input
2163 * stream is closed in adev_close_input_stream()
2164 */
2165 ALOGV("%s: Ignore Standby in VOIP call", __func__);
2166 return status;
2167 }
2168
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002169 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002170 if (!in->standby && in->is_st_session) {
2171 ALOGD("%s: sound trigger pcm stop lab", __func__);
2172 audio_extn_sound_trigger_stop_lab(in);
2173 in->standby = 1;
2174 }
2175
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002176 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08002177 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002178 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08002179 if (in->pcm) {
2180 pcm_close(in->pcm);
2181 in->pcm = NULL;
2182 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002183 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002184 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002185 }
2186 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002187 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002188 return status;
2189}
2190
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002191static int in_dump(const struct audio_stream *stream __unused,
2192 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002193{
2194 return 0;
2195}
2196
2197static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
2198{
2199 struct stream_in *in = (struct stream_in *)stream;
2200 struct audio_device *adev = in->dev;
2201 struct str_parms *parms;
2202 char *str;
2203 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002204 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002205
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302206 ALOGD("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002207 parms = str_parms_create_str(kvpairs);
2208
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302209 if (!parms)
2210 goto error;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002211 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002212 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002213
2214 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
2215 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002216 val = atoi(value);
2217 /* no audio source uses val == 0 */
2218 if ((in->source != val) && (val != 0)) {
2219 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002220 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2221 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2222 (voice_extn_compress_voip_is_format_supported(in->format)) &&
2223 (in->config.rate == 8000 || in->config.rate == 16000) &&
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002224 (audio_channel_count_from_in_mask(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002225 err = voice_extn_compress_voip_open_input_stream(in);
2226 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002227 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08002228 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002229 }
2230 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002231 }
2232 }
2233
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002234 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2235 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002236 val = atoi(value);
2237 if ((in->device != val) && (val != 0)) {
2238 in->device = val;
2239 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002240 if (!in->standby && !in->is_st_session)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002241 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002242 }
2243 }
2244
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002245done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002246 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002247 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002248
2249 str_parms_destroy(parms);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302250error:
Eric Laurent994a6932013-07-17 11:51:42 -07002251 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002252 return ret;
2253}
2254
2255static char* in_get_parameters(const struct audio_stream *stream,
2256 const char *keys)
2257{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002258 struct stream_in *in = (struct stream_in *)stream;
2259 struct str_parms *query = str_parms_create_str(keys);
2260 char *str;
2261 char value[256];
2262 struct str_parms *reply = str_parms_create();
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002263
2264 if (!query || !reply) {
2265 ALOGE("in_get_parameters: failed to create query or reply");
2266 return NULL;
2267 }
2268
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002269 ALOGV("%s: enter: keys - %s", __func__, keys);
2270
2271 voice_extn_in_get_parameters(in, query, reply);
2272
2273 str = str_parms_to_str(reply);
2274 str_parms_destroy(query);
2275 str_parms_destroy(reply);
2276
2277 ALOGV("%s: exit: returns - %s", __func__, str);
2278 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002279}
2280
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002281static int in_set_gain(struct audio_stream_in *stream __unused,
2282 float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002283{
2284 return 0;
2285}
2286
2287static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2288 size_t bytes)
2289{
2290 struct stream_in *in = (struct stream_in *)stream;
2291 struct audio_device *adev = in->dev;
2292 int i, ret = -1;
Naresh Tanniru80659832014-06-04 18:17:56 +05302293 int snd_scard_state = get_snd_card_state(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002294
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002295 pthread_mutex_lock(&in->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302296
2297 if (in->pcm) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302298 if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05302299 ALOGD(" %s: sound card is not active/SSR state", __func__);
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302300 ret= -EIO;;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302301 goto exit;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302302 }
2303 }
2304
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002305 if (in->standby) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002306 if (!in->is_st_session) {
2307 pthread_mutex_lock(&adev->lock);
2308 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2309 ret = voice_extn_compress_voip_start_input_stream(in);
2310 else
2311 ret = start_input_stream(in);
2312 pthread_mutex_unlock(&adev->lock);
2313 if (ret != 0) {
2314 goto exit;
2315 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002316 }
2317 in->standby = 0;
2318 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002319
2320 if (in->pcm) {
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002321 if (audio_extn_ssr_get_enabled() &&
2322 audio_channel_count_from_in_mask(in->channel_mask) == 6)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002323 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002324 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2325 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002326 else
2327 ret = pcm_read(in->pcm, buffer, bytes);
Dhanalakshmi Siddani37ca1d62014-08-20 12:28:34 +05302328 if (ret < 0)
2329 ret = -errno;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002330 }
2331
2332 /*
2333 * Instead of writing zeroes here, we could trust the hardware
2334 * to always provide zeroes when muted.
2335 */
kunleizc5a639b2014-04-24 18:46:22 +08002336 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call_rec_stream(in))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002337 memset(buffer, 0, bytes);
2338
2339exit:
Dhanalakshmi Siddani8fc6d912014-05-26 18:03:42 +05302340 /* ToDo: There may be a corner case when SSR happens back to back during
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302341 start/stop. Need to post different error to handle that. */
Naresh Tanniru4c630392014-05-12 01:05:52 +05302342 if (-ENETRESET == ret) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302343 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302344 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002345 pthread_mutex_unlock(&in->lock);
2346
2347 if (ret != 0) {
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302348 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302349 pthread_mutex_lock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302350 voice_extn_compress_voip_close_input_stream(&in->stream.common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302351 pthread_mutex_unlock(&adev->lock);
Venkata Narendra Kumar Guttabc9c9ca2014-06-25 20:38:03 +05302352 in->standby = true;
2353 }
Dhanalakshmi Siddani4d57e992014-07-17 16:37:51 +05302354 memset(buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002355 in_standby(&in->stream.common);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002356 ALOGV("%s: read failed status %d- sleeping for buffer duration", __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002357 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Naresh Tanniru4c630392014-05-12 01:05:52 +05302358 in_get_sample_rate(&in->stream.common));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002359 }
2360 return bytes;
2361}
2362
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002363static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002364{
2365 return 0;
2366}
2367
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002368static int add_remove_audio_effect(const struct audio_stream *stream,
2369 effect_handle_t effect,
2370 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002371{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002372 struct stream_in *in = (struct stream_in *)stream;
2373 int status = 0;
2374 effect_descriptor_t desc;
2375
2376 status = (*effect)->get_descriptor(effect, &desc);
2377 if (status != 0)
2378 return status;
2379
2380 pthread_mutex_lock(&in->lock);
2381 pthread_mutex_lock(&in->dev->lock);
2382 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2383 in->enable_aec != enable &&
2384 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2385 in->enable_aec = enable;
2386 if (!in->standby)
2387 select_devices(in->dev, in->usecase);
2388 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002389 if (in->enable_ns != enable &&
2390 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2391 in->enable_ns = enable;
2392 if (!in->standby)
2393 select_devices(in->dev, in->usecase);
2394 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002395 pthread_mutex_unlock(&in->dev->lock);
2396 pthread_mutex_unlock(&in->lock);
2397
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002398 return 0;
2399}
2400
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002401static int in_add_audio_effect(const struct audio_stream *stream,
2402 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002403{
Eric Laurent994a6932013-07-17 11:51:42 -07002404 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002405 return add_remove_audio_effect(stream, effect, true);
2406}
2407
2408static int in_remove_audio_effect(const struct audio_stream *stream,
2409 effect_handle_t effect)
2410{
Eric Laurent994a6932013-07-17 11:51:42 -07002411 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002412 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002413}
2414
2415static int adev_open_output_stream(struct audio_hw_device *dev,
2416 audio_io_handle_t handle,
2417 audio_devices_t devices,
2418 audio_output_flags_t flags,
2419 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002420 struct audio_stream_out **stream_out,
2421 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002422{
2423 struct audio_device *adev = (struct audio_device *)dev;
2424 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002425 int i, ret = 0;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002426 audio_format_t format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002427
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002428 *stream_out = NULL;
Naresh Tanniru80659832014-06-04 18:17:56 +05302429
2430 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
2431 (SND_CARD_STATE_OFFLINE == get_snd_card_state(adev))) {
2432 ALOGE(" sound card is not active rejecting compress output open request");
2433 return -EINVAL;
2434 }
2435
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002436 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2437
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302438 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)\
2439 stream_handle(%p)",__func__, config->sample_rate, config->channel_mask,
2440 devices, flags, &out->stream);
2441
2442
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002443 if (!out) {
2444 return -ENOMEM;
2445 }
2446
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002447 if (devices == AUDIO_DEVICE_NONE)
2448 devices = AUDIO_DEVICE_OUT_SPEAKER;
2449
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002450 out->flags = flags;
2451 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002452 out->dev = adev;
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002453 format = out->format = config->format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002454 out->sample_rate = config->sample_rate;
2455 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2456 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002457 out->handle = handle;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002458 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002459
2460 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002461 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2462 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2463 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2464
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002465 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002466 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2467 ret = read_hdmi_channel_masks(out);
2468
2469 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2470 ret = audio_extn_read_afe_proxy_channel_masks(out);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002471 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002472 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002473 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002474
2475 if (config->sample_rate == 0)
2476 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2477 if (config->channel_mask == 0)
2478 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2479
2480 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002481 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002482 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2483 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002484 out->config.rate = config->sample_rate;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002485 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002486 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002487 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2488 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002489 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002490 ret = voice_extn_compress_voip_open_output_stream(out);
2491 if (ret != 0) {
2492 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2493 __func__, ret);
2494 goto error_open;
2495 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002496 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2497 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2498 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2499 ALOGE("%s: Unsupported Offload information", __func__);
2500 ret = -EINVAL;
2501 goto error_open;
2502 }
Mingming Yin90310102013-11-13 16:57:00 -08002503 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002504 !audio_extn_is_dolby_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002505 ALOGE("%s: Unsupported audio format", __func__);
2506 ret = -EINVAL;
2507 goto error_open;
2508 }
2509
2510 out->compr_config.codec = (struct snd_codec *)
2511 calloc(1, sizeof(struct snd_codec));
2512
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002513 if (!out->compr_config.codec) {
2514 ret = -ENOMEM;
2515 goto error_open;
2516 }
2517
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002518 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002519 if (config->offload_info.channel_mask)
2520 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002521 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002522 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002523 config->offload_info.channel_mask = config->channel_mask;
2524 }
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002525 format = out->format = config->offload_info.format;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002526 out->sample_rate = config->offload_info.sample_rate;
2527
2528 out->stream.set_callback = out_set_callback;
2529 out->stream.pause = out_pause;
2530 out->stream.resume = out_resume;
2531 out->stream.drain = out_drain;
2532 out->stream.flush = out_flush;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002533 out->bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002534
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002535 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002536 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002537 audio_extn_dolby_get_snd_codec_id(adev, out,
2538 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002539 else
2540 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002541 get_snd_codec_id(config->offload_info.format);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002542 if (audio_is_offload_pcm(config->offload_info.format)) {
2543 out->compr_config.fragment_size =
2544 platform_get_pcm_offload_buffer_size(&config->offload_info);
2545 } else {
2546 out->compr_config.fragment_size =
2547 platform_get_compress_offload_buffer_size(&config->offload_info);
2548 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002549 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2550 out->compr_config.codec->sample_rate =
2551 compress_get_alsa_rate(config->offload_info.sample_rate);
2552 out->compr_config.codec->bit_rate =
2553 config->offload_info.bit_rate;
2554 out->compr_config.codec->ch_in =
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002555 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002556 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002557 out->bit_width = PCM_OUTPUT_BIT_WIDTH;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002558
Mingming Yin3ee55c62014-08-04 14:23:35 -07002559 if (config->offload_info.format == AUDIO_FORMAT_AAC)
2560 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002561 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2562 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002563 if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002564 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2565
Mingming Yin3ee55c62014-08-04 14:23:35 -07002566 if (out->bit_width == 24) {
2567 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2568 }
2569
2570 if (out->bit_width == 24 && !platform_check_24_bit_support()) {
2571 ALOGW("24 bit support is not enabled, using 16 bit backend");
2572 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
2573 }
2574
Amit Shekhar6f461b12014-08-01 14:52:58 -07002575 if (config->offload_info.format == AUDIO_FORMAT_FLAC)
Mingming Yinc9efb4f2014-09-19 12:16:36 -07002576 out->compr_config.codec->options.flac_dec.sample_size = PCM_OUTPUT_BIT_WIDTH;
Mingming Yin3ee55c62014-08-04 14:23:35 -07002577
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002578 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2579 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002580
2581 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002582 out->offload_state = OFFLOAD_STATE_IDLE;
2583 out->playback_started = 0;
2584
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002585 create_offload_callback_thread(out);
2586 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2587 __func__, config->offload_info.version,
2588 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002589 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -08002590 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002591
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002592 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2593 ret = voice_check_and_set_incall_music_usecase(adev, out);
2594 if (ret != 0) {
2595 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2596 __func__, ret);
2597 goto error_open;
2598 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002599 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002600 format = AUDIO_FORMAT_PCM_16_BIT;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002601 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2602 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002603 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002604 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002605 /* primary path is the default path selected if no other outputs are available/suitable */
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002606 format = AUDIO_FORMAT_PCM_16_BIT;
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002607 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002608 out->config = pcm_config_deep_buffer;
2609 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002610 }
2611
Amit Shekhar1d896042014-10-03 13:16:09 -07002612 ALOGV("%s devices %d,flags %x, format %x, out->sample_rate %d, out->bit_width %d",
2613 __func__, devices, flags, format, out->sample_rate, out->bit_width);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07002614 audio_extn_utils_update_stream_app_type_cfg(adev->platform,
2615 &adev->streams_output_cfg_list,
Amit Shekhar1d896042014-10-03 13:16:09 -07002616 devices, flags, format, out->sample_rate,
Amit Shekhar6f461b12014-08-01 14:52:58 -07002617 out->bit_width, &out->app_type_cfg);
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002618 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2619 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2620 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002621 if(adev->primary_output == NULL)
2622 adev->primary_output = out;
2623 else {
2624 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002625 ret = -EEXIST;
2626 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002627 }
2628 }
2629
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002630 /* Check if this usecase is already existing */
2631 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07002632 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2633 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002634 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002635 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002636 ret = -EEXIST;
2637 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002638 }
2639 pthread_mutex_unlock(&adev->lock);
2640
2641 out->stream.common.get_sample_rate = out_get_sample_rate;
2642 out->stream.common.set_sample_rate = out_set_sample_rate;
2643 out->stream.common.get_buffer_size = out_get_buffer_size;
2644 out->stream.common.get_channels = out_get_channels;
2645 out->stream.common.get_format = out_get_format;
2646 out->stream.common.set_format = out_set_format;
2647 out->stream.common.standby = out_standby;
2648 out->stream.common.dump = out_dump;
2649 out->stream.common.set_parameters = out_set_parameters;
2650 out->stream.common.get_parameters = out_get_parameters;
2651 out->stream.common.add_audio_effect = out_add_audio_effect;
2652 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2653 out->stream.get_latency = out_get_latency;
2654 out->stream.set_volume = out_set_volume;
2655 out->stream.write = out_write;
2656 out->stream.get_render_position = out_get_render_position;
2657 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002658 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002659
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002660 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002661 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002662 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002663
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002664 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2665 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2666
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002667 config->format = out->stream.common.get_format(&out->stream.common);
2668 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2669 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2670
2671 *stream_out = &out->stream;
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302672 ALOGD("%s: Stream (%p) picks up usecase (%s)", __func__, &out->stream,
2673 use_case_table[out->usecase]);
Eric Laurent994a6932013-07-17 11:51:42 -07002674 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002675 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002676
2677error_open:
2678 free(out);
2679 *stream_out = NULL;
2680 ALOGD("%s: exit: ret %d", __func__, ret);
2681 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002682}
2683
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002684static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002685 struct audio_stream_out *stream)
2686{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002687 struct stream_out *out = (struct stream_out *)stream;
2688 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002689 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002690
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302691 ALOGD("%s: enter:stream_handle(%p)",__func__, out);
2692
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002693 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302694 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002695 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05302696 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002697 if(ret != 0)
2698 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2699 __func__, ret);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002700 } else
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002701 out_standby(&stream->common);
2702
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002703 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002704 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002705 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002706 if (out->compr_config.codec != NULL)
2707 free(out->compr_config.codec);
2708 }
2709 pthread_cond_destroy(&out->cond);
2710 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002711 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002712 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002713}
2714
2715static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2716{
2717 struct audio_device *adev = (struct audio_device *)dev;
2718 struct str_parms *parms;
2719 char *str;
2720 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002721 int val;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002722 int ret;
2723 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002724
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002725 ALOGD("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002726 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002727
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302728 if (!parms)
2729 goto error;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002730 ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
2731 if (ret >= 0) {
Naresh Tanniru4c630392014-05-12 01:05:52 +05302732 char *snd_card_status = value+2;
Naresh Tanniru4c630392014-05-12 01:05:52 +05302733 if (strstr(snd_card_status, "OFFLINE")) {
Naresh Tanniru80659832014-06-04 18:17:56 +05302734 struct listnode *node;
2735 struct audio_usecase *usecase;
2736
Naresh Tanniru4c630392014-05-12 01:05:52 +05302737 ALOGD("Received sound card OFFLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302738 set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
2739
2740 pthread_mutex_lock(&adev->lock);
2741 //close compress session on OFFLINE status
2742 usecase = get_usecase_from_list(adev,USECASE_AUDIO_PLAYBACK_OFFLOAD);
2743 if (usecase && usecase->stream.out) {
2744 ALOGD(" %s closing compress session on OFFLINE state", __func__);
2745
2746 struct stream_out *out = usecase->stream.out;
2747
2748 pthread_mutex_unlock(&adev->lock);
2749 out_standby(&out->stream.common);
2750 } else
2751 pthread_mutex_unlock(&adev->lock);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302752 } else if (strstr(snd_card_status, "ONLINE")) {
2753 ALOGD("Received sound card ONLINE status");
Naresh Tanniru80659832014-06-04 18:17:56 +05302754 set_snd_card_state(adev,SND_CARD_STATE_ONLINE);
Naresh Tanniru4c630392014-05-12 01:05:52 +05302755 }
Naresh Tanniru4c630392014-05-12 01:05:52 +05302756 }
2757
2758 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002759 status = voice_set_parameters(adev, parms);
2760 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002761 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002762
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002763 status = platform_set_parameters(adev->platform, parms);
2764 if (status != 0)
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002765 goto done;
2766
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002767 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2768 if (ret >= 0) {
Vicky Sehrawate240e5d2014-08-12 17:17:04 -07002769 /* When set to false, HAL should disable EC and NS */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002770 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2771 adev->bluetooth_nrec = true;
2772 else
2773 adev->bluetooth_nrec = false;
2774 }
2775
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002776 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2777 if (ret >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002778 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2779 adev->screen_off = false;
2780 else
2781 adev->screen_off = true;
2782 }
2783
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002784 ret = str_parms_get_int(parms, "rotation", &val);
2785 if (ret >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002786 bool reverse_speakers = false;
2787 switch(val) {
2788 // FIXME: note that the code below assumes that the speakers are in the correct placement
2789 // relative to the user when the device is rotated 90deg from its default rotation. This
2790 // assumption is device-specific, not platform-specific like this code.
2791 case 270:
2792 reverse_speakers = true;
2793 break;
2794 case 0:
2795 case 90:
2796 case 180:
2797 break;
2798 default:
2799 ALOGE("%s: unexpected rotation of %d", __func__, val);
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002800 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002801 }
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002802 if (status == 0) {
2803 if (adev->speaker_lr_swap != reverse_speakers) {
2804 adev->speaker_lr_swap = reverse_speakers;
2805 // only update the selected device if there is active pcm playback
2806 struct audio_usecase *usecase;
2807 struct listnode *node;
2808 list_for_each(node, &adev->usecase_list) {
2809 usecase = node_to_item(node, struct audio_usecase, list);
2810 if (usecase->type == PCM_PLAYBACK) {
2811 select_devices(adev, usecase->id);
2812 break;
2813 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002814 }
2815 }
2816 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002817 }
2818
Mingming Yin514a8bc2014-07-29 15:22:21 -07002819 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2820 if (ret >= 0) {
2821 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2822 adev->bt_wb_speech_enabled = true;
2823 else
2824 adev->bt_wb_speech_enabled = false;
2825 }
2826
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002827 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002828
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002829done:
2830 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002831 pthread_mutex_unlock(&adev->lock);
Preetam Singh Ranawata5f32b42014-09-23 12:12:47 +05302832error:
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002833 ALOGV("%s: exit with code(%d)", __func__, status);
2834 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002835}
2836
2837static char* adev_get_parameters(const struct audio_hw_device *dev,
2838 const char *keys)
2839{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002840 struct audio_device *adev = (struct audio_device *)dev;
2841 struct str_parms *reply = str_parms_create();
2842 struct str_parms *query = str_parms_create_str(keys);
2843 char *str;
Naresh Tannirud7205b62014-06-20 02:54:48 +05302844 char value[256] = {0};
2845 int ret = 0;
2846
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002847 if (!query || !reply) {
2848 ALOGE("adev_get_parameters: failed to create query or reply");
2849 return NULL;
2850 }
2851
Naresh Tannirud7205b62014-06-20 02:54:48 +05302852 ret = str_parms_get_str(query, "SND_CARD_STATUS", value,
2853 sizeof(value));
2854 if (ret >=0) {
2855 int val = 1;
2856 pthread_mutex_lock(&adev->snd_card_status.lock);
2857 if (SND_CARD_STATE_OFFLINE == adev->snd_card_status.state)
2858 val = 0;
2859 pthread_mutex_unlock(&adev->snd_card_status.lock);
2860 str_parms_add_int(reply, "SND_CARD_STATUS", val);
2861 goto exit;
2862 }
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002863
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002864 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002865 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002866 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002867 platform_get_parameters(adev->platform, query, reply);
Naresh Tanniru80659832014-06-04 18:17:56 +05302868 pthread_mutex_unlock(&adev->lock);
2869
Naresh Tannirud7205b62014-06-20 02:54:48 +05302870exit:
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002871 str = str_parms_to_str(reply);
2872 str_parms_destroy(query);
2873 str_parms_destroy(reply);
2874
2875 ALOGV("%s: exit: returns - %s", __func__, str);
2876 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002877}
2878
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002879static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002880{
2881 return 0;
2882}
2883
2884static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2885{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002886 int ret;
2887 struct audio_device *adev = (struct audio_device *)dev;
2888 pthread_mutex_lock(&adev->lock);
2889 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002890 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002891 pthread_mutex_unlock(&adev->lock);
2892 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002893}
2894
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002895static int adev_set_master_volume(struct audio_hw_device *dev __unused,
2896 float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002897{
2898 return -ENOSYS;
2899}
2900
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002901static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2902 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002903{
2904 return -ENOSYS;
2905}
2906
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002907static int adev_set_master_mute(struct audio_hw_device *dev __unused,
2908 bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002909{
2910 return -ENOSYS;
2911}
2912
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002913static int adev_get_master_mute(struct audio_hw_device *dev __unused,
2914 bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002915{
2916 return -ENOSYS;
2917}
2918
2919static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2920{
2921 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002922 pthread_mutex_lock(&adev->lock);
2923 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002924 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002925 adev->mode = mode;
2926 }
2927 pthread_mutex_unlock(&adev->lock);
2928 return 0;
2929}
2930
2931static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2932{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002933 int ret;
2934
2935 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002936 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002937 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2938 pthread_mutex_unlock(&adev->lock);
2939
2940 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002941}
2942
2943static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2944{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002945 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002946 return 0;
2947}
2948
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002949static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002950 const struct audio_config *config)
2951{
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002952 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002953
2954 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2955}
2956
2957static int adev_open_input_stream(struct audio_hw_device *dev,
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07002958 audio_io_handle_t handle __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002959 audio_devices_t devices,
2960 struct audio_config *config,
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002961 struct audio_stream_in **stream_in,
2962 audio_input_flags_t flags __unused,
2963 const char *address __unused,
2964 audio_source_t source __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002965{
2966 struct audio_device *adev = (struct audio_device *)dev;
2967 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002968 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07002969 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002970
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302971
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002972 *stream_in = NULL;
2973 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2974 return -EINVAL;
2975
2976 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07002977
2978 if (!in) {
2979 ALOGE("failed to allocate input stream");
2980 return -ENOMEM;
2981 }
2982
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05302983 ALOGD("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x)\
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07002984 stream_handle(%p) io_handle(%d)",__func__, config->sample_rate, config->channel_mask,
2985 devices, &in->stream, handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002986
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07002987 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2988
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002989 in->stream.common.get_sample_rate = in_get_sample_rate;
2990 in->stream.common.set_sample_rate = in_set_sample_rate;
2991 in->stream.common.get_buffer_size = in_get_buffer_size;
2992 in->stream.common.get_channels = in_get_channels;
2993 in->stream.common.get_format = in_get_format;
2994 in->stream.common.set_format = in_set_format;
2995 in->stream.common.standby = in_standby;
2996 in->stream.common.dump = in_dump;
2997 in->stream.common.set_parameters = in_set_parameters;
2998 in->stream.common.get_parameters = in_get_parameters;
2999 in->stream.common.add_audio_effect = in_add_audio_effect;
3000 in->stream.common.remove_audio_effect = in_remove_audio_effect;
3001 in->stream.set_gain = in_set_gain;
3002 in->stream.read = in_read;
3003 in->stream.get_input_frames_lost = in_get_input_frames_lost;
3004
3005 in->device = devices;
3006 in->source = AUDIO_SOURCE_DEFAULT;
3007 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003008 in->standby = 1;
3009 in->channel_mask = config->channel_mask;
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003010 in->capture_handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003011
3012 /* Update config params with the requested sample rate and channels */
3013 in->usecase = USECASE_AUDIO_RECORD;
3014 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003015 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003016 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003017
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08003018 if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003019 if(audio_extn_ssr_get_enabled()) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003020 if(audio_extn_ssr_init(in)) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003021 ALOGE("%s: audio_extn_ssr_init failed", __func__);
3022 ret = -EINVAL;
3023 goto err_open;
3024 }
3025 } else {
Mingming Yin3cf99da2014-09-16 17:41:33 -07003026 ALOGW("%s: surround sound recording is not supported", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003027 }
Mingming Yine62d7842013-10-25 16:26:03 -07003028 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08003029 audio_extn_compr_cap_format_supported(config->format) &&
3030 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003031 audio_extn_compr_cap_init(in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003032 } else {
3033 in->config.channels = channel_count;
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003034 frame_size = audio_stream_in_frame_size(&in->stream);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003035 buffer_size = get_input_buffer_size(config->sample_rate,
3036 config->format,
3037 channel_count);
3038 in->config.period_size = buffer_size / frame_size;
3039 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003040
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003041 /* This stream could be for sound trigger lab,
3042 get sound trigger pcm if present */
3043 audio_extn_sound_trigger_check_and_get_session(in);
3044
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003045 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07003046 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08003047 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003048
3049err_open:
3050 free(in);
3051 *stream_in = NULL;
3052 return ret;
3053}
3054
3055static void adev_close_input_stream(struct audio_hw_device *dev,
3056 struct audio_stream_in *stream)
3057{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003058 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003059 struct stream_in *in = (struct stream_in *)stream;
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003060 struct audio_device *adev = (struct audio_device *)dev;
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05303061
Sidipotu Ashokf43018c2014-05-02 16:21:50 +05303062 ALOGD("%s: enter:stream_handle(%p)",__func__, in);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003063
Venkata Narendra Kumar Gutta1bbbf542014-09-04 19:11:25 +05303064 /* Disable echo reference while closing input stream */
3065 platform_set_echo_reference(adev->platform, false);
3066
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003067 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05303068 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003069 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
Venkata Narendra Kumar Gutta91812142014-08-11 18:20:49 +05303070 pthread_mutex_unlock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08003071 if (ret != 0)
3072 ALOGE("%s: Compress voip input cannot be closed, error:%d",
3073 __func__, ret);
3074 } else
3075 in_standby(&stream->common);
3076
Ravi Kumar Alamandac3bc0812014-09-08 15:34:32 -07003077 if (audio_extn_ssr_get_enabled() &&
3078 (audio_channel_count_from_in_mask(in->channel_mask) == 6)) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003079 audio_extn_ssr_deinit();
3080 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003081
Mingming Yine62d7842013-10-25 16:26:03 -07003082 if(audio_extn_compr_cap_enabled() &&
3083 audio_extn_compr_cap_format_supported(in->config.format))
3084 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003085
3086 free(stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003087 return;
3088}
3089
Ravi Kumar Alamandabdf14162014-09-05 16:14:17 -07003090static int adev_dump(const audio_hw_device_t *device __unused,
3091 int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003092{
3093 return 0;
3094}
3095
3096static int adev_close(hw_device_t *device)
3097{
3098 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07003099
3100 if (!adev)
3101 return 0;
3102
3103 pthread_mutex_lock(&adev_init_lock);
3104
3105 if ((--audio_device_ref_count) == 0) {
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003106 audio_extn_sound_trigger_deinit(adev);
Kiran Kandide144c82013-11-20 15:58:32 -08003107 audio_extn_listen_deinit(adev);
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003108 audio_extn_utils_release_streams_output_cfg_list(&adev->streams_output_cfg_list);
Kiran Kandi910e1862013-10-29 13:29:42 -07003109 audio_route_free(adev->audio_route);
3110 free(adev->snd_dev_ref_cnt);
3111 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07003112 free(device);
3113 adev = NULL;
3114 }
3115 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003116 return 0;
3117}
3118
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003119static int adev_open(const hw_module_t *module, const char *name,
3120 hw_device_t **device)
3121{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07003122 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003123
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08003124 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003125 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
3126
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003127 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07003128 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003129 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07003130 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003131 ALOGD("%s: returning existing instance of adev", __func__);
3132 ALOGD("%s: exit", __func__);
3133 pthread_mutex_unlock(&adev_init_lock);
3134 return 0;
3135 }
3136
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003137 adev = calloc(1, sizeof(struct audio_device));
3138
Haynes Mathew Georgeb51ceb12014-06-30 13:56:18 -07003139 if (!adev) {
3140 pthread_mutex_unlock(&adev_init_lock);
3141 return -ENOMEM;
3142 }
3143
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07003144 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
3145
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003146 adev->device.common.tag = HARDWARE_DEVICE_TAG;
3147 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
3148 adev->device.common.module = (struct hw_module_t *)module;
3149 adev->device.common.close = adev_close;
3150
3151 adev->device.init_check = adev_init_check;
3152 adev->device.set_voice_volume = adev_set_voice_volume;
3153 adev->device.set_master_volume = adev_set_master_volume;
3154 adev->device.get_master_volume = adev_get_master_volume;
3155 adev->device.set_master_mute = adev_set_master_mute;
3156 adev->device.get_master_mute = adev_get_master_mute;
3157 adev->device.set_mode = adev_set_mode;
3158 adev->device.set_mic_mute = adev_set_mic_mute;
3159 adev->device.get_mic_mute = adev_get_mic_mute;
3160 adev->device.set_parameters = adev_set_parameters;
3161 adev->device.get_parameters = adev_get_parameters;
3162 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
3163 adev->device.open_output_stream = adev_open_output_stream;
3164 adev->device.close_output_stream = adev_close_output_stream;
3165 adev->device.open_input_stream = adev_open_input_stream;
3166 adev->device.close_input_stream = adev_close_input_stream;
3167 adev->device.dump = adev_dump;
3168
3169 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003170 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08003171 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08003172 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003173 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003174 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08003175 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07003176 /* adev->cur_hdmi_channels = 0; by calloc() */
Mingming Yin3ee55c62014-08-04 14:23:35 -07003177 adev->cur_codec_backend_samplerate = CODEC_BACKEND_DEFAULT_SAMPLE_RATE;
3178 adev->cur_codec_backend_bit_width = CODEC_BACKEND_DEFAULT_BIT_WIDTH;
Eric Laurentb23d5282013-05-14 15:27:20 -07003179 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07003180 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08003181 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08003182 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07003183 adev->offload_usecases_state = 0;
Naresh Tanniru4c630392014-05-12 01:05:52 +05303184
3185 pthread_mutex_init(&adev->snd_card_status.lock, (const pthread_mutexattr_t *) NULL);
3186 adev->snd_card_status.state = SND_CARD_STATE_OFFLINE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003187 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07003188 adev->platform = platform_init(adev);
3189 if (!adev->platform) {
3190 free(adev->snd_dev_ref_cnt);
3191 free(adev);
3192 ALOGE("%s: Failed to init platform data, aborting.", __func__);
3193 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08003194 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07003195 return -EINVAL;
3196 }
Eric Laurentc4aef752013-09-12 17:45:53 -07003197
Naresh Tanniru4c630392014-05-12 01:05:52 +05303198 adev->snd_card_status.state = SND_CARD_STATE_ONLINE;
3199
Eric Laurentc4aef752013-09-12 17:45:53 -07003200 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
3201 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
3202 if (adev->visualizer_lib == NULL) {
3203 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
3204 } else {
3205 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
3206 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003207 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003208 "visualizer_hal_start_output");
3209 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003210 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07003211 "visualizer_hal_stop_output");
3212 }
3213 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08003214 audio_extn_listen_init(adev, adev->snd_card);
Ravi Kumar Alamanda8fa6b192014-09-09 16:06:42 -07003215 audio_extn_sound_trigger_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07003216
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08003217 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
3218 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
3219 if (adev->offload_effects_lib == NULL) {
3220 ALOGE("%s: DLOPEN failed for %s", __func__,
3221 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3222 } else {
3223 ALOGV("%s: DLOPEN successful for %s", __func__,
3224 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
3225 adev->offload_effects_start_output =
3226 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3227 "offload_effects_bundle_hal_start_output");
3228 adev->offload_effects_stop_output =
3229 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
3230 "offload_effects_bundle_hal_stop_output");
3231 }
3232 }
3233
Mingming Yin514a8bc2014-07-29 15:22:21 -07003234 adev->bt_wb_speech_enabled = false;
3235
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003236 *device = &adev->device.common;
3237
Subhash Chandra Bose Naripeddy19dc03b2014-03-10 14:43:05 -07003238 audio_extn_utils_update_streams_output_cfg_list(adev->platform, adev->mixer,
3239 &adev->streams_output_cfg_list);
3240
Kiran Kandi910e1862013-10-29 13:29:42 -07003241 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07003242 pthread_mutex_unlock(&adev_init_lock);
3243
Eric Laurent994a6932013-07-17 11:51:42 -07003244 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003245 return 0;
3246}
3247
Mingming Yin07972cc2014-06-06 17:11:23 -07003248int pcm_ioctl(struct pcm *pcm, int request, ...)
3249{
3250 va_list ap;
3251 void * arg;
3252 int pcm_fd = *(int*)pcm;
3253
3254 va_start(ap, request);
3255 arg = va_arg(ap, void *);
3256 va_end(ap);
3257
3258 return ioctl(pcm_fd, request, arg);
3259}
3260
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003261static struct hw_module_methods_t hal_module_methods = {
3262 .open = adev_open,
3263};
3264
3265struct audio_module HAL_MODULE_INFO_SYM = {
3266 .common = {
3267 .tag = HARDWARE_MODULE_TAG,
3268 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
3269 .hal_api_version = HARDWARE_HAL_API_VERSION,
3270 .id = AUDIO_HARDWARE_MODULE_ID,
3271 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08003272 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003273 .methods = &hal_module_methods,
3274 },
3275};