blob: 920dbc2ef5a1a9d6b8269a20ef3a6319cf5f31f0 [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",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800112 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700113 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700114
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700115 [USECASE_VOICE2_CALL] = "voice2-call",
116 [USECASE_VOLTE_CALL] = "volte-call",
117 [USECASE_QCHAT_CALL] = "qchat-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800118 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700119 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
120 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
121 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700122 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
123 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700124 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
125 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700126};
127
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800128
129#define STRING_TO_ENUM(string) { #string, string }
130
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800131struct string_to_enum {
132 const char *name;
133 uint32_t value;
134};
135
136static const struct string_to_enum out_channels_name_to_enum_table[] = {
137 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
138 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
139 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
140};
141
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700142static struct audio_device *adev = NULL;
143static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700144static unsigned int audio_device_ref_count;
145
Haynes Mathew George5191a852013-09-11 14:19:36 -0700146static int set_voice_volume_l(struct audio_device *adev, float volume);
147
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700148static bool is_supported_format(audio_format_t format)
149{
Eric Laurent86e17132013-09-12 17:49:30 -0700150 if (format == AUDIO_FORMAT_MP3 ||
151 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700152 return true;
153
154 return false;
155}
156
157static int get_snd_codec_id(audio_format_t format)
158{
159 int id = 0;
160
161 switch (format) {
162 case AUDIO_FORMAT_MP3:
163 id = SND_AUDIOCODEC_MP3;
164 break;
165 case AUDIO_FORMAT_AAC:
166 id = SND_AUDIOCODEC_AAC;
167 break;
168 default:
Mingming Yin90310102013-11-13 16:57:00 -0800169 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700170 }
171
172 return id;
173}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800174
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700175int enable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700176 struct audio_usecase *usecase,
177 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800178{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700179 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700180 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800181
182 if (usecase == NULL)
183 return -EINVAL;
184
185 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
186
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800187 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700188 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800189 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700190 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800191
192 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700193 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700194 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700195 audio_route_apply_path(adev->audio_route, mixer_path);
196 if (update_mixer)
197 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800198
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800199 ALOGV("%s: exit", __func__);
200 return 0;
201}
202
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700203int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700204 struct audio_usecase *usecase,
205 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800206{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700207 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700208 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800209
210 if (usecase == NULL)
211 return -EINVAL;
212
213 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700214 if (usecase->type == PCM_CAPTURE)
215 snd_device = usecase->in_snd_device;
216 else
217 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800218 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700219 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700220 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700221 audio_route_reset_path(adev->audio_route, mixer_path);
222 if (update_mixer)
223 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800224
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800225 ALOGV("%s: exit", __func__);
226 return 0;
227}
228
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700229int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700230 snd_device_t snd_device,
231 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800232{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700233 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
234
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800235 if (snd_device < SND_DEVICE_MIN ||
236 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800237 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800238 return -EINVAL;
239 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700240
241 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700242
243 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
244 ALOGE("%s: Invalid sound device returned", __func__);
245 return -EINVAL;
246 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700247 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700248 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700249 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700250 return 0;
251 }
252
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700253 /* start usb playback thread */
254 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
255 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
256 audio_extn_usb_start_playback(adev);
257
258 /* start usb capture thread */
259 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
260 audio_extn_usb_start_capture(adev);
261
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700262 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
263 audio_extn_spkr_prot_is_enabled()) {
264 if (audio_extn_spkr_prot_start_processing(snd_device)) {
265 ALOGE("%s: spkr_start_processing failed", __func__);
266 return -EINVAL;
267 }
268 } else {
269 ALOGV("%s: snd_device(%d: %s)", __func__,
270 snd_device, device_name);
271 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
272 adev->snd_dev_ref_cnt[snd_device]--;
273 return -EINVAL;
274 }
Kiran Kandide144c82013-11-20 15:58:32 -0800275 audio_extn_listen_update_status(snd_device,
276 LISTEN_EVENT_SND_DEVICE_BUSY);
277
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700278 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800279 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700280 if (update_mixer)
281 audio_route_update_mixer(adev->audio_route);
282
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800283 return 0;
284}
285
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700286int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700287 snd_device_t snd_device,
288 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800289{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700290 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
291
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800292 if (snd_device < SND_DEVICE_MIN ||
293 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800294 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800295 return -EINVAL;
296 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700297 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
298 ALOGE("%s: device ref cnt is already 0", __func__);
299 return -EINVAL;
300 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700301
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700302 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700303
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700304 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
305 ALOGE("%s: Invalid sound device returned", __func__);
306 return -EINVAL;
307 }
308
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700309 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700310 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700311 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800312 /* exit usb play back thread */
313 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
314 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
315 audio_extn_usb_stop_playback();
316
317 /* exit usb capture thread */
318 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
319 audio_extn_usb_stop_capture(adev);
320
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700321 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
322 audio_extn_spkr_prot_is_enabled()) {
323 audio_extn_spkr_prot_stop_processing();
324 } else
325 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800326
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700327 if (update_mixer)
328 audio_route_update_mixer(adev->audio_route);
Kiran Kandide144c82013-11-20 15:58:32 -0800329
330 audio_extn_listen_update_status(snd_device,
331 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700332 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700333
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800334 return 0;
335}
336
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700337static void check_usecases_codec_backend(struct audio_device *adev,
338 struct audio_usecase *uc_info,
339 snd_device_t snd_device)
340{
341 struct listnode *node;
342 struct audio_usecase *usecase;
343 bool switch_device[AUDIO_USECASE_MAX];
344 int i, num_uc_to_switch = 0;
345
346 /*
347 * This function is to make sure that all the usecases that are active on
348 * the hardware codec backend are always routed to any one device that is
349 * handled by the hardware codec.
350 * For example, if low-latency and deep-buffer usecases are currently active
351 * on speaker and out_set_parameters(headset) is received on low-latency
352 * output, then we have to make sure deep-buffer is also switched to headset,
353 * because of the limitation that both the devices cannot be enabled
354 * at the same time as they share the same backend.
355 */
356 /* Disable all the usecases on the shared backend other than the
357 specified usecase */
358 for (i = 0; i < AUDIO_USECASE_MAX; i++)
359 switch_device[i] = false;
360
361 list_for_each(node, &adev->usecase_list) {
362 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700363 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700364 usecase != uc_info &&
365 usecase->out_snd_device != snd_device &&
366 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
367 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
368 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700369 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700370 disable_audio_route(adev, usecase, false);
371 switch_device[usecase->id] = true;
372 num_uc_to_switch++;
373 }
374 }
375
376 if (num_uc_to_switch) {
377 /* Make sure all the streams are de-routed before disabling the device */
378 audio_route_update_mixer(adev->audio_route);
379
380 list_for_each(node, &adev->usecase_list) {
381 usecase = node_to_item(node, struct audio_usecase, list);
382 if (switch_device[usecase->id]) {
383 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700384 }
385 }
386
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700387 list_for_each(node, &adev->usecase_list) {
388 usecase = node_to_item(node, struct audio_usecase, list);
389 if (switch_device[usecase->id]) {
390 enable_snd_device(adev, snd_device, false);
391 }
392 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700393 /* Make sure new snd device is enabled before re-routing the streams */
394 audio_route_update_mixer(adev->audio_route);
395
396 /* Re-route all the usecases on the shared backend other than the
397 specified usecase to new snd devices */
398 list_for_each(node, &adev->usecase_list) {
399 usecase = node_to_item(node, struct audio_usecase, list);
400 /* Update the out_snd_device only before enabling the audio route */
401 if (switch_device[usecase->id] ) {
402 usecase->out_snd_device = snd_device;
403 enable_audio_route(adev, usecase, false);
404 }
405 }
406
407 audio_route_update_mixer(adev->audio_route);
408 }
409}
410
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700411static void check_and_route_capture_usecases(struct audio_device *adev,
412 struct audio_usecase *uc_info,
413 snd_device_t snd_device)
414{
415 struct listnode *node;
416 struct audio_usecase *usecase;
417 bool switch_device[AUDIO_USECASE_MAX];
418 int i, num_uc_to_switch = 0;
419
420 /*
421 * This function is to make sure that all the active capture usecases
422 * are always routed to the same input sound device.
423 * For example, if audio-record and voice-call usecases are currently
424 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
425 * is received for voice call then we have to make sure that audio-record
426 * usecase is also switched to earpiece i.e. voice-dmic-ef,
427 * because of the limitation that two devices cannot be enabled
428 * at the same time if they share the same backend.
429 */
430 for (i = 0; i < AUDIO_USECASE_MAX; i++)
431 switch_device[i] = false;
432
433 list_for_each(node, &adev->usecase_list) {
434 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700435 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700436 usecase != uc_info &&
437 usecase->in_snd_device != snd_device) {
438 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
439 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700440 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700441 disable_audio_route(adev, usecase, false);
442 switch_device[usecase->id] = true;
443 num_uc_to_switch++;
444 }
445 }
446
447 if (num_uc_to_switch) {
448 /* Make sure all the streams are de-routed before disabling the device */
449 audio_route_update_mixer(adev->audio_route);
450
451 list_for_each(node, &adev->usecase_list) {
452 usecase = node_to_item(node, struct audio_usecase, list);
453 if (switch_device[usecase->id]) {
454 disable_snd_device(adev, usecase->in_snd_device, false);
455 enable_snd_device(adev, snd_device, false);
456 }
457 }
458
459 /* Make sure new snd device is enabled before re-routing the streams */
460 audio_route_update_mixer(adev->audio_route);
461
462 /* Re-route all the usecases on the shared backend other than the
463 specified usecase to new snd devices */
464 list_for_each(node, &adev->usecase_list) {
465 usecase = node_to_item(node, struct audio_usecase, list);
466 /* Update the in_snd_device only before enabling the audio route */
467 if (switch_device[usecase->id] ) {
468 usecase->in_snd_device = snd_device;
469 enable_audio_route(adev, usecase, false);
470 }
471 }
472
473 audio_route_update_mixer(adev->audio_route);
474 }
475}
476
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700477static int disable_all_usecases_of_type(struct audio_device *adev,
478 usecase_type_t usecase_type,
479 bool update_mixer)
480{
481 struct audio_usecase *usecase;
482 struct listnode *node;
483 int ret = 0;
484
485 list_for_each(node, &adev->usecase_list) {
486 usecase = node_to_item(node, struct audio_usecase, list);
487 if (usecase->type == usecase_type) {
488 ALOGV("%s: usecase id %d", __func__, usecase->id);
489 ret = disable_audio_route(adev, usecase, update_mixer);
490 if (ret) {
491 ALOGE("%s: Failed to disable usecase id %d",
492 __func__, usecase->id);
493 }
494 }
495 }
496
497 return ret;
498}
499
500static int enable_all_usecases_of_type(struct audio_device *adev,
501 usecase_type_t usecase_type,
502 bool update_mixer)
503{
504 struct audio_usecase *usecase;
505 struct listnode *node;
506 int ret = 0;
507
508 list_for_each(node, &adev->usecase_list) {
509 usecase = node_to_item(node, struct audio_usecase, list);
510 if (usecase->type == usecase_type) {
511 ALOGV("%s: usecase id %d", __func__, usecase->id);
512 ret = enable_audio_route(adev, usecase, update_mixer);
513 if (ret) {
514 ALOGE("%s: Failed to enable usecase id %d",
515 __func__, usecase->id);
516 }
517 }
518 }
519
520 return ret;
521}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800522
523/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700524static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800525{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700526 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700527 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800528
529 switch (channels) {
530 /*
531 * Do not handle stereo output in Multi-channel cases
532 * Stereo case is handled in normal playback path
533 */
534 case 6:
535 ALOGV("%s: HDMI supports 5.1", __func__);
536 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
537 break;
538 case 8:
539 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
540 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
541 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
542 break;
543 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700544 ALOGE("HDMI does not support multi channel playback");
545 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800546 break;
547 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700548 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800549}
550
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700551static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
552{
553 struct audio_usecase *usecase;
554 struct listnode *node;
555
556 list_for_each(node, &adev->usecase_list) {
557 usecase = node_to_item(node, struct audio_usecase, list);
558 if (usecase->type == VOICE_CALL) {
559 ALOGV("%s: usecase id %d", __func__, usecase->id);
560 return usecase->id;
561 }
562 }
563 return USECASE_INVALID;
564}
565
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700566struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700567 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700568{
569 struct audio_usecase *usecase;
570 struct listnode *node;
571
572 list_for_each(node, &adev->usecase_list) {
573 usecase = node_to_item(node, struct audio_usecase, list);
574 if (usecase->id == uc_id)
575 return usecase;
576 }
577 return NULL;
578}
579
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700580int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800581{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800582 snd_device_t out_snd_device = SND_DEVICE_NONE;
583 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700584 struct audio_usecase *usecase = NULL;
585 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800586 struct audio_usecase *voip_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800587 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700588 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800589
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700590 usecase = get_usecase_from_list(adev, uc_id);
591 if (usecase == NULL) {
592 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
593 return -EINVAL;
594 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800595
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800596 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800597 (usecase->type == VOIP_CALL) ||
598 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700599 out_snd_device = platform_get_output_snd_device(adev->platform,
600 usecase->stream.out->devices);
601 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700602 usecase->devices = usecase->stream.out->devices;
603 } else {
604 /*
605 * If the voice call is active, use the sound devices of voice call usecase
606 * so that it would not result any device switch. All the usecases will
607 * be switched to new device when select_devices() is called for voice call
608 * usecase. This is to avoid switching devices for voice call when
609 * check_usecases_codec_backend() is called below.
610 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700611 if (voice_is_in_call(adev)) {
612 vc_usecase = get_usecase_from_list(adev,
613 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700614 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
615 in_snd_device = vc_usecase->in_snd_device;
616 out_snd_device = vc_usecase->out_snd_device;
617 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800618 } else if (voice_extn_compress_voip_is_active(adev)) {
619 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
620 if (voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
621 in_snd_device = voip_usecase->in_snd_device;
622 out_snd_device = voip_usecase->out_snd_device;
623 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700624 }
625 if (usecase->type == PCM_PLAYBACK) {
626 usecase->devices = usecase->stream.out->devices;
627 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700628 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700629 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700630 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700631 if (usecase->stream.out == adev->primary_output &&
632 adev->active_input &&
633 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
634 select_devices(adev, adev->active_input->usecase);
635 }
636 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700637 } else if (usecase->type == PCM_CAPTURE) {
638 usecase->devices = usecase->stream.in->device;
639 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700640 if (in_snd_device == SND_DEVICE_NONE) {
641 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
642 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700643 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700644 adev->primary_output->devices);
645 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700646 in_snd_device = platform_get_input_snd_device(adev->platform,
647 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700648 }
649 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700650 }
651 }
652
653 if (out_snd_device == usecase->out_snd_device &&
654 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800655 return 0;
656 }
657
sangwoobc677242013-08-08 16:53:43 +0900658 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700659 out_snd_device, platform_get_snd_device_name(out_snd_device),
660 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800661
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800662 /*
663 * Limitation: While in call, to do a device switch we need to disable
664 * and enable both RX and TX devices though one of them is same as current
665 * device.
666 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800667 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700668 status = platform_switch_voice_call_device_pre(adev->platform);
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800669 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800670 }
671
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700672 /* Disable current sound devices */
673 if (usecase->out_snd_device != SND_DEVICE_NONE) {
674 disable_audio_route(adev, usecase, true);
675 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800676 }
677
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700678 if (usecase->in_snd_device != SND_DEVICE_NONE) {
679 disable_audio_route(adev, usecase, true);
680 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800681 }
682
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700683 /* Enable new sound devices */
684 if (out_snd_device != SND_DEVICE_NONE) {
685 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
686 check_usecases_codec_backend(adev, usecase, out_snd_device);
687 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800688 }
689
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700690 if (in_snd_device != SND_DEVICE_NONE) {
691 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700692 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700693 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700694
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800695 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700696 status = platform_switch_voice_call_device_post(adev->platform,
697 out_snd_device,
698 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800699
sangwoo170731f2013-06-08 15:36:36 +0900700 audio_route_update_mixer(adev->audio_route);
701
702 usecase->in_snd_device = in_snd_device;
703 usecase->out_snd_device = out_snd_device;
704
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800705 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
706 enable_all_usecases_of_type(adev, usecase->type, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700707 else
708 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900709
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800710 /* Applicable only on the targets that has external modem.
711 * Enable device command should be sent to modem only after
712 * enabling voice call mixer controls
713 */
714 if (usecase->type == VOICE_CALL)
715 status = platform_switch_voice_call_usecase_route_post(adev->platform,
716 out_snd_device,
717 in_snd_device);
718
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800719 return status;
720}
721
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800722static int stop_input_stream(struct stream_in *in)
723{
724 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800725 struct audio_usecase *uc_info;
726 struct audio_device *adev = in->dev;
727
Eric Laurentc8400632013-02-14 19:04:54 -0800728 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800729
Eric Laurent994a6932013-07-17 11:51:42 -0700730 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700731 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732 uc_info = get_usecase_from_list(adev, in->usecase);
733 if (uc_info == NULL) {
734 ALOGE("%s: Could not find the usecase (%d) in the list",
735 __func__, in->usecase);
736 return -EINVAL;
737 }
738
Eric Laurent150dbfe2013-02-27 14:31:02 -0800739 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700740 disable_audio_route(adev, uc_info, true);
741
742 /* 2. Disable the tx device */
743 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800744
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800745 list_remove(&uc_info->list);
746 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800747
Eric Laurent994a6932013-07-17 11:51:42 -0700748 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800749 return ret;
750}
751
752int start_input_stream(struct stream_in *in)
753{
754 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800755 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800756 struct audio_usecase *uc_info;
757 struct audio_device *adev = in->dev;
758
Mingming Yine62d7842013-10-25 16:26:03 -0700759 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Eric Laurent994a6932013-07-17 11:51:42 -0700760 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700761
762 /* Check if source matches incall recording usecase criteria */
763 ret = voice_check_and_set_incall_rec_usecase(adev, in);
764 if (ret)
765 goto error_config;
766 else
767 ALOGV("%s: usecase(%d)", __func__, in->usecase);
768
Eric Laurentb23d5282013-05-14 15:27:20 -0700769 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800770 if (in->pcm_device_id < 0) {
771 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
772 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800773 ret = -EINVAL;
774 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800775 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700776
777 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800778 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
779 uc_info->id = in->usecase;
780 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800781 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700782 uc_info->devices = in->device;
783 uc_info->in_snd_device = SND_DEVICE_NONE;
784 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800785
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800786 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700787 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800788
Eric Laurentc8400632013-02-14 19:04:54 -0800789 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
790 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800791 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
792 PCM_IN, &in->config);
793 if (in->pcm && !pcm_is_ready(in->pcm)) {
794 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
795 pcm_close(in->pcm);
796 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800797 ret = -EIO;
798 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800799 }
Eric Laurent994a6932013-07-17 11:51:42 -0700800 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800801 return ret;
802
803error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800804 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800805
806error_config:
807 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700808 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800809
810 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800811}
812
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700813/* must be called with out->lock locked */
814static int send_offload_cmd_l(struct stream_out* out, int command)
815{
816 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
817
818 ALOGVV("%s %d", __func__, command);
819
820 cmd->cmd = command;
821 list_add_tail(&out->offload_cmd_list, &cmd->node);
822 pthread_cond_signal(&out->offload_cond);
823 return 0;
824}
825
826/* must be called iwth out->lock locked */
827static void stop_compressed_output_l(struct stream_out *out)
828{
829 out->offload_state = OFFLOAD_STATE_IDLE;
830 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700831 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700832 if (out->compr != NULL) {
833 compress_stop(out->compr);
834 while (out->offload_thread_blocked) {
835 pthread_cond_wait(&out->cond, &out->lock);
836 }
837 }
838}
839
840static void *offload_thread_loop(void *context)
841{
842 struct stream_out *out = (struct stream_out *) context;
843 struct listnode *item;
844
845 out->offload_state = OFFLOAD_STATE_IDLE;
846 out->playback_started = 0;
847
848 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
849 set_sched_policy(0, SP_FOREGROUND);
850 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
851
852 ALOGV("%s", __func__);
853 pthread_mutex_lock(&out->lock);
854 for (;;) {
855 struct offload_cmd *cmd = NULL;
856 stream_callback_event_t event;
857 bool send_callback = false;
858
859 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
860 __func__, list_empty(&out->offload_cmd_list),
861 out->offload_state);
862 if (list_empty(&out->offload_cmd_list)) {
863 ALOGV("%s SLEEPING", __func__);
864 pthread_cond_wait(&out->offload_cond, &out->lock);
865 ALOGV("%s RUNNING", __func__);
866 continue;
867 }
868
869 item = list_head(&out->offload_cmd_list);
870 cmd = node_to_item(item, struct offload_cmd, node);
871 list_remove(item);
872
873 ALOGVV("%s STATE %d CMD %d out->compr %p",
874 __func__, out->offload_state, cmd->cmd, out->compr);
875
876 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
877 free(cmd);
878 break;
879 }
880
881 if (out->compr == NULL) {
882 ALOGE("%s: Compress handle is NULL", __func__);
883 pthread_cond_signal(&out->cond);
884 continue;
885 }
886 out->offload_thread_blocked = true;
887 pthread_mutex_unlock(&out->lock);
888 send_callback = false;
889 switch(cmd->cmd) {
890 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
891 compress_wait(out->compr, -1);
892 send_callback = true;
893 event = STREAM_CBK_EVENT_WRITE_READY;
894 break;
895 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700896 compress_next_track(out->compr);
897 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700898 send_callback = true;
899 event = STREAM_CBK_EVENT_DRAIN_READY;
900 break;
901 case OFFLOAD_CMD_DRAIN:
902 compress_drain(out->compr);
903 send_callback = true;
904 event = STREAM_CBK_EVENT_DRAIN_READY;
905 break;
906 default:
907 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
908 break;
909 }
910 pthread_mutex_lock(&out->lock);
911 out->offload_thread_blocked = false;
912 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700913 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700914 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700915 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700916 free(cmd);
917 }
918
919 pthread_cond_signal(&out->cond);
920 while (!list_empty(&out->offload_cmd_list)) {
921 item = list_head(&out->offload_cmd_list);
922 list_remove(item);
923 free(node_to_item(item, struct offload_cmd, node));
924 }
925 pthread_mutex_unlock(&out->lock);
926
927 return NULL;
928}
929
930static int create_offload_callback_thread(struct stream_out *out)
931{
932 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
933 list_init(&out->offload_cmd_list);
934 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
935 offload_thread_loop, out);
936 return 0;
937}
938
939static int destroy_offload_callback_thread(struct stream_out *out)
940{
941 pthread_mutex_lock(&out->lock);
942 stop_compressed_output_l(out);
943 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
944
945 pthread_mutex_unlock(&out->lock);
946 pthread_join(out->offload_thread, (void **) NULL);
947 pthread_cond_destroy(&out->offload_cond);
948
949 return 0;
950}
951
Eric Laurent07eeafd2013-10-06 12:52:49 -0700952static bool allow_hdmi_channel_config(struct audio_device *adev)
953{
954 struct listnode *node;
955 struct audio_usecase *usecase;
956 bool ret = true;
957
958 list_for_each(node, &adev->usecase_list) {
959 usecase = node_to_item(node, struct audio_usecase, list);
960 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
961 /*
962 * If voice call is already existing, do not proceed further to avoid
963 * disabling/enabling both RX and TX devices, CSD calls, etc.
964 * Once the voice call done, the HDMI channels can be configured to
965 * max channels of remaining use cases.
966 */
967 if (usecase->id == USECASE_VOICE_CALL) {
968 ALOGD("%s: voice call is active, no change in HDMI channels",
969 __func__);
970 ret = false;
971 break;
972 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
973 ALOGD("%s: multi channel playback is active, "
974 "no change in HDMI channels", __func__);
975 ret = false;
976 break;
977 }
978 }
979 }
980 return ret;
981}
982
983static int check_and_set_hdmi_channels(struct audio_device *adev,
984 unsigned int channels)
985{
986 struct listnode *node;
987 struct audio_usecase *usecase;
988
989 /* Check if change in HDMI channel config is allowed */
990 if (!allow_hdmi_channel_config(adev))
991 return 0;
992
993 if (channels == adev->cur_hdmi_channels) {
994 ALOGD("%s: Requested channels are same as current", __func__);
995 return 0;
996 }
997
998 platform_set_hdmi_channels(adev->platform, channels);
999 adev->cur_hdmi_channels = channels;
1000
1001 /*
1002 * Deroute all the playback streams routed to HDMI so that
1003 * the back end is deactivated. Note that backend will not
1004 * be deactivated if any one stream is connected to it.
1005 */
1006 list_for_each(node, &adev->usecase_list) {
1007 usecase = node_to_item(node, struct audio_usecase, list);
1008 if (usecase->type == PCM_PLAYBACK &&
1009 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1010 disable_audio_route(adev, usecase, true);
1011 }
1012 }
1013
1014 /*
1015 * Enable all the streams disabled above. Now the HDMI backend
1016 * will be activated with new channel configuration
1017 */
1018 list_for_each(node, &adev->usecase_list) {
1019 usecase = node_to_item(node, struct audio_usecase, list);
1020 if (usecase->type == PCM_PLAYBACK &&
1021 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1022 enable_audio_route(adev, usecase, true);
1023 }
1024 }
1025
1026 return 0;
1027}
1028
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001029static int stop_output_stream(struct stream_out *out)
1030{
1031 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001032 struct audio_usecase *uc_info;
1033 struct audio_device *adev = out->dev;
1034
Eric Laurent994a6932013-07-17 11:51:42 -07001035 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001036 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001037 uc_info = get_usecase_from_list(adev, out->usecase);
1038 if (uc_info == NULL) {
1039 ALOGE("%s: Could not find the usecase (%d) in the list",
1040 __func__, out->usecase);
1041 return -EINVAL;
1042 }
1043
Eric Laurentc4aef752013-09-12 17:45:53 -07001044 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
1045 adev->visualizer_stop_output != NULL)
1046 adev->visualizer_stop_output(out->handle);
1047
Eric Laurent150dbfe2013-02-27 14:31:02 -08001048 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001049 disable_audio_route(adev, uc_info, true);
1050
1051 /* 2. Disable the rx device */
1052 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001053
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001054 list_remove(&uc_info->list);
1055 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001056
Eric Laurent07eeafd2013-10-06 12:52:49 -07001057 /* Must be called after removing the usecase from list */
1058 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1059 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1060
Eric Laurent994a6932013-07-17 11:51:42 -07001061 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001062 return ret;
1063}
1064
1065int start_output_stream(struct stream_out *out)
1066{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001067 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001068 struct audio_usecase *uc_info;
1069 struct audio_device *adev = out->dev;
1070
Eric Laurent994a6932013-07-17 11:51:42 -07001071 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001072 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001073 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001074 if (out->pcm_device_id < 0) {
1075 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1076 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001077 ret = -EINVAL;
1078 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001079 }
1080
1081 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1082 uc_info->id = out->usecase;
1083 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001084 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001085 uc_info->devices = out->devices;
1086 uc_info->in_snd_device = SND_DEVICE_NONE;
1087 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001088
Eric Laurent07eeafd2013-10-06 12:52:49 -07001089 /* This must be called before adding this usecase to the list */
1090 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1091 check_and_set_hdmi_channels(adev, out->config.channels);
1092
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001093 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001094
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001095 select_devices(adev, out->usecase);
1096
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001097 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1098 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001099 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1100 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001101 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001102 if (out->pcm && !pcm_is_ready(out->pcm)) {
1103 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1104 pcm_close(out->pcm);
1105 out->pcm = NULL;
1106 ret = -EIO;
1107 goto error_open;
1108 }
1109 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001110 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001111 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1112 COMPRESS_IN, &out->compr_config);
1113 if (out->compr && !is_compress_ready(out->compr)) {
1114 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1115 compress_close(out->compr);
1116 out->compr = NULL;
1117 ret = -EIO;
1118 goto error_open;
1119 }
1120 if (out->offload_callback)
1121 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001122
1123 if (adev->visualizer_start_output != NULL)
1124 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001125 }
Eric Laurent994a6932013-07-17 11:51:42 -07001126 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001127 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001128error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001129 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001130error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001131 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001132}
1133
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001134static int check_input_parameters(uint32_t sample_rate,
1135 audio_format_t format,
1136 int channel_count)
1137{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001138 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001139
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001140 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001141 !voice_extn_compress_voip_is_format_supported(format) &&
1142 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001143
1144 switch (channel_count) {
1145 case 1:
1146 case 2:
1147 case 6:
1148 break;
1149 default:
1150 ret = -EINVAL;
1151 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001152
1153 switch (sample_rate) {
1154 case 8000:
1155 case 11025:
1156 case 12000:
1157 case 16000:
1158 case 22050:
1159 case 24000:
1160 case 32000:
1161 case 44100:
1162 case 48000:
1163 break;
1164 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001165 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001166 }
1167
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001168 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001169}
1170
1171static size_t get_input_buffer_size(uint32_t sample_rate,
1172 audio_format_t format,
1173 int channel_count)
1174{
1175 size_t size = 0;
1176
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001177 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1178 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001179
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001180 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1181 /* ToDo: should use frame_size computed based on the format and
1182 channel_count here. */
1183 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001184
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001185 /* make sure the size is multiple of 64 */
1186 size += 0x3f;
1187 size &= ~0x3f;
1188
1189 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001190}
1191
1192static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1193{
1194 struct stream_out *out = (struct stream_out *)stream;
1195
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001196 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001197}
1198
1199static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1200{
1201 return -ENOSYS;
1202}
1203
1204static size_t out_get_buffer_size(const struct audio_stream *stream)
1205{
1206 struct stream_out *out = (struct stream_out *)stream;
1207
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001208 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001209 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001210 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1211 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001212
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001213 return out->config.period_size * audio_stream_frame_size(stream);
1214}
1215
1216static uint32_t out_get_channels(const struct audio_stream *stream)
1217{
1218 struct stream_out *out = (struct stream_out *)stream;
1219
1220 return out->channel_mask;
1221}
1222
1223static audio_format_t out_get_format(const struct audio_stream *stream)
1224{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001225 struct stream_out *out = (struct stream_out *)stream;
1226
1227 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001228}
1229
1230static int out_set_format(struct audio_stream *stream, audio_format_t format)
1231{
1232 return -ENOSYS;
1233}
1234
1235static int out_standby(struct audio_stream *stream)
1236{
1237 struct stream_out *out = (struct stream_out *)stream;
1238 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001239
Eric Laurent994a6932013-07-17 11:51:42 -07001240 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001241 out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001242 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1243 /* Ignore standby in case of voip call because the voip output
1244 * stream is closed in adev_close_output_stream()
1245 */
1246 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1247 return 0;
1248 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001249
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001250 pthread_mutex_lock(&out->lock);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001251 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001252 if (!out->standby) {
1253 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001254 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1255 if (out->pcm) {
1256 pcm_close(out->pcm);
1257 out->pcm = NULL;
1258 }
1259 } else {
1260 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001261 out->gapless_mdata.encoder_delay = 0;
1262 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001263 if (out->compr != NULL) {
1264 compress_close(out->compr);
1265 out->compr = NULL;
1266 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001267 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001268 stop_output_stream(out);
1269 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001270 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001271 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001272 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273 return 0;
1274}
1275
1276static int out_dump(const struct audio_stream *stream, int fd)
1277{
1278 return 0;
1279}
1280
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001281static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1282{
1283 int ret = 0;
1284 char value[32];
1285 struct compr_gapless_mdata tmp_mdata;
1286
1287 if (!out || !parms) {
1288 return -EINVAL;
1289 }
1290
1291 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1292 if (ret >= 0) {
1293 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1294 } else {
1295 return -EINVAL;
1296 }
1297
1298 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1299 if (ret >= 0) {
1300 tmp_mdata.encoder_padding = atoi(value);
1301 } else {
1302 return -EINVAL;
1303 }
1304
1305 out->gapless_mdata = tmp_mdata;
1306 out->send_new_metadata = 1;
1307 ALOGV("%s new encoder delay %u and padding %u", __func__,
1308 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1309
1310 return 0;
1311}
1312
1313
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001314static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1315{
1316 struct stream_out *out = (struct stream_out *)stream;
1317 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001318 struct audio_usecase *usecase;
1319 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001320 struct str_parms *parms;
1321 char value[32];
1322 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001323 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001324
sangwoobc677242013-08-08 16:53:43 +09001325 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001326 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001327 parms = str_parms_create_str(kvpairs);
1328 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1329 if (ret >= 0) {
1330 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001331 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001332 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001333
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001334 /*
1335 * When HDMI cable is unplugged the music playback is paused and
1336 * the policy manager sends routing=0. But the audioflinger
1337 * continues to write data until standby time (3sec).
1338 * As the HDMI core is turned off, the write gets blocked.
1339 * Avoid this by routing audio to speaker until standby.
1340 */
1341 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1342 val == AUDIO_DEVICE_NONE) {
1343 val = AUDIO_DEVICE_OUT_SPEAKER;
1344 }
1345
1346 /*
1347 * select_devices() call below switches all the usecases on the same
1348 * backend to the new device. Refer to check_usecases_codec_backend() in
1349 * the select_devices(). But how do we undo this?
1350 *
1351 * For example, music playback is active on headset (deep-buffer usecase)
1352 * and if we go to ringtones and select a ringtone, low-latency usecase
1353 * will be started on headset+speaker. As we can't enable headset+speaker
1354 * and headset devices at the same time, select_devices() switches the music
1355 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1356 * So when the ringtone playback is completed, how do we undo the same?
1357 *
1358 * We are relying on the out_set_parameters() call on deep-buffer output,
1359 * once the ringtone playback is ended.
1360 * NOTE: We should not check if the current devices are same as new devices.
1361 * Because select_devices() must be called to switch back the music
1362 * playback to headset.
1363 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001364 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001365 out->devices = val;
1366
1367 if (!out->standby)
1368 select_devices(adev, out->usecase);
1369
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001370 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1371 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001372 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001373 voice_start_call(adev);
1374 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1375 voice_is_in_call(adev) &&
1376 (out == adev->primary_output)) {
1377 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001378 }
1379 }
1380
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001381 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1382 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001383 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001384 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001385 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001386
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001387 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001388 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001389 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001390
1391 if (out == adev->primary_output) {
1392 pthread_mutex_lock(&adev->lock);
1393 audio_extn_set_parameters(adev, parms);
1394 pthread_mutex_unlock(&adev->lock);
1395 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001396 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1397 parse_compress_metadata(out, parms);
1398 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001399
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001400 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001401 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001402 return ret;
1403}
1404
1405static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1406{
1407 struct stream_out *out = (struct stream_out *)stream;
1408 struct str_parms *query = str_parms_create_str(keys);
1409 char *str;
1410 char value[256];
1411 struct str_parms *reply = str_parms_create();
1412 size_t i, j;
1413 int ret;
1414 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001415 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001416 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1417 if (ret >= 0) {
1418 value[0] = '\0';
1419 i = 0;
1420 while (out->supported_channel_masks[i] != 0) {
1421 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1422 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1423 if (!first) {
1424 strcat(value, "|");
1425 }
1426 strcat(value, out_channels_name_to_enum_table[j].name);
1427 first = false;
1428 break;
1429 }
1430 }
1431 i++;
1432 }
1433 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1434 str = str_parms_to_str(reply);
1435 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001436 voice_extn_out_get_parameters(out, query, reply);
1437 str = str_parms_to_str(reply);
1438 if (!strncmp(str, "", sizeof(""))) {
1439 str = strdup(keys);
1440 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001441 }
1442 str_parms_destroy(query);
1443 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001444 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001445 return str;
1446}
1447
1448static uint32_t out_get_latency(const struct audio_stream_out *stream)
1449{
1450 struct stream_out *out = (struct stream_out *)stream;
1451
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001452 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1453 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1454
1455 return (out->config.period_count * out->config.period_size * 1000) /
1456 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001457}
1458
1459static int out_set_volume(struct audio_stream_out *stream, float left,
1460 float right)
1461{
Eric Laurenta9024de2013-04-04 09:19:12 -07001462 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001463 int volume[2];
1464
Eric Laurenta9024de2013-04-04 09:19:12 -07001465 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1466 /* only take left channel into account: the API is for stereo anyway */
1467 out->muted = (left == 0.0f);
1468 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001469 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1470 const char *mixer_ctl_name = "Compress Playback Volume";
1471 struct audio_device *adev = out->dev;
1472 struct mixer_ctl *ctl;
1473
1474 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1475 if (!ctl) {
1476 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1477 __func__, mixer_ctl_name);
1478 return -EINVAL;
1479 }
1480 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1481 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1482 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1483 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001484 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001485
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001486 return -ENOSYS;
1487}
1488
1489static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1490 size_t bytes)
1491{
1492 struct stream_out *out = (struct stream_out *)stream;
1493 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001494 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001495
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001496 pthread_mutex_lock(&out->lock);
1497 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001498 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001499 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001500 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1501 ret = voice_extn_compress_voip_start_output_stream(out);
1502 else
1503 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001504 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001505 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001506 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001507 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001508 goto exit;
1509 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001510 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001511
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001512 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001513 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1514 if (out->send_new_metadata) {
1515 ALOGVV("send new gapless metadata");
1516 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1517 out->send_new_metadata = 0;
1518 }
1519
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001520 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001521 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001522 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001523 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1524 }
1525 if (!out->playback_started) {
1526 compress_start(out->compr);
1527 out->playback_started = 1;
1528 out->offload_state = OFFLOAD_STATE_PLAYING;
1529 }
1530 pthread_mutex_unlock(&out->lock);
1531 return ret;
1532 } else {
1533 if (out->pcm) {
1534 if (out->muted)
1535 memset((void *)buffer, 0, bytes);
1536 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1537 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001538 if (ret == 0)
1539 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001540 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001541 }
1542
1543exit:
1544 pthread_mutex_unlock(&out->lock);
1545
1546 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001547 if (out->pcm)
1548 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001549 out_standby(&out->stream.common);
1550 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1551 out_get_sample_rate(&out->stream.common));
1552 }
1553 return bytes;
1554}
1555
1556static int out_get_render_position(const struct audio_stream_out *stream,
1557 uint32_t *dsp_frames)
1558{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001559 struct stream_out *out = (struct stream_out *)stream;
1560 *dsp_frames = 0;
1561 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1562 pthread_mutex_lock(&out->lock);
1563 if (out->compr != NULL) {
1564 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1565 &out->sample_rate);
1566 ALOGVV("%s rendered frames %d sample_rate %d",
1567 __func__, *dsp_frames, out->sample_rate);
1568 }
1569 pthread_mutex_unlock(&out->lock);
1570 return 0;
1571 } else
1572 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001573}
1574
1575static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1576{
1577 return 0;
1578}
1579
1580static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1581{
1582 return 0;
1583}
1584
1585static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1586 int64_t *timestamp)
1587{
1588 return -EINVAL;
1589}
1590
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001591static int out_get_presentation_position(const struct audio_stream_out *stream,
1592 uint64_t *frames, struct timespec *timestamp)
1593{
1594 struct stream_out *out = (struct stream_out *)stream;
1595 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001596 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001597
1598 pthread_mutex_lock(&out->lock);
1599
Eric Laurent949a0892013-09-20 09:20:13 -07001600 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1601 if (out->compr != NULL) {
1602 compress_get_tstamp(out->compr, &dsp_frames,
1603 &out->sample_rate);
1604 ALOGVV("%s rendered frames %ld sample_rate %d",
1605 __func__, dsp_frames, out->sample_rate);
1606 *frames = dsp_frames;
1607 ret = 0;
1608 /* this is the best we can do */
1609 clock_gettime(CLOCK_MONOTONIC, timestamp);
1610 }
1611 } else {
1612 if (out->pcm) {
1613 size_t avail;
1614 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1615 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001616 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001617 // This adjustment accounts for buffering after app processor.
1618 // It is based on estimated DSP latency per use case, rather than exact.
1619 signed_frames -=
1620 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1621
Eric Laurent949a0892013-09-20 09:20:13 -07001622 // It would be unusual for this value to be negative, but check just in case ...
1623 if (signed_frames >= 0) {
1624 *frames = signed_frames;
1625 ret = 0;
1626 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001627 }
1628 }
1629 }
1630
1631 pthread_mutex_unlock(&out->lock);
1632
1633 return ret;
1634}
1635
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001636static int out_set_callback(struct audio_stream_out *stream,
1637 stream_callback_t callback, void *cookie)
1638{
1639 struct stream_out *out = (struct stream_out *)stream;
1640
1641 ALOGV("%s", __func__);
1642 pthread_mutex_lock(&out->lock);
1643 out->offload_callback = callback;
1644 out->offload_cookie = cookie;
1645 pthread_mutex_unlock(&out->lock);
1646 return 0;
1647}
1648
1649static int out_pause(struct audio_stream_out* stream)
1650{
1651 struct stream_out *out = (struct stream_out *)stream;
1652 int status = -ENOSYS;
1653 ALOGV("%s", __func__);
1654 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1655 pthread_mutex_lock(&out->lock);
1656 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1657 status = compress_pause(out->compr);
1658 out->offload_state = OFFLOAD_STATE_PAUSED;
1659 }
1660 pthread_mutex_unlock(&out->lock);
1661 }
1662 return status;
1663}
1664
1665static int out_resume(struct audio_stream_out* stream)
1666{
1667 struct stream_out *out = (struct stream_out *)stream;
1668 int status = -ENOSYS;
1669 ALOGV("%s", __func__);
1670 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1671 status = 0;
1672 pthread_mutex_lock(&out->lock);
1673 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1674 status = compress_resume(out->compr);
1675 out->offload_state = OFFLOAD_STATE_PLAYING;
1676 }
1677 pthread_mutex_unlock(&out->lock);
1678 }
1679 return status;
1680}
1681
1682static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1683{
1684 struct stream_out *out = (struct stream_out *)stream;
1685 int status = -ENOSYS;
1686 ALOGV("%s", __func__);
1687 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1688 pthread_mutex_lock(&out->lock);
1689 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1690 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1691 else
1692 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1693 pthread_mutex_unlock(&out->lock);
1694 }
1695 return status;
1696}
1697
1698static int out_flush(struct audio_stream_out* stream)
1699{
1700 struct stream_out *out = (struct stream_out *)stream;
1701 ALOGV("%s", __func__);
1702 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1703 pthread_mutex_lock(&out->lock);
1704 stop_compressed_output_l(out);
1705 pthread_mutex_unlock(&out->lock);
1706 return 0;
1707 }
1708 return -ENOSYS;
1709}
1710
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001711/** audio_stream_in implementation **/
1712static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1713{
1714 struct stream_in *in = (struct stream_in *)stream;
1715
1716 return in->config.rate;
1717}
1718
1719static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1720{
1721 return -ENOSYS;
1722}
1723
1724static size_t in_get_buffer_size(const struct audio_stream *stream)
1725{
1726 struct stream_in *in = (struct stream_in *)stream;
1727
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001728 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1729 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001730 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1731 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001732
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001733 return in->config.period_size * audio_stream_frame_size(stream);
1734}
1735
1736static uint32_t in_get_channels(const struct audio_stream *stream)
1737{
1738 struct stream_in *in = (struct stream_in *)stream;
1739
1740 return in->channel_mask;
1741}
1742
1743static audio_format_t in_get_format(const struct audio_stream *stream)
1744{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001745 struct stream_in *in = (struct stream_in *)stream;
1746
1747 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001748}
1749
1750static int in_set_format(struct audio_stream *stream, audio_format_t format)
1751{
1752 return -ENOSYS;
1753}
1754
1755static int in_standby(struct audio_stream *stream)
1756{
1757 struct stream_in *in = (struct stream_in *)stream;
1758 struct audio_device *adev = in->dev;
1759 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001760 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001761
1762 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1763 /* Ignore standby in case of voip call because the voip input
1764 * stream is closed in adev_close_input_stream()
1765 */
1766 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1767 return status;
1768 }
1769
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001770 pthread_mutex_lock(&in->lock);
1771 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001772 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001773 if (in->pcm) {
1774 pcm_close(in->pcm);
1775 in->pcm = NULL;
1776 }
1777 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001778 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001779 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001780 }
1781 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001782 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001783 return status;
1784}
1785
1786static int in_dump(const struct audio_stream *stream, int fd)
1787{
1788 return 0;
1789}
1790
1791static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1792{
1793 struct stream_in *in = (struct stream_in *)stream;
1794 struct audio_device *adev = in->dev;
1795 struct str_parms *parms;
1796 char *str;
1797 char value[32];
1798 int ret, val = 0;
1799
Eric Laurent994a6932013-07-17 11:51:42 -07001800 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001801 parms = str_parms_create_str(kvpairs);
1802
1803 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1804
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001805 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001806 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001807 if (ret >= 0) {
1808 val = atoi(value);
1809 /* no audio source uses val == 0 */
1810 if ((in->source != val) && (val != 0)) {
1811 in->source = val;
1812 }
1813 }
1814
1815 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1816 if (ret >= 0) {
1817 val = atoi(value);
1818 if ((in->device != val) && (val != 0)) {
1819 in->device = val;
1820 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001821 if (!in->standby)
1822 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001823 }
1824 }
1825
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001826 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001827 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001828
1829 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001830 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001831 return ret;
1832}
1833
1834static char* in_get_parameters(const struct audio_stream *stream,
1835 const char *keys)
1836{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001837 struct stream_in *in = (struct stream_in *)stream;
1838 struct str_parms *query = str_parms_create_str(keys);
1839 char *str;
1840 char value[256];
1841 struct str_parms *reply = str_parms_create();
1842 ALOGV("%s: enter: keys - %s", __func__, keys);
1843
1844 voice_extn_in_get_parameters(in, query, reply);
1845
1846 str = str_parms_to_str(reply);
1847 str_parms_destroy(query);
1848 str_parms_destroy(reply);
1849
1850 ALOGV("%s: exit: returns - %s", __func__, str);
1851 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001852}
1853
1854static int in_set_gain(struct audio_stream_in *stream, float gain)
1855{
1856 return 0;
1857}
1858
1859static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1860 size_t bytes)
1861{
1862 struct stream_in *in = (struct stream_in *)stream;
1863 struct audio_device *adev = in->dev;
1864 int i, ret = -1;
1865
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001866 pthread_mutex_lock(&in->lock);
1867 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001868 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001869 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
1870 ret = voice_extn_compress_voip_start_input_stream(in);
1871 else
1872 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001873 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001874 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001875 goto exit;
1876 }
1877 in->standby = 0;
1878 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001879
1880 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001881 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1882 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07001883 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
1884 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001885 else
1886 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001887 }
1888
1889 /*
1890 * Instead of writing zeroes here, we could trust the hardware
1891 * to always provide zeroes when muted.
1892 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001893 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001894 memset(buffer, 0, bytes);
1895
1896exit:
1897 pthread_mutex_unlock(&in->lock);
1898
1899 if (ret != 0) {
1900 in_standby(&in->stream.common);
1901 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1902 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1903 in_get_sample_rate(&in->stream.common));
1904 }
1905 return bytes;
1906}
1907
1908static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1909{
1910 return 0;
1911}
1912
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001913static int add_remove_audio_effect(const struct audio_stream *stream,
1914 effect_handle_t effect,
1915 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001916{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001917 struct stream_in *in = (struct stream_in *)stream;
1918 int status = 0;
1919 effect_descriptor_t desc;
1920
1921 status = (*effect)->get_descriptor(effect, &desc);
1922 if (status != 0)
1923 return status;
1924
1925 pthread_mutex_lock(&in->lock);
1926 pthread_mutex_lock(&in->dev->lock);
1927 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1928 in->enable_aec != enable &&
1929 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1930 in->enable_aec = enable;
1931 if (!in->standby)
1932 select_devices(in->dev, in->usecase);
1933 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08001934 if (in->enable_ns != enable &&
1935 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1936 in->enable_ns = enable;
1937 if (!in->standby)
1938 select_devices(in->dev, in->usecase);
1939 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001940 pthread_mutex_unlock(&in->dev->lock);
1941 pthread_mutex_unlock(&in->lock);
1942
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001943 return 0;
1944}
1945
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001946static int in_add_audio_effect(const struct audio_stream *stream,
1947 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001948{
Eric Laurent994a6932013-07-17 11:51:42 -07001949 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001950 return add_remove_audio_effect(stream, effect, true);
1951}
1952
1953static int in_remove_audio_effect(const struct audio_stream *stream,
1954 effect_handle_t effect)
1955{
Eric Laurent994a6932013-07-17 11:51:42 -07001956 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001957 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001958}
1959
1960static int adev_open_output_stream(struct audio_hw_device *dev,
1961 audio_io_handle_t handle,
1962 audio_devices_t devices,
1963 audio_output_flags_t flags,
1964 struct audio_config *config,
1965 struct audio_stream_out **stream_out)
1966{
1967 struct audio_device *adev = (struct audio_device *)dev;
1968 struct stream_out *out;
1969 int i, ret;
1970
Eric Laurent994a6932013-07-17 11:51:42 -07001971 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001972 __func__, config->sample_rate, config->channel_mask, devices, flags);
1973 *stream_out = NULL;
1974 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1975
1976 if (devices == AUDIO_DEVICE_NONE)
1977 devices = AUDIO_DEVICE_OUT_SPEAKER;
1978
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001979 out->flags = flags;
1980 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001981 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001982 out->format = config->format;
1983 out->sample_rate = config->sample_rate;
1984 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1985 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001986 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001987
1988 /* Init use case and pcm_config */
Ravi Kumar Alamanda86b0e2b2013-11-20 14:52:30 -08001989 if (out->flags == AUDIO_OUTPUT_FLAG_DIRECT &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001990 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001991 pthread_mutex_lock(&adev->lock);
1992 ret = read_hdmi_channel_masks(out);
1993 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001994 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001995 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001996
1997 if (config->sample_rate == 0)
1998 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1999 if (config->channel_mask == 0)
2000 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2001
2002 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002003 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002004 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2005 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002006 out->config.rate = config->sample_rate;
2007 out->config.channels = popcount(out->channel_mask);
2008 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002009 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2010 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
2011 (voice_extn_compress_voip_is_format_supported(out->format))) {
2012 ret = voice_extn_compress_voip_open_output_stream(out);
2013 if (ret != 0) {
2014 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2015 __func__, ret);
2016 goto error_open;
2017 }
2018 out->config.rate = config->sample_rate;
2019 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002020 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2021 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2022 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2023 ALOGE("%s: Unsupported Offload information", __func__);
2024 ret = -EINVAL;
2025 goto error_open;
2026 }
Mingming Yin90310102013-11-13 16:57:00 -08002027 if (!is_supported_format(config->offload_info.format) &&
2028 !audio_extn_dolby_is_supported_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002029 ALOGE("%s: Unsupported audio format", __func__);
2030 ret = -EINVAL;
2031 goto error_open;
2032 }
2033
2034 out->compr_config.codec = (struct snd_codec *)
2035 calloc(1, sizeof(struct snd_codec));
2036
2037 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2038 if (config->offload_info.channel_mask)
2039 out->channel_mask = config->offload_info.channel_mask;
2040 else if (config->channel_mask)
2041 out->channel_mask = config->channel_mask;
2042 out->format = config->offload_info.format;
2043 out->sample_rate = config->offload_info.sample_rate;
2044
2045 out->stream.set_callback = out_set_callback;
2046 out->stream.pause = out_pause;
2047 out->stream.resume = out_resume;
2048 out->stream.drain = out_drain;
2049 out->stream.flush = out_flush;
2050
Mingming Yin90310102013-11-13 16:57:00 -08002051 if (audio_extn_dolby_is_supported_format(config->offload_info.format))
2052 out->compr_config.codec->id =
2053 audio_extn_dolby_get_snd_codec_id(config->offload_info.format);
2054 else
2055 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002056 get_snd_codec_id(config->offload_info.format);
2057 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2058 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2059 out->compr_config.codec->sample_rate =
2060 compress_get_alsa_rate(config->offload_info.sample_rate);
2061 out->compr_config.codec->bit_rate =
2062 config->offload_info.bit_rate;
2063 out->compr_config.codec->ch_in =
2064 popcount(config->channel_mask);
2065 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2066
2067 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2068 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002069
2070 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002071 create_offload_callback_thread(out);
2072 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2073 __func__, config->offload_info.version,
2074 config->offload_info.bit_rate);
Mingming Yin90310102013-11-13 16:57:00 -08002075
2076 if (audio_extn_dolby_is_supported_format(out->format)) {
2077 ret = audio_extn_dolby_set_DMID(adev);
2078 if (ret != 0) {
2079 ALOGE("%s: Dolby DMID cannot be set error:%d",
2080 __func__, ret);
2081 goto error_open;
2082 }
2083 }
2084
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002085 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2086 ret = voice_check_and_set_incall_music_usecase(adev, out);
2087 if (ret != 0) {
2088 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2089 __func__, ret);
2090 goto error_open;
2091 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002092 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002093 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2094 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002095 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002096 } else {
2097 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2098 out->config = pcm_config_deep_buffer;
2099 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002100 }
2101
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002102 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2103 if(adev->primary_output == NULL)
2104 adev->primary_output = out;
2105 else {
2106 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002107 ret = -EEXIST;
2108 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002109 }
2110 }
2111
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002112 /* Check if this usecase is already existing */
2113 pthread_mutex_lock(&adev->lock);
2114 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2115 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002116 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002117 ret = -EEXIST;
2118 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002119 }
2120 pthread_mutex_unlock(&adev->lock);
2121
2122 out->stream.common.get_sample_rate = out_get_sample_rate;
2123 out->stream.common.set_sample_rate = out_set_sample_rate;
2124 out->stream.common.get_buffer_size = out_get_buffer_size;
2125 out->stream.common.get_channels = out_get_channels;
2126 out->stream.common.get_format = out_get_format;
2127 out->stream.common.set_format = out_set_format;
2128 out->stream.common.standby = out_standby;
2129 out->stream.common.dump = out_dump;
2130 out->stream.common.set_parameters = out_set_parameters;
2131 out->stream.common.get_parameters = out_get_parameters;
2132 out->stream.common.add_audio_effect = out_add_audio_effect;
2133 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2134 out->stream.get_latency = out_get_latency;
2135 out->stream.set_volume = out_set_volume;
2136 out->stream.write = out_write;
2137 out->stream.get_render_position = out_get_render_position;
2138 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002139 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002140
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002141 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002142 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002143 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002144
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002145 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2146 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002147
2148 config->format = out->stream.common.get_format(&out->stream.common);
2149 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2150 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2151
2152 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002153 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002154 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002155
2156error_open:
2157 free(out);
2158 *stream_out = NULL;
2159 ALOGD("%s: exit: ret %d", __func__, ret);
2160 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002161}
2162
2163static void adev_close_output_stream(struct audio_hw_device *dev,
2164 struct audio_stream_out *stream)
2165{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002166 struct stream_out *out = (struct stream_out *)stream;
2167 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002168 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002169
Eric Laurent994a6932013-07-17 11:51:42 -07002170 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002171 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2172 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2173 if(ret != 0)
2174 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2175 __func__, ret);
2176 }
2177 else
2178 out_standby(&stream->common);
2179
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002180 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2181 destroy_offload_callback_thread(out);
2182
2183 if (out->compr_config.codec != NULL)
2184 free(out->compr_config.codec);
2185 }
2186 pthread_cond_destroy(&out->cond);
2187 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002188 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002189 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002190}
2191
2192static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2193{
2194 struct audio_device *adev = (struct audio_device *)dev;
2195 struct str_parms *parms;
2196 char *str;
2197 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002198 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002199 int ret;
2200
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002201 ALOGD("%s: enter: %s", __func__, kvpairs);
2202
2203 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002204 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002205
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002206 voice_set_parameters(adev, parms);
2207 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002208
2209 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2210 if (ret >= 0) {
2211 /* When set to false, HAL should disable EC and NS
2212 * But it is currently not supported.
2213 */
2214 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2215 adev->bluetooth_nrec = true;
2216 else
2217 adev->bluetooth_nrec = false;
2218 }
2219
2220 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2221 if (ret >= 0) {
2222 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2223 adev->screen_off = false;
2224 else
2225 adev->screen_off = true;
2226 }
2227
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002228 ret = str_parms_get_int(parms, "rotation", &val);
2229 if (ret >= 0) {
2230 bool reverse_speakers = false;
2231 switch(val) {
2232 // FIXME: note that the code below assumes that the speakers are in the correct placement
2233 // relative to the user when the device is rotated 90deg from its default rotation. This
2234 // assumption is device-specific, not platform-specific like this code.
2235 case 270:
2236 reverse_speakers = true;
2237 break;
2238 case 0:
2239 case 90:
2240 case 180:
2241 break;
2242 default:
2243 ALOGE("%s: unexpected rotation of %d", __func__, val);
2244 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002245 if (adev->speaker_lr_swap != reverse_speakers) {
2246 adev->speaker_lr_swap = reverse_speakers;
2247 // only update the selected device if there is active pcm playback
2248 struct audio_usecase *usecase;
2249 struct listnode *node;
2250 list_for_each(node, &adev->usecase_list) {
2251 usecase = node_to_item(node, struct audio_usecase, list);
2252 if (usecase->type == PCM_PLAYBACK) {
2253 select_devices(adev, usecase->id);
2254 break;
2255 }
2256 }
2257 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002258 }
2259
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002260 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002261 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002262
2263 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002264 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002265 return ret;
2266}
2267
2268static char* adev_get_parameters(const struct audio_hw_device *dev,
2269 const char *keys)
2270{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002271 struct audio_device *adev = (struct audio_device *)dev;
2272 struct str_parms *reply = str_parms_create();
2273 struct str_parms *query = str_parms_create_str(keys);
2274 char *str;
2275
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002276 pthread_mutex_lock(&adev->lock);
2277
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002278 audio_extn_get_parameters(adev, query, reply);
2279 platform_get_parameters(adev->platform, query, reply);
2280 str = str_parms_to_str(reply);
2281 str_parms_destroy(query);
2282 str_parms_destroy(reply);
2283
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002284 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002285 ALOGV("%s: exit: returns - %s", __func__, str);
2286 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002287}
2288
2289static int adev_init_check(const struct audio_hw_device *dev)
2290{
2291 return 0;
2292}
2293
2294static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2295{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002296 int ret;
2297 struct audio_device *adev = (struct audio_device *)dev;
2298 pthread_mutex_lock(&adev->lock);
2299 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002300 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002301 pthread_mutex_unlock(&adev->lock);
2302 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002303}
2304
2305static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2306{
2307 return -ENOSYS;
2308}
2309
2310static int adev_get_master_volume(struct audio_hw_device *dev,
2311 float *volume)
2312{
2313 return -ENOSYS;
2314}
2315
2316static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2317{
2318 return -ENOSYS;
2319}
2320
2321static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2322{
2323 return -ENOSYS;
2324}
2325
2326static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2327{
2328 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002329 pthread_mutex_lock(&adev->lock);
2330 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002331 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002332 adev->mode = mode;
2333 }
2334 pthread_mutex_unlock(&adev->lock);
2335 return 0;
2336}
2337
2338static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2339{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002340 int ret;
2341
2342 pthread_mutex_lock(&adev->lock);
2343 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2344 pthread_mutex_unlock(&adev->lock);
2345
2346 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002347}
2348
2349static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2350{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002351 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002352 return 0;
2353}
2354
2355static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2356 const struct audio_config *config)
2357{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002358 int channel_count = popcount(config->channel_mask);
2359
2360 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2361}
2362
2363static int adev_open_input_stream(struct audio_hw_device *dev,
2364 audio_io_handle_t handle,
2365 audio_devices_t devices,
2366 struct audio_config *config,
2367 struct audio_stream_in **stream_in)
2368{
2369 struct audio_device *adev = (struct audio_device *)dev;
2370 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002371 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002372 int channel_count = popcount(config->channel_mask);
2373
Eric Laurent994a6932013-07-17 11:51:42 -07002374 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002375 *stream_in = NULL;
2376 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2377 return -EINVAL;
2378
2379 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2380
2381 in->stream.common.get_sample_rate = in_get_sample_rate;
2382 in->stream.common.set_sample_rate = in_set_sample_rate;
2383 in->stream.common.get_buffer_size = in_get_buffer_size;
2384 in->stream.common.get_channels = in_get_channels;
2385 in->stream.common.get_format = in_get_format;
2386 in->stream.common.set_format = in_set_format;
2387 in->stream.common.standby = in_standby;
2388 in->stream.common.dump = in_dump;
2389 in->stream.common.set_parameters = in_set_parameters;
2390 in->stream.common.get_parameters = in_get_parameters;
2391 in->stream.common.add_audio_effect = in_add_audio_effect;
2392 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2393 in->stream.set_gain = in_set_gain;
2394 in->stream.read = in_read;
2395 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2396
2397 in->device = devices;
2398 in->source = AUDIO_SOURCE_DEFAULT;
2399 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002400 in->standby = 1;
2401 in->channel_mask = config->channel_mask;
2402
2403 /* Update config params with the requested sample rate and channels */
2404 in->usecase = USECASE_AUDIO_RECORD;
2405 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002406 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002407 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002408
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002409 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2410 (voice_extn_compress_voip_is_format_supported(in->format))) {
2411 ret = voice_extn_compress_voip_open_input_stream(in);
2412 if (ret != 0)
2413 {
2414 ALOGE("%s: Compress voip input cannot be opened, error:%d",
2415 __func__, ret);
2416 goto err_open;
2417 }
Mingming Yine62d7842013-10-25 16:26:03 -07002418 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002419 if(audio_extn_ssr_get_enabled()) {
2420 if(audio_extn_ssr_init(adev, in)) {
2421 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2422 ret = -EINVAL;
2423 goto err_open;
2424 }
2425 } else {
2426 ret = -EINVAL;
2427 goto err_open;
2428 }
Mingming Yine62d7842013-10-25 16:26:03 -07002429 } else if (audio_extn_compr_cap_enabled() &&
2430 audio_extn_compr_cap_format_supported(config->format)) {
2431 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002432 } else {
2433 in->config.channels = channel_count;
2434 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2435 buffer_size = get_input_buffer_size(config->sample_rate,
2436 config->format,
2437 channel_count);
2438 in->config.period_size = buffer_size / frame_size;
2439 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002440
2441 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002442 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002443 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002444
2445err_open:
2446 free(in);
2447 *stream_in = NULL;
2448 return ret;
2449}
2450
2451static void adev_close_input_stream(struct audio_hw_device *dev,
2452 struct audio_stream_in *stream)
2453{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002454 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002455 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002456 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002457
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002458 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2459 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2460 if (ret != 0)
2461 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2462 __func__, ret);
2463 } else
2464 in_standby(&stream->common);
2465
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002466 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2467 audio_extn_ssr_deinit();
2468 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002469 free(stream);
2470
Mingming Yine62d7842013-10-25 16:26:03 -07002471 if(audio_extn_compr_cap_enabled() &&
2472 audio_extn_compr_cap_format_supported(in->config.format))
2473 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002474 return;
2475}
2476
2477static int adev_dump(const audio_hw_device_t *device, int fd)
2478{
2479 return 0;
2480}
2481
2482static int adev_close(hw_device_t *device)
2483{
2484 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002485
2486 if (!adev)
2487 return 0;
2488
2489 pthread_mutex_lock(&adev_init_lock);
2490
2491 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002492 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07002493 audio_route_free(adev->audio_route);
2494 free(adev->snd_dev_ref_cnt);
2495 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002496 free(device);
2497 adev = NULL;
2498 }
2499 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002500 return 0;
2501}
2502
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002503static int adev_open(const hw_module_t *module, const char *name,
2504 hw_device_t **device)
2505{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002506 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002507
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002508 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002509 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2510
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002511 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002512 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002513 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002514 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002515 ALOGD("%s: returning existing instance of adev", __func__);
2516 ALOGD("%s: exit", __func__);
2517 pthread_mutex_unlock(&adev_init_lock);
2518 return 0;
2519 }
2520
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002521 adev = calloc(1, sizeof(struct audio_device));
2522
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002523 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2524 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2525 adev->device.common.module = (struct hw_module_t *)module;
2526 adev->device.common.close = adev_close;
2527
2528 adev->device.init_check = adev_init_check;
2529 adev->device.set_voice_volume = adev_set_voice_volume;
2530 adev->device.set_master_volume = adev_set_master_volume;
2531 adev->device.get_master_volume = adev_get_master_volume;
2532 adev->device.set_master_mute = adev_set_master_mute;
2533 adev->device.get_master_mute = adev_get_master_mute;
2534 adev->device.set_mode = adev_set_mode;
2535 adev->device.set_mic_mute = adev_set_mic_mute;
2536 adev->device.get_mic_mute = adev_get_mic_mute;
2537 adev->device.set_parameters = adev_set_parameters;
2538 adev->device.get_parameters = adev_get_parameters;
2539 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2540 adev->device.open_output_stream = adev_open_output_stream;
2541 adev->device.close_output_stream = adev_close_output_stream;
2542 adev->device.open_input_stream = adev_open_input_stream;
2543 adev->device.close_input_stream = adev_close_input_stream;
2544 adev->device.dump = adev_dump;
2545
2546 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002547 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002548 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002549 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002550 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002551 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002552 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002553 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002554 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002555 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002556 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002557
2558 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002559 adev->platform = platform_init(adev);
2560 if (!adev->platform) {
2561 free(adev->snd_dev_ref_cnt);
2562 free(adev);
2563 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2564 *device = NULL;
2565 return -EINVAL;
2566 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002567
2568 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2569 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2570 if (adev->visualizer_lib == NULL) {
2571 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2572 } else {
2573 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2574 adev->visualizer_start_output =
2575 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2576 "visualizer_hal_start_output");
2577 adev->visualizer_stop_output =
2578 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2579 "visualizer_hal_stop_output");
2580 }
2581 }
Kiran Kandide144c82013-11-20 15:58:32 -08002582 audio_extn_listen_init(adev, SOUND_CARD);
Eric Laurentc4aef752013-09-12 17:45:53 -07002583
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002584 *device = &adev->device.common;
2585
Kiran Kandi910e1862013-10-29 13:29:42 -07002586 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002587 pthread_mutex_unlock(&adev_init_lock);
2588
Eric Laurent994a6932013-07-17 11:51:42 -07002589 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002590 return 0;
2591}
2592
2593static struct hw_module_methods_t hal_module_methods = {
2594 .open = adev_open,
2595};
2596
2597struct audio_module HAL_MODULE_INFO_SYM = {
2598 .common = {
2599 .tag = HARDWARE_MODULE_TAG,
2600 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2601 .hal_api_version = HARDWARE_HAL_API_VERSION,
2602 .id = AUDIO_HARDWARE_MODULE_ID,
2603 .name = "QCOM Audio HAL",
2604 .author = "Code Aurora Forum",
2605 .methods = &hal_module_methods,
2606 },
2607};