blob: 20abb2b18c5885d454a5792da6ef71484438d90e [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
4 *
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_primary"
21/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070022/*#define VERY_VERY_VERBOSE_LOGGING*/
23#ifdef VERY_VERY_VERBOSE_LOGGING
24#define ALOGVV ALOGV
25#else
26#define ALOGVV(a...) do { } while(0)
27#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080028
29#include <errno.h>
30#include <pthread.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080034#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070035#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070036#include <sys/resource.h>
37#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038
39#include <cutils/log.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070042#include <cutils/atomic.h>
43#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080044
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <hardware/audio_effect.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070050#include "platform_api.h"
51#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070052#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080053#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include "sound/compress_params.h"
56
57#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
58#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
59/* ToDo: Check and update a proper value in msec */
60#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
61#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
62
Eric Laurentb23d5282013-05-14 15:27:20 -070063struct pcm_config pcm_config_deep_buffer = {
64 .channels = 2,
65 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
66 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
67 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
68 .format = PCM_FORMAT_S16_LE,
69 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
70 .stop_threshold = INT_MAX,
71 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
72};
73
74struct pcm_config pcm_config_low_latency = {
75 .channels = 2,
76 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
77 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
78 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
79 .format = PCM_FORMAT_S16_LE,
80 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
81 .stop_threshold = INT_MAX,
82 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
83};
84
85struct pcm_config pcm_config_hdmi_multi = {
86 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
87 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
88 .period_size = HDMI_MULTI_PERIOD_SIZE,
89 .period_count = HDMI_MULTI_PERIOD_COUNT,
90 .format = PCM_FORMAT_S16_LE,
91 .start_threshold = 0,
92 .stop_threshold = INT_MAX,
93 .avail_min = 0,
94};
95
96struct pcm_config pcm_config_audio_capture = {
97 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070098 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
99 .format = PCM_FORMAT_S16_LE,
100};
101
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800102const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700103 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
104 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
105 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700106 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700107 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700108 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700109 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700110 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700111 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800112 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700113 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700114
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700115 [USECASE_VOICE2_CALL] = "voice2-call",
116 [USECASE_VOLTE_CALL] = "volte-call",
117 [USECASE_QCHAT_CALL] = "qchat-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800118 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700119 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
120 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
121 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700122 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
123 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700124 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
125 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700126};
127
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800128
129#define STRING_TO_ENUM(string) { #string, string }
130
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800131struct string_to_enum {
132 const char *name;
133 uint32_t value;
134};
135
136static const struct string_to_enum out_channels_name_to_enum_table[] = {
137 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
138 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
139 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
140};
141
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700142static struct audio_device *adev = NULL;
143static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700144static unsigned int audio_device_ref_count;
145
Haynes Mathew George5191a852013-09-11 14:19:36 -0700146static int set_voice_volume_l(struct audio_device *adev, float volume);
147
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700148static bool is_supported_format(audio_format_t format)
149{
Eric Laurent86e17132013-09-12 17:49:30 -0700150 if (format == AUDIO_FORMAT_MP3 ||
151 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700152 return true;
153
154 return false;
155}
156
157static int get_snd_codec_id(audio_format_t format)
158{
159 int id = 0;
160
161 switch (format) {
162 case AUDIO_FORMAT_MP3:
163 id = SND_AUDIOCODEC_MP3;
164 break;
165 case AUDIO_FORMAT_AAC:
166 id = SND_AUDIOCODEC_AAC;
167 break;
168 default:
Mingming Yin90310102013-11-13 16:57:00 -0800169 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700170 }
171
172 return id;
173}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800174
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700175int enable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700176 struct audio_usecase *usecase,
177 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800178{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700179 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700180 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800181
182 if (usecase == NULL)
183 return -EINVAL;
184
185 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
186
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800187 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700188 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800189 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700190 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800191
192 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700193 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700194 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700195 audio_route_apply_path(adev->audio_route, mixer_path);
196 if (update_mixer)
197 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800198
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800199 ALOGV("%s: exit", __func__);
200 return 0;
201}
202
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700203int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700204 struct audio_usecase *usecase,
205 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800206{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700207 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700208 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800209
210 if (usecase == NULL)
211 return -EINVAL;
212
213 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700214 if (usecase->type == PCM_CAPTURE)
215 snd_device = usecase->in_snd_device;
216 else
217 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800218 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700219 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700220 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700221 audio_route_reset_path(adev->audio_route, mixer_path);
222 if (update_mixer)
223 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800224
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800225 ALOGV("%s: exit", __func__);
226 return 0;
227}
228
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700229int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700230 snd_device_t snd_device,
231 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800232{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700233 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
234
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800235 if (snd_device < SND_DEVICE_MIN ||
236 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800237 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800238 return -EINVAL;
239 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700240
241 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700242
243 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
244 ALOGE("%s: Invalid sound device returned", __func__);
245 return -EINVAL;
246 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700247 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700248 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700249 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700250 return 0;
251 }
252
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700253 /* start usb playback thread */
254 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
255 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
256 audio_extn_usb_start_playback(adev);
257
258 /* start usb capture thread */
259 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
260 audio_extn_usb_start_capture(adev);
261
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700262 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
263 audio_extn_spkr_prot_is_enabled()) {
264 if (audio_extn_spkr_prot_start_processing(snd_device)) {
265 ALOGE("%s: spkr_start_processing failed", __func__);
266 return -EINVAL;
267 }
268 } else {
269 ALOGV("%s: snd_device(%d: %s)", __func__,
270 snd_device, device_name);
271 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
272 adev->snd_dev_ref_cnt[snd_device]--;
273 return -EINVAL;
274 }
Kiran Kandide144c82013-11-20 15:58:32 -0800275 audio_extn_listen_update_status(snd_device,
276 LISTEN_EVENT_SND_DEVICE_BUSY);
277
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700278 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800279 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700280 if (update_mixer)
281 audio_route_update_mixer(adev->audio_route);
282
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800283 return 0;
284}
285
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700286int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700287 snd_device_t snd_device,
288 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800289{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700290 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
291
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800292 if (snd_device < SND_DEVICE_MIN ||
293 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800294 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800295 return -EINVAL;
296 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700297 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
298 ALOGE("%s: device ref cnt is already 0", __func__);
299 return -EINVAL;
300 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700301
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700302 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700303
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700304 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
305 ALOGE("%s: Invalid sound device returned", __func__);
306 return -EINVAL;
307 }
308
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700309 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700310 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700311 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800312 /* exit usb play back thread */
313 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
314 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
315 audio_extn_usb_stop_playback();
316
317 /* exit usb capture thread */
318 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
319 audio_extn_usb_stop_capture(adev);
320
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700321 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
322 audio_extn_spkr_prot_is_enabled()) {
323 audio_extn_spkr_prot_stop_processing();
324 } else
325 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800326
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700327 if (update_mixer)
328 audio_route_update_mixer(adev->audio_route);
Kiran Kandide144c82013-11-20 15:58:32 -0800329
330 audio_extn_listen_update_status(snd_device,
331 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700332 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700333
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800334 return 0;
335}
336
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700337static void check_usecases_codec_backend(struct audio_device *adev,
338 struct audio_usecase *uc_info,
339 snd_device_t snd_device)
340{
341 struct listnode *node;
342 struct audio_usecase *usecase;
343 bool switch_device[AUDIO_USECASE_MAX];
344 int i, num_uc_to_switch = 0;
345
346 /*
347 * This function is to make sure that all the usecases that are active on
348 * the hardware codec backend are always routed to any one device that is
349 * handled by the hardware codec.
350 * For example, if low-latency and deep-buffer usecases are currently active
351 * on speaker and out_set_parameters(headset) is received on low-latency
352 * output, then we have to make sure deep-buffer is also switched to headset,
353 * because of the limitation that both the devices cannot be enabled
354 * at the same time as they share the same backend.
355 */
356 /* Disable all the usecases on the shared backend other than the
357 specified usecase */
358 for (i = 0; i < AUDIO_USECASE_MAX; i++)
359 switch_device[i] = false;
360
361 list_for_each(node, &adev->usecase_list) {
362 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700363 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700364 usecase != uc_info &&
365 usecase->out_snd_device != snd_device &&
366 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
367 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
368 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700369 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700370 disable_audio_route(adev, usecase, false);
371 switch_device[usecase->id] = true;
372 num_uc_to_switch++;
373 }
374 }
375
376 if (num_uc_to_switch) {
377 /* Make sure all the streams are de-routed before disabling the device */
378 audio_route_update_mixer(adev->audio_route);
379
380 list_for_each(node, &adev->usecase_list) {
381 usecase = node_to_item(node, struct audio_usecase, list);
382 if (switch_device[usecase->id]) {
383 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700384 }
385 }
386
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700387 list_for_each(node, &adev->usecase_list) {
388 usecase = node_to_item(node, struct audio_usecase, list);
389 if (switch_device[usecase->id]) {
390 enable_snd_device(adev, snd_device, false);
391 }
392 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700393 /* Make sure new snd device is enabled before re-routing the streams */
394 audio_route_update_mixer(adev->audio_route);
395
396 /* Re-route all the usecases on the shared backend other than the
397 specified usecase to new snd devices */
398 list_for_each(node, &adev->usecase_list) {
399 usecase = node_to_item(node, struct audio_usecase, list);
400 /* Update the out_snd_device only before enabling the audio route */
401 if (switch_device[usecase->id] ) {
402 usecase->out_snd_device = snd_device;
403 enable_audio_route(adev, usecase, false);
404 }
405 }
406
407 audio_route_update_mixer(adev->audio_route);
408 }
409}
410
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700411static void check_and_route_capture_usecases(struct audio_device *adev,
412 struct audio_usecase *uc_info,
413 snd_device_t snd_device)
414{
415 struct listnode *node;
416 struct audio_usecase *usecase;
417 bool switch_device[AUDIO_USECASE_MAX];
418 int i, num_uc_to_switch = 0;
419
420 /*
421 * This function is to make sure that all the active capture usecases
422 * are always routed to the same input sound device.
423 * For example, if audio-record and voice-call usecases are currently
424 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
425 * is received for voice call then we have to make sure that audio-record
426 * usecase is also switched to earpiece i.e. voice-dmic-ef,
427 * because of the limitation that two devices cannot be enabled
428 * at the same time if they share the same backend.
429 */
430 for (i = 0; i < AUDIO_USECASE_MAX; i++)
431 switch_device[i] = false;
432
433 list_for_each(node, &adev->usecase_list) {
434 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700435 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700436 usecase != uc_info &&
437 usecase->in_snd_device != snd_device) {
438 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
439 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700440 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700441 disable_audio_route(adev, usecase, false);
442 switch_device[usecase->id] = true;
443 num_uc_to_switch++;
444 }
445 }
446
447 if (num_uc_to_switch) {
448 /* Make sure all the streams are de-routed before disabling the device */
449 audio_route_update_mixer(adev->audio_route);
450
451 list_for_each(node, &adev->usecase_list) {
452 usecase = node_to_item(node, struct audio_usecase, list);
453 if (switch_device[usecase->id]) {
454 disable_snd_device(adev, usecase->in_snd_device, false);
455 enable_snd_device(adev, snd_device, false);
456 }
457 }
458
459 /* Make sure new snd device is enabled before re-routing the streams */
460 audio_route_update_mixer(adev->audio_route);
461
462 /* Re-route all the usecases on the shared backend other than the
463 specified usecase to new snd devices */
464 list_for_each(node, &adev->usecase_list) {
465 usecase = node_to_item(node, struct audio_usecase, list);
466 /* Update the in_snd_device only before enabling the audio route */
467 if (switch_device[usecase->id] ) {
468 usecase->in_snd_device = snd_device;
469 enable_audio_route(adev, usecase, false);
470 }
471 }
472
473 audio_route_update_mixer(adev->audio_route);
474 }
475}
476
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700477static int disable_all_usecases_of_type(struct audio_device *adev,
478 usecase_type_t usecase_type,
479 bool update_mixer)
480{
481 struct audio_usecase *usecase;
482 struct listnode *node;
483 int ret = 0;
484
485 list_for_each(node, &adev->usecase_list) {
486 usecase = node_to_item(node, struct audio_usecase, list);
487 if (usecase->type == usecase_type) {
488 ALOGV("%s: usecase id %d", __func__, usecase->id);
489 ret = disable_audio_route(adev, usecase, update_mixer);
490 if (ret) {
491 ALOGE("%s: Failed to disable usecase id %d",
492 __func__, usecase->id);
493 }
494 }
495 }
496
497 return ret;
498}
499
500static int enable_all_usecases_of_type(struct audio_device *adev,
501 usecase_type_t usecase_type,
502 bool update_mixer)
503{
504 struct audio_usecase *usecase;
505 struct listnode *node;
506 int ret = 0;
507
508 list_for_each(node, &adev->usecase_list) {
509 usecase = node_to_item(node, struct audio_usecase, list);
510 if (usecase->type == usecase_type) {
511 ALOGV("%s: usecase id %d", __func__, usecase->id);
512 ret = enable_audio_route(adev, usecase, update_mixer);
513 if (ret) {
514 ALOGE("%s: Failed to enable usecase id %d",
515 __func__, usecase->id);
516 }
517 }
518 }
519
520 return ret;
521}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800522
523/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700524static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800525{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700526 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700527 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800528
529 switch (channels) {
530 /*
531 * Do not handle stereo output in Multi-channel cases
532 * Stereo case is handled in normal playback path
533 */
534 case 6:
535 ALOGV("%s: HDMI supports 5.1", __func__);
536 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
537 break;
538 case 8:
539 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
540 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
541 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
542 break;
543 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700544 ALOGE("HDMI does not support multi channel playback");
545 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800546 break;
547 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700548 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800549}
550
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700551static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
552{
553 struct audio_usecase *usecase;
554 struct listnode *node;
555
556 list_for_each(node, &adev->usecase_list) {
557 usecase = node_to_item(node, struct audio_usecase, list);
558 if (usecase->type == VOICE_CALL) {
559 ALOGV("%s: usecase id %d", __func__, usecase->id);
560 return usecase->id;
561 }
562 }
563 return USECASE_INVALID;
564}
565
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700566struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700567 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700568{
569 struct audio_usecase *usecase;
570 struct listnode *node;
571
572 list_for_each(node, &adev->usecase_list) {
573 usecase = node_to_item(node, struct audio_usecase, list);
574 if (usecase->id == uc_id)
575 return usecase;
576 }
577 return NULL;
578}
579
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700580int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800581{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800582 snd_device_t out_snd_device = SND_DEVICE_NONE;
583 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700584 struct audio_usecase *usecase = NULL;
585 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800586 struct audio_usecase *voip_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800587 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700588 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800589
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700590 usecase = get_usecase_from_list(adev, uc_id);
591 if (usecase == NULL) {
592 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
593 return -EINVAL;
594 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800595
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800596 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800597 (usecase->type == VOIP_CALL) ||
598 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700599 out_snd_device = platform_get_output_snd_device(adev->platform,
600 usecase->stream.out->devices);
601 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700602 usecase->devices = usecase->stream.out->devices;
603 } else {
604 /*
605 * If the voice call is active, use the sound devices of voice call usecase
606 * so that it would not result any device switch. All the usecases will
607 * be switched to new device when select_devices() is called for voice call
608 * usecase. This is to avoid switching devices for voice call when
609 * check_usecases_codec_backend() is called below.
610 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700611 if (voice_is_in_call(adev)) {
612 vc_usecase = get_usecase_from_list(adev,
613 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700614 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
615 in_snd_device = vc_usecase->in_snd_device;
616 out_snd_device = vc_usecase->out_snd_device;
617 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800618 } else if (voice_extn_compress_voip_is_active(adev)) {
619 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
620 if (voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
621 in_snd_device = voip_usecase->in_snd_device;
622 out_snd_device = voip_usecase->out_snd_device;
623 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700624 }
625 if (usecase->type == PCM_PLAYBACK) {
626 usecase->devices = usecase->stream.out->devices;
627 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700628 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700629 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700630 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700631 if (usecase->stream.out == adev->primary_output &&
632 adev->active_input &&
633 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
634 select_devices(adev, adev->active_input->usecase);
635 }
636 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700637 } else if (usecase->type == PCM_CAPTURE) {
638 usecase->devices = usecase->stream.in->device;
639 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700640 if (in_snd_device == SND_DEVICE_NONE) {
641 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
642 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700643 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700644 adev->primary_output->devices);
645 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700646 in_snd_device = platform_get_input_snd_device(adev->platform,
647 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700648 }
649 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700650 }
651 }
652
653 if (out_snd_device == usecase->out_snd_device &&
654 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800655 return 0;
656 }
657
sangwoobc677242013-08-08 16:53:43 +0900658 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700659 out_snd_device, platform_get_snd_device_name(out_snd_device),
660 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800661
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800662 /*
663 * Limitation: While in call, to do a device switch we need to disable
664 * and enable both RX and TX devices though one of them is same as current
665 * device.
666 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800667 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700668 status = platform_switch_voice_call_device_pre(adev->platform);
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800669 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800670 }
671
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700672 /* Disable current sound devices */
673 if (usecase->out_snd_device != SND_DEVICE_NONE) {
674 disable_audio_route(adev, usecase, true);
675 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800676 }
677
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700678 if (usecase->in_snd_device != SND_DEVICE_NONE) {
679 disable_audio_route(adev, usecase, true);
680 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800681 }
682
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700683 /* Enable new sound devices */
684 if (out_snd_device != SND_DEVICE_NONE) {
685 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
686 check_usecases_codec_backend(adev, usecase, out_snd_device);
687 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800688 }
689
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700690 if (in_snd_device != SND_DEVICE_NONE) {
691 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700692 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700693 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700694
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800695 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700696 status = platform_switch_voice_call_device_post(adev->platform,
697 out_snd_device,
698 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800699
sangwoo170731f2013-06-08 15:36:36 +0900700 audio_route_update_mixer(adev->audio_route);
701
702 usecase->in_snd_device = in_snd_device;
703 usecase->out_snd_device = out_snd_device;
704
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800705 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
706 enable_all_usecases_of_type(adev, usecase->type, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700707 else
708 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900709
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800710 /* Applicable only on the targets that has external modem.
711 * Enable device command should be sent to modem only after
712 * enabling voice call mixer controls
713 */
714 if (usecase->type == VOICE_CALL)
715 status = platform_switch_voice_call_usecase_route_post(adev->platform,
716 out_snd_device,
717 in_snd_device);
718
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800719 return status;
720}
721
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800722static int stop_input_stream(struct stream_in *in)
723{
724 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800725 struct audio_usecase *uc_info;
726 struct audio_device *adev = in->dev;
727
Eric Laurentc8400632013-02-14 19:04:54 -0800728 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800729
Eric Laurent994a6932013-07-17 11:51:42 -0700730 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700731 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732 uc_info = get_usecase_from_list(adev, in->usecase);
733 if (uc_info == NULL) {
734 ALOGE("%s: Could not find the usecase (%d) in the list",
735 __func__, in->usecase);
736 return -EINVAL;
737 }
738
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800739 /* Close in-call recording streams */
740 voice_check_and_stop_incall_rec_usecase(adev, in);
741
Eric Laurent150dbfe2013-02-27 14:31:02 -0800742 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700743 disable_audio_route(adev, uc_info, true);
744
745 /* 2. Disable the tx device */
746 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800747
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800748 list_remove(&uc_info->list);
749 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800750
Eric Laurent994a6932013-07-17 11:51:42 -0700751 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800752 return ret;
753}
754
755int start_input_stream(struct stream_in *in)
756{
757 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800758 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800759 struct audio_usecase *uc_info;
760 struct audio_device *adev = in->dev;
761
Mingming Yine62d7842013-10-25 16:26:03 -0700762 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Eric Laurent994a6932013-07-17 11:51:42 -0700763 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700764
765 /* Check if source matches incall recording usecase criteria */
766 ret = voice_check_and_set_incall_rec_usecase(adev, in);
767 if (ret)
768 goto error_config;
769 else
770 ALOGV("%s: usecase(%d)", __func__, in->usecase);
771
Eric Laurentb23d5282013-05-14 15:27:20 -0700772 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800773 if (in->pcm_device_id < 0) {
774 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
775 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800776 ret = -EINVAL;
777 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800778 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700779
780 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800781 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
782 uc_info->id = in->usecase;
783 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800784 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700785 uc_info->devices = in->device;
786 uc_info->in_snd_device = SND_DEVICE_NONE;
787 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800788
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800789 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700790 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800791
Eric Laurentc8400632013-02-14 19:04:54 -0800792 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
793 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800794 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
795 PCM_IN, &in->config);
796 if (in->pcm && !pcm_is_ready(in->pcm)) {
797 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
798 pcm_close(in->pcm);
799 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800800 ret = -EIO;
801 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800802 }
Eric Laurent994a6932013-07-17 11:51:42 -0700803 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800804 return ret;
805
806error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800807 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800808
809error_config:
810 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700811 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800812
813 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800814}
815
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700816/* must be called with out->lock locked */
817static int send_offload_cmd_l(struct stream_out* out, int command)
818{
819 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
820
821 ALOGVV("%s %d", __func__, command);
822
823 cmd->cmd = command;
824 list_add_tail(&out->offload_cmd_list, &cmd->node);
825 pthread_cond_signal(&out->offload_cond);
826 return 0;
827}
828
829/* must be called iwth out->lock locked */
830static void stop_compressed_output_l(struct stream_out *out)
831{
832 out->offload_state = OFFLOAD_STATE_IDLE;
833 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700834 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700835 if (out->compr != NULL) {
836 compress_stop(out->compr);
837 while (out->offload_thread_blocked) {
838 pthread_cond_wait(&out->cond, &out->lock);
839 }
840 }
841}
842
843static void *offload_thread_loop(void *context)
844{
845 struct stream_out *out = (struct stream_out *) context;
846 struct listnode *item;
847
848 out->offload_state = OFFLOAD_STATE_IDLE;
849 out->playback_started = 0;
850
851 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
852 set_sched_policy(0, SP_FOREGROUND);
853 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
854
855 ALOGV("%s", __func__);
856 pthread_mutex_lock(&out->lock);
857 for (;;) {
858 struct offload_cmd *cmd = NULL;
859 stream_callback_event_t event;
860 bool send_callback = false;
861
862 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
863 __func__, list_empty(&out->offload_cmd_list),
864 out->offload_state);
865 if (list_empty(&out->offload_cmd_list)) {
866 ALOGV("%s SLEEPING", __func__);
867 pthread_cond_wait(&out->offload_cond, &out->lock);
868 ALOGV("%s RUNNING", __func__);
869 continue;
870 }
871
872 item = list_head(&out->offload_cmd_list);
873 cmd = node_to_item(item, struct offload_cmd, node);
874 list_remove(item);
875
876 ALOGVV("%s STATE %d CMD %d out->compr %p",
877 __func__, out->offload_state, cmd->cmd, out->compr);
878
879 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
880 free(cmd);
881 break;
882 }
883
884 if (out->compr == NULL) {
885 ALOGE("%s: Compress handle is NULL", __func__);
886 pthread_cond_signal(&out->cond);
887 continue;
888 }
889 out->offload_thread_blocked = true;
890 pthread_mutex_unlock(&out->lock);
891 send_callback = false;
892 switch(cmd->cmd) {
893 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
894 compress_wait(out->compr, -1);
895 send_callback = true;
896 event = STREAM_CBK_EVENT_WRITE_READY;
897 break;
898 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700899 compress_next_track(out->compr);
900 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700901 send_callback = true;
902 event = STREAM_CBK_EVENT_DRAIN_READY;
903 break;
904 case OFFLOAD_CMD_DRAIN:
905 compress_drain(out->compr);
906 send_callback = true;
907 event = STREAM_CBK_EVENT_DRAIN_READY;
908 break;
909 default:
910 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
911 break;
912 }
913 pthread_mutex_lock(&out->lock);
914 out->offload_thread_blocked = false;
915 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700916 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700917 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700918 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700919 free(cmd);
920 }
921
922 pthread_cond_signal(&out->cond);
923 while (!list_empty(&out->offload_cmd_list)) {
924 item = list_head(&out->offload_cmd_list);
925 list_remove(item);
926 free(node_to_item(item, struct offload_cmd, node));
927 }
928 pthread_mutex_unlock(&out->lock);
929
930 return NULL;
931}
932
933static int create_offload_callback_thread(struct stream_out *out)
934{
935 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
936 list_init(&out->offload_cmd_list);
937 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
938 offload_thread_loop, out);
939 return 0;
940}
941
942static int destroy_offload_callback_thread(struct stream_out *out)
943{
944 pthread_mutex_lock(&out->lock);
945 stop_compressed_output_l(out);
946 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
947
948 pthread_mutex_unlock(&out->lock);
949 pthread_join(out->offload_thread, (void **) NULL);
950 pthread_cond_destroy(&out->offload_cond);
951
952 return 0;
953}
954
Eric Laurent07eeafd2013-10-06 12:52:49 -0700955static bool allow_hdmi_channel_config(struct audio_device *adev)
956{
957 struct listnode *node;
958 struct audio_usecase *usecase;
959 bool ret = true;
960
961 list_for_each(node, &adev->usecase_list) {
962 usecase = node_to_item(node, struct audio_usecase, list);
963 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
964 /*
965 * If voice call is already existing, do not proceed further to avoid
966 * disabling/enabling both RX and TX devices, CSD calls, etc.
967 * Once the voice call done, the HDMI channels can be configured to
968 * max channels of remaining use cases.
969 */
970 if (usecase->id == USECASE_VOICE_CALL) {
971 ALOGD("%s: voice call is active, no change in HDMI channels",
972 __func__);
973 ret = false;
974 break;
975 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
976 ALOGD("%s: multi channel playback is active, "
977 "no change in HDMI channels", __func__);
978 ret = false;
979 break;
980 }
981 }
982 }
983 return ret;
984}
985
986static int check_and_set_hdmi_channels(struct audio_device *adev,
987 unsigned int channels)
988{
989 struct listnode *node;
990 struct audio_usecase *usecase;
991
992 /* Check if change in HDMI channel config is allowed */
993 if (!allow_hdmi_channel_config(adev))
994 return 0;
995
996 if (channels == adev->cur_hdmi_channels) {
997 ALOGD("%s: Requested channels are same as current", __func__);
998 return 0;
999 }
1000
1001 platform_set_hdmi_channels(adev->platform, channels);
1002 adev->cur_hdmi_channels = channels;
1003
1004 /*
1005 * Deroute all the playback streams routed to HDMI so that
1006 * the back end is deactivated. Note that backend will not
1007 * be deactivated if any one stream is connected to it.
1008 */
1009 list_for_each(node, &adev->usecase_list) {
1010 usecase = node_to_item(node, struct audio_usecase, list);
1011 if (usecase->type == PCM_PLAYBACK &&
1012 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1013 disable_audio_route(adev, usecase, true);
1014 }
1015 }
1016
1017 /*
1018 * Enable all the streams disabled above. Now the HDMI backend
1019 * will be activated with new channel configuration
1020 */
1021 list_for_each(node, &adev->usecase_list) {
1022 usecase = node_to_item(node, struct audio_usecase, list);
1023 if (usecase->type == PCM_PLAYBACK &&
1024 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1025 enable_audio_route(adev, usecase, true);
1026 }
1027 }
1028
1029 return 0;
1030}
1031
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001032static int stop_output_stream(struct stream_out *out)
1033{
1034 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001035 struct audio_usecase *uc_info;
1036 struct audio_device *adev = out->dev;
1037
Eric Laurent994a6932013-07-17 11:51:42 -07001038 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001039 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001040 uc_info = get_usecase_from_list(adev, out->usecase);
1041 if (uc_info == NULL) {
1042 ALOGE("%s: Could not find the usecase (%d) in the list",
1043 __func__, out->usecase);
1044 return -EINVAL;
1045 }
1046
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001047 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1048 if (adev->visualizer_stop_output != NULL)
1049 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1050 if (adev->offload_effects_stop_output != NULL)
1051 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1052 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001053
Eric Laurent150dbfe2013-02-27 14:31:02 -08001054 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001055 disable_audio_route(adev, uc_info, true);
1056
1057 /* 2. Disable the rx device */
1058 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001059
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001060 list_remove(&uc_info->list);
1061 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001062
Eric Laurent07eeafd2013-10-06 12:52:49 -07001063 /* Must be called after removing the usecase from list */
1064 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1065 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1066
Eric Laurent994a6932013-07-17 11:51:42 -07001067 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001068 return ret;
1069}
1070
1071int start_output_stream(struct stream_out *out)
1072{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001073 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001074 struct audio_usecase *uc_info;
1075 struct audio_device *adev = out->dev;
1076
Eric Laurent994a6932013-07-17 11:51:42 -07001077 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001078 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001079 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001080 if (out->pcm_device_id < 0) {
1081 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1082 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001083 ret = -EINVAL;
1084 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001085 }
1086
1087 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1088 uc_info->id = out->usecase;
1089 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001090 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001091 uc_info->devices = out->devices;
1092 uc_info->in_snd_device = SND_DEVICE_NONE;
1093 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001094
Eric Laurent07eeafd2013-10-06 12:52:49 -07001095 /* This must be called before adding this usecase to the list */
1096 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1097 check_and_set_hdmi_channels(adev, out->config.channels);
1098
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001099 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001100
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001101 select_devices(adev, out->usecase);
1102
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001103 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1104 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001105 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1106 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001107 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001108 if (out->pcm && !pcm_is_ready(out->pcm)) {
1109 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1110 pcm_close(out->pcm);
1111 out->pcm = NULL;
1112 ret = -EIO;
1113 goto error_open;
1114 }
1115 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001116 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001117 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1118 COMPRESS_IN, &out->compr_config);
1119 if (out->compr && !is_compress_ready(out->compr)) {
1120 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1121 compress_close(out->compr);
1122 out->compr = NULL;
1123 ret = -EIO;
1124 goto error_open;
1125 }
1126 if (out->offload_callback)
1127 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001128
1129 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001130 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1131 if (adev->offload_effects_start_output != NULL)
1132 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001133 }
Eric Laurent994a6932013-07-17 11:51:42 -07001134 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001135 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001136error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001137 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001138error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001139 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001140}
1141
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001142static int check_input_parameters(uint32_t sample_rate,
1143 audio_format_t format,
1144 int channel_count)
1145{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001146 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001147
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001148 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001149 !voice_extn_compress_voip_is_format_supported(format) &&
1150 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001151
1152 switch (channel_count) {
1153 case 1:
1154 case 2:
1155 case 6:
1156 break;
1157 default:
1158 ret = -EINVAL;
1159 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001160
1161 switch (sample_rate) {
1162 case 8000:
1163 case 11025:
1164 case 12000:
1165 case 16000:
1166 case 22050:
1167 case 24000:
1168 case 32000:
1169 case 44100:
1170 case 48000:
1171 break;
1172 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001173 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001174 }
1175
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001176 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001177}
1178
1179static size_t get_input_buffer_size(uint32_t sample_rate,
1180 audio_format_t format,
1181 int channel_count)
1182{
1183 size_t size = 0;
1184
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001185 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1186 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001187
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001188 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1189 /* ToDo: should use frame_size computed based on the format and
1190 channel_count here. */
1191 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001192
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001193 /* make sure the size is multiple of 64 */
1194 size += 0x3f;
1195 size &= ~0x3f;
1196
1197 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001198}
1199
1200static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1201{
1202 struct stream_out *out = (struct stream_out *)stream;
1203
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001204 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001205}
1206
1207static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1208{
1209 return -ENOSYS;
1210}
1211
1212static size_t out_get_buffer_size(const struct audio_stream *stream)
1213{
1214 struct stream_out *out = (struct stream_out *)stream;
1215
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001216 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001217 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001218 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1219 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001220
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001221 return out->config.period_size * audio_stream_frame_size(stream);
1222}
1223
1224static uint32_t out_get_channels(const struct audio_stream *stream)
1225{
1226 struct stream_out *out = (struct stream_out *)stream;
1227
1228 return out->channel_mask;
1229}
1230
1231static audio_format_t out_get_format(const struct audio_stream *stream)
1232{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001233 struct stream_out *out = (struct stream_out *)stream;
1234
1235 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001236}
1237
1238static int out_set_format(struct audio_stream *stream, audio_format_t format)
1239{
1240 return -ENOSYS;
1241}
1242
1243static int out_standby(struct audio_stream *stream)
1244{
1245 struct stream_out *out = (struct stream_out *)stream;
1246 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001247
Eric Laurent994a6932013-07-17 11:51:42 -07001248 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001249 out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001250 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1251 /* Ignore standby in case of voip call because the voip output
1252 * stream is closed in adev_close_output_stream()
1253 */
1254 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1255 return 0;
1256 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001257
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001258 pthread_mutex_lock(&out->lock);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001259 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001260 if (!out->standby) {
1261 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001262 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1263 if (out->pcm) {
1264 pcm_close(out->pcm);
1265 out->pcm = NULL;
1266 }
1267 } else {
1268 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001269 out->gapless_mdata.encoder_delay = 0;
1270 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001271 if (out->compr != NULL) {
1272 compress_close(out->compr);
1273 out->compr = NULL;
1274 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001275 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001276 stop_output_stream(out);
1277 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001278 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001279 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001280 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001281 return 0;
1282}
1283
1284static int out_dump(const struct audio_stream *stream, int fd)
1285{
1286 return 0;
1287}
1288
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001289static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1290{
1291 int ret = 0;
1292 char value[32];
1293 struct compr_gapless_mdata tmp_mdata;
1294
1295 if (!out || !parms) {
1296 return -EINVAL;
1297 }
1298
1299 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1300 if (ret >= 0) {
1301 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1302 } else {
1303 return -EINVAL;
1304 }
1305
1306 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1307 if (ret >= 0) {
1308 tmp_mdata.encoder_padding = atoi(value);
1309 } else {
1310 return -EINVAL;
1311 }
1312
1313 out->gapless_mdata = tmp_mdata;
1314 out->send_new_metadata = 1;
1315 ALOGV("%s new encoder delay %u and padding %u", __func__,
1316 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1317
1318 return 0;
1319}
1320
1321
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001322static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1323{
1324 struct stream_out *out = (struct stream_out *)stream;
1325 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001326 struct audio_usecase *usecase;
1327 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001328 struct str_parms *parms;
1329 char value[32];
1330 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001331 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001332
sangwoobc677242013-08-08 16:53:43 +09001333 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001334 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001335 parms = str_parms_create_str(kvpairs);
1336 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1337 if (ret >= 0) {
1338 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001339 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001340 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001341
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001342 /*
1343 * When HDMI cable is unplugged the music playback is paused and
1344 * the policy manager sends routing=0. But the audioflinger
1345 * continues to write data until standby time (3sec).
1346 * As the HDMI core is turned off, the write gets blocked.
1347 * Avoid this by routing audio to speaker until standby.
1348 */
1349 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1350 val == AUDIO_DEVICE_NONE) {
1351 val = AUDIO_DEVICE_OUT_SPEAKER;
1352 }
1353
1354 /*
1355 * select_devices() call below switches all the usecases on the same
1356 * backend to the new device. Refer to check_usecases_codec_backend() in
1357 * the select_devices(). But how do we undo this?
1358 *
1359 * For example, music playback is active on headset (deep-buffer usecase)
1360 * and if we go to ringtones and select a ringtone, low-latency usecase
1361 * will be started on headset+speaker. As we can't enable headset+speaker
1362 * and headset devices at the same time, select_devices() switches the music
1363 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1364 * So when the ringtone playback is completed, how do we undo the same?
1365 *
1366 * We are relying on the out_set_parameters() call on deep-buffer output,
1367 * once the ringtone playback is ended.
1368 * NOTE: We should not check if the current devices are same as new devices.
1369 * Because select_devices() must be called to switch back the music
1370 * playback to headset.
1371 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001372 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001373 out->devices = val;
1374
1375 if (!out->standby)
1376 select_devices(adev, out->usecase);
1377
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001378 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1379 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001380 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001381 voice_start_call(adev);
1382 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1383 voice_is_in_call(adev) &&
1384 (out == adev->primary_output)) {
1385 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001386 }
1387 }
1388
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001389 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1390 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001391 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001392 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001393 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001394
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001395 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001396 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001397 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001398
1399 if (out == adev->primary_output) {
1400 pthread_mutex_lock(&adev->lock);
1401 audio_extn_set_parameters(adev, parms);
1402 pthread_mutex_unlock(&adev->lock);
1403 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001404 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1405 parse_compress_metadata(out, parms);
1406 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001407
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001408 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001409 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001410 return ret;
1411}
1412
1413static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1414{
1415 struct stream_out *out = (struct stream_out *)stream;
1416 struct str_parms *query = str_parms_create_str(keys);
1417 char *str;
1418 char value[256];
1419 struct str_parms *reply = str_parms_create();
1420 size_t i, j;
1421 int ret;
1422 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001423 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001424 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1425 if (ret >= 0) {
1426 value[0] = '\0';
1427 i = 0;
1428 while (out->supported_channel_masks[i] != 0) {
1429 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1430 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1431 if (!first) {
1432 strcat(value, "|");
1433 }
1434 strcat(value, out_channels_name_to_enum_table[j].name);
1435 first = false;
1436 break;
1437 }
1438 }
1439 i++;
1440 }
1441 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1442 str = str_parms_to_str(reply);
1443 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001444 voice_extn_out_get_parameters(out, query, reply);
1445 str = str_parms_to_str(reply);
1446 if (!strncmp(str, "", sizeof(""))) {
1447 str = strdup(keys);
1448 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001449 }
1450 str_parms_destroy(query);
1451 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001452 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001453 return str;
1454}
1455
1456static uint32_t out_get_latency(const struct audio_stream_out *stream)
1457{
1458 struct stream_out *out = (struct stream_out *)stream;
1459
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001460 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1461 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1462
1463 return (out->config.period_count * out->config.period_size * 1000) /
1464 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001465}
1466
1467static int out_set_volume(struct audio_stream_out *stream, float left,
1468 float right)
1469{
Eric Laurenta9024de2013-04-04 09:19:12 -07001470 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001471 int volume[2];
1472
Eric Laurenta9024de2013-04-04 09:19:12 -07001473 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1474 /* only take left channel into account: the API is for stereo anyway */
1475 out->muted = (left == 0.0f);
1476 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001477 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001478 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001479 struct audio_device *adev = out->dev;
1480 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001481 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1482 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001483
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001484 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1485 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001486 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1487 if (!ctl) {
1488 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1489 __func__, mixer_ctl_name);
1490 return -EINVAL;
1491 }
1492 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1493 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1494 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1495 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001496 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001497
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001498 return -ENOSYS;
1499}
1500
1501static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1502 size_t bytes)
1503{
1504 struct stream_out *out = (struct stream_out *)stream;
1505 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001506 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001507
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001508 pthread_mutex_lock(&out->lock);
1509 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001510 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001511 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001512 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1513 ret = voice_extn_compress_voip_start_output_stream(out);
1514 else
1515 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001516 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001517 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001518 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001519 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001520 goto exit;
1521 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001522 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001523
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001524 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001525 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1526 if (out->send_new_metadata) {
1527 ALOGVV("send new gapless metadata");
1528 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1529 out->send_new_metadata = 0;
1530 }
1531
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001532 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001533 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001534 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001535 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1536 }
1537 if (!out->playback_started) {
1538 compress_start(out->compr);
1539 out->playback_started = 1;
1540 out->offload_state = OFFLOAD_STATE_PLAYING;
1541 }
1542 pthread_mutex_unlock(&out->lock);
1543 return ret;
1544 } else {
1545 if (out->pcm) {
1546 if (out->muted)
1547 memset((void *)buffer, 0, bytes);
1548 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1549 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001550 if (ret == 0)
1551 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001552 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001553 }
1554
1555exit:
1556 pthread_mutex_unlock(&out->lock);
1557
1558 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001559 if (out->pcm)
1560 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001561 out_standby(&out->stream.common);
1562 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1563 out_get_sample_rate(&out->stream.common));
1564 }
1565 return bytes;
1566}
1567
1568static int out_get_render_position(const struct audio_stream_out *stream,
1569 uint32_t *dsp_frames)
1570{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001571 struct stream_out *out = (struct stream_out *)stream;
1572 *dsp_frames = 0;
1573 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1574 pthread_mutex_lock(&out->lock);
1575 if (out->compr != NULL) {
1576 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1577 &out->sample_rate);
1578 ALOGVV("%s rendered frames %d sample_rate %d",
1579 __func__, *dsp_frames, out->sample_rate);
1580 }
1581 pthread_mutex_unlock(&out->lock);
1582 return 0;
1583 } else
1584 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001585}
1586
1587static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1588{
1589 return 0;
1590}
1591
1592static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1593{
1594 return 0;
1595}
1596
1597static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1598 int64_t *timestamp)
1599{
1600 return -EINVAL;
1601}
1602
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001603static int out_get_presentation_position(const struct audio_stream_out *stream,
1604 uint64_t *frames, struct timespec *timestamp)
1605{
1606 struct stream_out *out = (struct stream_out *)stream;
1607 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001608 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001609
1610 pthread_mutex_lock(&out->lock);
1611
Eric Laurent949a0892013-09-20 09:20:13 -07001612 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1613 if (out->compr != NULL) {
1614 compress_get_tstamp(out->compr, &dsp_frames,
1615 &out->sample_rate);
1616 ALOGVV("%s rendered frames %ld sample_rate %d",
1617 __func__, dsp_frames, out->sample_rate);
1618 *frames = dsp_frames;
1619 ret = 0;
1620 /* this is the best we can do */
1621 clock_gettime(CLOCK_MONOTONIC, timestamp);
1622 }
1623 } else {
1624 if (out->pcm) {
1625 size_t avail;
1626 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1627 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001628 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001629 // This adjustment accounts for buffering after app processor.
1630 // It is based on estimated DSP latency per use case, rather than exact.
1631 signed_frames -=
1632 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1633
Eric Laurent949a0892013-09-20 09:20:13 -07001634 // It would be unusual for this value to be negative, but check just in case ...
1635 if (signed_frames >= 0) {
1636 *frames = signed_frames;
1637 ret = 0;
1638 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001639 }
1640 }
1641 }
1642
1643 pthread_mutex_unlock(&out->lock);
1644
1645 return ret;
1646}
1647
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001648static int out_set_callback(struct audio_stream_out *stream,
1649 stream_callback_t callback, void *cookie)
1650{
1651 struct stream_out *out = (struct stream_out *)stream;
1652
1653 ALOGV("%s", __func__);
1654 pthread_mutex_lock(&out->lock);
1655 out->offload_callback = callback;
1656 out->offload_cookie = cookie;
1657 pthread_mutex_unlock(&out->lock);
1658 return 0;
1659}
1660
1661static int out_pause(struct audio_stream_out* stream)
1662{
1663 struct stream_out *out = (struct stream_out *)stream;
1664 int status = -ENOSYS;
1665 ALOGV("%s", __func__);
1666 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1667 pthread_mutex_lock(&out->lock);
1668 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1669 status = compress_pause(out->compr);
1670 out->offload_state = OFFLOAD_STATE_PAUSED;
1671 }
1672 pthread_mutex_unlock(&out->lock);
1673 }
1674 return status;
1675}
1676
1677static int out_resume(struct audio_stream_out* stream)
1678{
1679 struct stream_out *out = (struct stream_out *)stream;
1680 int status = -ENOSYS;
1681 ALOGV("%s", __func__);
1682 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1683 status = 0;
1684 pthread_mutex_lock(&out->lock);
1685 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1686 status = compress_resume(out->compr);
1687 out->offload_state = OFFLOAD_STATE_PLAYING;
1688 }
1689 pthread_mutex_unlock(&out->lock);
1690 }
1691 return status;
1692}
1693
1694static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1695{
1696 struct stream_out *out = (struct stream_out *)stream;
1697 int status = -ENOSYS;
1698 ALOGV("%s", __func__);
1699 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1700 pthread_mutex_lock(&out->lock);
1701 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1702 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1703 else
1704 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1705 pthread_mutex_unlock(&out->lock);
1706 }
1707 return status;
1708}
1709
1710static int out_flush(struct audio_stream_out* stream)
1711{
1712 struct stream_out *out = (struct stream_out *)stream;
1713 ALOGV("%s", __func__);
1714 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1715 pthread_mutex_lock(&out->lock);
1716 stop_compressed_output_l(out);
1717 pthread_mutex_unlock(&out->lock);
1718 return 0;
1719 }
1720 return -ENOSYS;
1721}
1722
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001723/** audio_stream_in implementation **/
1724static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1725{
1726 struct stream_in *in = (struct stream_in *)stream;
1727
1728 return in->config.rate;
1729}
1730
1731static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1732{
1733 return -ENOSYS;
1734}
1735
1736static size_t in_get_buffer_size(const struct audio_stream *stream)
1737{
1738 struct stream_in *in = (struct stream_in *)stream;
1739
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001740 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1741 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001742 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1743 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001744
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001745 return in->config.period_size * audio_stream_frame_size(stream);
1746}
1747
1748static uint32_t in_get_channels(const struct audio_stream *stream)
1749{
1750 struct stream_in *in = (struct stream_in *)stream;
1751
1752 return in->channel_mask;
1753}
1754
1755static audio_format_t in_get_format(const struct audio_stream *stream)
1756{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001757 struct stream_in *in = (struct stream_in *)stream;
1758
1759 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001760}
1761
1762static int in_set_format(struct audio_stream *stream, audio_format_t format)
1763{
1764 return -ENOSYS;
1765}
1766
1767static int in_standby(struct audio_stream *stream)
1768{
1769 struct stream_in *in = (struct stream_in *)stream;
1770 struct audio_device *adev = in->dev;
1771 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001772 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001773
1774 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1775 /* Ignore standby in case of voip call because the voip input
1776 * stream is closed in adev_close_input_stream()
1777 */
1778 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1779 return status;
1780 }
1781
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001782 pthread_mutex_lock(&in->lock);
1783 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001784 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001785 if (in->pcm) {
1786 pcm_close(in->pcm);
1787 in->pcm = NULL;
1788 }
1789 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001791 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001792 }
1793 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001794 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001795 return status;
1796}
1797
1798static int in_dump(const struct audio_stream *stream, int fd)
1799{
1800 return 0;
1801}
1802
1803static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1804{
1805 struct stream_in *in = (struct stream_in *)stream;
1806 struct audio_device *adev = in->dev;
1807 struct str_parms *parms;
1808 char *str;
1809 char value[32];
1810 int ret, val = 0;
1811
Eric Laurent994a6932013-07-17 11:51:42 -07001812 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001813 parms = str_parms_create_str(kvpairs);
1814
1815 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1816
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001817 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001818 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001819 if (ret >= 0) {
1820 val = atoi(value);
1821 /* no audio source uses val == 0 */
1822 if ((in->source != val) && (val != 0)) {
1823 in->source = val;
1824 }
1825 }
1826
1827 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1828 if (ret >= 0) {
1829 val = atoi(value);
1830 if ((in->device != val) && (val != 0)) {
1831 in->device = val;
1832 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001833 if (!in->standby)
1834 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001835 }
1836 }
1837
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001838 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001839 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001840
1841 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001842 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001843 return ret;
1844}
1845
1846static char* in_get_parameters(const struct audio_stream *stream,
1847 const char *keys)
1848{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001849 struct stream_in *in = (struct stream_in *)stream;
1850 struct str_parms *query = str_parms_create_str(keys);
1851 char *str;
1852 char value[256];
1853 struct str_parms *reply = str_parms_create();
1854 ALOGV("%s: enter: keys - %s", __func__, keys);
1855
1856 voice_extn_in_get_parameters(in, query, reply);
1857
1858 str = str_parms_to_str(reply);
1859 str_parms_destroy(query);
1860 str_parms_destroy(reply);
1861
1862 ALOGV("%s: exit: returns - %s", __func__, str);
1863 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001864}
1865
1866static int in_set_gain(struct audio_stream_in *stream, float gain)
1867{
1868 return 0;
1869}
1870
1871static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1872 size_t bytes)
1873{
1874 struct stream_in *in = (struct stream_in *)stream;
1875 struct audio_device *adev = in->dev;
1876 int i, ret = -1;
1877
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001878 pthread_mutex_lock(&in->lock);
1879 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001880 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001881 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
1882 ret = voice_extn_compress_voip_start_input_stream(in);
1883 else
1884 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001885 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001886 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001887 goto exit;
1888 }
1889 in->standby = 0;
1890 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001891
1892 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001893 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1894 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07001895 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
1896 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001897 else
1898 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001899 }
1900
1901 /*
1902 * Instead of writing zeroes here, we could trust the hardware
1903 * to always provide zeroes when muted.
1904 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001905 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001906 memset(buffer, 0, bytes);
1907
1908exit:
1909 pthread_mutex_unlock(&in->lock);
1910
1911 if (ret != 0) {
1912 in_standby(&in->stream.common);
1913 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1914 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1915 in_get_sample_rate(&in->stream.common));
1916 }
1917 return bytes;
1918}
1919
1920static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1921{
1922 return 0;
1923}
1924
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001925static int add_remove_audio_effect(const struct audio_stream *stream,
1926 effect_handle_t effect,
1927 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001928{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001929 struct stream_in *in = (struct stream_in *)stream;
1930 int status = 0;
1931 effect_descriptor_t desc;
1932
1933 status = (*effect)->get_descriptor(effect, &desc);
1934 if (status != 0)
1935 return status;
1936
1937 pthread_mutex_lock(&in->lock);
1938 pthread_mutex_lock(&in->dev->lock);
1939 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1940 in->enable_aec != enable &&
1941 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1942 in->enable_aec = enable;
1943 if (!in->standby)
1944 select_devices(in->dev, in->usecase);
1945 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08001946 if (in->enable_ns != enable &&
1947 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1948 in->enable_ns = enable;
1949 if (!in->standby)
1950 select_devices(in->dev, in->usecase);
1951 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001952 pthread_mutex_unlock(&in->dev->lock);
1953 pthread_mutex_unlock(&in->lock);
1954
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001955 return 0;
1956}
1957
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001958static int in_add_audio_effect(const struct audio_stream *stream,
1959 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001960{
Eric Laurent994a6932013-07-17 11:51:42 -07001961 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001962 return add_remove_audio_effect(stream, effect, true);
1963}
1964
1965static int in_remove_audio_effect(const struct audio_stream *stream,
1966 effect_handle_t effect)
1967{
Eric Laurent994a6932013-07-17 11:51:42 -07001968 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001969 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001970}
1971
1972static int adev_open_output_stream(struct audio_hw_device *dev,
1973 audio_io_handle_t handle,
1974 audio_devices_t devices,
1975 audio_output_flags_t flags,
1976 struct audio_config *config,
1977 struct audio_stream_out **stream_out)
1978{
1979 struct audio_device *adev = (struct audio_device *)dev;
1980 struct stream_out *out;
1981 int i, ret;
1982
Eric Laurent994a6932013-07-17 11:51:42 -07001983 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001984 __func__, config->sample_rate, config->channel_mask, devices, flags);
1985 *stream_out = NULL;
1986 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1987
1988 if (devices == AUDIO_DEVICE_NONE)
1989 devices = AUDIO_DEVICE_OUT_SPEAKER;
1990
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001991 out->flags = flags;
1992 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001993 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001994 out->format = config->format;
1995 out->sample_rate = config->sample_rate;
1996 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1997 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001998 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001999
2000 /* Init use case and pcm_config */
Ravi Kumar Alamanda86b0e2b2013-11-20 14:52:30 -08002001 if (out->flags == AUDIO_OUTPUT_FLAG_DIRECT &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002002 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002003 pthread_mutex_lock(&adev->lock);
2004 ret = read_hdmi_channel_masks(out);
2005 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002006 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002007 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002008
2009 if (config->sample_rate == 0)
2010 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2011 if (config->channel_mask == 0)
2012 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2013
2014 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002015 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002016 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2017 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002018 out->config.rate = config->sample_rate;
2019 out->config.channels = popcount(out->channel_mask);
2020 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002021 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2022 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
2023 (voice_extn_compress_voip_is_format_supported(out->format))) {
2024 ret = voice_extn_compress_voip_open_output_stream(out);
2025 if (ret != 0) {
2026 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2027 __func__, ret);
2028 goto error_open;
2029 }
2030 out->config.rate = config->sample_rate;
2031 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002032 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2033 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2034 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2035 ALOGE("%s: Unsupported Offload information", __func__);
2036 ret = -EINVAL;
2037 goto error_open;
2038 }
Mingming Yin90310102013-11-13 16:57:00 -08002039 if (!is_supported_format(config->offload_info.format) &&
2040 !audio_extn_dolby_is_supported_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002041 ALOGE("%s: Unsupported audio format", __func__);
2042 ret = -EINVAL;
2043 goto error_open;
2044 }
2045
2046 out->compr_config.codec = (struct snd_codec *)
2047 calloc(1, sizeof(struct snd_codec));
2048
2049 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2050 if (config->offload_info.channel_mask)
2051 out->channel_mask = config->offload_info.channel_mask;
2052 else if (config->channel_mask)
2053 out->channel_mask = config->channel_mask;
2054 out->format = config->offload_info.format;
2055 out->sample_rate = config->offload_info.sample_rate;
2056
2057 out->stream.set_callback = out_set_callback;
2058 out->stream.pause = out_pause;
2059 out->stream.resume = out_resume;
2060 out->stream.drain = out_drain;
2061 out->stream.flush = out_flush;
2062
Mingming Yin90310102013-11-13 16:57:00 -08002063 if (audio_extn_dolby_is_supported_format(config->offload_info.format))
2064 out->compr_config.codec->id =
2065 audio_extn_dolby_get_snd_codec_id(config->offload_info.format);
2066 else
2067 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002068 get_snd_codec_id(config->offload_info.format);
2069 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2070 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2071 out->compr_config.codec->sample_rate =
2072 compress_get_alsa_rate(config->offload_info.sample_rate);
2073 out->compr_config.codec->bit_rate =
2074 config->offload_info.bit_rate;
2075 out->compr_config.codec->ch_in =
2076 popcount(config->channel_mask);
2077 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2078
2079 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2080 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002081
2082 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002083 create_offload_callback_thread(out);
2084 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2085 __func__, config->offload_info.version,
2086 config->offload_info.bit_rate);
Mingming Yin90310102013-11-13 16:57:00 -08002087
2088 if (audio_extn_dolby_is_supported_format(out->format)) {
2089 ret = audio_extn_dolby_set_DMID(adev);
2090 if (ret != 0) {
2091 ALOGE("%s: Dolby DMID cannot be set error:%d",
2092 __func__, ret);
2093 goto error_open;
2094 }
2095 }
2096
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002097 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2098 ret = voice_check_and_set_incall_music_usecase(adev, out);
2099 if (ret != 0) {
2100 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2101 __func__, ret);
2102 goto error_open;
2103 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002104 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002105 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2106 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002107 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002108 } else {
2109 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2110 out->config = pcm_config_deep_buffer;
2111 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002112 }
2113
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002114 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2115 if(adev->primary_output == NULL)
2116 adev->primary_output = out;
2117 else {
2118 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002119 ret = -EEXIST;
2120 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002121 }
2122 }
2123
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002124 /* Check if this usecase is already existing */
2125 pthread_mutex_lock(&adev->lock);
2126 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2127 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002128 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002129 ret = -EEXIST;
2130 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002131 }
2132 pthread_mutex_unlock(&adev->lock);
2133
2134 out->stream.common.get_sample_rate = out_get_sample_rate;
2135 out->stream.common.set_sample_rate = out_set_sample_rate;
2136 out->stream.common.get_buffer_size = out_get_buffer_size;
2137 out->stream.common.get_channels = out_get_channels;
2138 out->stream.common.get_format = out_get_format;
2139 out->stream.common.set_format = out_set_format;
2140 out->stream.common.standby = out_standby;
2141 out->stream.common.dump = out_dump;
2142 out->stream.common.set_parameters = out_set_parameters;
2143 out->stream.common.get_parameters = out_get_parameters;
2144 out->stream.common.add_audio_effect = out_add_audio_effect;
2145 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2146 out->stream.get_latency = out_get_latency;
2147 out->stream.set_volume = out_set_volume;
2148 out->stream.write = out_write;
2149 out->stream.get_render_position = out_get_render_position;
2150 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002151 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002152
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002153 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002154 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002155 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002156
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002157 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2158 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002159
2160 config->format = out->stream.common.get_format(&out->stream.common);
2161 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2162 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2163
2164 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002165 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002166 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002167
2168error_open:
2169 free(out);
2170 *stream_out = NULL;
2171 ALOGD("%s: exit: ret %d", __func__, ret);
2172 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002173}
2174
2175static void adev_close_output_stream(struct audio_hw_device *dev,
2176 struct audio_stream_out *stream)
2177{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002178 struct stream_out *out = (struct stream_out *)stream;
2179 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002180 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002181
Eric Laurent994a6932013-07-17 11:51:42 -07002182 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002183 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2184 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2185 if(ret != 0)
2186 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2187 __func__, ret);
2188 }
2189 else
2190 out_standby(&stream->common);
2191
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002192 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2193 destroy_offload_callback_thread(out);
2194
2195 if (out->compr_config.codec != NULL)
2196 free(out->compr_config.codec);
2197 }
2198 pthread_cond_destroy(&out->cond);
2199 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002200 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002201 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002202}
2203
2204static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2205{
2206 struct audio_device *adev = (struct audio_device *)dev;
2207 struct str_parms *parms;
2208 char *str;
2209 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002210 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002211 int ret;
2212
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002213 ALOGD("%s: enter: %s", __func__, kvpairs);
2214
2215 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002216 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002217
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002218 voice_set_parameters(adev, parms);
2219 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002220
2221 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2222 if (ret >= 0) {
2223 /* When set to false, HAL should disable EC and NS
2224 * But it is currently not supported.
2225 */
2226 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2227 adev->bluetooth_nrec = true;
2228 else
2229 adev->bluetooth_nrec = false;
2230 }
2231
2232 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2233 if (ret >= 0) {
2234 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2235 adev->screen_off = false;
2236 else
2237 adev->screen_off = true;
2238 }
2239
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002240 ret = str_parms_get_int(parms, "rotation", &val);
2241 if (ret >= 0) {
2242 bool reverse_speakers = false;
2243 switch(val) {
2244 // FIXME: note that the code below assumes that the speakers are in the correct placement
2245 // relative to the user when the device is rotated 90deg from its default rotation. This
2246 // assumption is device-specific, not platform-specific like this code.
2247 case 270:
2248 reverse_speakers = true;
2249 break;
2250 case 0:
2251 case 90:
2252 case 180:
2253 break;
2254 default:
2255 ALOGE("%s: unexpected rotation of %d", __func__, val);
2256 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002257 if (adev->speaker_lr_swap != reverse_speakers) {
2258 adev->speaker_lr_swap = reverse_speakers;
2259 // only update the selected device if there is active pcm playback
2260 struct audio_usecase *usecase;
2261 struct listnode *node;
2262 list_for_each(node, &adev->usecase_list) {
2263 usecase = node_to_item(node, struct audio_usecase, list);
2264 if (usecase->type == PCM_PLAYBACK) {
2265 select_devices(adev, usecase->id);
2266 break;
2267 }
2268 }
2269 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002270 }
2271
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002272 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002273 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002274
2275 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002276 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002277 return ret;
2278}
2279
2280static char* adev_get_parameters(const struct audio_hw_device *dev,
2281 const char *keys)
2282{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002283 struct audio_device *adev = (struct audio_device *)dev;
2284 struct str_parms *reply = str_parms_create();
2285 struct str_parms *query = str_parms_create_str(keys);
2286 char *str;
2287
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002288 pthread_mutex_lock(&adev->lock);
2289
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002290 audio_extn_get_parameters(adev, query, reply);
2291 platform_get_parameters(adev->platform, query, reply);
2292 str = str_parms_to_str(reply);
2293 str_parms_destroy(query);
2294 str_parms_destroy(reply);
2295
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002296 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002297 ALOGV("%s: exit: returns - %s", __func__, str);
2298 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002299}
2300
2301static int adev_init_check(const struct audio_hw_device *dev)
2302{
2303 return 0;
2304}
2305
2306static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2307{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002308 int ret;
2309 struct audio_device *adev = (struct audio_device *)dev;
2310 pthread_mutex_lock(&adev->lock);
2311 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002312 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002313 pthread_mutex_unlock(&adev->lock);
2314 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002315}
2316
2317static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2318{
2319 return -ENOSYS;
2320}
2321
2322static int adev_get_master_volume(struct audio_hw_device *dev,
2323 float *volume)
2324{
2325 return -ENOSYS;
2326}
2327
2328static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2329{
2330 return -ENOSYS;
2331}
2332
2333static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2334{
2335 return -ENOSYS;
2336}
2337
2338static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2339{
2340 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002341 pthread_mutex_lock(&adev->lock);
2342 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002343 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002344 adev->mode = mode;
2345 }
2346 pthread_mutex_unlock(&adev->lock);
2347 return 0;
2348}
2349
2350static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2351{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002352 int ret;
2353
2354 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002355 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002356 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2357 pthread_mutex_unlock(&adev->lock);
2358
2359 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002360}
2361
2362static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2363{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002364 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002365 return 0;
2366}
2367
2368static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2369 const struct audio_config *config)
2370{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002371 int channel_count = popcount(config->channel_mask);
2372
2373 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2374}
2375
2376static int adev_open_input_stream(struct audio_hw_device *dev,
2377 audio_io_handle_t handle,
2378 audio_devices_t devices,
2379 struct audio_config *config,
2380 struct audio_stream_in **stream_in)
2381{
2382 struct audio_device *adev = (struct audio_device *)dev;
2383 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002384 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002385 int channel_count = popcount(config->channel_mask);
2386
Eric Laurent994a6932013-07-17 11:51:42 -07002387 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002388 *stream_in = NULL;
2389 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2390 return -EINVAL;
2391
2392 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2393
2394 in->stream.common.get_sample_rate = in_get_sample_rate;
2395 in->stream.common.set_sample_rate = in_set_sample_rate;
2396 in->stream.common.get_buffer_size = in_get_buffer_size;
2397 in->stream.common.get_channels = in_get_channels;
2398 in->stream.common.get_format = in_get_format;
2399 in->stream.common.set_format = in_set_format;
2400 in->stream.common.standby = in_standby;
2401 in->stream.common.dump = in_dump;
2402 in->stream.common.set_parameters = in_set_parameters;
2403 in->stream.common.get_parameters = in_get_parameters;
2404 in->stream.common.add_audio_effect = in_add_audio_effect;
2405 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2406 in->stream.set_gain = in_set_gain;
2407 in->stream.read = in_read;
2408 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2409
2410 in->device = devices;
2411 in->source = AUDIO_SOURCE_DEFAULT;
2412 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002413 in->standby = 1;
2414 in->channel_mask = config->channel_mask;
2415
2416 /* Update config params with the requested sample rate and channels */
2417 in->usecase = USECASE_AUDIO_RECORD;
2418 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002419 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002420 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002421
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002422 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2423 (voice_extn_compress_voip_is_format_supported(in->format))) {
2424 ret = voice_extn_compress_voip_open_input_stream(in);
2425 if (ret != 0)
2426 {
2427 ALOGE("%s: Compress voip input cannot be opened, error:%d",
2428 __func__, ret);
2429 goto err_open;
2430 }
Mingming Yine62d7842013-10-25 16:26:03 -07002431 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002432 if(audio_extn_ssr_get_enabled()) {
2433 if(audio_extn_ssr_init(adev, in)) {
2434 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2435 ret = -EINVAL;
2436 goto err_open;
2437 }
2438 } else {
2439 ret = -EINVAL;
2440 goto err_open;
2441 }
Mingming Yine62d7842013-10-25 16:26:03 -07002442 } else if (audio_extn_compr_cap_enabled() &&
2443 audio_extn_compr_cap_format_supported(config->format)) {
2444 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002445 } else {
2446 in->config.channels = channel_count;
2447 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2448 buffer_size = get_input_buffer_size(config->sample_rate,
2449 config->format,
2450 channel_count);
2451 in->config.period_size = buffer_size / frame_size;
2452 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002453
2454 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002455 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002456 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002457
2458err_open:
2459 free(in);
2460 *stream_in = NULL;
2461 return ret;
2462}
2463
2464static void adev_close_input_stream(struct audio_hw_device *dev,
2465 struct audio_stream_in *stream)
2466{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002467 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002468 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002469 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002470
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002471 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2472 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2473 if (ret != 0)
2474 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2475 __func__, ret);
2476 } else
2477 in_standby(&stream->common);
2478
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002479 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2480 audio_extn_ssr_deinit();
2481 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002482 free(stream);
2483
Mingming Yine62d7842013-10-25 16:26:03 -07002484 if(audio_extn_compr_cap_enabled() &&
2485 audio_extn_compr_cap_format_supported(in->config.format))
2486 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002487 return;
2488}
2489
2490static int adev_dump(const audio_hw_device_t *device, int fd)
2491{
2492 return 0;
2493}
2494
2495static int adev_close(hw_device_t *device)
2496{
2497 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002498
2499 if (!adev)
2500 return 0;
2501
2502 pthread_mutex_lock(&adev_init_lock);
2503
2504 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002505 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07002506 audio_route_free(adev->audio_route);
2507 free(adev->snd_dev_ref_cnt);
2508 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002509 free(device);
2510 adev = NULL;
2511 }
2512 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002513 return 0;
2514}
2515
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002516static int adev_open(const hw_module_t *module, const char *name,
2517 hw_device_t **device)
2518{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002519 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002520
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002521 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002522 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2523
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002524 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002525 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002526 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002527 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002528 ALOGD("%s: returning existing instance of adev", __func__);
2529 ALOGD("%s: exit", __func__);
2530 pthread_mutex_unlock(&adev_init_lock);
2531 return 0;
2532 }
2533
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002534 adev = calloc(1, sizeof(struct audio_device));
2535
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002536 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2537 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2538 adev->device.common.module = (struct hw_module_t *)module;
2539 adev->device.common.close = adev_close;
2540
2541 adev->device.init_check = adev_init_check;
2542 adev->device.set_voice_volume = adev_set_voice_volume;
2543 adev->device.set_master_volume = adev_set_master_volume;
2544 adev->device.get_master_volume = adev_get_master_volume;
2545 adev->device.set_master_mute = adev_set_master_mute;
2546 adev->device.get_master_mute = adev_get_master_mute;
2547 adev->device.set_mode = adev_set_mode;
2548 adev->device.set_mic_mute = adev_set_mic_mute;
2549 adev->device.get_mic_mute = adev_get_mic_mute;
2550 adev->device.set_parameters = adev_set_parameters;
2551 adev->device.get_parameters = adev_get_parameters;
2552 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2553 adev->device.open_output_stream = adev_open_output_stream;
2554 adev->device.close_output_stream = adev_close_output_stream;
2555 adev->device.open_input_stream = adev_open_input_stream;
2556 adev->device.close_input_stream = adev_close_input_stream;
2557 adev->device.dump = adev_dump;
2558
2559 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002560 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002561 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002562 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002563 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002564 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002565 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002566 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002567 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002568 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002569 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002570
2571 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002572 adev->platform = platform_init(adev);
2573 if (!adev->platform) {
2574 free(adev->snd_dev_ref_cnt);
2575 free(adev);
2576 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2577 *device = NULL;
2578 return -EINVAL;
2579 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002580
2581 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2582 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2583 if (adev->visualizer_lib == NULL) {
2584 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2585 } else {
2586 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2587 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002588 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002589 "visualizer_hal_start_output");
2590 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002591 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002592 "visualizer_hal_stop_output");
2593 }
2594 }
Kiran Kandide144c82013-11-20 15:58:32 -08002595 audio_extn_listen_init(adev, SOUND_CARD);
Eric Laurentc4aef752013-09-12 17:45:53 -07002596
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002597 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2598 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2599 if (adev->offload_effects_lib == NULL) {
2600 ALOGE("%s: DLOPEN failed for %s", __func__,
2601 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2602 } else {
2603 ALOGV("%s: DLOPEN successful for %s", __func__,
2604 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2605 adev->offload_effects_start_output =
2606 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2607 "offload_effects_bundle_hal_start_output");
2608 adev->offload_effects_stop_output =
2609 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2610 "offload_effects_bundle_hal_stop_output");
2611 }
2612 }
2613
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002614 *device = &adev->device.common;
2615
Kiran Kandi910e1862013-10-29 13:29:42 -07002616 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002617 pthread_mutex_unlock(&adev_init_lock);
2618
Eric Laurent994a6932013-07-17 11:51:42 -07002619 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002620 return 0;
2621}
2622
2623static struct hw_module_methods_t hal_module_methods = {
2624 .open = adev_open,
2625};
2626
2627struct audio_module HAL_MODULE_INFO_SYM = {
2628 .common = {
2629 .tag = HARDWARE_MODULE_TAG,
2630 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2631 .hal_api_version = HARDWARE_HAL_API_VERSION,
2632 .id = AUDIO_HARDWARE_MODULE_ID,
2633 .name = "QCOM Audio HAL",
2634 .author = "Code Aurora Forum",
2635 .methods = &hal_module_methods,
2636 },
2637};