blob: cd730949047cc46bdd7f0a8f7325af2ba9cbb050 [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));
Helen Zeng067b96b2013-11-26 12:10:29 -0800614 if ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
615 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700616 in_snd_device = vc_usecase->in_snd_device;
617 out_snd_device = vc_usecase->out_snd_device;
618 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800619 } else if (voice_extn_compress_voip_is_active(adev)) {
620 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
621 if (voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
622 in_snd_device = voip_usecase->in_snd_device;
623 out_snd_device = voip_usecase->out_snd_device;
624 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700625 }
626 if (usecase->type == PCM_PLAYBACK) {
627 usecase->devices = usecase->stream.out->devices;
628 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700629 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700630 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700631 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700632 if (usecase->stream.out == adev->primary_output &&
633 adev->active_input &&
634 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
635 select_devices(adev, adev->active_input->usecase);
636 }
637 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700638 } else if (usecase->type == PCM_CAPTURE) {
639 usecase->devices = usecase->stream.in->device;
640 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700641 if (in_snd_device == SND_DEVICE_NONE) {
642 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
643 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700644 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700645 adev->primary_output->devices);
646 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700647 in_snd_device = platform_get_input_snd_device(adev->platform,
648 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700649 }
650 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700651 }
652 }
653
654 if (out_snd_device == usecase->out_snd_device &&
655 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800656 return 0;
657 }
658
sangwoobc677242013-08-08 16:53:43 +0900659 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700660 out_snd_device, platform_get_snd_device_name(out_snd_device),
661 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800662
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800663 /*
664 * Limitation: While in call, to do a device switch we need to disable
665 * and enable both RX and TX devices though one of them is same as current
666 * device.
667 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800668 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700669 status = platform_switch_voice_call_device_pre(adev->platform);
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800670 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800671 }
672
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700673 /* Disable current sound devices */
674 if (usecase->out_snd_device != SND_DEVICE_NONE) {
675 disable_audio_route(adev, usecase, true);
676 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800677 }
678
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700679 if (usecase->in_snd_device != SND_DEVICE_NONE) {
680 disable_audio_route(adev, usecase, true);
681 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800682 }
683
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700684 /* Enable new sound devices */
685 if (out_snd_device != SND_DEVICE_NONE) {
686 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
687 check_usecases_codec_backend(adev, usecase, out_snd_device);
688 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800689 }
690
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700691 if (in_snd_device != SND_DEVICE_NONE) {
692 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700693 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700694 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700695
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800696 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700697 status = platform_switch_voice_call_device_post(adev->platform,
698 out_snd_device,
699 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800700
sangwoo170731f2013-06-08 15:36:36 +0900701 audio_route_update_mixer(adev->audio_route);
702
703 usecase->in_snd_device = in_snd_device;
704 usecase->out_snd_device = out_snd_device;
705
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800706 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
707 enable_all_usecases_of_type(adev, usecase->type, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700708 else
709 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900710
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800711 /* Applicable only on the targets that has external modem.
712 * Enable device command should be sent to modem only after
713 * enabling voice call mixer controls
714 */
715 if (usecase->type == VOICE_CALL)
716 status = platform_switch_voice_call_usecase_route_post(adev->platform,
717 out_snd_device,
718 in_snd_device);
719
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800720 return status;
721}
722
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800723static int stop_input_stream(struct stream_in *in)
724{
725 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800726 struct audio_usecase *uc_info;
727 struct audio_device *adev = in->dev;
728
Eric Laurentc8400632013-02-14 19:04:54 -0800729 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800730
Eric Laurent994a6932013-07-17 11:51:42 -0700731 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700732 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800733 uc_info = get_usecase_from_list(adev, in->usecase);
734 if (uc_info == NULL) {
735 ALOGE("%s: Could not find the usecase (%d) in the list",
736 __func__, in->usecase);
737 return -EINVAL;
738 }
739
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800740 /* Close in-call recording streams */
741 voice_check_and_stop_incall_rec_usecase(adev, in);
742
Eric Laurent150dbfe2013-02-27 14:31:02 -0800743 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700744 disable_audio_route(adev, uc_info, true);
745
746 /* 2. Disable the tx device */
747 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800748
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800749 list_remove(&uc_info->list);
750 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800751
Eric Laurent994a6932013-07-17 11:51:42 -0700752 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800753 return ret;
754}
755
756int start_input_stream(struct stream_in *in)
757{
758 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800759 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800760 struct audio_usecase *uc_info;
761 struct audio_device *adev = in->dev;
762
Mingming Yine62d7842013-10-25 16:26:03 -0700763 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Eric Laurent994a6932013-07-17 11:51:42 -0700764 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700765
766 /* Check if source matches incall recording usecase criteria */
767 ret = voice_check_and_set_incall_rec_usecase(adev, in);
768 if (ret)
769 goto error_config;
770 else
771 ALOGV("%s: usecase(%d)", __func__, in->usecase);
772
Eric Laurentb23d5282013-05-14 15:27:20 -0700773 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800774 if (in->pcm_device_id < 0) {
775 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
776 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800777 ret = -EINVAL;
778 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800779 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700780
781 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800782 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
783 uc_info->id = in->usecase;
784 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800785 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700786 uc_info->devices = in->device;
787 uc_info->in_snd_device = SND_DEVICE_NONE;
788 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800789
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800790 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700791 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800792
Eric Laurentc8400632013-02-14 19:04:54 -0800793 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
794 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800795 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
796 PCM_IN, &in->config);
797 if (in->pcm && !pcm_is_ready(in->pcm)) {
798 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
799 pcm_close(in->pcm);
800 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800801 ret = -EIO;
802 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800803 }
Eric Laurent994a6932013-07-17 11:51:42 -0700804 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800805 return ret;
806
807error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800808 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800809
810error_config:
811 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700812 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800813
814 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800815}
816
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700817/* must be called with out->lock locked */
818static int send_offload_cmd_l(struct stream_out* out, int command)
819{
820 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
821
822 ALOGVV("%s %d", __func__, command);
823
824 cmd->cmd = command;
825 list_add_tail(&out->offload_cmd_list, &cmd->node);
826 pthread_cond_signal(&out->offload_cond);
827 return 0;
828}
829
830/* must be called iwth out->lock locked */
831static void stop_compressed_output_l(struct stream_out *out)
832{
833 out->offload_state = OFFLOAD_STATE_IDLE;
834 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700835 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700836 if (out->compr != NULL) {
837 compress_stop(out->compr);
838 while (out->offload_thread_blocked) {
839 pthread_cond_wait(&out->cond, &out->lock);
840 }
841 }
842}
843
844static void *offload_thread_loop(void *context)
845{
846 struct stream_out *out = (struct stream_out *) context;
847 struct listnode *item;
848
849 out->offload_state = OFFLOAD_STATE_IDLE;
850 out->playback_started = 0;
851
852 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
853 set_sched_policy(0, SP_FOREGROUND);
854 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
855
856 ALOGV("%s", __func__);
857 pthread_mutex_lock(&out->lock);
858 for (;;) {
859 struct offload_cmd *cmd = NULL;
860 stream_callback_event_t event;
861 bool send_callback = false;
862
863 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
864 __func__, list_empty(&out->offload_cmd_list),
865 out->offload_state);
866 if (list_empty(&out->offload_cmd_list)) {
867 ALOGV("%s SLEEPING", __func__);
868 pthread_cond_wait(&out->offload_cond, &out->lock);
869 ALOGV("%s RUNNING", __func__);
870 continue;
871 }
872
873 item = list_head(&out->offload_cmd_list);
874 cmd = node_to_item(item, struct offload_cmd, node);
875 list_remove(item);
876
877 ALOGVV("%s STATE %d CMD %d out->compr %p",
878 __func__, out->offload_state, cmd->cmd, out->compr);
879
880 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
881 free(cmd);
882 break;
883 }
884
885 if (out->compr == NULL) {
886 ALOGE("%s: Compress handle is NULL", __func__);
887 pthread_cond_signal(&out->cond);
888 continue;
889 }
890 out->offload_thread_blocked = true;
891 pthread_mutex_unlock(&out->lock);
892 send_callback = false;
893 switch(cmd->cmd) {
894 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
895 compress_wait(out->compr, -1);
896 send_callback = true;
897 event = STREAM_CBK_EVENT_WRITE_READY;
898 break;
899 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700900 compress_next_track(out->compr);
901 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700902 send_callback = true;
903 event = STREAM_CBK_EVENT_DRAIN_READY;
904 break;
905 case OFFLOAD_CMD_DRAIN:
906 compress_drain(out->compr);
907 send_callback = true;
908 event = STREAM_CBK_EVENT_DRAIN_READY;
909 break;
910 default:
911 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
912 break;
913 }
914 pthread_mutex_lock(&out->lock);
915 out->offload_thread_blocked = false;
916 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700917 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700918 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700919 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700920 free(cmd);
921 }
922
923 pthread_cond_signal(&out->cond);
924 while (!list_empty(&out->offload_cmd_list)) {
925 item = list_head(&out->offload_cmd_list);
926 list_remove(item);
927 free(node_to_item(item, struct offload_cmd, node));
928 }
929 pthread_mutex_unlock(&out->lock);
930
931 return NULL;
932}
933
934static int create_offload_callback_thread(struct stream_out *out)
935{
936 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
937 list_init(&out->offload_cmd_list);
938 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
939 offload_thread_loop, out);
940 return 0;
941}
942
943static int destroy_offload_callback_thread(struct stream_out *out)
944{
945 pthread_mutex_lock(&out->lock);
946 stop_compressed_output_l(out);
947 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
948
949 pthread_mutex_unlock(&out->lock);
950 pthread_join(out->offload_thread, (void **) NULL);
951 pthread_cond_destroy(&out->offload_cond);
952
953 return 0;
954}
955
Eric Laurent07eeafd2013-10-06 12:52:49 -0700956static bool allow_hdmi_channel_config(struct audio_device *adev)
957{
958 struct listnode *node;
959 struct audio_usecase *usecase;
960 bool ret = true;
961
962 list_for_each(node, &adev->usecase_list) {
963 usecase = node_to_item(node, struct audio_usecase, list);
964 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
965 /*
966 * If voice call is already existing, do not proceed further to avoid
967 * disabling/enabling both RX and TX devices, CSD calls, etc.
968 * Once the voice call done, the HDMI channels can be configured to
969 * max channels of remaining use cases.
970 */
971 if (usecase->id == USECASE_VOICE_CALL) {
972 ALOGD("%s: voice call is active, no change in HDMI channels",
973 __func__);
974 ret = false;
975 break;
976 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
977 ALOGD("%s: multi channel playback is active, "
978 "no change in HDMI channels", __func__);
979 ret = false;
980 break;
981 }
982 }
983 }
984 return ret;
985}
986
987static int check_and_set_hdmi_channels(struct audio_device *adev,
988 unsigned int channels)
989{
990 struct listnode *node;
991 struct audio_usecase *usecase;
992
993 /* Check if change in HDMI channel config is allowed */
994 if (!allow_hdmi_channel_config(adev))
995 return 0;
996
997 if (channels == adev->cur_hdmi_channels) {
998 ALOGD("%s: Requested channels are same as current", __func__);
999 return 0;
1000 }
1001
1002 platform_set_hdmi_channels(adev->platform, channels);
1003 adev->cur_hdmi_channels = channels;
1004
1005 /*
1006 * Deroute all the playback streams routed to HDMI so that
1007 * the back end is deactivated. Note that backend will not
1008 * be deactivated if any one stream is connected to it.
1009 */
1010 list_for_each(node, &adev->usecase_list) {
1011 usecase = node_to_item(node, struct audio_usecase, list);
1012 if (usecase->type == PCM_PLAYBACK &&
1013 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1014 disable_audio_route(adev, usecase, true);
1015 }
1016 }
1017
1018 /*
1019 * Enable all the streams disabled above. Now the HDMI backend
1020 * will be activated with new channel configuration
1021 */
1022 list_for_each(node, &adev->usecase_list) {
1023 usecase = node_to_item(node, struct audio_usecase, list);
1024 if (usecase->type == PCM_PLAYBACK &&
1025 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1026 enable_audio_route(adev, usecase, true);
1027 }
1028 }
1029
1030 return 0;
1031}
1032
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001033static int stop_output_stream(struct stream_out *out)
1034{
1035 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001036 struct audio_usecase *uc_info;
1037 struct audio_device *adev = out->dev;
1038
Eric Laurent994a6932013-07-17 11:51:42 -07001039 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001040 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001041 uc_info = get_usecase_from_list(adev, out->usecase);
1042 if (uc_info == NULL) {
1043 ALOGE("%s: Could not find the usecase (%d) in the list",
1044 __func__, out->usecase);
1045 return -EINVAL;
1046 }
1047
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001048 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1049 if (adev->visualizer_stop_output != NULL)
1050 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1051 if (adev->offload_effects_stop_output != NULL)
1052 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1053 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001054
Eric Laurent150dbfe2013-02-27 14:31:02 -08001055 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001056 disable_audio_route(adev, uc_info, true);
1057
1058 /* 2. Disable the rx device */
1059 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001060
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001061 list_remove(&uc_info->list);
1062 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001063
Eric Laurent07eeafd2013-10-06 12:52:49 -07001064 /* Must be called after removing the usecase from list */
1065 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1066 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1067
Eric Laurent994a6932013-07-17 11:51:42 -07001068 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001069 return ret;
1070}
1071
1072int start_output_stream(struct stream_out *out)
1073{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001074 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001075 struct audio_usecase *uc_info;
1076 struct audio_device *adev = out->dev;
1077
Eric Laurent994a6932013-07-17 11:51:42 -07001078 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001079 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001080 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001081 if (out->pcm_device_id < 0) {
1082 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1083 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001084 ret = -EINVAL;
1085 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001086 }
1087
1088 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1089 uc_info->id = out->usecase;
1090 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001091 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001092 uc_info->devices = out->devices;
1093 uc_info->in_snd_device = SND_DEVICE_NONE;
1094 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001095
Eric Laurent07eeafd2013-10-06 12:52:49 -07001096 /* This must be called before adding this usecase to the list */
1097 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1098 check_and_set_hdmi_channels(adev, out->config.channels);
1099
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001100 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001101
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001102 select_devices(adev, out->usecase);
1103
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001104 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1105 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001106 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1107 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001108 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001109 if (out->pcm && !pcm_is_ready(out->pcm)) {
1110 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1111 pcm_close(out->pcm);
1112 out->pcm = NULL;
1113 ret = -EIO;
1114 goto error_open;
1115 }
1116 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001117 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001118 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1119 COMPRESS_IN, &out->compr_config);
1120 if (out->compr && !is_compress_ready(out->compr)) {
1121 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1122 compress_close(out->compr);
1123 out->compr = NULL;
1124 ret = -EIO;
1125 goto error_open;
1126 }
1127 if (out->offload_callback)
1128 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001129
1130 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001131 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1132 if (adev->offload_effects_start_output != NULL)
1133 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001134 }
Eric Laurent994a6932013-07-17 11:51:42 -07001135 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001136 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001137error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001138 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001139error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001140 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001141}
1142
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001143static int check_input_parameters(uint32_t sample_rate,
1144 audio_format_t format,
1145 int channel_count)
1146{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001147 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001148
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001149 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001150 !voice_extn_compress_voip_is_format_supported(format) &&
1151 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001152
1153 switch (channel_count) {
1154 case 1:
1155 case 2:
1156 case 6:
1157 break;
1158 default:
1159 ret = -EINVAL;
1160 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001161
1162 switch (sample_rate) {
1163 case 8000:
1164 case 11025:
1165 case 12000:
1166 case 16000:
1167 case 22050:
1168 case 24000:
1169 case 32000:
1170 case 44100:
1171 case 48000:
1172 break;
1173 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001174 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001175 }
1176
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001177 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001178}
1179
1180static size_t get_input_buffer_size(uint32_t sample_rate,
1181 audio_format_t format,
1182 int channel_count)
1183{
1184 size_t size = 0;
1185
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001186 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1187 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001188
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001189 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1190 /* ToDo: should use frame_size computed based on the format and
1191 channel_count here. */
1192 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001193
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001194 /* make sure the size is multiple of 64 */
1195 size += 0x3f;
1196 size &= ~0x3f;
1197
1198 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001199}
1200
1201static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1202{
1203 struct stream_out *out = (struct stream_out *)stream;
1204
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001205 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001206}
1207
1208static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1209{
1210 return -ENOSYS;
1211}
1212
1213static size_t out_get_buffer_size(const struct audio_stream *stream)
1214{
1215 struct stream_out *out = (struct stream_out *)stream;
1216
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001217 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001218 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001219 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1220 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001221
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001222 return out->config.period_size * audio_stream_frame_size(stream);
1223}
1224
1225static uint32_t out_get_channels(const struct audio_stream *stream)
1226{
1227 struct stream_out *out = (struct stream_out *)stream;
1228
1229 return out->channel_mask;
1230}
1231
1232static audio_format_t out_get_format(const struct audio_stream *stream)
1233{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001234 struct stream_out *out = (struct stream_out *)stream;
1235
1236 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001237}
1238
1239static int out_set_format(struct audio_stream *stream, audio_format_t format)
1240{
1241 return -ENOSYS;
1242}
1243
1244static int out_standby(struct audio_stream *stream)
1245{
1246 struct stream_out *out = (struct stream_out *)stream;
1247 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001248
Eric Laurent994a6932013-07-17 11:51:42 -07001249 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001250 out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001251 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1252 /* Ignore standby in case of voip call because the voip output
1253 * stream is closed in adev_close_output_stream()
1254 */
1255 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1256 return 0;
1257 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001258
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001259 pthread_mutex_lock(&out->lock);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001260 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001261 if (!out->standby) {
1262 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001263 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1264 if (out->pcm) {
1265 pcm_close(out->pcm);
1266 out->pcm = NULL;
1267 }
1268 } else {
1269 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001270 out->gapless_mdata.encoder_delay = 0;
1271 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001272 if (out->compr != NULL) {
1273 compress_close(out->compr);
1274 out->compr = NULL;
1275 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001276 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277 stop_output_stream(out);
1278 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001279 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001280 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001281 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001282 return 0;
1283}
1284
1285static int out_dump(const struct audio_stream *stream, int fd)
1286{
1287 return 0;
1288}
1289
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001290static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1291{
1292 int ret = 0;
1293 char value[32];
1294 struct compr_gapless_mdata tmp_mdata;
1295
1296 if (!out || !parms) {
1297 return -EINVAL;
1298 }
1299
1300 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1301 if (ret >= 0) {
1302 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1303 } else {
1304 return -EINVAL;
1305 }
1306
1307 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1308 if (ret >= 0) {
1309 tmp_mdata.encoder_padding = atoi(value);
1310 } else {
1311 return -EINVAL;
1312 }
1313
1314 out->gapless_mdata = tmp_mdata;
1315 out->send_new_metadata = 1;
1316 ALOGV("%s new encoder delay %u and padding %u", __func__,
1317 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1318
1319 return 0;
1320}
1321
1322
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1324{
1325 struct stream_out *out = (struct stream_out *)stream;
1326 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001327 struct audio_usecase *usecase;
1328 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001329 struct str_parms *parms;
1330 char value[32];
1331 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001332 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001333
sangwoobc677242013-08-08 16:53:43 +09001334 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001335 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001336 parms = str_parms_create_str(kvpairs);
1337 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1338 if (ret >= 0) {
1339 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001340 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001341 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001342
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001343 /*
1344 * When HDMI cable is unplugged the music playback is paused and
1345 * the policy manager sends routing=0. But the audioflinger
1346 * continues to write data until standby time (3sec).
1347 * As the HDMI core is turned off, the write gets blocked.
1348 * Avoid this by routing audio to speaker until standby.
1349 */
1350 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1351 val == AUDIO_DEVICE_NONE) {
1352 val = AUDIO_DEVICE_OUT_SPEAKER;
1353 }
1354
1355 /*
1356 * select_devices() call below switches all the usecases on the same
1357 * backend to the new device. Refer to check_usecases_codec_backend() in
1358 * the select_devices(). But how do we undo this?
1359 *
1360 * For example, music playback is active on headset (deep-buffer usecase)
1361 * and if we go to ringtones and select a ringtone, low-latency usecase
1362 * will be started on headset+speaker. As we can't enable headset+speaker
1363 * and headset devices at the same time, select_devices() switches the music
1364 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1365 * So when the ringtone playback is completed, how do we undo the same?
1366 *
1367 * We are relying on the out_set_parameters() call on deep-buffer output,
1368 * once the ringtone playback is ended.
1369 * NOTE: We should not check if the current devices are same as new devices.
1370 * Because select_devices() must be called to switch back the music
1371 * playback to headset.
1372 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001373 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001374 out->devices = val;
1375
1376 if (!out->standby)
1377 select_devices(adev, out->usecase);
1378
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001379 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1380 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001381 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001382 voice_start_call(adev);
1383 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1384 voice_is_in_call(adev) &&
1385 (out == adev->primary_output)) {
1386 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001387 }
1388 }
1389
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001390 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1391 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001392 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001393 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001394 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001395
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001396 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001397 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001398 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001399
1400 if (out == adev->primary_output) {
1401 pthread_mutex_lock(&adev->lock);
1402 audio_extn_set_parameters(adev, parms);
1403 pthread_mutex_unlock(&adev->lock);
1404 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001405 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1406 parse_compress_metadata(out, parms);
1407 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001408
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001410 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001411 return ret;
1412}
1413
1414static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1415{
1416 struct stream_out *out = (struct stream_out *)stream;
1417 struct str_parms *query = str_parms_create_str(keys);
1418 char *str;
1419 char value[256];
1420 struct str_parms *reply = str_parms_create();
1421 size_t i, j;
1422 int ret;
1423 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001424 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001425 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1426 if (ret >= 0) {
1427 value[0] = '\0';
1428 i = 0;
1429 while (out->supported_channel_masks[i] != 0) {
1430 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1431 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1432 if (!first) {
1433 strcat(value, "|");
1434 }
1435 strcat(value, out_channels_name_to_enum_table[j].name);
1436 first = false;
1437 break;
1438 }
1439 }
1440 i++;
1441 }
1442 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1443 str = str_parms_to_str(reply);
1444 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001445 voice_extn_out_get_parameters(out, query, reply);
1446 str = str_parms_to_str(reply);
1447 if (!strncmp(str, "", sizeof(""))) {
1448 str = strdup(keys);
1449 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001450 }
1451 str_parms_destroy(query);
1452 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001453 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001454 return str;
1455}
1456
1457static uint32_t out_get_latency(const struct audio_stream_out *stream)
1458{
1459 struct stream_out *out = (struct stream_out *)stream;
1460
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001461 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1462 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1463
1464 return (out->config.period_count * out->config.period_size * 1000) /
1465 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001466}
1467
1468static int out_set_volume(struct audio_stream_out *stream, float left,
1469 float right)
1470{
Eric Laurenta9024de2013-04-04 09:19:12 -07001471 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001472 int volume[2];
1473
Eric Laurenta9024de2013-04-04 09:19:12 -07001474 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1475 /* only take left channel into account: the API is for stereo anyway */
1476 out->muted = (left == 0.0f);
1477 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001478 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001479 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001480 struct audio_device *adev = out->dev;
1481 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001482 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1483 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001484
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001485 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1486 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001487 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1488 if (!ctl) {
1489 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1490 __func__, mixer_ctl_name);
1491 return -EINVAL;
1492 }
1493 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1494 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1495 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1496 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001497 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001498
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001499 return -ENOSYS;
1500}
1501
1502static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1503 size_t bytes)
1504{
1505 struct stream_out *out = (struct stream_out *)stream;
1506 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001507 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001508
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001509 pthread_mutex_lock(&out->lock);
1510 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001511 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001512 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001513 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1514 ret = voice_extn_compress_voip_start_output_stream(out);
1515 else
1516 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001517 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001518 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001519 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001520 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001521 goto exit;
1522 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001523 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001524
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001525 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001526 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1527 if (out->send_new_metadata) {
1528 ALOGVV("send new gapless metadata");
1529 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1530 out->send_new_metadata = 0;
1531 }
1532
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001533 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001534 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001535 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001536 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1537 }
1538 if (!out->playback_started) {
1539 compress_start(out->compr);
1540 out->playback_started = 1;
1541 out->offload_state = OFFLOAD_STATE_PLAYING;
1542 }
1543 pthread_mutex_unlock(&out->lock);
1544 return ret;
1545 } else {
1546 if (out->pcm) {
1547 if (out->muted)
1548 memset((void *)buffer, 0, bytes);
1549 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1550 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001551 if (ret == 0)
1552 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001553 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001554 }
1555
1556exit:
1557 pthread_mutex_unlock(&out->lock);
1558
1559 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001560 if (out->pcm)
1561 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001562 out_standby(&out->stream.common);
1563 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1564 out_get_sample_rate(&out->stream.common));
1565 }
1566 return bytes;
1567}
1568
1569static int out_get_render_position(const struct audio_stream_out *stream,
1570 uint32_t *dsp_frames)
1571{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001572 struct stream_out *out = (struct stream_out *)stream;
1573 *dsp_frames = 0;
1574 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1575 pthread_mutex_lock(&out->lock);
1576 if (out->compr != NULL) {
1577 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1578 &out->sample_rate);
1579 ALOGVV("%s rendered frames %d sample_rate %d",
1580 __func__, *dsp_frames, out->sample_rate);
1581 }
1582 pthread_mutex_unlock(&out->lock);
1583 return 0;
1584 } else
1585 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001586}
1587
1588static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1589{
1590 return 0;
1591}
1592
1593static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1594{
1595 return 0;
1596}
1597
1598static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1599 int64_t *timestamp)
1600{
1601 return -EINVAL;
1602}
1603
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001604static int out_get_presentation_position(const struct audio_stream_out *stream,
1605 uint64_t *frames, struct timespec *timestamp)
1606{
1607 struct stream_out *out = (struct stream_out *)stream;
1608 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001609 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001610
1611 pthread_mutex_lock(&out->lock);
1612
Eric Laurent949a0892013-09-20 09:20:13 -07001613 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1614 if (out->compr != NULL) {
1615 compress_get_tstamp(out->compr, &dsp_frames,
1616 &out->sample_rate);
1617 ALOGVV("%s rendered frames %ld sample_rate %d",
1618 __func__, dsp_frames, out->sample_rate);
1619 *frames = dsp_frames;
1620 ret = 0;
1621 /* this is the best we can do */
1622 clock_gettime(CLOCK_MONOTONIC, timestamp);
1623 }
1624 } else {
1625 if (out->pcm) {
1626 size_t avail;
1627 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1628 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001629 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001630 // This adjustment accounts for buffering after app processor.
1631 // It is based on estimated DSP latency per use case, rather than exact.
1632 signed_frames -=
1633 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1634
Eric Laurent949a0892013-09-20 09:20:13 -07001635 // It would be unusual for this value to be negative, but check just in case ...
1636 if (signed_frames >= 0) {
1637 *frames = signed_frames;
1638 ret = 0;
1639 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001640 }
1641 }
1642 }
1643
1644 pthread_mutex_unlock(&out->lock);
1645
1646 return ret;
1647}
1648
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001649static int out_set_callback(struct audio_stream_out *stream,
1650 stream_callback_t callback, void *cookie)
1651{
1652 struct stream_out *out = (struct stream_out *)stream;
1653
1654 ALOGV("%s", __func__);
1655 pthread_mutex_lock(&out->lock);
1656 out->offload_callback = callback;
1657 out->offload_cookie = cookie;
1658 pthread_mutex_unlock(&out->lock);
1659 return 0;
1660}
1661
1662static int out_pause(struct audio_stream_out* stream)
1663{
1664 struct stream_out *out = (struct stream_out *)stream;
1665 int status = -ENOSYS;
1666 ALOGV("%s", __func__);
1667 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1668 pthread_mutex_lock(&out->lock);
1669 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1670 status = compress_pause(out->compr);
1671 out->offload_state = OFFLOAD_STATE_PAUSED;
1672 }
1673 pthread_mutex_unlock(&out->lock);
1674 }
1675 return status;
1676}
1677
1678static int out_resume(struct audio_stream_out* stream)
1679{
1680 struct stream_out *out = (struct stream_out *)stream;
1681 int status = -ENOSYS;
1682 ALOGV("%s", __func__);
1683 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1684 status = 0;
1685 pthread_mutex_lock(&out->lock);
1686 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1687 status = compress_resume(out->compr);
1688 out->offload_state = OFFLOAD_STATE_PLAYING;
1689 }
1690 pthread_mutex_unlock(&out->lock);
1691 }
1692 return status;
1693}
1694
1695static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1696{
1697 struct stream_out *out = (struct stream_out *)stream;
1698 int status = -ENOSYS;
1699 ALOGV("%s", __func__);
1700 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1701 pthread_mutex_lock(&out->lock);
1702 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1703 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1704 else
1705 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1706 pthread_mutex_unlock(&out->lock);
1707 }
1708 return status;
1709}
1710
1711static int out_flush(struct audio_stream_out* stream)
1712{
1713 struct stream_out *out = (struct stream_out *)stream;
1714 ALOGV("%s", __func__);
1715 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1716 pthread_mutex_lock(&out->lock);
1717 stop_compressed_output_l(out);
1718 pthread_mutex_unlock(&out->lock);
1719 return 0;
1720 }
1721 return -ENOSYS;
1722}
1723
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001724/** audio_stream_in implementation **/
1725static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1726{
1727 struct stream_in *in = (struct stream_in *)stream;
1728
1729 return in->config.rate;
1730}
1731
1732static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1733{
1734 return -ENOSYS;
1735}
1736
1737static size_t in_get_buffer_size(const struct audio_stream *stream)
1738{
1739 struct stream_in *in = (struct stream_in *)stream;
1740
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001741 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1742 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001743 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1744 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001745
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001746 return in->config.period_size * audio_stream_frame_size(stream);
1747}
1748
1749static uint32_t in_get_channels(const struct audio_stream *stream)
1750{
1751 struct stream_in *in = (struct stream_in *)stream;
1752
1753 return in->channel_mask;
1754}
1755
1756static audio_format_t in_get_format(const struct audio_stream *stream)
1757{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001758 struct stream_in *in = (struct stream_in *)stream;
1759
1760 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001761}
1762
1763static int in_set_format(struct audio_stream *stream, audio_format_t format)
1764{
1765 return -ENOSYS;
1766}
1767
1768static int in_standby(struct audio_stream *stream)
1769{
1770 struct stream_in *in = (struct stream_in *)stream;
1771 struct audio_device *adev = in->dev;
1772 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001773 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001774
1775 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1776 /* Ignore standby in case of voip call because the voip input
1777 * stream is closed in adev_close_input_stream()
1778 */
1779 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1780 return status;
1781 }
1782
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001783 pthread_mutex_lock(&in->lock);
1784 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001785 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001786 if (in->pcm) {
1787 pcm_close(in->pcm);
1788 in->pcm = NULL;
1789 }
1790 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001791 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001792 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001793 }
1794 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001795 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001796 return status;
1797}
1798
1799static int in_dump(const struct audio_stream *stream, int fd)
1800{
1801 return 0;
1802}
1803
1804static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1805{
1806 struct stream_in *in = (struct stream_in *)stream;
1807 struct audio_device *adev = in->dev;
1808 struct str_parms *parms;
1809 char *str;
1810 char value[32];
1811 int ret, val = 0;
1812
Eric Laurent994a6932013-07-17 11:51:42 -07001813 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001814 parms = str_parms_create_str(kvpairs);
1815
1816 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1817
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001818 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001819 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001820 if (ret >= 0) {
1821 val = atoi(value);
1822 /* no audio source uses val == 0 */
1823 if ((in->source != val) && (val != 0)) {
1824 in->source = val;
1825 }
1826 }
1827
1828 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1829 if (ret >= 0) {
1830 val = atoi(value);
1831 if ((in->device != val) && (val != 0)) {
1832 in->device = val;
1833 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001834 if (!in->standby)
1835 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001836 }
1837 }
1838
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001839 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001840 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001841
1842 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001843 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001844 return ret;
1845}
1846
1847static char* in_get_parameters(const struct audio_stream *stream,
1848 const char *keys)
1849{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001850 struct stream_in *in = (struct stream_in *)stream;
1851 struct str_parms *query = str_parms_create_str(keys);
1852 char *str;
1853 char value[256];
1854 struct str_parms *reply = str_parms_create();
1855 ALOGV("%s: enter: keys - %s", __func__, keys);
1856
1857 voice_extn_in_get_parameters(in, query, reply);
1858
1859 str = str_parms_to_str(reply);
1860 str_parms_destroy(query);
1861 str_parms_destroy(reply);
1862
1863 ALOGV("%s: exit: returns - %s", __func__, str);
1864 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001865}
1866
1867static int in_set_gain(struct audio_stream_in *stream, float gain)
1868{
1869 return 0;
1870}
1871
1872static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1873 size_t bytes)
1874{
1875 struct stream_in *in = (struct stream_in *)stream;
1876 struct audio_device *adev = in->dev;
1877 int i, ret = -1;
1878
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001879 pthread_mutex_lock(&in->lock);
1880 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001881 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001882 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
1883 ret = voice_extn_compress_voip_start_input_stream(in);
1884 else
1885 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001886 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001887 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001888 goto exit;
1889 }
1890 in->standby = 0;
1891 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001892
1893 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001894 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1895 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07001896 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
1897 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001898 else
1899 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001900 }
1901
1902 /*
1903 * Instead of writing zeroes here, we could trust the hardware
1904 * to always provide zeroes when muted.
1905 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001906 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001907 memset(buffer, 0, bytes);
1908
1909exit:
1910 pthread_mutex_unlock(&in->lock);
1911
1912 if (ret != 0) {
1913 in_standby(&in->stream.common);
1914 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1915 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1916 in_get_sample_rate(&in->stream.common));
1917 }
1918 return bytes;
1919}
1920
1921static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1922{
1923 return 0;
1924}
1925
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001926static int add_remove_audio_effect(const struct audio_stream *stream,
1927 effect_handle_t effect,
1928 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001929{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001930 struct stream_in *in = (struct stream_in *)stream;
1931 int status = 0;
1932 effect_descriptor_t desc;
1933
1934 status = (*effect)->get_descriptor(effect, &desc);
1935 if (status != 0)
1936 return status;
1937
1938 pthread_mutex_lock(&in->lock);
1939 pthread_mutex_lock(&in->dev->lock);
1940 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1941 in->enable_aec != enable &&
1942 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1943 in->enable_aec = enable;
1944 if (!in->standby)
1945 select_devices(in->dev, in->usecase);
1946 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08001947 if (in->enable_ns != enable &&
1948 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1949 in->enable_ns = enable;
1950 if (!in->standby)
1951 select_devices(in->dev, in->usecase);
1952 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001953 pthread_mutex_unlock(&in->dev->lock);
1954 pthread_mutex_unlock(&in->lock);
1955
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001956 return 0;
1957}
1958
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001959static int in_add_audio_effect(const struct audio_stream *stream,
1960 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001961{
Eric Laurent994a6932013-07-17 11:51:42 -07001962 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001963 return add_remove_audio_effect(stream, effect, true);
1964}
1965
1966static int in_remove_audio_effect(const struct audio_stream *stream,
1967 effect_handle_t effect)
1968{
Eric Laurent994a6932013-07-17 11:51:42 -07001969 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001970 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001971}
1972
1973static int adev_open_output_stream(struct audio_hw_device *dev,
1974 audio_io_handle_t handle,
1975 audio_devices_t devices,
1976 audio_output_flags_t flags,
1977 struct audio_config *config,
1978 struct audio_stream_out **stream_out)
1979{
1980 struct audio_device *adev = (struct audio_device *)dev;
1981 struct stream_out *out;
1982 int i, ret;
1983
Eric Laurent994a6932013-07-17 11:51:42 -07001984 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001985 __func__, config->sample_rate, config->channel_mask, devices, flags);
1986 *stream_out = NULL;
1987 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1988
1989 if (devices == AUDIO_DEVICE_NONE)
1990 devices = AUDIO_DEVICE_OUT_SPEAKER;
1991
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001992 out->flags = flags;
1993 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001994 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001995 out->format = config->format;
1996 out->sample_rate = config->sample_rate;
1997 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1998 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001999 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000
2001 /* Init use case and pcm_config */
Ravi Kumar Alamanda86b0e2b2013-11-20 14:52:30 -08002002 if (out->flags == AUDIO_OUTPUT_FLAG_DIRECT &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002003 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002004 pthread_mutex_lock(&adev->lock);
2005 ret = read_hdmi_channel_masks(out);
2006 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002007 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002008 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002009
2010 if (config->sample_rate == 0)
2011 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2012 if (config->channel_mask == 0)
2013 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2014
2015 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002016 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002017 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2018 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002019 out->config.rate = config->sample_rate;
2020 out->config.channels = popcount(out->channel_mask);
2021 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002022 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2023 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002024 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002025 ret = voice_extn_compress_voip_open_output_stream(out);
2026 if (ret != 0) {
2027 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2028 __func__, ret);
2029 goto error_open;
2030 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002031 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2032 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2033 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2034 ALOGE("%s: Unsupported Offload information", __func__);
2035 ret = -EINVAL;
2036 goto error_open;
2037 }
Mingming Yin90310102013-11-13 16:57:00 -08002038 if (!is_supported_format(config->offload_info.format) &&
2039 !audio_extn_dolby_is_supported_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002040 ALOGE("%s: Unsupported audio format", __func__);
2041 ret = -EINVAL;
2042 goto error_open;
2043 }
2044
2045 out->compr_config.codec = (struct snd_codec *)
2046 calloc(1, sizeof(struct snd_codec));
2047
2048 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2049 if (config->offload_info.channel_mask)
2050 out->channel_mask = config->offload_info.channel_mask;
2051 else if (config->channel_mask)
2052 out->channel_mask = config->channel_mask;
2053 out->format = config->offload_info.format;
2054 out->sample_rate = config->offload_info.sample_rate;
2055
2056 out->stream.set_callback = out_set_callback;
2057 out->stream.pause = out_pause;
2058 out->stream.resume = out_resume;
2059 out->stream.drain = out_drain;
2060 out->stream.flush = out_flush;
2061
Mingming Yin90310102013-11-13 16:57:00 -08002062 if (audio_extn_dolby_is_supported_format(config->offload_info.format))
2063 out->compr_config.codec->id =
2064 audio_extn_dolby_get_snd_codec_id(config->offload_info.format);
2065 else
2066 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002067 get_snd_codec_id(config->offload_info.format);
2068 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2069 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2070 out->compr_config.codec->sample_rate =
2071 compress_get_alsa_rate(config->offload_info.sample_rate);
2072 out->compr_config.codec->bit_rate =
2073 config->offload_info.bit_rate;
2074 out->compr_config.codec->ch_in =
2075 popcount(config->channel_mask);
2076 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2077
2078 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2079 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002080
2081 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002082 create_offload_callback_thread(out);
2083 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2084 __func__, config->offload_info.version,
2085 config->offload_info.bit_rate);
Mingming Yin90310102013-11-13 16:57:00 -08002086
2087 if (audio_extn_dolby_is_supported_format(out->format)) {
2088 ret = audio_extn_dolby_set_DMID(adev);
2089 if (ret != 0) {
2090 ALOGE("%s: Dolby DMID cannot be set error:%d",
2091 __func__, ret);
2092 goto error_open;
2093 }
2094 }
2095
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002096 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2097 ret = voice_check_and_set_incall_music_usecase(adev, out);
2098 if (ret != 0) {
2099 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2100 __func__, ret);
2101 goto error_open;
2102 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002103 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002104 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2105 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002106 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002107 } else {
2108 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2109 out->config = pcm_config_deep_buffer;
2110 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002111 }
2112
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002113 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2114 if(adev->primary_output == NULL)
2115 adev->primary_output = out;
2116 else {
2117 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002118 ret = -EEXIST;
2119 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002120 }
2121 }
2122
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002123 /* Check if this usecase is already existing */
2124 pthread_mutex_lock(&adev->lock);
2125 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2126 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002127 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002128 ret = -EEXIST;
2129 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002130 }
2131 pthread_mutex_unlock(&adev->lock);
2132
2133 out->stream.common.get_sample_rate = out_get_sample_rate;
2134 out->stream.common.set_sample_rate = out_set_sample_rate;
2135 out->stream.common.get_buffer_size = out_get_buffer_size;
2136 out->stream.common.get_channels = out_get_channels;
2137 out->stream.common.get_format = out_get_format;
2138 out->stream.common.set_format = out_set_format;
2139 out->stream.common.standby = out_standby;
2140 out->stream.common.dump = out_dump;
2141 out->stream.common.set_parameters = out_set_parameters;
2142 out->stream.common.get_parameters = out_get_parameters;
2143 out->stream.common.add_audio_effect = out_add_audio_effect;
2144 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2145 out->stream.get_latency = out_get_latency;
2146 out->stream.set_volume = out_set_volume;
2147 out->stream.write = out_write;
2148 out->stream.get_render_position = out_get_render_position;
2149 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002150 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002151
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002152 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002153 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002154 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002155
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002156 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2157 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002158
2159 config->format = out->stream.common.get_format(&out->stream.common);
2160 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2161 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2162
2163 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002164 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002165 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002166
2167error_open:
2168 free(out);
2169 *stream_out = NULL;
2170 ALOGD("%s: exit: ret %d", __func__, ret);
2171 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002172}
2173
2174static void adev_close_output_stream(struct audio_hw_device *dev,
2175 struct audio_stream_out *stream)
2176{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002177 struct stream_out *out = (struct stream_out *)stream;
2178 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002179 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002180
Eric Laurent994a6932013-07-17 11:51:42 -07002181 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002182 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2183 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2184 if(ret != 0)
2185 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2186 __func__, ret);
2187 }
2188 else
2189 out_standby(&stream->common);
2190
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002191 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2192 destroy_offload_callback_thread(out);
2193
2194 if (out->compr_config.codec != NULL)
2195 free(out->compr_config.codec);
2196 }
2197 pthread_cond_destroy(&out->cond);
2198 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002199 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002200 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002201}
2202
2203static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2204{
2205 struct audio_device *adev = (struct audio_device *)dev;
2206 struct str_parms *parms;
2207 char *str;
2208 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002209 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002210 int ret;
2211
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002212 ALOGD("%s: enter: %s", __func__, kvpairs);
2213
2214 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002215 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002216
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002217 voice_set_parameters(adev, parms);
2218 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002219
2220 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2221 if (ret >= 0) {
2222 /* When set to false, HAL should disable EC and NS
2223 * But it is currently not supported.
2224 */
2225 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2226 adev->bluetooth_nrec = true;
2227 else
2228 adev->bluetooth_nrec = false;
2229 }
2230
2231 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2232 if (ret >= 0) {
2233 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2234 adev->screen_off = false;
2235 else
2236 adev->screen_off = true;
2237 }
2238
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002239 ret = str_parms_get_int(parms, "rotation", &val);
2240 if (ret >= 0) {
2241 bool reverse_speakers = false;
2242 switch(val) {
2243 // FIXME: note that the code below assumes that the speakers are in the correct placement
2244 // relative to the user when the device is rotated 90deg from its default rotation. This
2245 // assumption is device-specific, not platform-specific like this code.
2246 case 270:
2247 reverse_speakers = true;
2248 break;
2249 case 0:
2250 case 90:
2251 case 180:
2252 break;
2253 default:
2254 ALOGE("%s: unexpected rotation of %d", __func__, val);
2255 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002256 if (adev->speaker_lr_swap != reverse_speakers) {
2257 adev->speaker_lr_swap = reverse_speakers;
2258 // only update the selected device if there is active pcm playback
2259 struct audio_usecase *usecase;
2260 struct listnode *node;
2261 list_for_each(node, &adev->usecase_list) {
2262 usecase = node_to_item(node, struct audio_usecase, list);
2263 if (usecase->type == PCM_PLAYBACK) {
2264 select_devices(adev, usecase->id);
2265 break;
2266 }
2267 }
2268 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002269 }
2270
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002271 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002272 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002273
2274 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002275 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002276 return ret;
2277}
2278
2279static char* adev_get_parameters(const struct audio_hw_device *dev,
2280 const char *keys)
2281{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002282 struct audio_device *adev = (struct audio_device *)dev;
2283 struct str_parms *reply = str_parms_create();
2284 struct str_parms *query = str_parms_create_str(keys);
2285 char *str;
2286
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002287 pthread_mutex_lock(&adev->lock);
2288
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002289 audio_extn_get_parameters(adev, query, reply);
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002290 voice_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002291 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) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002423 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002424 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};