blob: 1a83b6356e971c7ca79ccebcd73f386856cf9793 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
4 *
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_primary"
21/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070022/*#define VERY_VERY_VERBOSE_LOGGING*/
23#ifdef VERY_VERY_VERBOSE_LOGGING
24#define ALOGVV ALOGV
25#else
26#define ALOGVV(a...) do { } while(0)
27#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080028
29#include <errno.h>
30#include <pthread.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080034#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070035#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070036#include <sys/resource.h>
37#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038
39#include <cutils/log.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070042#include <cutils/atomic.h>
43#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080044
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <hardware/audio_effect.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070050#include "platform_api.h"
51#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070052#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080053#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include "sound/compress_params.h"
56
57#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
58#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
59/* ToDo: Check and update a proper value in msec */
60#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
61#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
62
Eric Laurentb23d5282013-05-14 15:27:20 -070063struct pcm_config pcm_config_deep_buffer = {
64 .channels = 2,
65 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
66 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
67 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
68 .format = PCM_FORMAT_S16_LE,
69 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
70 .stop_threshold = INT_MAX,
71 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
72};
73
74struct pcm_config pcm_config_low_latency = {
75 .channels = 2,
76 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
77 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
78 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
79 .format = PCM_FORMAT_S16_LE,
80 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
81 .stop_threshold = INT_MAX,
82 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
83};
84
85struct pcm_config pcm_config_hdmi_multi = {
86 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
87 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
88 .period_size = HDMI_MULTI_PERIOD_SIZE,
89 .period_count = HDMI_MULTI_PERIOD_COUNT,
90 .format = PCM_FORMAT_S16_LE,
91 .start_threshold = 0,
92 .stop_threshold = INT_MAX,
93 .avail_min = 0,
94};
95
96struct pcm_config pcm_config_audio_capture = {
97 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070098 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
99 .format = PCM_FORMAT_S16_LE,
100};
101
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800102const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700103 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
104 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
105 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700106 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700107 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700108 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700109 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700110 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700111 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700112 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700113
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700114 [USECASE_VOICE2_CALL] = "voice2-call",
115 [USECASE_VOLTE_CALL] = "volte-call",
116 [USECASE_QCHAT_CALL] = "qchat-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800117 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700118 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
119 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
120 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700121 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
122 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700123 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
124 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700125};
126
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800127
128#define STRING_TO_ENUM(string) { #string, string }
129
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800130struct string_to_enum {
131 const char *name;
132 uint32_t value;
133};
134
135static const struct string_to_enum out_channels_name_to_enum_table[] = {
136 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
137 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
138 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
139};
140
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700141static struct audio_device *adev = NULL;
142static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700143static unsigned int audio_device_ref_count;
144
Haynes Mathew George5191a852013-09-11 14:19:36 -0700145static int set_voice_volume_l(struct audio_device *adev, float volume);
146
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700147static bool is_supported_format(audio_format_t format)
148{
Eric Laurent86e17132013-09-12 17:49:30 -0700149 if (format == AUDIO_FORMAT_MP3 ||
150 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700151 return true;
152
153 return false;
154}
155
156static int get_snd_codec_id(audio_format_t format)
157{
158 int id = 0;
159
160 switch (format) {
161 case AUDIO_FORMAT_MP3:
162 id = SND_AUDIOCODEC_MP3;
163 break;
164 case AUDIO_FORMAT_AAC:
165 id = SND_AUDIOCODEC_AAC;
166 break;
167 default:
Mingming Yin90310102013-11-13 16:57:00 -0800168 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700169 }
170
171 return id;
172}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800173
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700174int enable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700175 struct audio_usecase *usecase,
176 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800177{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700178 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700179 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800180
181 if (usecase == NULL)
182 return -EINVAL;
183
184 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
185
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800186 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700187 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800188 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700189 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800190
191 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700192 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700193 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700194 audio_route_apply_path(adev->audio_route, mixer_path);
195 if (update_mixer)
196 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800197
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800198 ALOGV("%s: exit", __func__);
199 return 0;
200}
201
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700202int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700203 struct audio_usecase *usecase,
204 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800205{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700206 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700207 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800208
209 if (usecase == NULL)
210 return -EINVAL;
211
212 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700213 if (usecase->type == PCM_CAPTURE)
214 snd_device = usecase->in_snd_device;
215 else
216 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800217 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700218 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700219 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700220 audio_route_reset_path(adev->audio_route, mixer_path);
221 if (update_mixer)
222 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800223
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800224 ALOGV("%s: exit", __func__);
225 return 0;
226}
227
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700228int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700229 snd_device_t snd_device,
230 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800231{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700232 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
233
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800234 if (snd_device < SND_DEVICE_MIN ||
235 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800236 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800237 return -EINVAL;
238 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700239
240 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700241
242 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
243 ALOGE("%s: Invalid sound device returned", __func__);
244 return -EINVAL;
245 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700246 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700247 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700248 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249 return 0;
250 }
251
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700252 /* start usb playback thread */
253 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
254 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
255 audio_extn_usb_start_playback(adev);
256
257 /* start usb capture thread */
258 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
259 audio_extn_usb_start_capture(adev);
260
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700261 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
262 audio_extn_spkr_prot_is_enabled()) {
263 if (audio_extn_spkr_prot_start_processing(snd_device)) {
264 ALOGE("%s: spkr_start_processing failed", __func__);
265 return -EINVAL;
266 }
267 } else {
268 ALOGV("%s: snd_device(%d: %s)", __func__,
269 snd_device, device_name);
270 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
271 adev->snd_dev_ref_cnt[snd_device]--;
272 return -EINVAL;
273 }
Kiran Kandide144c82013-11-20 15:58:32 -0800274 audio_extn_listen_update_status(snd_device,
275 LISTEN_EVENT_SND_DEVICE_BUSY);
276
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700277 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800278 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700279 if (update_mixer)
280 audio_route_update_mixer(adev->audio_route);
281
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800282 return 0;
283}
284
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700285int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700286 snd_device_t snd_device,
287 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800288{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700289 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
290
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800291 if (snd_device < SND_DEVICE_MIN ||
292 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800293 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800294 return -EINVAL;
295 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700296 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
297 ALOGE("%s: device ref cnt is already 0", __func__);
298 return -EINVAL;
299 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700300
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700301 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700302
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700303 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
304 ALOGE("%s: Invalid sound device returned", __func__);
305 return -EINVAL;
306 }
307
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700308 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700309 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700310 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800311 /* exit usb play back thread */
312 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
313 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
314 audio_extn_usb_stop_playback();
315
316 /* exit usb capture thread */
317 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
318 audio_extn_usb_stop_capture(adev);
319
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700320 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
321 audio_extn_spkr_prot_is_enabled()) {
322 audio_extn_spkr_prot_stop_processing();
323 } else
324 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800325
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700326 if (update_mixer)
327 audio_route_update_mixer(adev->audio_route);
Kiran Kandide144c82013-11-20 15:58:32 -0800328
329 audio_extn_listen_update_status(snd_device,
330 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700331 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700332
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800333 return 0;
334}
335
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700336static void check_usecases_codec_backend(struct audio_device *adev,
337 struct audio_usecase *uc_info,
338 snd_device_t snd_device)
339{
340 struct listnode *node;
341 struct audio_usecase *usecase;
342 bool switch_device[AUDIO_USECASE_MAX];
343 int i, num_uc_to_switch = 0;
344
345 /*
346 * This function is to make sure that all the usecases that are active on
347 * the hardware codec backend are always routed to any one device that is
348 * handled by the hardware codec.
349 * For example, if low-latency and deep-buffer usecases are currently active
350 * on speaker and out_set_parameters(headset) is received on low-latency
351 * output, then we have to make sure deep-buffer is also switched to headset,
352 * because of the limitation that both the devices cannot be enabled
353 * at the same time as they share the same backend.
354 */
355 /* Disable all the usecases on the shared backend other than the
356 specified usecase */
357 for (i = 0; i < AUDIO_USECASE_MAX; i++)
358 switch_device[i] = false;
359
360 list_for_each(node, &adev->usecase_list) {
361 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700362 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700363 usecase != uc_info &&
364 usecase->out_snd_device != snd_device &&
365 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
366 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
367 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700368 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700369 disable_audio_route(adev, usecase, false);
370 switch_device[usecase->id] = true;
371 num_uc_to_switch++;
372 }
373 }
374
375 if (num_uc_to_switch) {
376 /* Make sure all the streams are de-routed before disabling the device */
377 audio_route_update_mixer(adev->audio_route);
378
379 list_for_each(node, &adev->usecase_list) {
380 usecase = node_to_item(node, struct audio_usecase, list);
381 if (switch_device[usecase->id]) {
382 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700383 }
384 }
385
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700386 list_for_each(node, &adev->usecase_list) {
387 usecase = node_to_item(node, struct audio_usecase, list);
388 if (switch_device[usecase->id]) {
389 enable_snd_device(adev, snd_device, false);
390 }
391 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700392 /* Make sure new snd device is enabled before re-routing the streams */
393 audio_route_update_mixer(adev->audio_route);
394
395 /* Re-route all the usecases on the shared backend other than the
396 specified usecase to new snd devices */
397 list_for_each(node, &adev->usecase_list) {
398 usecase = node_to_item(node, struct audio_usecase, list);
399 /* Update the out_snd_device only before enabling the audio route */
400 if (switch_device[usecase->id] ) {
401 usecase->out_snd_device = snd_device;
402 enable_audio_route(adev, usecase, false);
403 }
404 }
405
406 audio_route_update_mixer(adev->audio_route);
407 }
408}
409
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700410static void check_and_route_capture_usecases(struct audio_device *adev,
411 struct audio_usecase *uc_info,
412 snd_device_t snd_device)
413{
414 struct listnode *node;
415 struct audio_usecase *usecase;
416 bool switch_device[AUDIO_USECASE_MAX];
417 int i, num_uc_to_switch = 0;
418
419 /*
420 * This function is to make sure that all the active capture usecases
421 * are always routed to the same input sound device.
422 * For example, if audio-record and voice-call usecases are currently
423 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
424 * is received for voice call then we have to make sure that audio-record
425 * usecase is also switched to earpiece i.e. voice-dmic-ef,
426 * because of the limitation that two devices cannot be enabled
427 * at the same time if they share the same backend.
428 */
429 for (i = 0; i < AUDIO_USECASE_MAX; i++)
430 switch_device[i] = false;
431
432 list_for_each(node, &adev->usecase_list) {
433 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700434 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700435 usecase != uc_info &&
436 usecase->in_snd_device != snd_device) {
437 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
438 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700439 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700440 disable_audio_route(adev, usecase, false);
441 switch_device[usecase->id] = true;
442 num_uc_to_switch++;
443 }
444 }
445
446 if (num_uc_to_switch) {
447 /* Make sure all the streams are de-routed before disabling the device */
448 audio_route_update_mixer(adev->audio_route);
449
450 list_for_each(node, &adev->usecase_list) {
451 usecase = node_to_item(node, struct audio_usecase, list);
452 if (switch_device[usecase->id]) {
453 disable_snd_device(adev, usecase->in_snd_device, false);
454 enable_snd_device(adev, snd_device, false);
455 }
456 }
457
458 /* Make sure new snd device is enabled before re-routing the streams */
459 audio_route_update_mixer(adev->audio_route);
460
461 /* Re-route all the usecases on the shared backend other than the
462 specified usecase to new snd devices */
463 list_for_each(node, &adev->usecase_list) {
464 usecase = node_to_item(node, struct audio_usecase, list);
465 /* Update the in_snd_device only before enabling the audio route */
466 if (switch_device[usecase->id] ) {
467 usecase->in_snd_device = snd_device;
468 enable_audio_route(adev, usecase, false);
469 }
470 }
471
472 audio_route_update_mixer(adev->audio_route);
473 }
474}
475
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700476static int disable_all_usecases_of_type(struct audio_device *adev,
477 usecase_type_t usecase_type,
478 bool update_mixer)
479{
480 struct audio_usecase *usecase;
481 struct listnode *node;
482 int ret = 0;
483
484 list_for_each(node, &adev->usecase_list) {
485 usecase = node_to_item(node, struct audio_usecase, list);
486 if (usecase->type == usecase_type) {
487 ALOGV("%s: usecase id %d", __func__, usecase->id);
488 ret = disable_audio_route(adev, usecase, update_mixer);
489 if (ret) {
490 ALOGE("%s: Failed to disable usecase id %d",
491 __func__, usecase->id);
492 }
493 }
494 }
495
496 return ret;
497}
498
499static int enable_all_usecases_of_type(struct audio_device *adev,
500 usecase_type_t usecase_type,
501 bool update_mixer)
502{
503 struct audio_usecase *usecase;
504 struct listnode *node;
505 int ret = 0;
506
507 list_for_each(node, &adev->usecase_list) {
508 usecase = node_to_item(node, struct audio_usecase, list);
509 if (usecase->type == usecase_type) {
510 ALOGV("%s: usecase id %d", __func__, usecase->id);
511 ret = enable_audio_route(adev, usecase, update_mixer);
512 if (ret) {
513 ALOGE("%s: Failed to enable usecase id %d",
514 __func__, usecase->id);
515 }
516 }
517 }
518
519 return ret;
520}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800521
522/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700523static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800524{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700525 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700526 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800527
528 switch (channels) {
529 /*
530 * Do not handle stereo output in Multi-channel cases
531 * Stereo case is handled in normal playback path
532 */
533 case 6:
534 ALOGV("%s: HDMI supports 5.1", __func__);
535 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
536 break;
537 case 8:
538 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
539 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
540 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
541 break;
542 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700543 ALOGE("HDMI does not support multi channel playback");
544 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800545 break;
546 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700547 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800548}
549
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700550static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
551{
552 struct audio_usecase *usecase;
553 struct listnode *node;
554
555 list_for_each(node, &adev->usecase_list) {
556 usecase = node_to_item(node, struct audio_usecase, list);
557 if (usecase->type == VOICE_CALL) {
558 ALOGV("%s: usecase id %d", __func__, usecase->id);
559 return usecase->id;
560 }
561 }
562 return USECASE_INVALID;
563}
564
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700565struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700566 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700567{
568 struct audio_usecase *usecase;
569 struct listnode *node;
570
571 list_for_each(node, &adev->usecase_list) {
572 usecase = node_to_item(node, struct audio_usecase, list);
573 if (usecase->id == uc_id)
574 return usecase;
575 }
576 return NULL;
577}
578
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700579int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800580{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800581 snd_device_t out_snd_device = SND_DEVICE_NONE;
582 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700583 struct audio_usecase *usecase = NULL;
584 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800585 struct audio_usecase *voip_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800586 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700587 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800588
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700589 usecase = get_usecase_from_list(adev, uc_id);
590 if (usecase == NULL) {
591 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
592 return -EINVAL;
593 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800594
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800595 if ((usecase->type == VOICE_CALL) ||
596 (usecase->type == VOIP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700597 out_snd_device = platform_get_output_snd_device(adev->platform,
598 usecase->stream.out->devices);
599 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700600 usecase->devices = usecase->stream.out->devices;
601 } else {
602 /*
603 * If the voice call is active, use the sound devices of voice call usecase
604 * so that it would not result any device switch. All the usecases will
605 * be switched to new device when select_devices() is called for voice call
606 * usecase. This is to avoid switching devices for voice call when
607 * check_usecases_codec_backend() is called below.
608 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700609 if (voice_is_in_call(adev)) {
610 vc_usecase = get_usecase_from_list(adev,
611 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700612 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
613 in_snd_device = vc_usecase->in_snd_device;
614 out_snd_device = vc_usecase->out_snd_device;
615 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800616 } else if (voice_extn_compress_voip_is_active(adev)) {
617 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
618 if (voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
619 in_snd_device = voip_usecase->in_snd_device;
620 out_snd_device = voip_usecase->out_snd_device;
621 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700622 }
623 if (usecase->type == PCM_PLAYBACK) {
624 usecase->devices = usecase->stream.out->devices;
625 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700626 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700627 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700628 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700629 if (usecase->stream.out == adev->primary_output &&
630 adev->active_input &&
631 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
632 select_devices(adev, adev->active_input->usecase);
633 }
634 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700635 } else if (usecase->type == PCM_CAPTURE) {
636 usecase->devices = usecase->stream.in->device;
637 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700638 if (in_snd_device == SND_DEVICE_NONE) {
639 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
640 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700641 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700642 adev->primary_output->devices);
643 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700644 in_snd_device = platform_get_input_snd_device(adev->platform,
645 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700646 }
647 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700648 }
649 }
650
651 if (out_snd_device == usecase->out_snd_device &&
652 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800653 return 0;
654 }
655
sangwoobc677242013-08-08 16:53:43 +0900656 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700657 out_snd_device, platform_get_snd_device_name(out_snd_device),
658 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800659
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800660 /*
661 * Limitation: While in call, to do a device switch we need to disable
662 * and enable both RX and TX devices though one of them is same as current
663 * device.
664 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800665 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700666 status = platform_switch_voice_call_device_pre(adev->platform);
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800667 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800668 }
669
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700670 /* Disable current sound devices */
671 if (usecase->out_snd_device != SND_DEVICE_NONE) {
672 disable_audio_route(adev, usecase, true);
673 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800674 }
675
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700676 if (usecase->in_snd_device != SND_DEVICE_NONE) {
677 disable_audio_route(adev, usecase, true);
678 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800679 }
680
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700681 /* Enable new sound devices */
682 if (out_snd_device != SND_DEVICE_NONE) {
683 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
684 check_usecases_codec_backend(adev, usecase, out_snd_device);
685 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800686 }
687
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700688 if (in_snd_device != SND_DEVICE_NONE) {
689 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700690 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700691 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700692
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800693 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700694 status = platform_switch_voice_call_device_post(adev->platform,
695 out_snd_device,
696 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800697
sangwoo170731f2013-06-08 15:36:36 +0900698 audio_route_update_mixer(adev->audio_route);
699
700 usecase->in_snd_device = in_snd_device;
701 usecase->out_snd_device = out_snd_device;
702
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800703 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
704 enable_all_usecases_of_type(adev, usecase->type, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700705 else
706 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900707
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800708 /* Applicable only on the targets that has external modem.
709 * Enable device command should be sent to modem only after
710 * enabling voice call mixer controls
711 */
712 if (usecase->type == VOICE_CALL)
713 status = platform_switch_voice_call_usecase_route_post(adev->platform,
714 out_snd_device,
715 in_snd_device);
716
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800717 return status;
718}
719
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800720static int stop_input_stream(struct stream_in *in)
721{
722 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800723 struct audio_usecase *uc_info;
724 struct audio_device *adev = in->dev;
725
Eric Laurentc8400632013-02-14 19:04:54 -0800726 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800727
Eric Laurent994a6932013-07-17 11:51:42 -0700728 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700729 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800730 uc_info = get_usecase_from_list(adev, in->usecase);
731 if (uc_info == NULL) {
732 ALOGE("%s: Could not find the usecase (%d) in the list",
733 __func__, in->usecase);
734 return -EINVAL;
735 }
736
Eric Laurent150dbfe2013-02-27 14:31:02 -0800737 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700738 disable_audio_route(adev, uc_info, true);
739
740 /* 2. Disable the tx device */
741 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800742
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800743 list_remove(&uc_info->list);
744 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745
Eric Laurent994a6932013-07-17 11:51:42 -0700746 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800747 return ret;
748}
749
750int start_input_stream(struct stream_in *in)
751{
752 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800753 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800754 struct audio_usecase *uc_info;
755 struct audio_device *adev = in->dev;
756
Mingming Yine62d7842013-10-25 16:26:03 -0700757 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Eric Laurent994a6932013-07-17 11:51:42 -0700758 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700759
760 /* Check if source matches incall recording usecase criteria */
761 ret = voice_check_and_set_incall_rec_usecase(adev, in);
762 if (ret)
763 goto error_config;
764 else
765 ALOGV("%s: usecase(%d)", __func__, in->usecase);
766
Eric Laurentb23d5282013-05-14 15:27:20 -0700767 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800768 if (in->pcm_device_id < 0) {
769 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
770 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800771 ret = -EINVAL;
772 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800773 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700774
775 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800776 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
777 uc_info->id = in->usecase;
778 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800779 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700780 uc_info->devices = in->device;
781 uc_info->in_snd_device = SND_DEVICE_NONE;
782 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800783
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800784 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700785 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800786
Eric Laurentc8400632013-02-14 19:04:54 -0800787 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
788 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800789 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
790 PCM_IN, &in->config);
791 if (in->pcm && !pcm_is_ready(in->pcm)) {
792 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
793 pcm_close(in->pcm);
794 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800795 ret = -EIO;
796 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800797 }
Eric Laurent994a6932013-07-17 11:51:42 -0700798 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800799 return ret;
800
801error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800802 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800803
804error_config:
805 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700806 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800807
808 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800809}
810
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700811/* must be called with out->lock locked */
812static int send_offload_cmd_l(struct stream_out* out, int command)
813{
814 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
815
816 ALOGVV("%s %d", __func__, command);
817
818 cmd->cmd = command;
819 list_add_tail(&out->offload_cmd_list, &cmd->node);
820 pthread_cond_signal(&out->offload_cond);
821 return 0;
822}
823
824/* must be called iwth out->lock locked */
825static void stop_compressed_output_l(struct stream_out *out)
826{
827 out->offload_state = OFFLOAD_STATE_IDLE;
828 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700829 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700830 if (out->compr != NULL) {
831 compress_stop(out->compr);
832 while (out->offload_thread_blocked) {
833 pthread_cond_wait(&out->cond, &out->lock);
834 }
835 }
836}
837
838static void *offload_thread_loop(void *context)
839{
840 struct stream_out *out = (struct stream_out *) context;
841 struct listnode *item;
842
843 out->offload_state = OFFLOAD_STATE_IDLE;
844 out->playback_started = 0;
845
846 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
847 set_sched_policy(0, SP_FOREGROUND);
848 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
849
850 ALOGV("%s", __func__);
851 pthread_mutex_lock(&out->lock);
852 for (;;) {
853 struct offload_cmd *cmd = NULL;
854 stream_callback_event_t event;
855 bool send_callback = false;
856
857 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
858 __func__, list_empty(&out->offload_cmd_list),
859 out->offload_state);
860 if (list_empty(&out->offload_cmd_list)) {
861 ALOGV("%s SLEEPING", __func__);
862 pthread_cond_wait(&out->offload_cond, &out->lock);
863 ALOGV("%s RUNNING", __func__);
864 continue;
865 }
866
867 item = list_head(&out->offload_cmd_list);
868 cmd = node_to_item(item, struct offload_cmd, node);
869 list_remove(item);
870
871 ALOGVV("%s STATE %d CMD %d out->compr %p",
872 __func__, out->offload_state, cmd->cmd, out->compr);
873
874 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
875 free(cmd);
876 break;
877 }
878
879 if (out->compr == NULL) {
880 ALOGE("%s: Compress handle is NULL", __func__);
881 pthread_cond_signal(&out->cond);
882 continue;
883 }
884 out->offload_thread_blocked = true;
885 pthread_mutex_unlock(&out->lock);
886 send_callback = false;
887 switch(cmd->cmd) {
888 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
889 compress_wait(out->compr, -1);
890 send_callback = true;
891 event = STREAM_CBK_EVENT_WRITE_READY;
892 break;
893 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700894 compress_next_track(out->compr);
895 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700896 send_callback = true;
897 event = STREAM_CBK_EVENT_DRAIN_READY;
898 break;
899 case OFFLOAD_CMD_DRAIN:
900 compress_drain(out->compr);
901 send_callback = true;
902 event = STREAM_CBK_EVENT_DRAIN_READY;
903 break;
904 default:
905 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
906 break;
907 }
908 pthread_mutex_lock(&out->lock);
909 out->offload_thread_blocked = false;
910 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700911 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700912 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700913 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700914 free(cmd);
915 }
916
917 pthread_cond_signal(&out->cond);
918 while (!list_empty(&out->offload_cmd_list)) {
919 item = list_head(&out->offload_cmd_list);
920 list_remove(item);
921 free(node_to_item(item, struct offload_cmd, node));
922 }
923 pthread_mutex_unlock(&out->lock);
924
925 return NULL;
926}
927
928static int create_offload_callback_thread(struct stream_out *out)
929{
930 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
931 list_init(&out->offload_cmd_list);
932 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
933 offload_thread_loop, out);
934 return 0;
935}
936
937static int destroy_offload_callback_thread(struct stream_out *out)
938{
939 pthread_mutex_lock(&out->lock);
940 stop_compressed_output_l(out);
941 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
942
943 pthread_mutex_unlock(&out->lock);
944 pthread_join(out->offload_thread, (void **) NULL);
945 pthread_cond_destroy(&out->offload_cond);
946
947 return 0;
948}
949
Eric Laurent07eeafd2013-10-06 12:52:49 -0700950static bool allow_hdmi_channel_config(struct audio_device *adev)
951{
952 struct listnode *node;
953 struct audio_usecase *usecase;
954 bool ret = true;
955
956 list_for_each(node, &adev->usecase_list) {
957 usecase = node_to_item(node, struct audio_usecase, list);
958 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
959 /*
960 * If voice call is already existing, do not proceed further to avoid
961 * disabling/enabling both RX and TX devices, CSD calls, etc.
962 * Once the voice call done, the HDMI channels can be configured to
963 * max channels of remaining use cases.
964 */
965 if (usecase->id == USECASE_VOICE_CALL) {
966 ALOGD("%s: voice call is active, no change in HDMI channels",
967 __func__);
968 ret = false;
969 break;
970 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
971 ALOGD("%s: multi channel playback is active, "
972 "no change in HDMI channels", __func__);
973 ret = false;
974 break;
975 }
976 }
977 }
978 return ret;
979}
980
981static int check_and_set_hdmi_channels(struct audio_device *adev,
982 unsigned int channels)
983{
984 struct listnode *node;
985 struct audio_usecase *usecase;
986
987 /* Check if change in HDMI channel config is allowed */
988 if (!allow_hdmi_channel_config(adev))
989 return 0;
990
991 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -0800992 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -0700993 return 0;
994 }
995
996 platform_set_hdmi_channels(adev->platform, channels);
997 adev->cur_hdmi_channels = channels;
998
999 /*
1000 * Deroute all the playback streams routed to HDMI so that
1001 * the back end is deactivated. Note that backend will not
1002 * be deactivated if any one stream is connected to it.
1003 */
1004 list_for_each(node, &adev->usecase_list) {
1005 usecase = node_to_item(node, struct audio_usecase, list);
1006 if (usecase->type == PCM_PLAYBACK &&
1007 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1008 disable_audio_route(adev, usecase, true);
1009 }
1010 }
1011
1012 /*
1013 * Enable all the streams disabled above. Now the HDMI backend
1014 * will be activated with new channel configuration
1015 */
1016 list_for_each(node, &adev->usecase_list) {
1017 usecase = node_to_item(node, struct audio_usecase, list);
1018 if (usecase->type == PCM_PLAYBACK &&
1019 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1020 enable_audio_route(adev, usecase, true);
1021 }
1022 }
1023
1024 return 0;
1025}
1026
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001027static int stop_output_stream(struct stream_out *out)
1028{
1029 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001030 struct audio_usecase *uc_info;
1031 struct audio_device *adev = out->dev;
1032
Eric Laurent994a6932013-07-17 11:51:42 -07001033 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001034 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001035 uc_info = get_usecase_from_list(adev, out->usecase);
1036 if (uc_info == NULL) {
1037 ALOGE("%s: Could not find the usecase (%d) in the list",
1038 __func__, out->usecase);
1039 return -EINVAL;
1040 }
1041
Eric Laurentc4aef752013-09-12 17:45:53 -07001042 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
1043 adev->visualizer_stop_output != NULL)
1044 adev->visualizer_stop_output(out->handle);
1045
Eric Laurent150dbfe2013-02-27 14:31:02 -08001046 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001047 disable_audio_route(adev, uc_info, true);
1048
1049 /* 2. Disable the rx device */
1050 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001051
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001052 list_remove(&uc_info->list);
1053 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001054
Eric Laurent07eeafd2013-10-06 12:52:49 -07001055 /* Must be called after removing the usecase from list */
1056 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1057 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1058
Eric Laurent994a6932013-07-17 11:51:42 -07001059 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001060 return ret;
1061}
1062
1063int start_output_stream(struct stream_out *out)
1064{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001065 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001066 struct audio_usecase *uc_info;
1067 struct audio_device *adev = out->dev;
1068
Eric Laurent994a6932013-07-17 11:51:42 -07001069 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001070 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001071 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001072 if (out->pcm_device_id < 0) {
1073 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1074 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001075 ret = -EINVAL;
1076 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001077 }
1078
1079 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1080 uc_info->id = out->usecase;
1081 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001082 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001083 uc_info->devices = out->devices;
1084 uc_info->in_snd_device = SND_DEVICE_NONE;
1085 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001086
Eric Laurent07eeafd2013-10-06 12:52:49 -07001087 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001088 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1089 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1090 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1091 else
1092 check_and_set_hdmi_channels(adev, out->config.channels);
1093 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001094
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001095 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001096
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001097 select_devices(adev, out->usecase);
1098
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001099 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1100 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001101 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1102 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001103 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001104 if (out->pcm && !pcm_is_ready(out->pcm)) {
1105 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1106 pcm_close(out->pcm);
1107 out->pcm = NULL;
1108 ret = -EIO;
1109 goto error_open;
1110 }
1111 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001112 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001113 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1114 COMPRESS_IN, &out->compr_config);
1115 if (out->compr && !is_compress_ready(out->compr)) {
1116 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1117 compress_close(out->compr);
1118 out->compr = NULL;
1119 ret = -EIO;
1120 goto error_open;
1121 }
1122 if (out->offload_callback)
1123 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001124
1125 if (adev->visualizer_start_output != NULL)
1126 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001127 }
Eric Laurent994a6932013-07-17 11:51:42 -07001128 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001129 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001130error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001131 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001132error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001133 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001134}
1135
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001136static int check_input_parameters(uint32_t sample_rate,
1137 audio_format_t format,
1138 int channel_count)
1139{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001140 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001141
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001142 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001143 !voice_extn_compress_voip_is_format_supported(format) &&
1144 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001145
1146 switch (channel_count) {
1147 case 1:
1148 case 2:
1149 case 6:
1150 break;
1151 default:
1152 ret = -EINVAL;
1153 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001154
1155 switch (sample_rate) {
1156 case 8000:
1157 case 11025:
1158 case 12000:
1159 case 16000:
1160 case 22050:
1161 case 24000:
1162 case 32000:
1163 case 44100:
1164 case 48000:
1165 break;
1166 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001167 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001168 }
1169
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001170 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001171}
1172
1173static size_t get_input_buffer_size(uint32_t sample_rate,
1174 audio_format_t format,
1175 int channel_count)
1176{
1177 size_t size = 0;
1178
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001179 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1180 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001181
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001182 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1183 /* ToDo: should use frame_size computed based on the format and
1184 channel_count here. */
1185 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001186
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001187 /* make sure the size is multiple of 64 */
1188 size += 0x3f;
1189 size &= ~0x3f;
1190
1191 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001192}
1193
1194static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1195{
1196 struct stream_out *out = (struct stream_out *)stream;
1197
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001198 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001199}
1200
1201static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1202{
1203 return -ENOSYS;
1204}
1205
1206static size_t out_get_buffer_size(const struct audio_stream *stream)
1207{
1208 struct stream_out *out = (struct stream_out *)stream;
1209
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001210 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001211 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001212 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1213 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001214
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001215 return out->config.period_size * audio_stream_frame_size(stream);
1216}
1217
1218static uint32_t out_get_channels(const struct audio_stream *stream)
1219{
1220 struct stream_out *out = (struct stream_out *)stream;
1221
1222 return out->channel_mask;
1223}
1224
1225static audio_format_t out_get_format(const struct audio_stream *stream)
1226{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001227 struct stream_out *out = (struct stream_out *)stream;
1228
1229 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001230}
1231
1232static int out_set_format(struct audio_stream *stream, audio_format_t format)
1233{
1234 return -ENOSYS;
1235}
1236
1237static int out_standby(struct audio_stream *stream)
1238{
1239 struct stream_out *out = (struct stream_out *)stream;
1240 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001241
Eric Laurent994a6932013-07-17 11:51:42 -07001242 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001243 out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001244 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1245 /* Ignore standby in case of voip call because the voip output
1246 * stream is closed in adev_close_output_stream()
1247 */
1248 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1249 return 0;
1250 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001251
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001252 pthread_mutex_lock(&out->lock);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001253 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001254 if (!out->standby) {
1255 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001256 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1257 if (out->pcm) {
1258 pcm_close(out->pcm);
1259 out->pcm = NULL;
1260 }
1261 } else {
1262 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001263 out->gapless_mdata.encoder_delay = 0;
1264 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001265 if (out->compr != NULL) {
1266 compress_close(out->compr);
1267 out->compr = NULL;
1268 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001269 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001270 stop_output_stream(out);
1271 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001272 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001274 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001275 return 0;
1276}
1277
1278static int out_dump(const struct audio_stream *stream, int fd)
1279{
1280 return 0;
1281}
1282
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001283static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1284{
1285 int ret = 0;
1286 char value[32];
1287 struct compr_gapless_mdata tmp_mdata;
1288
1289 if (!out || !parms) {
1290 return -EINVAL;
1291 }
1292
1293 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1294 if (ret >= 0) {
1295 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1296 } else {
1297 return -EINVAL;
1298 }
1299
1300 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1301 if (ret >= 0) {
1302 tmp_mdata.encoder_padding = atoi(value);
1303 } else {
1304 return -EINVAL;
1305 }
1306
1307 out->gapless_mdata = tmp_mdata;
1308 out->send_new_metadata = 1;
1309 ALOGV("%s new encoder delay %u and padding %u", __func__,
1310 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1311
1312 return 0;
1313}
1314
1315
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001316static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1317{
1318 struct stream_out *out = (struct stream_out *)stream;
1319 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001320 struct audio_usecase *usecase;
1321 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001322 struct str_parms *parms;
1323 char value[32];
1324 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001325 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001326
sangwoobc677242013-08-08 16:53:43 +09001327 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001328 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001329 parms = str_parms_create_str(kvpairs);
1330 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1331 if (ret >= 0) {
1332 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001333 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001334 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001335
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001336 /*
1337 * When HDMI cable is unplugged the music playback is paused and
1338 * the policy manager sends routing=0. But the audioflinger
1339 * continues to write data until standby time (3sec).
1340 * As the HDMI core is turned off, the write gets blocked.
1341 * Avoid this by routing audio to speaker until standby.
1342 */
1343 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1344 val == AUDIO_DEVICE_NONE) {
1345 val = AUDIO_DEVICE_OUT_SPEAKER;
1346 }
1347
1348 /*
1349 * select_devices() call below switches all the usecases on the same
1350 * backend to the new device. Refer to check_usecases_codec_backend() in
1351 * the select_devices(). But how do we undo this?
1352 *
1353 * For example, music playback is active on headset (deep-buffer usecase)
1354 * and if we go to ringtones and select a ringtone, low-latency usecase
1355 * will be started on headset+speaker. As we can't enable headset+speaker
1356 * and headset devices at the same time, select_devices() switches the music
1357 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1358 * So when the ringtone playback is completed, how do we undo the same?
1359 *
1360 * We are relying on the out_set_parameters() call on deep-buffer output,
1361 * once the ringtone playback is ended.
1362 * NOTE: We should not check if the current devices are same as new devices.
1363 * Because select_devices() must be called to switch back the music
1364 * playback to headset.
1365 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001366 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001367 out->devices = val;
1368
1369 if (!out->standby)
1370 select_devices(adev, out->usecase);
1371
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001372 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1373 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001374 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001375 voice_start_call(adev);
1376 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1377 voice_is_in_call(adev) &&
1378 (out == adev->primary_output)) {
1379 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001380 }
1381 }
1382
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001383 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1384 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001385 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001386 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001387 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001388
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001389 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001390 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001391 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001392
1393 if (out == adev->primary_output) {
1394 pthread_mutex_lock(&adev->lock);
1395 audio_extn_set_parameters(adev, parms);
1396 pthread_mutex_unlock(&adev->lock);
1397 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001398 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1399 parse_compress_metadata(out, parms);
1400 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001401
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001402 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001403 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404 return ret;
1405}
1406
1407static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1408{
1409 struct stream_out *out = (struct stream_out *)stream;
1410 struct str_parms *query = str_parms_create_str(keys);
1411 char *str;
1412 char value[256];
1413 struct str_parms *reply = str_parms_create();
1414 size_t i, j;
1415 int ret;
1416 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001417 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001418 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1419 if (ret >= 0) {
1420 value[0] = '\0';
1421 i = 0;
1422 while (out->supported_channel_masks[i] != 0) {
1423 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1424 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1425 if (!first) {
1426 strcat(value, "|");
1427 }
1428 strcat(value, out_channels_name_to_enum_table[j].name);
1429 first = false;
1430 break;
1431 }
1432 }
1433 i++;
1434 }
1435 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1436 str = str_parms_to_str(reply);
1437 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001438 voice_extn_out_get_parameters(out, query, reply);
1439 str = str_parms_to_str(reply);
1440 if (!strncmp(str, "", sizeof(""))) {
1441 str = strdup(keys);
1442 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001443 }
1444 str_parms_destroy(query);
1445 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001446 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001447 return str;
1448}
1449
1450static uint32_t out_get_latency(const struct audio_stream_out *stream)
1451{
1452 struct stream_out *out = (struct stream_out *)stream;
1453
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001454 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1455 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1456
1457 return (out->config.period_count * out->config.period_size * 1000) /
1458 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001459}
1460
1461static int out_set_volume(struct audio_stream_out *stream, float left,
1462 float right)
1463{
Eric Laurenta9024de2013-04-04 09:19:12 -07001464 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001465 int volume[2];
1466
Eric Laurenta9024de2013-04-04 09:19:12 -07001467 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1468 /* only take left channel into account: the API is for stereo anyway */
1469 out->muted = (left == 0.0f);
1470 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001471 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1472 const char *mixer_ctl_name = "Compress Playback Volume";
1473 struct audio_device *adev = out->dev;
1474 struct mixer_ctl *ctl;
1475
1476 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1477 if (!ctl) {
1478 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1479 __func__, mixer_ctl_name);
1480 return -EINVAL;
1481 }
1482 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1483 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1484 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1485 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001486 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001487
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001488 return -ENOSYS;
1489}
1490
1491static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1492 size_t bytes)
1493{
1494 struct stream_out *out = (struct stream_out *)stream;
1495 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001496 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001497
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001498 pthread_mutex_lock(&out->lock);
1499 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001500 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001501 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001502 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1503 ret = voice_extn_compress_voip_start_output_stream(out);
1504 else
1505 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001506 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001507 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001508 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001509 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001510 goto exit;
1511 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001512 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001513
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001514 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001515 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1516 if (out->send_new_metadata) {
1517 ALOGVV("send new gapless metadata");
1518 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1519 out->send_new_metadata = 0;
1520 }
1521
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001522 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001523 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001524 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001525 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1526 }
1527 if (!out->playback_started) {
1528 compress_start(out->compr);
1529 out->playback_started = 1;
1530 out->offload_state = OFFLOAD_STATE_PLAYING;
1531 }
1532 pthread_mutex_unlock(&out->lock);
1533 return ret;
1534 } else {
1535 if (out->pcm) {
1536 if (out->muted)
1537 memset((void *)buffer, 0, bytes);
1538 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1539 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001540 if (ret == 0)
1541 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001542 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001543 }
1544
1545exit:
1546 pthread_mutex_unlock(&out->lock);
1547
1548 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001549 if (out->pcm)
1550 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001551 out_standby(&out->stream.common);
1552 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1553 out_get_sample_rate(&out->stream.common));
1554 }
1555 return bytes;
1556}
1557
1558static int out_get_render_position(const struct audio_stream_out *stream,
1559 uint32_t *dsp_frames)
1560{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001561 struct stream_out *out = (struct stream_out *)stream;
1562 *dsp_frames = 0;
1563 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1564 pthread_mutex_lock(&out->lock);
1565 if (out->compr != NULL) {
1566 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1567 &out->sample_rate);
1568 ALOGVV("%s rendered frames %d sample_rate %d",
1569 __func__, *dsp_frames, out->sample_rate);
1570 }
1571 pthread_mutex_unlock(&out->lock);
1572 return 0;
1573 } else
1574 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001575}
1576
1577static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1578{
1579 return 0;
1580}
1581
1582static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1583{
1584 return 0;
1585}
1586
1587static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1588 int64_t *timestamp)
1589{
1590 return -EINVAL;
1591}
1592
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001593static int out_get_presentation_position(const struct audio_stream_out *stream,
1594 uint64_t *frames, struct timespec *timestamp)
1595{
1596 struct stream_out *out = (struct stream_out *)stream;
1597 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001598 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001599
1600 pthread_mutex_lock(&out->lock);
1601
Eric Laurent949a0892013-09-20 09:20:13 -07001602 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1603 if (out->compr != NULL) {
1604 compress_get_tstamp(out->compr, &dsp_frames,
1605 &out->sample_rate);
1606 ALOGVV("%s rendered frames %ld sample_rate %d",
1607 __func__, dsp_frames, out->sample_rate);
1608 *frames = dsp_frames;
1609 ret = 0;
1610 /* this is the best we can do */
1611 clock_gettime(CLOCK_MONOTONIC, timestamp);
1612 }
1613 } else {
1614 if (out->pcm) {
1615 size_t avail;
1616 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1617 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001618 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001619 // This adjustment accounts for buffering after app processor.
1620 // It is based on estimated DSP latency per use case, rather than exact.
1621 signed_frames -=
1622 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1623
Eric Laurent949a0892013-09-20 09:20:13 -07001624 // It would be unusual for this value to be negative, but check just in case ...
1625 if (signed_frames >= 0) {
1626 *frames = signed_frames;
1627 ret = 0;
1628 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001629 }
1630 }
1631 }
1632
1633 pthread_mutex_unlock(&out->lock);
1634
1635 return ret;
1636}
1637
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001638static int out_set_callback(struct audio_stream_out *stream,
1639 stream_callback_t callback, void *cookie)
1640{
1641 struct stream_out *out = (struct stream_out *)stream;
1642
1643 ALOGV("%s", __func__);
1644 pthread_mutex_lock(&out->lock);
1645 out->offload_callback = callback;
1646 out->offload_cookie = cookie;
1647 pthread_mutex_unlock(&out->lock);
1648 return 0;
1649}
1650
1651static int out_pause(struct audio_stream_out* stream)
1652{
1653 struct stream_out *out = (struct stream_out *)stream;
1654 int status = -ENOSYS;
1655 ALOGV("%s", __func__);
1656 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1657 pthread_mutex_lock(&out->lock);
1658 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1659 status = compress_pause(out->compr);
1660 out->offload_state = OFFLOAD_STATE_PAUSED;
1661 }
1662 pthread_mutex_unlock(&out->lock);
1663 }
1664 return status;
1665}
1666
1667static int out_resume(struct audio_stream_out* stream)
1668{
1669 struct stream_out *out = (struct stream_out *)stream;
1670 int status = -ENOSYS;
1671 ALOGV("%s", __func__);
1672 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1673 status = 0;
1674 pthread_mutex_lock(&out->lock);
1675 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1676 status = compress_resume(out->compr);
1677 out->offload_state = OFFLOAD_STATE_PLAYING;
1678 }
1679 pthread_mutex_unlock(&out->lock);
1680 }
1681 return status;
1682}
1683
1684static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1685{
1686 struct stream_out *out = (struct stream_out *)stream;
1687 int status = -ENOSYS;
1688 ALOGV("%s", __func__);
1689 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1690 pthread_mutex_lock(&out->lock);
1691 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1692 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1693 else
1694 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1695 pthread_mutex_unlock(&out->lock);
1696 }
1697 return status;
1698}
1699
1700static int out_flush(struct audio_stream_out* stream)
1701{
1702 struct stream_out *out = (struct stream_out *)stream;
1703 ALOGV("%s", __func__);
1704 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1705 pthread_mutex_lock(&out->lock);
1706 stop_compressed_output_l(out);
1707 pthread_mutex_unlock(&out->lock);
1708 return 0;
1709 }
1710 return -ENOSYS;
1711}
1712
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001713/** audio_stream_in implementation **/
1714static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1715{
1716 struct stream_in *in = (struct stream_in *)stream;
1717
1718 return in->config.rate;
1719}
1720
1721static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1722{
1723 return -ENOSYS;
1724}
1725
1726static size_t in_get_buffer_size(const struct audio_stream *stream)
1727{
1728 struct stream_in *in = (struct stream_in *)stream;
1729
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001730 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1731 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001732 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1733 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001734
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001735 return in->config.period_size * audio_stream_frame_size(stream);
1736}
1737
1738static uint32_t in_get_channels(const struct audio_stream *stream)
1739{
1740 struct stream_in *in = (struct stream_in *)stream;
1741
1742 return in->channel_mask;
1743}
1744
1745static audio_format_t in_get_format(const struct audio_stream *stream)
1746{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001747 struct stream_in *in = (struct stream_in *)stream;
1748
1749 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001750}
1751
1752static int in_set_format(struct audio_stream *stream, audio_format_t format)
1753{
1754 return -ENOSYS;
1755}
1756
1757static int in_standby(struct audio_stream *stream)
1758{
1759 struct stream_in *in = (struct stream_in *)stream;
1760 struct audio_device *adev = in->dev;
1761 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001762 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001763
1764 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1765 /* Ignore standby in case of voip call because the voip input
1766 * stream is closed in adev_close_input_stream()
1767 */
1768 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1769 return status;
1770 }
1771
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001772 pthread_mutex_lock(&in->lock);
1773 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001774 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001775 if (in->pcm) {
1776 pcm_close(in->pcm);
1777 in->pcm = NULL;
1778 }
1779 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001780 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001781 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001782 }
1783 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001784 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001785 return status;
1786}
1787
1788static int in_dump(const struct audio_stream *stream, int fd)
1789{
1790 return 0;
1791}
1792
1793static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1794{
1795 struct stream_in *in = (struct stream_in *)stream;
1796 struct audio_device *adev = in->dev;
1797 struct str_parms *parms;
1798 char *str;
1799 char value[32];
1800 int ret, val = 0;
1801
Eric Laurent994a6932013-07-17 11:51:42 -07001802 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001803 parms = str_parms_create_str(kvpairs);
1804
1805 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1806
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001807 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001808 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001809 if (ret >= 0) {
1810 val = atoi(value);
1811 /* no audio source uses val == 0 */
1812 if ((in->source != val) && (val != 0)) {
1813 in->source = val;
1814 }
1815 }
1816
1817 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1818 if (ret >= 0) {
1819 val = atoi(value);
1820 if ((in->device != val) && (val != 0)) {
1821 in->device = val;
1822 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001823 if (!in->standby)
1824 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001825 }
1826 }
1827
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001828 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001829 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001830
1831 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001832 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001833 return ret;
1834}
1835
1836static char* in_get_parameters(const struct audio_stream *stream,
1837 const char *keys)
1838{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001839 struct stream_in *in = (struct stream_in *)stream;
1840 struct str_parms *query = str_parms_create_str(keys);
1841 char *str;
1842 char value[256];
1843 struct str_parms *reply = str_parms_create();
1844 ALOGV("%s: enter: keys - %s", __func__, keys);
1845
1846 voice_extn_in_get_parameters(in, query, reply);
1847
1848 str = str_parms_to_str(reply);
1849 str_parms_destroy(query);
1850 str_parms_destroy(reply);
1851
1852 ALOGV("%s: exit: returns - %s", __func__, str);
1853 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001854}
1855
1856static int in_set_gain(struct audio_stream_in *stream, float gain)
1857{
1858 return 0;
1859}
1860
1861static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1862 size_t bytes)
1863{
1864 struct stream_in *in = (struct stream_in *)stream;
1865 struct audio_device *adev = in->dev;
1866 int i, ret = -1;
1867
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001868 pthread_mutex_lock(&in->lock);
1869 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001870 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001871 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
1872 ret = voice_extn_compress_voip_start_input_stream(in);
1873 else
1874 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001875 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001876 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001877 goto exit;
1878 }
1879 in->standby = 0;
1880 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001881
1882 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001883 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1884 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07001885 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
1886 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001887 else
1888 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001889 }
1890
1891 /*
1892 * Instead of writing zeroes here, we could trust the hardware
1893 * to always provide zeroes when muted.
1894 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001895 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001896 memset(buffer, 0, bytes);
1897
1898exit:
1899 pthread_mutex_unlock(&in->lock);
1900
1901 if (ret != 0) {
1902 in_standby(&in->stream.common);
1903 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1904 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1905 in_get_sample_rate(&in->stream.common));
1906 }
1907 return bytes;
1908}
1909
1910static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1911{
1912 return 0;
1913}
1914
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001915static int add_remove_audio_effect(const struct audio_stream *stream,
1916 effect_handle_t effect,
1917 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001918{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001919 struct stream_in *in = (struct stream_in *)stream;
1920 int status = 0;
1921 effect_descriptor_t desc;
1922
1923 status = (*effect)->get_descriptor(effect, &desc);
1924 if (status != 0)
1925 return status;
1926
1927 pthread_mutex_lock(&in->lock);
1928 pthread_mutex_lock(&in->dev->lock);
1929 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1930 in->enable_aec != enable &&
1931 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1932 in->enable_aec = enable;
1933 if (!in->standby)
1934 select_devices(in->dev, in->usecase);
1935 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08001936 if (in->enable_ns != enable &&
1937 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1938 in->enable_ns = enable;
1939 if (!in->standby)
1940 select_devices(in->dev, in->usecase);
1941 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001942 pthread_mutex_unlock(&in->dev->lock);
1943 pthread_mutex_unlock(&in->lock);
1944
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001945 return 0;
1946}
1947
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001948static int in_add_audio_effect(const struct audio_stream *stream,
1949 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001950{
Eric Laurent994a6932013-07-17 11:51:42 -07001951 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001952 return add_remove_audio_effect(stream, effect, true);
1953}
1954
1955static int in_remove_audio_effect(const struct audio_stream *stream,
1956 effect_handle_t effect)
1957{
Eric Laurent994a6932013-07-17 11:51:42 -07001958 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001959 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001960}
1961
1962static int adev_open_output_stream(struct audio_hw_device *dev,
1963 audio_io_handle_t handle,
1964 audio_devices_t devices,
1965 audio_output_flags_t flags,
1966 struct audio_config *config,
1967 struct audio_stream_out **stream_out)
1968{
1969 struct audio_device *adev = (struct audio_device *)dev;
1970 struct stream_out *out;
1971 int i, ret;
1972
Eric Laurent994a6932013-07-17 11:51:42 -07001973 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001974 __func__, config->sample_rate, config->channel_mask, devices, flags);
1975 *stream_out = NULL;
1976 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1977
1978 if (devices == AUDIO_DEVICE_NONE)
1979 devices = AUDIO_DEVICE_OUT_SPEAKER;
1980
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001981 out->flags = flags;
1982 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001983 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001984 out->format = config->format;
1985 out->sample_rate = config->sample_rate;
1986 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1987 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001988 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001989
1990 /* Init use case and pcm_config */
Ravi Kumar Alamanda86b0e2b2013-11-20 14:52:30 -08001991 if (out->flags == AUDIO_OUTPUT_FLAG_DIRECT &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001992 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001993 pthread_mutex_lock(&adev->lock);
1994 ret = read_hdmi_channel_masks(out);
1995 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001996 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001997 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001998
1999 if (config->sample_rate == 0)
2000 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2001 if (config->channel_mask == 0)
2002 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2003
2004 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002005 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002006 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2007 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002008 out->config.rate = config->sample_rate;
2009 out->config.channels = popcount(out->channel_mask);
2010 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002011 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2012 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
2013 (voice_extn_compress_voip_is_format_supported(out->format))) {
2014 ret = voice_extn_compress_voip_open_output_stream(out);
2015 if (ret != 0) {
2016 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2017 __func__, ret);
2018 goto error_open;
2019 }
2020 out->config.rate = config->sample_rate;
2021 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002022 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2023 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2024 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2025 ALOGE("%s: Unsupported Offload information", __func__);
2026 ret = -EINVAL;
2027 goto error_open;
2028 }
Mingming Yin90310102013-11-13 16:57:00 -08002029 if (!is_supported_format(config->offload_info.format) &&
2030 !audio_extn_dolby_is_supported_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002031 ALOGE("%s: Unsupported audio format", __func__);
2032 ret = -EINVAL;
2033 goto error_open;
2034 }
2035
2036 out->compr_config.codec = (struct snd_codec *)
2037 calloc(1, sizeof(struct snd_codec));
2038
2039 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2040 if (config->offload_info.channel_mask)
2041 out->channel_mask = config->offload_info.channel_mask;
2042 else if (config->channel_mask)
2043 out->channel_mask = config->channel_mask;
2044 out->format = config->offload_info.format;
2045 out->sample_rate = config->offload_info.sample_rate;
2046
2047 out->stream.set_callback = out_set_callback;
2048 out->stream.pause = out_pause;
2049 out->stream.resume = out_resume;
2050 out->stream.drain = out_drain;
2051 out->stream.flush = out_flush;
2052
Mingming Yin90310102013-11-13 16:57:00 -08002053 if (audio_extn_dolby_is_supported_format(config->offload_info.format))
2054 out->compr_config.codec->id =
2055 audio_extn_dolby_get_snd_codec_id(config->offload_info.format);
2056 else
2057 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002058 get_snd_codec_id(config->offload_info.format);
2059 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2060 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2061 out->compr_config.codec->sample_rate =
2062 compress_get_alsa_rate(config->offload_info.sample_rate);
2063 out->compr_config.codec->bit_rate =
2064 config->offload_info.bit_rate;
2065 out->compr_config.codec->ch_in =
2066 popcount(config->channel_mask);
2067 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2068
2069 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2070 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002071
2072 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002073 create_offload_callback_thread(out);
2074 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2075 __func__, config->offload_info.version,
2076 config->offload_info.bit_rate);
Mingming Yin90310102013-11-13 16:57:00 -08002077
2078 if (audio_extn_dolby_is_supported_format(out->format)) {
2079 ret = audio_extn_dolby_set_DMID(adev);
2080 if (ret != 0) {
2081 ALOGE("%s: Dolby DMID cannot be set error:%d",
2082 __func__, ret);
2083 goto error_open;
2084 }
2085 }
2086
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002087 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2088 ret = voice_check_and_set_incall_music_usecase(adev, out);
2089 if (ret != 0) {
2090 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2091 __func__, ret);
2092 goto error_open;
2093 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002094 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002095 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2096 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002097 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002098 } else {
2099 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2100 out->config = pcm_config_deep_buffer;
2101 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002102 }
2103
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002104 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2105 if(adev->primary_output == NULL)
2106 adev->primary_output = out;
2107 else {
2108 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002109 ret = -EEXIST;
2110 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002111 }
2112 }
2113
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002114 /* Check if this usecase is already existing */
2115 pthread_mutex_lock(&adev->lock);
2116 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2117 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002118 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002119 ret = -EEXIST;
2120 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002121 }
2122 pthread_mutex_unlock(&adev->lock);
2123
2124 out->stream.common.get_sample_rate = out_get_sample_rate;
2125 out->stream.common.set_sample_rate = out_set_sample_rate;
2126 out->stream.common.get_buffer_size = out_get_buffer_size;
2127 out->stream.common.get_channels = out_get_channels;
2128 out->stream.common.get_format = out_get_format;
2129 out->stream.common.set_format = out_set_format;
2130 out->stream.common.standby = out_standby;
2131 out->stream.common.dump = out_dump;
2132 out->stream.common.set_parameters = out_set_parameters;
2133 out->stream.common.get_parameters = out_get_parameters;
2134 out->stream.common.add_audio_effect = out_add_audio_effect;
2135 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2136 out->stream.get_latency = out_get_latency;
2137 out->stream.set_volume = out_set_volume;
2138 out->stream.write = out_write;
2139 out->stream.get_render_position = out_get_render_position;
2140 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002141 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002142
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002143 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002144 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002145 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002146
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002147 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2148 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002149
2150 config->format = out->stream.common.get_format(&out->stream.common);
2151 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2152 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2153
2154 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002155 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002156 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002157
2158error_open:
2159 free(out);
2160 *stream_out = NULL;
2161 ALOGD("%s: exit: ret %d", __func__, ret);
2162 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002163}
2164
2165static void adev_close_output_stream(struct audio_hw_device *dev,
2166 struct audio_stream_out *stream)
2167{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002168 struct stream_out *out = (struct stream_out *)stream;
2169 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002170 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002171
Eric Laurent994a6932013-07-17 11:51:42 -07002172 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002173 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2174 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2175 if(ret != 0)
2176 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2177 __func__, ret);
2178 }
2179 else
2180 out_standby(&stream->common);
2181
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002182 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2183 destroy_offload_callback_thread(out);
2184
2185 if (out->compr_config.codec != NULL)
2186 free(out->compr_config.codec);
2187 }
2188 pthread_cond_destroy(&out->cond);
2189 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002190 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002191 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002192}
2193
2194static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2195{
2196 struct audio_device *adev = (struct audio_device *)dev;
2197 struct str_parms *parms;
2198 char *str;
2199 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002200 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002201 int ret;
2202
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002203 ALOGD("%s: enter: %s", __func__, kvpairs);
2204
2205 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002206 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002207
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002208 voice_set_parameters(adev, parms);
2209 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002210
2211 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2212 if (ret >= 0) {
2213 /* When set to false, HAL should disable EC and NS
2214 * But it is currently not supported.
2215 */
2216 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2217 adev->bluetooth_nrec = true;
2218 else
2219 adev->bluetooth_nrec = false;
2220 }
2221
2222 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2223 if (ret >= 0) {
2224 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2225 adev->screen_off = false;
2226 else
2227 adev->screen_off = true;
2228 }
2229
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002230 ret = str_parms_get_int(parms, "rotation", &val);
2231 if (ret >= 0) {
2232 bool reverse_speakers = false;
2233 switch(val) {
2234 // FIXME: note that the code below assumes that the speakers are in the correct placement
2235 // relative to the user when the device is rotated 90deg from its default rotation. This
2236 // assumption is device-specific, not platform-specific like this code.
2237 case 270:
2238 reverse_speakers = true;
2239 break;
2240 case 0:
2241 case 90:
2242 case 180:
2243 break;
2244 default:
2245 ALOGE("%s: unexpected rotation of %d", __func__, val);
2246 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002247 if (adev->speaker_lr_swap != reverse_speakers) {
2248 adev->speaker_lr_swap = reverse_speakers;
2249 // only update the selected device if there is active pcm playback
2250 struct audio_usecase *usecase;
2251 struct listnode *node;
2252 list_for_each(node, &adev->usecase_list) {
2253 usecase = node_to_item(node, struct audio_usecase, list);
2254 if (usecase->type == PCM_PLAYBACK) {
2255 select_devices(adev, usecase->id);
2256 break;
2257 }
2258 }
2259 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002260 }
2261
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002262 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002263 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002264
2265 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002266 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002267 return ret;
2268}
2269
2270static char* adev_get_parameters(const struct audio_hw_device *dev,
2271 const char *keys)
2272{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002273 struct audio_device *adev = (struct audio_device *)dev;
2274 struct str_parms *reply = str_parms_create();
2275 struct str_parms *query = str_parms_create_str(keys);
2276 char *str;
2277
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002278 pthread_mutex_lock(&adev->lock);
2279
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002280 audio_extn_get_parameters(adev, query, reply);
2281 platform_get_parameters(adev->platform, query, reply);
2282 str = str_parms_to_str(reply);
2283 str_parms_destroy(query);
2284 str_parms_destroy(reply);
2285
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002286 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002287 ALOGV("%s: exit: returns - %s", __func__, str);
2288 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002289}
2290
2291static int adev_init_check(const struct audio_hw_device *dev)
2292{
2293 return 0;
2294}
2295
2296static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2297{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002298 int ret;
2299 struct audio_device *adev = (struct audio_device *)dev;
2300 pthread_mutex_lock(&adev->lock);
2301 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002302 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002303 pthread_mutex_unlock(&adev->lock);
2304 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002305}
2306
2307static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2308{
2309 return -ENOSYS;
2310}
2311
2312static int adev_get_master_volume(struct audio_hw_device *dev,
2313 float *volume)
2314{
2315 return -ENOSYS;
2316}
2317
2318static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2319{
2320 return -ENOSYS;
2321}
2322
2323static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2324{
2325 return -ENOSYS;
2326}
2327
2328static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2329{
2330 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002331 pthread_mutex_lock(&adev->lock);
2332 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002333 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002334 adev->mode = mode;
2335 }
2336 pthread_mutex_unlock(&adev->lock);
2337 return 0;
2338}
2339
2340static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2341{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002342 int ret;
2343
2344 pthread_mutex_lock(&adev->lock);
2345 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2346 pthread_mutex_unlock(&adev->lock);
2347
2348 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002349}
2350
2351static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2352{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002353 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002354 return 0;
2355}
2356
2357static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2358 const struct audio_config *config)
2359{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002360 int channel_count = popcount(config->channel_mask);
2361
2362 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2363}
2364
2365static int adev_open_input_stream(struct audio_hw_device *dev,
2366 audio_io_handle_t handle,
2367 audio_devices_t devices,
2368 struct audio_config *config,
2369 struct audio_stream_in **stream_in)
2370{
2371 struct audio_device *adev = (struct audio_device *)dev;
2372 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002373 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002374 int channel_count = popcount(config->channel_mask);
2375
Eric Laurent994a6932013-07-17 11:51:42 -07002376 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002377 *stream_in = NULL;
2378 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2379 return -EINVAL;
2380
2381 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2382
2383 in->stream.common.get_sample_rate = in_get_sample_rate;
2384 in->stream.common.set_sample_rate = in_set_sample_rate;
2385 in->stream.common.get_buffer_size = in_get_buffer_size;
2386 in->stream.common.get_channels = in_get_channels;
2387 in->stream.common.get_format = in_get_format;
2388 in->stream.common.set_format = in_set_format;
2389 in->stream.common.standby = in_standby;
2390 in->stream.common.dump = in_dump;
2391 in->stream.common.set_parameters = in_set_parameters;
2392 in->stream.common.get_parameters = in_get_parameters;
2393 in->stream.common.add_audio_effect = in_add_audio_effect;
2394 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2395 in->stream.set_gain = in_set_gain;
2396 in->stream.read = in_read;
2397 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2398
2399 in->device = devices;
2400 in->source = AUDIO_SOURCE_DEFAULT;
2401 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002402 in->standby = 1;
2403 in->channel_mask = config->channel_mask;
2404
2405 /* Update config params with the requested sample rate and channels */
2406 in->usecase = USECASE_AUDIO_RECORD;
2407 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002408 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002409 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002410
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002411 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2412 (voice_extn_compress_voip_is_format_supported(in->format))) {
2413 ret = voice_extn_compress_voip_open_input_stream(in);
2414 if (ret != 0)
2415 {
2416 ALOGE("%s: Compress voip input cannot be opened, error:%d",
2417 __func__, ret);
2418 goto err_open;
2419 }
Mingming Yine62d7842013-10-25 16:26:03 -07002420 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002421 if(audio_extn_ssr_get_enabled()) {
2422 if(audio_extn_ssr_init(adev, in)) {
2423 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2424 ret = -EINVAL;
2425 goto err_open;
2426 }
2427 } else {
2428 ret = -EINVAL;
2429 goto err_open;
2430 }
Mingming Yine62d7842013-10-25 16:26:03 -07002431 } else if (audio_extn_compr_cap_enabled() &&
2432 audio_extn_compr_cap_format_supported(config->format)) {
2433 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002434 } else {
2435 in->config.channels = channel_count;
2436 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2437 buffer_size = get_input_buffer_size(config->sample_rate,
2438 config->format,
2439 channel_count);
2440 in->config.period_size = buffer_size / frame_size;
2441 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002442
2443 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002444 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002445 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002446
2447err_open:
2448 free(in);
2449 *stream_in = NULL;
2450 return ret;
2451}
2452
2453static void adev_close_input_stream(struct audio_hw_device *dev,
2454 struct audio_stream_in *stream)
2455{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002456 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002457 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002458 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002459
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002460 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2461 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2462 if (ret != 0)
2463 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2464 __func__, ret);
2465 } else
2466 in_standby(&stream->common);
2467
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002468 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2469 audio_extn_ssr_deinit();
2470 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002471 free(stream);
2472
Mingming Yine62d7842013-10-25 16:26:03 -07002473 if(audio_extn_compr_cap_enabled() &&
2474 audio_extn_compr_cap_format_supported(in->config.format))
2475 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002476 return;
2477}
2478
2479static int adev_dump(const audio_hw_device_t *device, int fd)
2480{
2481 return 0;
2482}
2483
2484static int adev_close(hw_device_t *device)
2485{
2486 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002487
2488 if (!adev)
2489 return 0;
2490
2491 pthread_mutex_lock(&adev_init_lock);
2492
2493 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002494 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07002495 audio_route_free(adev->audio_route);
2496 free(adev->snd_dev_ref_cnt);
2497 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002498 free(device);
2499 adev = NULL;
2500 }
2501 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002502 return 0;
2503}
2504
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002505static int adev_open(const hw_module_t *module, const char *name,
2506 hw_device_t **device)
2507{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002508 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002509
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002510 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002511 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2512
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002513 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002514 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002515 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002516 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002517 ALOGD("%s: returning existing instance of adev", __func__);
2518 ALOGD("%s: exit", __func__);
2519 pthread_mutex_unlock(&adev_init_lock);
2520 return 0;
2521 }
2522
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002523 adev = calloc(1, sizeof(struct audio_device));
2524
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002525 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2526 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2527 adev->device.common.module = (struct hw_module_t *)module;
2528 adev->device.common.close = adev_close;
2529
2530 adev->device.init_check = adev_init_check;
2531 adev->device.set_voice_volume = adev_set_voice_volume;
2532 adev->device.set_master_volume = adev_set_master_volume;
2533 adev->device.get_master_volume = adev_get_master_volume;
2534 adev->device.set_master_mute = adev_set_master_mute;
2535 adev->device.get_master_mute = adev_get_master_mute;
2536 adev->device.set_mode = adev_set_mode;
2537 adev->device.set_mic_mute = adev_set_mic_mute;
2538 adev->device.get_mic_mute = adev_get_mic_mute;
2539 adev->device.set_parameters = adev_set_parameters;
2540 adev->device.get_parameters = adev_get_parameters;
2541 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2542 adev->device.open_output_stream = adev_open_output_stream;
2543 adev->device.close_output_stream = adev_close_output_stream;
2544 adev->device.open_input_stream = adev_open_input_stream;
2545 adev->device.close_input_stream = adev_close_input_stream;
2546 adev->device.dump = adev_dump;
2547
2548 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002549 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002550 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002551 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002552 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002553 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002554 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002555 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002556 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002557 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002558 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002559
2560 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002561 adev->platform = platform_init(adev);
2562 if (!adev->platform) {
2563 free(adev->snd_dev_ref_cnt);
2564 free(adev);
2565 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2566 *device = NULL;
2567 return -EINVAL;
2568 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002569
2570 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2571 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2572 if (adev->visualizer_lib == NULL) {
2573 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2574 } else {
2575 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2576 adev->visualizer_start_output =
2577 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2578 "visualizer_hal_start_output");
2579 adev->visualizer_stop_output =
2580 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2581 "visualizer_hal_stop_output");
2582 }
2583 }
Kiran Kandide144c82013-11-20 15:58:32 -08002584 audio_extn_listen_init(adev, SOUND_CARD);
Eric Laurentc4aef752013-09-12 17:45:53 -07002585
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002586 *device = &adev->device.common;
2587
Kiran Kandi910e1862013-10-29 13:29:42 -07002588 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002589 pthread_mutex_unlock(&adev_init_lock);
2590
Eric Laurent994a6932013-07-17 11:51:42 -07002591 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002592 return 0;
2593}
2594
2595static struct hw_module_methods_t hal_module_methods = {
2596 .open = adev_open,
2597};
2598
2599struct audio_module HAL_MODULE_INFO_SYM = {
2600 .common = {
2601 .tag = HARDWARE_MODULE_TAG,
2602 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2603 .hal_api_version = HARDWARE_HAL_API_VERSION,
2604 .id = AUDIO_HARDWARE_MODULE_ID,
2605 .name = "QCOM Audio HAL",
2606 .author = "Code Aurora Forum",
2607 .methods = &hal_module_methods,
2608 },
2609};