blob: 12faa8860f69f452a80ff020b3764dada3d3a107 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
4 *
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005 * Copyright (C) 2013 The Android Open Source Project
6 *
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"
56
57#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
58#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
Eric Laurentb23d5282013-05-14 15:27:20 -070063struct pcm_config pcm_config_deep_buffer = {
64 .channels = 2,
65 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
66 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
67 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
68 .format = PCM_FORMAT_S16_LE,
69 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
70 .stop_threshold = INT_MAX,
71 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
72};
73
74struct pcm_config pcm_config_low_latency = {
75 .channels = 2,
76 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
77 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
78 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
79 .format = PCM_FORMAT_S16_LE,
80 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
81 .stop_threshold = INT_MAX,
82 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
83};
84
85struct pcm_config pcm_config_hdmi_multi = {
86 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
87 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
88 .period_size = HDMI_MULTI_PERIOD_SIZE,
89 .period_count = HDMI_MULTI_PERIOD_COUNT,
90 .format = PCM_FORMAT_S16_LE,
91 .start_threshold = 0,
92 .stop_threshold = INT_MAX,
93 .avail_min = 0,
94};
95
96struct pcm_config pcm_config_audio_capture = {
97 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070098 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
99 .format = PCM_FORMAT_S16_LE,
100};
101
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800102const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700103 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
104 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
105 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700106 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700107 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700108 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700109 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700110 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700111 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700112 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700113
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700114 [USECASE_VOICE2_CALL] = "voice2-call",
115 [USECASE_VOLTE_CALL] = "volte-call",
116 [USECASE_QCHAT_CALL] = "qchat-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800117 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700118 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
119 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
120 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700121 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
122 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700123 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
124 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700125};
126
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800127
128#define STRING_TO_ENUM(string) { #string, string }
129
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800130struct string_to_enum {
131 const char *name;
132 uint32_t value;
133};
134
135static const struct string_to_enum out_channels_name_to_enum_table[] = {
136 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
137 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
138 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
139};
140
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700141static struct audio_device *adev = NULL;
142static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700143static unsigned int audio_device_ref_count;
144
Haynes Mathew George5191a852013-09-11 14:19:36 -0700145static int set_voice_volume_l(struct audio_device *adev, float volume);
146
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700147static bool is_supported_format(audio_format_t format)
148{
Eric Laurent86e17132013-09-12 17:49:30 -0700149 if (format == AUDIO_FORMAT_MP3 ||
150 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700151 return true;
152
153 return false;
154}
155
156static int get_snd_codec_id(audio_format_t format)
157{
158 int id = 0;
159
160 switch (format) {
161 case AUDIO_FORMAT_MP3:
162 id = SND_AUDIOCODEC_MP3;
163 break;
164 case AUDIO_FORMAT_AAC:
165 id = SND_AUDIOCODEC_AAC;
166 break;
167 default:
Mingming Yin90310102013-11-13 16:57:00 -0800168 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700169 }
170
171 return id;
172}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800173
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700174int enable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700175 struct audio_usecase *usecase,
176 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800177{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700178 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700179 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800180
181 if (usecase == NULL)
182 return -EINVAL;
183
184 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
185
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800186 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700187 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800188 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700189 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800190
191 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700192 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700193 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700194 audio_route_apply_path(adev->audio_route, mixer_path);
195 if (update_mixer)
196 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800197
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800198 ALOGV("%s: exit", __func__);
199 return 0;
200}
201
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700202int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700203 struct audio_usecase *usecase,
204 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800205{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700206 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700207 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800208
209 if (usecase == NULL)
210 return -EINVAL;
211
212 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700213 if (usecase->type == PCM_CAPTURE)
214 snd_device = usecase->in_snd_device;
215 else
216 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800217 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700218 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700219 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700220 audio_route_reset_path(adev->audio_route, mixer_path);
221 if (update_mixer)
222 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800223
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800224 ALOGV("%s: exit", __func__);
225 return 0;
226}
227
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700228int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700229 snd_device_t snd_device,
230 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800231{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700232 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
233
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800234 if (snd_device < SND_DEVICE_MIN ||
235 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800236 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800237 return -EINVAL;
238 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700239
240 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700241
242 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
243 ALOGE("%s: Invalid sound device returned", __func__);
244 return -EINVAL;
245 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700246 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700247 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700248 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249 return 0;
250 }
251
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700252 /* start usb playback thread */
253 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
254 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
255 audio_extn_usb_start_playback(adev);
256
257 /* start usb capture thread */
258 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
259 audio_extn_usb_start_capture(adev);
260
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700261 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
262 audio_extn_spkr_prot_is_enabled()) {
263 if (audio_extn_spkr_prot_start_processing(snd_device)) {
264 ALOGE("%s: spkr_start_processing failed", __func__);
265 return -EINVAL;
266 }
267 } else {
268 ALOGV("%s: snd_device(%d: %s)", __func__,
269 snd_device, device_name);
270 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
271 adev->snd_dev_ref_cnt[snd_device]--;
272 return -EINVAL;
273 }
Kiran Kandide144c82013-11-20 15:58:32 -0800274 audio_extn_listen_update_status(snd_device,
275 LISTEN_EVENT_SND_DEVICE_BUSY);
276
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700277 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800278 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700279 if (update_mixer)
280 audio_route_update_mixer(adev->audio_route);
281
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800282 return 0;
283}
284
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700285int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700286 snd_device_t snd_device,
287 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800288{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700289 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
290
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800291 if (snd_device < SND_DEVICE_MIN ||
292 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800293 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800294 return -EINVAL;
295 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700296 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
297 ALOGE("%s: device ref cnt is already 0", __func__);
298 return -EINVAL;
299 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700300
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700301 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700302
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700303 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
304 ALOGE("%s: Invalid sound device returned", __func__);
305 return -EINVAL;
306 }
307
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700308 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700309 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700310 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800311 /* exit usb play back thread */
312 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
313 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
314 audio_extn_usb_stop_playback();
315
316 /* exit usb capture thread */
317 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
318 audio_extn_usb_stop_capture(adev);
319
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700320 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
321 audio_extn_spkr_prot_is_enabled()) {
322 audio_extn_spkr_prot_stop_processing();
323 } else
324 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800325
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700326 if (update_mixer)
327 audio_route_update_mixer(adev->audio_route);
Kiran Kandide144c82013-11-20 15:58:32 -0800328
329 audio_extn_listen_update_status(snd_device,
330 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700331 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700332
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800333 return 0;
334}
335
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700336static void check_usecases_codec_backend(struct audio_device *adev,
337 struct audio_usecase *uc_info,
338 snd_device_t snd_device)
339{
340 struct listnode *node;
341 struct audio_usecase *usecase;
342 bool switch_device[AUDIO_USECASE_MAX];
343 int i, num_uc_to_switch = 0;
344
345 /*
346 * This function is to make sure that all the usecases that are active on
347 * the hardware codec backend are always routed to any one device that is
348 * handled by the hardware codec.
349 * For example, if low-latency and deep-buffer usecases are currently active
350 * on speaker and out_set_parameters(headset) is received on low-latency
351 * output, then we have to make sure deep-buffer is also switched to headset,
352 * because of the limitation that both the devices cannot be enabled
353 * at the same time as they share the same backend.
354 */
355 /* Disable all the usecases on the shared backend other than the
356 specified usecase */
357 for (i = 0; i < AUDIO_USECASE_MAX; i++)
358 switch_device[i] = false;
359
360 list_for_each(node, &adev->usecase_list) {
361 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700362 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700363 usecase != uc_info &&
364 usecase->out_snd_device != snd_device &&
365 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
366 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
367 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700368 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700369 disable_audio_route(adev, usecase, false);
370 switch_device[usecase->id] = true;
371 num_uc_to_switch++;
372 }
373 }
374
375 if (num_uc_to_switch) {
376 /* Make sure all the streams are de-routed before disabling the device */
377 audio_route_update_mixer(adev->audio_route);
378
379 list_for_each(node, &adev->usecase_list) {
380 usecase = node_to_item(node, struct audio_usecase, list);
381 if (switch_device[usecase->id]) {
382 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700383 }
384 }
385
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700386 list_for_each(node, &adev->usecase_list) {
387 usecase = node_to_item(node, struct audio_usecase, list);
388 if (switch_device[usecase->id]) {
389 enable_snd_device(adev, snd_device, false);
390 }
391 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700392 /* Make sure new snd device is enabled before re-routing the streams */
393 audio_route_update_mixer(adev->audio_route);
394
395 /* Re-route all the usecases on the shared backend other than the
396 specified usecase to new snd devices */
397 list_for_each(node, &adev->usecase_list) {
398 usecase = node_to_item(node, struct audio_usecase, list);
399 /* Update the out_snd_device only before enabling the audio route */
400 if (switch_device[usecase->id] ) {
401 usecase->out_snd_device = snd_device;
402 enable_audio_route(adev, usecase, false);
403 }
404 }
405
406 audio_route_update_mixer(adev->audio_route);
407 }
408}
409
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700410static void check_and_route_capture_usecases(struct audio_device *adev,
411 struct audio_usecase *uc_info,
412 snd_device_t snd_device)
413{
414 struct listnode *node;
415 struct audio_usecase *usecase;
416 bool switch_device[AUDIO_USECASE_MAX];
417 int i, num_uc_to_switch = 0;
418
419 /*
420 * This function is to make sure that all the active capture usecases
421 * are always routed to the same input sound device.
422 * For example, if audio-record and voice-call usecases are currently
423 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
424 * is received for voice call then we have to make sure that audio-record
425 * usecase is also switched to earpiece i.e. voice-dmic-ef,
426 * because of the limitation that two devices cannot be enabled
427 * at the same time if they share the same backend.
428 */
429 for (i = 0; i < AUDIO_USECASE_MAX; i++)
430 switch_device[i] = false;
431
432 list_for_each(node, &adev->usecase_list) {
433 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700434 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700435 usecase != uc_info &&
436 usecase->in_snd_device != snd_device) {
437 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
438 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700439 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700440 disable_audio_route(adev, usecase, false);
441 switch_device[usecase->id] = true;
442 num_uc_to_switch++;
443 }
444 }
445
446 if (num_uc_to_switch) {
447 /* Make sure all the streams are de-routed before disabling the device */
448 audio_route_update_mixer(adev->audio_route);
449
450 list_for_each(node, &adev->usecase_list) {
451 usecase = node_to_item(node, struct audio_usecase, list);
452 if (switch_device[usecase->id]) {
453 disable_snd_device(adev, usecase->in_snd_device, false);
454 enable_snd_device(adev, snd_device, false);
455 }
456 }
457
458 /* Make sure new snd device is enabled before re-routing the streams */
459 audio_route_update_mixer(adev->audio_route);
460
461 /* Re-route all the usecases on the shared backend other than the
462 specified usecase to new snd devices */
463 list_for_each(node, &adev->usecase_list) {
464 usecase = node_to_item(node, struct audio_usecase, list);
465 /* Update the in_snd_device only before enabling the audio route */
466 if (switch_device[usecase->id] ) {
467 usecase->in_snd_device = snd_device;
468 enable_audio_route(adev, usecase, false);
469 }
470 }
471
472 audio_route_update_mixer(adev->audio_route);
473 }
474}
475
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700476static int disable_all_usecases_of_type(struct audio_device *adev,
477 usecase_type_t usecase_type,
478 bool update_mixer)
479{
480 struct audio_usecase *usecase;
481 struct listnode *node;
482 int ret = 0;
483
484 list_for_each(node, &adev->usecase_list) {
485 usecase = node_to_item(node, struct audio_usecase, list);
486 if (usecase->type == usecase_type) {
487 ALOGV("%s: usecase id %d", __func__, usecase->id);
488 ret = disable_audio_route(adev, usecase, update_mixer);
489 if (ret) {
490 ALOGE("%s: Failed to disable usecase id %d",
491 __func__, usecase->id);
492 }
493 }
494 }
495
496 return ret;
497}
498
499static int enable_all_usecases_of_type(struct audio_device *adev,
500 usecase_type_t usecase_type,
501 bool update_mixer)
502{
503 struct audio_usecase *usecase;
504 struct listnode *node;
505 int ret = 0;
506
507 list_for_each(node, &adev->usecase_list) {
508 usecase = node_to_item(node, struct audio_usecase, list);
509 if (usecase->type == usecase_type) {
510 ALOGV("%s: usecase id %d", __func__, usecase->id);
511 ret = enable_audio_route(adev, usecase, update_mixer);
512 if (ret) {
513 ALOGE("%s: Failed to enable usecase id %d",
514 __func__, usecase->id);
515 }
516 }
517 }
518
519 return ret;
520}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800521
522/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700523static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800524{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700525 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700526 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800527
528 switch (channels) {
529 /*
530 * Do not handle stereo output in Multi-channel cases
531 * Stereo case is handled in normal playback path
532 */
533 case 6:
534 ALOGV("%s: HDMI supports 5.1", __func__);
535 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
536 break;
537 case 8:
538 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
539 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
540 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
541 break;
542 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700543 ALOGE("HDMI does not support multi channel playback");
544 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800545 break;
546 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700547 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800548}
549
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700550static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
551{
552 struct audio_usecase *usecase;
553 struct listnode *node;
554
555 list_for_each(node, &adev->usecase_list) {
556 usecase = node_to_item(node, struct audio_usecase, list);
557 if (usecase->type == VOICE_CALL) {
558 ALOGV("%s: usecase id %d", __func__, usecase->id);
559 return usecase->id;
560 }
561 }
562 return USECASE_INVALID;
563}
564
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700565struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700566 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700567{
568 struct audio_usecase *usecase;
569 struct listnode *node;
570
571 list_for_each(node, &adev->usecase_list) {
572 usecase = node_to_item(node, struct audio_usecase, list);
573 if (usecase->id == uc_id)
574 return usecase;
575 }
576 return NULL;
577}
578
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700579int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800580{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800581 snd_device_t out_snd_device = SND_DEVICE_NONE;
582 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700583 struct audio_usecase *usecase = NULL;
584 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800585 struct audio_usecase *voip_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800586 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700587 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800588
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700589 usecase = get_usecase_from_list(adev, uc_id);
590 if (usecase == NULL) {
591 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
592 return -EINVAL;
593 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800594
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800595 if ((usecase->type == VOICE_CALL) ||
596 (usecase->type == VOIP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700597 out_snd_device = platform_get_output_snd_device(adev->platform,
598 usecase->stream.out->devices);
599 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700600 usecase->devices = usecase->stream.out->devices;
601 } else {
602 /*
603 * If the voice call is active, use the sound devices of voice call usecase
604 * so that it would not result any device switch. All the usecases will
605 * be switched to new device when select_devices() is called for voice call
606 * usecase. This is to avoid switching devices for voice call when
607 * check_usecases_codec_backend() is called below.
608 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700609 if (voice_is_in_call(adev)) {
610 vc_usecase = get_usecase_from_list(adev,
611 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700612 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
613 in_snd_device = vc_usecase->in_snd_device;
614 out_snd_device = vc_usecase->out_snd_device;
615 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800616 } else if (voice_extn_compress_voip_is_active(adev)) {
617 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
618 if (voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
619 in_snd_device = voip_usecase->in_snd_device;
620 out_snd_device = voip_usecase->out_snd_device;
621 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700622 }
623 if (usecase->type == PCM_PLAYBACK) {
624 usecase->devices = usecase->stream.out->devices;
625 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700626 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700627 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700628 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700629 if (usecase->stream.out == adev->primary_output &&
630 adev->active_input &&
631 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
632 select_devices(adev, adev->active_input->usecase);
633 }
634 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700635 } else if (usecase->type == PCM_CAPTURE) {
636 usecase->devices = usecase->stream.in->device;
637 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700638 if (in_snd_device == SND_DEVICE_NONE) {
639 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
640 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700641 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700642 adev->primary_output->devices);
643 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700644 in_snd_device = platform_get_input_snd_device(adev->platform,
645 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700646 }
647 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700648 }
649 }
650
651 if (out_snd_device == usecase->out_snd_device &&
652 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800653 return 0;
654 }
655
sangwoobc677242013-08-08 16:53:43 +0900656 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700657 out_snd_device, platform_get_snd_device_name(out_snd_device),
658 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800659
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800660 /*
661 * Limitation: While in call, to do a device switch we need to disable
662 * and enable both RX and TX devices though one of them is same as current
663 * device.
664 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800665 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700666 status = platform_switch_voice_call_device_pre(adev->platform);
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800667 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800668 }
669
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700670 /* Disable current sound devices */
671 if (usecase->out_snd_device != SND_DEVICE_NONE) {
672 disable_audio_route(adev, usecase, true);
673 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800674 }
675
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700676 if (usecase->in_snd_device != SND_DEVICE_NONE) {
677 disable_audio_route(adev, usecase, true);
678 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800679 }
680
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700681 /* Enable new sound devices */
682 if (out_snd_device != SND_DEVICE_NONE) {
683 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
684 check_usecases_codec_backend(adev, usecase, out_snd_device);
685 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800686 }
687
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700688 if (in_snd_device != SND_DEVICE_NONE) {
689 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700690 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700691 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700692
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800693 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700694 status = platform_switch_voice_call_device_post(adev->platform,
695 out_snd_device,
696 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800697
sangwoo170731f2013-06-08 15:36:36 +0900698 audio_route_update_mixer(adev->audio_route);
699
700 usecase->in_snd_device = in_snd_device;
701 usecase->out_snd_device = out_snd_device;
702
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800703 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
704 enable_all_usecases_of_type(adev, usecase->type, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700705 else
706 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900707
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800708 /* Applicable only on the targets that has external modem.
709 * Enable device command should be sent to modem only after
710 * enabling voice call mixer controls
711 */
712 if (usecase->type == VOICE_CALL)
713 status = platform_switch_voice_call_usecase_route_post(adev->platform,
714 out_snd_device,
715 in_snd_device);
716
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800717 return status;
718}
719
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800720static int stop_input_stream(struct stream_in *in)
721{
722 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800723 struct audio_usecase *uc_info;
724 struct audio_device *adev = in->dev;
725
Eric Laurentc8400632013-02-14 19:04:54 -0800726 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800727
Eric Laurent994a6932013-07-17 11:51:42 -0700728 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700729 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800730 uc_info = get_usecase_from_list(adev, in->usecase);
731 if (uc_info == NULL) {
732 ALOGE("%s: Could not find the usecase (%d) in the list",
733 __func__, in->usecase);
734 return -EINVAL;
735 }
736
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800737 /* Close in-call recording streams */
738 voice_check_and_stop_incall_rec_usecase(adev, in);
739
Eric Laurent150dbfe2013-02-27 14:31:02 -0800740 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700741 disable_audio_route(adev, uc_info, true);
742
743 /* 2. Disable the tx device */
744 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800746 list_remove(&uc_info->list);
747 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800748
Eric Laurent994a6932013-07-17 11:51:42 -0700749 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800750 return ret;
751}
752
753int start_input_stream(struct stream_in *in)
754{
755 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800756 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800757 struct audio_usecase *uc_info;
758 struct audio_device *adev = in->dev;
759
Mingming Yine62d7842013-10-25 16:26:03 -0700760 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Eric Laurent994a6932013-07-17 11:51:42 -0700761 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700762
763 /* Check if source matches incall recording usecase criteria */
764 ret = voice_check_and_set_incall_rec_usecase(adev, in);
765 if (ret)
766 goto error_config;
767 else
768 ALOGV("%s: usecase(%d)", __func__, in->usecase);
769
Eric Laurentb23d5282013-05-14 15:27:20 -0700770 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800771 if (in->pcm_device_id < 0) {
772 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
773 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800774 ret = -EINVAL;
775 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800776 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700777
778 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800779 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
780 uc_info->id = in->usecase;
781 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800782 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700783 uc_info->devices = in->device;
784 uc_info->in_snd_device = SND_DEVICE_NONE;
785 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800786
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800787 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700788 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800789
Eric Laurentc8400632013-02-14 19:04:54 -0800790 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
791 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800792 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
793 PCM_IN, &in->config);
794 if (in->pcm && !pcm_is_ready(in->pcm)) {
795 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
796 pcm_close(in->pcm);
797 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800798 ret = -EIO;
799 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800800 }
Eric Laurent994a6932013-07-17 11:51:42 -0700801 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800802 return ret;
803
804error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800805 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800806
807error_config:
808 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700809 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800810
811 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800812}
813
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700814/* must be called with out->lock locked */
815static int send_offload_cmd_l(struct stream_out* out, int command)
816{
817 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
818
819 ALOGVV("%s %d", __func__, command);
820
821 cmd->cmd = command;
822 list_add_tail(&out->offload_cmd_list, &cmd->node);
823 pthread_cond_signal(&out->offload_cond);
824 return 0;
825}
826
827/* must be called iwth out->lock locked */
828static void stop_compressed_output_l(struct stream_out *out)
829{
830 out->offload_state = OFFLOAD_STATE_IDLE;
831 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700832 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700833 if (out->compr != NULL) {
834 compress_stop(out->compr);
835 while (out->offload_thread_blocked) {
836 pthread_cond_wait(&out->cond, &out->lock);
837 }
838 }
839}
840
841static void *offload_thread_loop(void *context)
842{
843 struct stream_out *out = (struct stream_out *) context;
844 struct listnode *item;
845
846 out->offload_state = OFFLOAD_STATE_IDLE;
847 out->playback_started = 0;
848
849 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
850 set_sched_policy(0, SP_FOREGROUND);
851 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
852
853 ALOGV("%s", __func__);
854 pthread_mutex_lock(&out->lock);
855 for (;;) {
856 struct offload_cmd *cmd = NULL;
857 stream_callback_event_t event;
858 bool send_callback = false;
859
860 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
861 __func__, list_empty(&out->offload_cmd_list),
862 out->offload_state);
863 if (list_empty(&out->offload_cmd_list)) {
864 ALOGV("%s SLEEPING", __func__);
865 pthread_cond_wait(&out->offload_cond, &out->lock);
866 ALOGV("%s RUNNING", __func__);
867 continue;
868 }
869
870 item = list_head(&out->offload_cmd_list);
871 cmd = node_to_item(item, struct offload_cmd, node);
872 list_remove(item);
873
874 ALOGVV("%s STATE %d CMD %d out->compr %p",
875 __func__, out->offload_state, cmd->cmd, out->compr);
876
877 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
878 free(cmd);
879 break;
880 }
881
882 if (out->compr == NULL) {
883 ALOGE("%s: Compress handle is NULL", __func__);
884 pthread_cond_signal(&out->cond);
885 continue;
886 }
887 out->offload_thread_blocked = true;
888 pthread_mutex_unlock(&out->lock);
889 send_callback = false;
890 switch(cmd->cmd) {
891 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
892 compress_wait(out->compr, -1);
893 send_callback = true;
894 event = STREAM_CBK_EVENT_WRITE_READY;
895 break;
896 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700897 compress_next_track(out->compr);
898 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700899 send_callback = true;
900 event = STREAM_CBK_EVENT_DRAIN_READY;
901 break;
902 case OFFLOAD_CMD_DRAIN:
903 compress_drain(out->compr);
904 send_callback = true;
905 event = STREAM_CBK_EVENT_DRAIN_READY;
906 break;
907 default:
908 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
909 break;
910 }
911 pthread_mutex_lock(&out->lock);
912 out->offload_thread_blocked = false;
913 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700914 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700915 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700916 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700917 free(cmd);
918 }
919
920 pthread_cond_signal(&out->cond);
921 while (!list_empty(&out->offload_cmd_list)) {
922 item = list_head(&out->offload_cmd_list);
923 list_remove(item);
924 free(node_to_item(item, struct offload_cmd, node));
925 }
926 pthread_mutex_unlock(&out->lock);
927
928 return NULL;
929}
930
931static int create_offload_callback_thread(struct stream_out *out)
932{
933 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
934 list_init(&out->offload_cmd_list);
935 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
936 offload_thread_loop, out);
937 return 0;
938}
939
940static int destroy_offload_callback_thread(struct stream_out *out)
941{
942 pthread_mutex_lock(&out->lock);
943 stop_compressed_output_l(out);
944 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
945
946 pthread_mutex_unlock(&out->lock);
947 pthread_join(out->offload_thread, (void **) NULL);
948 pthread_cond_destroy(&out->offload_cond);
949
950 return 0;
951}
952
Eric Laurent07eeafd2013-10-06 12:52:49 -0700953static bool allow_hdmi_channel_config(struct audio_device *adev)
954{
955 struct listnode *node;
956 struct audio_usecase *usecase;
957 bool ret = true;
958
959 list_for_each(node, &adev->usecase_list) {
960 usecase = node_to_item(node, struct audio_usecase, list);
961 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
962 /*
963 * If voice call is already existing, do not proceed further to avoid
964 * disabling/enabling both RX and TX devices, CSD calls, etc.
965 * Once the voice call done, the HDMI channels can be configured to
966 * max channels of remaining use cases.
967 */
968 if (usecase->id == USECASE_VOICE_CALL) {
969 ALOGD("%s: voice call is active, no change in HDMI channels",
970 __func__);
971 ret = false;
972 break;
973 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
974 ALOGD("%s: multi channel playback is active, "
975 "no change in HDMI channels", __func__);
976 ret = false;
977 break;
978 }
979 }
980 }
981 return ret;
982}
983
984static int check_and_set_hdmi_channels(struct audio_device *adev,
985 unsigned int channels)
986{
987 struct listnode *node;
988 struct audio_usecase *usecase;
989
990 /* Check if change in HDMI channel config is allowed */
991 if (!allow_hdmi_channel_config(adev))
992 return 0;
993
994 if (channels == adev->cur_hdmi_channels) {
995 ALOGD("%s: Requested channels are same as current", __func__);
996 return 0;
997 }
998
999 platform_set_hdmi_channels(adev->platform, channels);
1000 adev->cur_hdmi_channels = channels;
1001
1002 /*
1003 * Deroute all the playback streams routed to HDMI so that
1004 * the back end is deactivated. Note that backend will not
1005 * be deactivated if any one stream is connected to it.
1006 */
1007 list_for_each(node, &adev->usecase_list) {
1008 usecase = node_to_item(node, struct audio_usecase, list);
1009 if (usecase->type == PCM_PLAYBACK &&
1010 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1011 disable_audio_route(adev, usecase, true);
1012 }
1013 }
1014
1015 /*
1016 * Enable all the streams disabled above. Now the HDMI backend
1017 * will be activated with new channel configuration
1018 */
1019 list_for_each(node, &adev->usecase_list) {
1020 usecase = node_to_item(node, struct audio_usecase, list);
1021 if (usecase->type == PCM_PLAYBACK &&
1022 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1023 enable_audio_route(adev, usecase, true);
1024 }
1025 }
1026
1027 return 0;
1028}
1029
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001030static int stop_output_stream(struct stream_out *out)
1031{
1032 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001033 struct audio_usecase *uc_info;
1034 struct audio_device *adev = out->dev;
1035
Eric Laurent994a6932013-07-17 11:51:42 -07001036 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001037 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001038 uc_info = get_usecase_from_list(adev, out->usecase);
1039 if (uc_info == NULL) {
1040 ALOGE("%s: Could not find the usecase (%d) in the list",
1041 __func__, out->usecase);
1042 return -EINVAL;
1043 }
1044
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001045 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1046 if (adev->visualizer_stop_output != NULL)
1047 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1048 if (adev->offload_effects_stop_output != NULL)
1049 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1050 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001051
Eric Laurent150dbfe2013-02-27 14:31:02 -08001052 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001053 disable_audio_route(adev, uc_info, true);
1054
1055 /* 2. Disable the rx device */
1056 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001057
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001058 list_remove(&uc_info->list);
1059 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001060
Eric Laurent07eeafd2013-10-06 12:52:49 -07001061 /* Must be called after removing the usecase from list */
1062 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1063 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1064
Eric Laurent994a6932013-07-17 11:51:42 -07001065 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001066 return ret;
1067}
1068
1069int start_output_stream(struct stream_out *out)
1070{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001071 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001072 struct audio_usecase *uc_info;
1073 struct audio_device *adev = out->dev;
1074
Eric Laurent994a6932013-07-17 11:51:42 -07001075 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001076 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001077 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001078 if (out->pcm_device_id < 0) {
1079 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1080 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001081 ret = -EINVAL;
1082 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001083 }
1084
1085 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1086 uc_info->id = out->usecase;
1087 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001088 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001089 uc_info->devices = out->devices;
1090 uc_info->in_snd_device = SND_DEVICE_NONE;
1091 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001092
Eric Laurent07eeafd2013-10-06 12:52:49 -07001093 /* This must be called before adding this usecase to the list */
1094 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1095 check_and_set_hdmi_channels(adev, out->config.channels);
1096
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001097 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001098
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001099 select_devices(adev, out->usecase);
1100
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001101 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1102 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001103 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1104 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001105 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001106 if (out->pcm && !pcm_is_ready(out->pcm)) {
1107 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1108 pcm_close(out->pcm);
1109 out->pcm = NULL;
1110 ret = -EIO;
1111 goto error_open;
1112 }
1113 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001114 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001115 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1116 COMPRESS_IN, &out->compr_config);
1117 if (out->compr && !is_compress_ready(out->compr)) {
1118 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1119 compress_close(out->compr);
1120 out->compr = NULL;
1121 ret = -EIO;
1122 goto error_open;
1123 }
1124 if (out->offload_callback)
1125 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001126
1127 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001128 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1129 if (adev->offload_effects_start_output != NULL)
1130 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001131 }
Eric Laurent994a6932013-07-17 11:51:42 -07001132 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001133 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001134error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001135 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001136error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001137 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001138}
1139
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001140static int check_input_parameters(uint32_t sample_rate,
1141 audio_format_t format,
1142 int channel_count)
1143{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001144 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001145
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001146 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001147 !voice_extn_compress_voip_is_format_supported(format) &&
1148 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001149
1150 switch (channel_count) {
1151 case 1:
1152 case 2:
1153 case 6:
1154 break;
1155 default:
1156 ret = -EINVAL;
1157 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001158
1159 switch (sample_rate) {
1160 case 8000:
1161 case 11025:
1162 case 12000:
1163 case 16000:
1164 case 22050:
1165 case 24000:
1166 case 32000:
1167 case 44100:
1168 case 48000:
1169 break;
1170 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001171 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001172 }
1173
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001174 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001175}
1176
1177static size_t get_input_buffer_size(uint32_t sample_rate,
1178 audio_format_t format,
1179 int channel_count)
1180{
1181 size_t size = 0;
1182
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001183 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1184 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001185
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001186 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1187 /* ToDo: should use frame_size computed based on the format and
1188 channel_count here. */
1189 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001190
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001191 /* make sure the size is multiple of 64 */
1192 size += 0x3f;
1193 size &= ~0x3f;
1194
1195 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001196}
1197
1198static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1199{
1200 struct stream_out *out = (struct stream_out *)stream;
1201
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001202 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001203}
1204
1205static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1206{
1207 return -ENOSYS;
1208}
1209
1210static size_t out_get_buffer_size(const struct audio_stream *stream)
1211{
1212 struct stream_out *out = (struct stream_out *)stream;
1213
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001214 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001215 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001216 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1217 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001218
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001219 return out->config.period_size * audio_stream_frame_size(stream);
1220}
1221
1222static uint32_t out_get_channels(const struct audio_stream *stream)
1223{
1224 struct stream_out *out = (struct stream_out *)stream;
1225
1226 return out->channel_mask;
1227}
1228
1229static audio_format_t out_get_format(const struct audio_stream *stream)
1230{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001231 struct stream_out *out = (struct stream_out *)stream;
1232
1233 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001234}
1235
1236static int out_set_format(struct audio_stream *stream, audio_format_t format)
1237{
1238 return -ENOSYS;
1239}
1240
1241static int out_standby(struct audio_stream *stream)
1242{
1243 struct stream_out *out = (struct stream_out *)stream;
1244 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001245
Eric Laurent994a6932013-07-17 11:51:42 -07001246 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001247 out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001248 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1249 /* Ignore standby in case of voip call because the voip output
1250 * stream is closed in adev_close_output_stream()
1251 */
1252 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1253 return 0;
1254 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001255
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001256 pthread_mutex_lock(&out->lock);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001257 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001258 if (!out->standby) {
1259 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001260 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1261 if (out->pcm) {
1262 pcm_close(out->pcm);
1263 out->pcm = NULL;
1264 }
1265 } else {
1266 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001267 out->gapless_mdata.encoder_delay = 0;
1268 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001269 if (out->compr != NULL) {
1270 compress_close(out->compr);
1271 out->compr = NULL;
1272 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001273 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001274 stop_output_stream(out);
1275 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001276 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001278 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001279 return 0;
1280}
1281
1282static int out_dump(const struct audio_stream *stream, int fd)
1283{
1284 return 0;
1285}
1286
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001287static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1288{
1289 int ret = 0;
1290 char value[32];
1291 struct compr_gapless_mdata tmp_mdata;
1292
1293 if (!out || !parms) {
1294 return -EINVAL;
1295 }
1296
1297 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1298 if (ret >= 0) {
1299 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1300 } else {
1301 return -EINVAL;
1302 }
1303
1304 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1305 if (ret >= 0) {
1306 tmp_mdata.encoder_padding = atoi(value);
1307 } else {
1308 return -EINVAL;
1309 }
1310
1311 out->gapless_mdata = tmp_mdata;
1312 out->send_new_metadata = 1;
1313 ALOGV("%s new encoder delay %u and padding %u", __func__,
1314 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1315
1316 return 0;
1317}
1318
1319
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001320static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1321{
1322 struct stream_out *out = (struct stream_out *)stream;
1323 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001324 struct audio_usecase *usecase;
1325 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001326 struct str_parms *parms;
1327 char value[32];
1328 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001329 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001330
sangwoobc677242013-08-08 16:53:43 +09001331 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001332 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001333 parms = str_parms_create_str(kvpairs);
1334 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1335 if (ret >= 0) {
1336 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001338 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001339
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001340 /*
1341 * When HDMI cable is unplugged the music playback is paused and
1342 * the policy manager sends routing=0. But the audioflinger
1343 * continues to write data until standby time (3sec).
1344 * As the HDMI core is turned off, the write gets blocked.
1345 * Avoid this by routing audio to speaker until standby.
1346 */
1347 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1348 val == AUDIO_DEVICE_NONE) {
1349 val = AUDIO_DEVICE_OUT_SPEAKER;
1350 }
1351
1352 /*
1353 * select_devices() call below switches all the usecases on the same
1354 * backend to the new device. Refer to check_usecases_codec_backend() in
1355 * the select_devices(). But how do we undo this?
1356 *
1357 * For example, music playback is active on headset (deep-buffer usecase)
1358 * and if we go to ringtones and select a ringtone, low-latency usecase
1359 * will be started on headset+speaker. As we can't enable headset+speaker
1360 * and headset devices at the same time, select_devices() switches the music
1361 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1362 * So when the ringtone playback is completed, how do we undo the same?
1363 *
1364 * We are relying on the out_set_parameters() call on deep-buffer output,
1365 * once the ringtone playback is ended.
1366 * NOTE: We should not check if the current devices are same as new devices.
1367 * Because select_devices() must be called to switch back the music
1368 * playback to headset.
1369 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001370 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001371 out->devices = val;
1372
1373 if (!out->standby)
1374 select_devices(adev, out->usecase);
1375
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001376 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1377 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001378 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001379 voice_start_call(adev);
1380 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1381 voice_is_in_call(adev) &&
1382 (out == adev->primary_output)) {
1383 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001384 }
1385 }
1386
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001387 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1388 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001389 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001390 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001391 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001392
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001393 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001394 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001395 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001396
1397 if (out == adev->primary_output) {
1398 pthread_mutex_lock(&adev->lock);
1399 audio_extn_set_parameters(adev, parms);
1400 pthread_mutex_unlock(&adev->lock);
1401 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001402 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1403 parse_compress_metadata(out, parms);
1404 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001405
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001406 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001407 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001408 return ret;
1409}
1410
1411static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1412{
1413 struct stream_out *out = (struct stream_out *)stream;
1414 struct str_parms *query = str_parms_create_str(keys);
1415 char *str;
1416 char value[256];
1417 struct str_parms *reply = str_parms_create();
1418 size_t i, j;
1419 int ret;
1420 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001421 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001422 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1423 if (ret >= 0) {
1424 value[0] = '\0';
1425 i = 0;
1426 while (out->supported_channel_masks[i] != 0) {
1427 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1428 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1429 if (!first) {
1430 strcat(value, "|");
1431 }
1432 strcat(value, out_channels_name_to_enum_table[j].name);
1433 first = false;
1434 break;
1435 }
1436 }
1437 i++;
1438 }
1439 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1440 str = str_parms_to_str(reply);
1441 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001442 voice_extn_out_get_parameters(out, query, reply);
1443 str = str_parms_to_str(reply);
1444 if (!strncmp(str, "", sizeof(""))) {
1445 str = strdup(keys);
1446 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001447 }
1448 str_parms_destroy(query);
1449 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001450 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001451 return str;
1452}
1453
1454static uint32_t out_get_latency(const struct audio_stream_out *stream)
1455{
1456 struct stream_out *out = (struct stream_out *)stream;
1457
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001458 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1459 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1460
1461 return (out->config.period_count * out->config.period_size * 1000) /
1462 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001463}
1464
1465static int out_set_volume(struct audio_stream_out *stream, float left,
1466 float right)
1467{
Eric Laurenta9024de2013-04-04 09:19:12 -07001468 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001469 int volume[2];
1470
Eric Laurenta9024de2013-04-04 09:19:12 -07001471 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1472 /* only take left channel into account: the API is for stereo anyway */
1473 out->muted = (left == 0.0f);
1474 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001475 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001476 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001477 struct audio_device *adev = out->dev;
1478 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001479 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1480 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001481
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001482 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1483 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001484 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1485 if (!ctl) {
1486 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1487 __func__, mixer_ctl_name);
1488 return -EINVAL;
1489 }
1490 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1491 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1492 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1493 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001494 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001495
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001496 return -ENOSYS;
1497}
1498
1499static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1500 size_t bytes)
1501{
1502 struct stream_out *out = (struct stream_out *)stream;
1503 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001504 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001505
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001506 pthread_mutex_lock(&out->lock);
1507 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001508 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001509 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001510 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1511 ret = voice_extn_compress_voip_start_output_stream(out);
1512 else
1513 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001514 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001515 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001516 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001517 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001518 goto exit;
1519 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001520 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001521
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001522 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001523 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1524 if (out->send_new_metadata) {
1525 ALOGVV("send new gapless metadata");
1526 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1527 out->send_new_metadata = 0;
1528 }
1529
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001530 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001531 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001532 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001533 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1534 }
1535 if (!out->playback_started) {
1536 compress_start(out->compr);
1537 out->playback_started = 1;
1538 out->offload_state = OFFLOAD_STATE_PLAYING;
1539 }
1540 pthread_mutex_unlock(&out->lock);
1541 return ret;
1542 } else {
1543 if (out->pcm) {
1544 if (out->muted)
1545 memset((void *)buffer, 0, bytes);
1546 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1547 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001548 if (ret == 0)
1549 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001550 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001551 }
1552
1553exit:
1554 pthread_mutex_unlock(&out->lock);
1555
1556 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001557 if (out->pcm)
1558 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001559 out_standby(&out->stream.common);
1560 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1561 out_get_sample_rate(&out->stream.common));
1562 }
1563 return bytes;
1564}
1565
1566static int out_get_render_position(const struct audio_stream_out *stream,
1567 uint32_t *dsp_frames)
1568{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001569 struct stream_out *out = (struct stream_out *)stream;
1570 *dsp_frames = 0;
1571 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1572 pthread_mutex_lock(&out->lock);
1573 if (out->compr != NULL) {
1574 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1575 &out->sample_rate);
1576 ALOGVV("%s rendered frames %d sample_rate %d",
1577 __func__, *dsp_frames, out->sample_rate);
1578 }
1579 pthread_mutex_unlock(&out->lock);
1580 return 0;
1581 } else
1582 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001583}
1584
1585static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1586{
1587 return 0;
1588}
1589
1590static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1591{
1592 return 0;
1593}
1594
1595static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1596 int64_t *timestamp)
1597{
1598 return -EINVAL;
1599}
1600
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001601static int out_get_presentation_position(const struct audio_stream_out *stream,
1602 uint64_t *frames, struct timespec *timestamp)
1603{
1604 struct stream_out *out = (struct stream_out *)stream;
1605 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001606 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001607
1608 pthread_mutex_lock(&out->lock);
1609
Eric Laurent949a0892013-09-20 09:20:13 -07001610 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1611 if (out->compr != NULL) {
1612 compress_get_tstamp(out->compr, &dsp_frames,
1613 &out->sample_rate);
1614 ALOGVV("%s rendered frames %ld sample_rate %d",
1615 __func__, dsp_frames, out->sample_rate);
1616 *frames = dsp_frames;
1617 ret = 0;
1618 /* this is the best we can do */
1619 clock_gettime(CLOCK_MONOTONIC, timestamp);
1620 }
1621 } else {
1622 if (out->pcm) {
1623 size_t avail;
1624 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1625 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001626 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001627 // This adjustment accounts for buffering after app processor.
1628 // It is based on estimated DSP latency per use case, rather than exact.
1629 signed_frames -=
1630 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1631
Eric Laurent949a0892013-09-20 09:20:13 -07001632 // It would be unusual for this value to be negative, but check just in case ...
1633 if (signed_frames >= 0) {
1634 *frames = signed_frames;
1635 ret = 0;
1636 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001637 }
1638 }
1639 }
1640
1641 pthread_mutex_unlock(&out->lock);
1642
1643 return ret;
1644}
1645
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001646static int out_set_callback(struct audio_stream_out *stream,
1647 stream_callback_t callback, void *cookie)
1648{
1649 struct stream_out *out = (struct stream_out *)stream;
1650
1651 ALOGV("%s", __func__);
1652 pthread_mutex_lock(&out->lock);
1653 out->offload_callback = callback;
1654 out->offload_cookie = cookie;
1655 pthread_mutex_unlock(&out->lock);
1656 return 0;
1657}
1658
1659static int out_pause(struct audio_stream_out* stream)
1660{
1661 struct stream_out *out = (struct stream_out *)stream;
1662 int status = -ENOSYS;
1663 ALOGV("%s", __func__);
1664 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1665 pthread_mutex_lock(&out->lock);
1666 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1667 status = compress_pause(out->compr);
1668 out->offload_state = OFFLOAD_STATE_PAUSED;
1669 }
1670 pthread_mutex_unlock(&out->lock);
1671 }
1672 return status;
1673}
1674
1675static int out_resume(struct audio_stream_out* stream)
1676{
1677 struct stream_out *out = (struct stream_out *)stream;
1678 int status = -ENOSYS;
1679 ALOGV("%s", __func__);
1680 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1681 status = 0;
1682 pthread_mutex_lock(&out->lock);
1683 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1684 status = compress_resume(out->compr);
1685 out->offload_state = OFFLOAD_STATE_PLAYING;
1686 }
1687 pthread_mutex_unlock(&out->lock);
1688 }
1689 return status;
1690}
1691
1692static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1693{
1694 struct stream_out *out = (struct stream_out *)stream;
1695 int status = -ENOSYS;
1696 ALOGV("%s", __func__);
1697 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1698 pthread_mutex_lock(&out->lock);
1699 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1700 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1701 else
1702 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1703 pthread_mutex_unlock(&out->lock);
1704 }
1705 return status;
1706}
1707
1708static int out_flush(struct audio_stream_out* stream)
1709{
1710 struct stream_out *out = (struct stream_out *)stream;
1711 ALOGV("%s", __func__);
1712 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1713 pthread_mutex_lock(&out->lock);
1714 stop_compressed_output_l(out);
1715 pthread_mutex_unlock(&out->lock);
1716 return 0;
1717 }
1718 return -ENOSYS;
1719}
1720
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001721/** audio_stream_in implementation **/
1722static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1723{
1724 struct stream_in *in = (struct stream_in *)stream;
1725
1726 return in->config.rate;
1727}
1728
1729static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1730{
1731 return -ENOSYS;
1732}
1733
1734static size_t in_get_buffer_size(const struct audio_stream *stream)
1735{
1736 struct stream_in *in = (struct stream_in *)stream;
1737
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001738 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1739 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001740 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1741 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001742
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001743 return in->config.period_size * audio_stream_frame_size(stream);
1744}
1745
1746static uint32_t in_get_channels(const struct audio_stream *stream)
1747{
1748 struct stream_in *in = (struct stream_in *)stream;
1749
1750 return in->channel_mask;
1751}
1752
1753static audio_format_t in_get_format(const struct audio_stream *stream)
1754{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001755 struct stream_in *in = (struct stream_in *)stream;
1756
1757 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001758}
1759
1760static int in_set_format(struct audio_stream *stream, audio_format_t format)
1761{
1762 return -ENOSYS;
1763}
1764
1765static int in_standby(struct audio_stream *stream)
1766{
1767 struct stream_in *in = (struct stream_in *)stream;
1768 struct audio_device *adev = in->dev;
1769 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001770 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001771
1772 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1773 /* Ignore standby in case of voip call because the voip input
1774 * stream is closed in adev_close_input_stream()
1775 */
1776 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1777 return status;
1778 }
1779
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001780 pthread_mutex_lock(&in->lock);
1781 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001782 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001783 if (in->pcm) {
1784 pcm_close(in->pcm);
1785 in->pcm = NULL;
1786 }
1787 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001788 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001789 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790 }
1791 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001792 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001793 return status;
1794}
1795
1796static int in_dump(const struct audio_stream *stream, int fd)
1797{
1798 return 0;
1799}
1800
1801static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1802{
1803 struct stream_in *in = (struct stream_in *)stream;
1804 struct audio_device *adev = in->dev;
1805 struct str_parms *parms;
1806 char *str;
1807 char value[32];
1808 int ret, val = 0;
1809
Eric Laurent994a6932013-07-17 11:51:42 -07001810 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001811 parms = str_parms_create_str(kvpairs);
1812
1813 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1814
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001815 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001816 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001817 if (ret >= 0) {
1818 val = atoi(value);
1819 /* no audio source uses val == 0 */
1820 if ((in->source != val) && (val != 0)) {
1821 in->source = val;
1822 }
1823 }
1824
1825 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1826 if (ret >= 0) {
1827 val = atoi(value);
1828 if ((in->device != val) && (val != 0)) {
1829 in->device = val;
1830 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001831 if (!in->standby)
1832 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001833 }
1834 }
1835
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001836 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001837 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001838
1839 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001840 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001841 return ret;
1842}
1843
1844static char* in_get_parameters(const struct audio_stream *stream,
1845 const char *keys)
1846{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001847 struct stream_in *in = (struct stream_in *)stream;
1848 struct str_parms *query = str_parms_create_str(keys);
1849 char *str;
1850 char value[256];
1851 struct str_parms *reply = str_parms_create();
1852 ALOGV("%s: enter: keys - %s", __func__, keys);
1853
1854 voice_extn_in_get_parameters(in, query, reply);
1855
1856 str = str_parms_to_str(reply);
1857 str_parms_destroy(query);
1858 str_parms_destroy(reply);
1859
1860 ALOGV("%s: exit: returns - %s", __func__, str);
1861 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001862}
1863
1864static int in_set_gain(struct audio_stream_in *stream, float gain)
1865{
1866 return 0;
1867}
1868
1869static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1870 size_t bytes)
1871{
1872 struct stream_in *in = (struct stream_in *)stream;
1873 struct audio_device *adev = in->dev;
1874 int i, ret = -1;
1875
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001876 pthread_mutex_lock(&in->lock);
1877 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001878 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001879 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
1880 ret = voice_extn_compress_voip_start_input_stream(in);
1881 else
1882 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001883 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001884 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001885 goto exit;
1886 }
1887 in->standby = 0;
1888 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001889
1890 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001891 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1892 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07001893 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
1894 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001895 else
1896 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001897 }
1898
1899 /*
1900 * Instead of writing zeroes here, we could trust the hardware
1901 * to always provide zeroes when muted.
1902 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001903 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001904 memset(buffer, 0, bytes);
1905
1906exit:
1907 pthread_mutex_unlock(&in->lock);
1908
1909 if (ret != 0) {
1910 in_standby(&in->stream.common);
1911 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1912 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1913 in_get_sample_rate(&in->stream.common));
1914 }
1915 return bytes;
1916}
1917
1918static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1919{
1920 return 0;
1921}
1922
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001923static int add_remove_audio_effect(const struct audio_stream *stream,
1924 effect_handle_t effect,
1925 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001926{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001927 struct stream_in *in = (struct stream_in *)stream;
1928 int status = 0;
1929 effect_descriptor_t desc;
1930
1931 status = (*effect)->get_descriptor(effect, &desc);
1932 if (status != 0)
1933 return status;
1934
1935 pthread_mutex_lock(&in->lock);
1936 pthread_mutex_lock(&in->dev->lock);
1937 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1938 in->enable_aec != enable &&
1939 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1940 in->enable_aec = enable;
1941 if (!in->standby)
1942 select_devices(in->dev, in->usecase);
1943 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08001944 if (in->enable_ns != enable &&
1945 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1946 in->enable_ns = enable;
1947 if (!in->standby)
1948 select_devices(in->dev, in->usecase);
1949 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001950 pthread_mutex_unlock(&in->dev->lock);
1951 pthread_mutex_unlock(&in->lock);
1952
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001953 return 0;
1954}
1955
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001956static int in_add_audio_effect(const struct audio_stream *stream,
1957 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001958{
Eric Laurent994a6932013-07-17 11:51:42 -07001959 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001960 return add_remove_audio_effect(stream, effect, true);
1961}
1962
1963static int in_remove_audio_effect(const struct audio_stream *stream,
1964 effect_handle_t effect)
1965{
Eric Laurent994a6932013-07-17 11:51:42 -07001966 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001967 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001968}
1969
1970static int adev_open_output_stream(struct audio_hw_device *dev,
1971 audio_io_handle_t handle,
1972 audio_devices_t devices,
1973 audio_output_flags_t flags,
1974 struct audio_config *config,
1975 struct audio_stream_out **stream_out)
1976{
1977 struct audio_device *adev = (struct audio_device *)dev;
1978 struct stream_out *out;
1979 int i, ret;
1980
Eric Laurent994a6932013-07-17 11:51:42 -07001981 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001982 __func__, config->sample_rate, config->channel_mask, devices, flags);
1983 *stream_out = NULL;
1984 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1985
1986 if (devices == AUDIO_DEVICE_NONE)
1987 devices = AUDIO_DEVICE_OUT_SPEAKER;
1988
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001989 out->flags = flags;
1990 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001991 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001992 out->format = config->format;
1993 out->sample_rate = config->sample_rate;
1994 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1995 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001996 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001997
1998 /* Init use case and pcm_config */
Ravi Kumar Alamanda86b0e2b2013-11-20 14:52:30 -08001999 if (out->flags == AUDIO_OUTPUT_FLAG_DIRECT &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002001 pthread_mutex_lock(&adev->lock);
2002 ret = read_hdmi_channel_masks(out);
2003 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002004 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002005 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002006
2007 if (config->sample_rate == 0)
2008 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2009 if (config->channel_mask == 0)
2010 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2011
2012 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002013 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002014 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2015 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002016 out->config.rate = config->sample_rate;
2017 out->config.channels = popcount(out->channel_mask);
2018 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002019 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2020 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
2021 (voice_extn_compress_voip_is_format_supported(out->format))) {
2022 ret = voice_extn_compress_voip_open_output_stream(out);
2023 if (ret != 0) {
2024 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2025 __func__, ret);
2026 goto error_open;
2027 }
2028 out->config.rate = config->sample_rate;
2029 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002030 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2031 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2032 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2033 ALOGE("%s: Unsupported Offload information", __func__);
2034 ret = -EINVAL;
2035 goto error_open;
2036 }
Mingming Yin90310102013-11-13 16:57:00 -08002037 if (!is_supported_format(config->offload_info.format) &&
2038 !audio_extn_dolby_is_supported_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002039 ALOGE("%s: Unsupported audio format", __func__);
2040 ret = -EINVAL;
2041 goto error_open;
2042 }
2043
2044 out->compr_config.codec = (struct snd_codec *)
2045 calloc(1, sizeof(struct snd_codec));
2046
2047 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2048 if (config->offload_info.channel_mask)
2049 out->channel_mask = config->offload_info.channel_mask;
2050 else if (config->channel_mask)
2051 out->channel_mask = config->channel_mask;
2052 out->format = config->offload_info.format;
2053 out->sample_rate = config->offload_info.sample_rate;
2054
2055 out->stream.set_callback = out_set_callback;
2056 out->stream.pause = out_pause;
2057 out->stream.resume = out_resume;
2058 out->stream.drain = out_drain;
2059 out->stream.flush = out_flush;
2060
Mingming Yin90310102013-11-13 16:57:00 -08002061 if (audio_extn_dolby_is_supported_format(config->offload_info.format))
2062 out->compr_config.codec->id =
2063 audio_extn_dolby_get_snd_codec_id(config->offload_info.format);
2064 else
2065 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002066 get_snd_codec_id(config->offload_info.format);
2067 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2068 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2069 out->compr_config.codec->sample_rate =
2070 compress_get_alsa_rate(config->offload_info.sample_rate);
2071 out->compr_config.codec->bit_rate =
2072 config->offload_info.bit_rate;
2073 out->compr_config.codec->ch_in =
2074 popcount(config->channel_mask);
2075 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2076
2077 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2078 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002079
2080 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002081 create_offload_callback_thread(out);
2082 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2083 __func__, config->offload_info.version,
2084 config->offload_info.bit_rate);
Mingming Yin90310102013-11-13 16:57:00 -08002085
2086 if (audio_extn_dolby_is_supported_format(out->format)) {
2087 ret = audio_extn_dolby_set_DMID(adev);
2088 if (ret != 0) {
2089 ALOGE("%s: Dolby DMID cannot be set error:%d",
2090 __func__, ret);
2091 goto error_open;
2092 }
2093 }
2094
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002095 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2096 ret = voice_check_and_set_incall_music_usecase(adev, out);
2097 if (ret != 0) {
2098 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2099 __func__, ret);
2100 goto error_open;
2101 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002102 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002103 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2104 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002105 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002106 } else {
2107 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2108 out->config = pcm_config_deep_buffer;
2109 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002110 }
2111
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002112 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2113 if(adev->primary_output == NULL)
2114 adev->primary_output = out;
2115 else {
2116 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002117 ret = -EEXIST;
2118 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002119 }
2120 }
2121
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002122 /* Check if this usecase is already existing */
2123 pthread_mutex_lock(&adev->lock);
2124 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2125 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002126 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002127 ret = -EEXIST;
2128 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002129 }
2130 pthread_mutex_unlock(&adev->lock);
2131
2132 out->stream.common.get_sample_rate = out_get_sample_rate;
2133 out->stream.common.set_sample_rate = out_set_sample_rate;
2134 out->stream.common.get_buffer_size = out_get_buffer_size;
2135 out->stream.common.get_channels = out_get_channels;
2136 out->stream.common.get_format = out_get_format;
2137 out->stream.common.set_format = out_set_format;
2138 out->stream.common.standby = out_standby;
2139 out->stream.common.dump = out_dump;
2140 out->stream.common.set_parameters = out_set_parameters;
2141 out->stream.common.get_parameters = out_get_parameters;
2142 out->stream.common.add_audio_effect = out_add_audio_effect;
2143 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2144 out->stream.get_latency = out_get_latency;
2145 out->stream.set_volume = out_set_volume;
2146 out->stream.write = out_write;
2147 out->stream.get_render_position = out_get_render_position;
2148 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002149 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002150
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002151 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002152 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002153 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002154
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002155 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2156 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002157
2158 config->format = out->stream.common.get_format(&out->stream.common);
2159 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2160 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2161
2162 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002163 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002164 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002165
2166error_open:
2167 free(out);
2168 *stream_out = NULL;
2169 ALOGD("%s: exit: ret %d", __func__, ret);
2170 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002171}
2172
2173static void adev_close_output_stream(struct audio_hw_device *dev,
2174 struct audio_stream_out *stream)
2175{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002176 struct stream_out *out = (struct stream_out *)stream;
2177 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002178 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002179
Eric Laurent994a6932013-07-17 11:51:42 -07002180 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002181 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2182 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2183 if(ret != 0)
2184 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2185 __func__, ret);
2186 }
2187 else
2188 out_standby(&stream->common);
2189
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002190 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2191 destroy_offload_callback_thread(out);
2192
2193 if (out->compr_config.codec != NULL)
2194 free(out->compr_config.codec);
2195 }
2196 pthread_cond_destroy(&out->cond);
2197 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002198 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002199 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002200}
2201
2202static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2203{
2204 struct audio_device *adev = (struct audio_device *)dev;
2205 struct str_parms *parms;
2206 char *str;
2207 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002208 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002209 int ret;
2210
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002211 ALOGD("%s: enter: %s", __func__, kvpairs);
2212
2213 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002214 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002215
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002216 voice_set_parameters(adev, parms);
2217 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002218
2219 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2220 if (ret >= 0) {
2221 /* When set to false, HAL should disable EC and NS
2222 * But it is currently not supported.
2223 */
2224 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2225 adev->bluetooth_nrec = true;
2226 else
2227 adev->bluetooth_nrec = false;
2228 }
2229
2230 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2231 if (ret >= 0) {
2232 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2233 adev->screen_off = false;
2234 else
2235 adev->screen_off = true;
2236 }
2237
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002238 ret = str_parms_get_int(parms, "rotation", &val);
2239 if (ret >= 0) {
2240 bool reverse_speakers = false;
2241 switch(val) {
2242 // FIXME: note that the code below assumes that the speakers are in the correct placement
2243 // relative to the user when the device is rotated 90deg from its default rotation. This
2244 // assumption is device-specific, not platform-specific like this code.
2245 case 270:
2246 reverse_speakers = true;
2247 break;
2248 case 0:
2249 case 90:
2250 case 180:
2251 break;
2252 default:
2253 ALOGE("%s: unexpected rotation of %d", __func__, val);
2254 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002255 if (adev->speaker_lr_swap != reverse_speakers) {
2256 adev->speaker_lr_swap = reverse_speakers;
2257 // only update the selected device if there is active pcm playback
2258 struct audio_usecase *usecase;
2259 struct listnode *node;
2260 list_for_each(node, &adev->usecase_list) {
2261 usecase = node_to_item(node, struct audio_usecase, list);
2262 if (usecase->type == PCM_PLAYBACK) {
2263 select_devices(adev, usecase->id);
2264 break;
2265 }
2266 }
2267 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002268 }
2269
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002270 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002271 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002272
2273 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002274 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002275 return ret;
2276}
2277
2278static char* adev_get_parameters(const struct audio_hw_device *dev,
2279 const char *keys)
2280{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002281 struct audio_device *adev = (struct audio_device *)dev;
2282 struct str_parms *reply = str_parms_create();
2283 struct str_parms *query = str_parms_create_str(keys);
2284 char *str;
2285
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002286 pthread_mutex_lock(&adev->lock);
2287
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002288 audio_extn_get_parameters(adev, query, reply);
2289 platform_get_parameters(adev->platform, query, reply);
2290 str = str_parms_to_str(reply);
2291 str_parms_destroy(query);
2292 str_parms_destroy(reply);
2293
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002294 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002295 ALOGV("%s: exit: returns - %s", __func__, str);
2296 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002297}
2298
2299static int adev_init_check(const struct audio_hw_device *dev)
2300{
2301 return 0;
2302}
2303
2304static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2305{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002306 int ret;
2307 struct audio_device *adev = (struct audio_device *)dev;
2308 pthread_mutex_lock(&adev->lock);
2309 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002310 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002311 pthread_mutex_unlock(&adev->lock);
2312 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002313}
2314
2315static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2316{
2317 return -ENOSYS;
2318}
2319
2320static int adev_get_master_volume(struct audio_hw_device *dev,
2321 float *volume)
2322{
2323 return -ENOSYS;
2324}
2325
2326static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2327{
2328 return -ENOSYS;
2329}
2330
2331static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2332{
2333 return -ENOSYS;
2334}
2335
2336static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2337{
2338 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002339 pthread_mutex_lock(&adev->lock);
2340 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002341 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002342 adev->mode = mode;
2343 }
2344 pthread_mutex_unlock(&adev->lock);
2345 return 0;
2346}
2347
2348static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2349{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002350 int ret;
2351
2352 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002353 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002354 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2355 pthread_mutex_unlock(&adev->lock);
2356
2357 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002358}
2359
2360static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2361{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002362 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002363 return 0;
2364}
2365
2366static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2367 const struct audio_config *config)
2368{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002369 int channel_count = popcount(config->channel_mask);
2370
2371 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2372}
2373
2374static int adev_open_input_stream(struct audio_hw_device *dev,
2375 audio_io_handle_t handle,
2376 audio_devices_t devices,
2377 struct audio_config *config,
2378 struct audio_stream_in **stream_in)
2379{
2380 struct audio_device *adev = (struct audio_device *)dev;
2381 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002382 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002383 int channel_count = popcount(config->channel_mask);
2384
Eric Laurent994a6932013-07-17 11:51:42 -07002385 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002386 *stream_in = NULL;
2387 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2388 return -EINVAL;
2389
2390 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2391
2392 in->stream.common.get_sample_rate = in_get_sample_rate;
2393 in->stream.common.set_sample_rate = in_set_sample_rate;
2394 in->stream.common.get_buffer_size = in_get_buffer_size;
2395 in->stream.common.get_channels = in_get_channels;
2396 in->stream.common.get_format = in_get_format;
2397 in->stream.common.set_format = in_set_format;
2398 in->stream.common.standby = in_standby;
2399 in->stream.common.dump = in_dump;
2400 in->stream.common.set_parameters = in_set_parameters;
2401 in->stream.common.get_parameters = in_get_parameters;
2402 in->stream.common.add_audio_effect = in_add_audio_effect;
2403 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2404 in->stream.set_gain = in_set_gain;
2405 in->stream.read = in_read;
2406 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2407
2408 in->device = devices;
2409 in->source = AUDIO_SOURCE_DEFAULT;
2410 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002411 in->standby = 1;
2412 in->channel_mask = config->channel_mask;
2413
2414 /* Update config params with the requested sample rate and channels */
2415 in->usecase = USECASE_AUDIO_RECORD;
2416 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002417 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002418 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002419
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002420 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2421 (voice_extn_compress_voip_is_format_supported(in->format))) {
2422 ret = voice_extn_compress_voip_open_input_stream(in);
2423 if (ret != 0)
2424 {
2425 ALOGE("%s: Compress voip input cannot be opened, error:%d",
2426 __func__, ret);
2427 goto err_open;
2428 }
Mingming Yine62d7842013-10-25 16:26:03 -07002429 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002430 if(audio_extn_ssr_get_enabled()) {
2431 if(audio_extn_ssr_init(adev, in)) {
2432 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2433 ret = -EINVAL;
2434 goto err_open;
2435 }
2436 } else {
2437 ret = -EINVAL;
2438 goto err_open;
2439 }
Mingming Yine62d7842013-10-25 16:26:03 -07002440 } else if (audio_extn_compr_cap_enabled() &&
2441 audio_extn_compr_cap_format_supported(config->format)) {
2442 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002443 } else {
2444 in->config.channels = channel_count;
2445 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2446 buffer_size = get_input_buffer_size(config->sample_rate,
2447 config->format,
2448 channel_count);
2449 in->config.period_size = buffer_size / frame_size;
2450 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002451
2452 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002453 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002454 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002455
2456err_open:
2457 free(in);
2458 *stream_in = NULL;
2459 return ret;
2460}
2461
2462static void adev_close_input_stream(struct audio_hw_device *dev,
2463 struct audio_stream_in *stream)
2464{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002465 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002466 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002467 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002468
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002469 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2470 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2471 if (ret != 0)
2472 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2473 __func__, ret);
2474 } else
2475 in_standby(&stream->common);
2476
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002477 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2478 audio_extn_ssr_deinit();
2479 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002480 free(stream);
2481
Mingming Yine62d7842013-10-25 16:26:03 -07002482 if(audio_extn_compr_cap_enabled() &&
2483 audio_extn_compr_cap_format_supported(in->config.format))
2484 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002485 return;
2486}
2487
2488static int adev_dump(const audio_hw_device_t *device, int fd)
2489{
2490 return 0;
2491}
2492
2493static int adev_close(hw_device_t *device)
2494{
2495 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002496
2497 if (!adev)
2498 return 0;
2499
2500 pthread_mutex_lock(&adev_init_lock);
2501
2502 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002503 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07002504 audio_route_free(adev->audio_route);
2505 free(adev->snd_dev_ref_cnt);
2506 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002507 free(device);
2508 adev = NULL;
2509 }
2510 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002511 return 0;
2512}
2513
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002514static int adev_open(const hw_module_t *module, const char *name,
2515 hw_device_t **device)
2516{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002517 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002518
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002519 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002520 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2521
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002522 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002523 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002524 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002525 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002526 ALOGD("%s: returning existing instance of adev", __func__);
2527 ALOGD("%s: exit", __func__);
2528 pthread_mutex_unlock(&adev_init_lock);
2529 return 0;
2530 }
2531
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002532 adev = calloc(1, sizeof(struct audio_device));
2533
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002534 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2535 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2536 adev->device.common.module = (struct hw_module_t *)module;
2537 adev->device.common.close = adev_close;
2538
2539 adev->device.init_check = adev_init_check;
2540 adev->device.set_voice_volume = adev_set_voice_volume;
2541 adev->device.set_master_volume = adev_set_master_volume;
2542 adev->device.get_master_volume = adev_get_master_volume;
2543 adev->device.set_master_mute = adev_set_master_mute;
2544 adev->device.get_master_mute = adev_get_master_mute;
2545 adev->device.set_mode = adev_set_mode;
2546 adev->device.set_mic_mute = adev_set_mic_mute;
2547 adev->device.get_mic_mute = adev_get_mic_mute;
2548 adev->device.set_parameters = adev_set_parameters;
2549 adev->device.get_parameters = adev_get_parameters;
2550 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2551 adev->device.open_output_stream = adev_open_output_stream;
2552 adev->device.close_output_stream = adev_close_output_stream;
2553 adev->device.open_input_stream = adev_open_input_stream;
2554 adev->device.close_input_stream = adev_close_input_stream;
2555 adev->device.dump = adev_dump;
2556
2557 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002558 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002559 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002560 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002561 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002562 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002563 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002564 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002565 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002566 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002567 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002568
2569 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002570 adev->platform = platform_init(adev);
2571 if (!adev->platform) {
2572 free(adev->snd_dev_ref_cnt);
2573 free(adev);
2574 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2575 *device = NULL;
2576 return -EINVAL;
2577 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002578
2579 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2580 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2581 if (adev->visualizer_lib == NULL) {
2582 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2583 } else {
2584 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2585 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002586 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002587 "visualizer_hal_start_output");
2588 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002589 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002590 "visualizer_hal_stop_output");
2591 }
2592 }
Kiran Kandide144c82013-11-20 15:58:32 -08002593 audio_extn_listen_init(adev, SOUND_CARD);
Eric Laurentc4aef752013-09-12 17:45:53 -07002594
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002595 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2596 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2597 if (adev->offload_effects_lib == NULL) {
2598 ALOGE("%s: DLOPEN failed for %s", __func__,
2599 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2600 } else {
2601 ALOGV("%s: DLOPEN successful for %s", __func__,
2602 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2603 adev->offload_effects_start_output =
2604 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2605 "offload_effects_bundle_hal_start_output");
2606 adev->offload_effects_stop_output =
2607 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2608 "offload_effects_bundle_hal_stop_output");
2609 }
2610 }
2611
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002612 *device = &adev->device.common;
2613
Kiran Kandi910e1862013-10-29 13:29:42 -07002614 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002615 pthread_mutex_unlock(&adev_init_lock);
2616
Eric Laurent994a6932013-07-17 11:51:42 -07002617 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002618 return 0;
2619}
2620
2621static struct hw_module_methods_t hal_module_methods = {
2622 .open = adev_open,
2623};
2624
2625struct audio_module HAL_MODULE_INFO_SYM = {
2626 .common = {
2627 .tag = HARDWARE_MODULE_TAG,
2628 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2629 .hal_api_version = HARDWARE_HAL_API_VERSION,
2630 .id = AUDIO_HARDWARE_MODULE_ID,
2631 .name = "QCOM Audio HAL",
2632 .author = "Code Aurora Forum",
2633 .methods = &hal_module_methods,
2634 },
2635};