blob: a90caa4ad1ccd29c26c37bf27b2e73dcaf57aacd [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"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080053
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070054#include "sound/compress_params.h"
55
56#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
57#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
58/* ToDo: Check and update a proper value in msec */
59#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
60#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
61
Eric Laurentb23d5282013-05-14 15:27:20 -070062struct pcm_config pcm_config_deep_buffer = {
63 .channels = 2,
64 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
65 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
66 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
67 .format = PCM_FORMAT_S16_LE,
68 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
69 .stop_threshold = INT_MAX,
70 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
71};
72
73struct pcm_config pcm_config_low_latency = {
74 .channels = 2,
75 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
76 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
77 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
78 .format = PCM_FORMAT_S16_LE,
79 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
80 .stop_threshold = INT_MAX,
81 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
82};
83
84struct pcm_config pcm_config_hdmi_multi = {
85 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
86 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
87 .period_size = HDMI_MULTI_PERIOD_SIZE,
88 .period_count = HDMI_MULTI_PERIOD_COUNT,
89 .format = PCM_FORMAT_S16_LE,
90 .start_threshold = 0,
91 .stop_threshold = INT_MAX,
92 .avail_min = 0,
93};
94
95struct pcm_config pcm_config_audio_capture = {
96 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070097 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
98 .format = PCM_FORMAT_S16_LE,
99};
100
Eric Laurentb23d5282013-05-14 15:27:20 -0700101static const char * const use_case_table[AUDIO_USECASE_MAX] = {
102 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
103 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
104 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700105 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700106 [USECASE_AUDIO_RECORD] = "audio-record",
107 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700108 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700109 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700110 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700111
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700112 [USECASE_VOICE2_CALL] = "voice2-call",
113 [USECASE_VOLTE_CALL] = "volte-call",
114 [USECASE_QCHAT_CALL] = "qchat-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700115 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
116 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
117 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700118 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
119 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700120 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
121 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700122};
123
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800124
125#define STRING_TO_ENUM(string) { #string, string }
126
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800127struct string_to_enum {
128 const char *name;
129 uint32_t value;
130};
131
132static const struct string_to_enum out_channels_name_to_enum_table[] = {
133 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
134 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
135 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
136};
137
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700138static struct audio_device *adev = NULL;
139static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700140static unsigned int audio_device_ref_count;
141
Haynes Mathew George5191a852013-09-11 14:19:36 -0700142static int set_voice_volume_l(struct audio_device *adev, float volume);
143
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700144static bool is_supported_format(audio_format_t format)
145{
Eric Laurent86e17132013-09-12 17:49:30 -0700146 if (format == AUDIO_FORMAT_MP3 ||
147 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700148 return true;
149
150 return false;
151}
152
153static int get_snd_codec_id(audio_format_t format)
154{
155 int id = 0;
156
157 switch (format) {
158 case AUDIO_FORMAT_MP3:
159 id = SND_AUDIOCODEC_MP3;
160 break;
161 case AUDIO_FORMAT_AAC:
162 id = SND_AUDIOCODEC_AAC;
163 break;
164 default:
165 ALOGE("%s: Unsupported audio format", __func__);
166 }
167
168 return id;
169}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800170
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700171int enable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700172 struct audio_usecase *usecase,
173 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800174{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700175 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700176 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800177
178 if (usecase == NULL)
179 return -EINVAL;
180
181 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
182
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800183 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700184 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800185 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700186 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800187
188 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700189 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700190 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700191 audio_route_apply_path(adev->audio_route, mixer_path);
192 if (update_mixer)
193 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800194
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800195 ALOGV("%s: exit", __func__);
196 return 0;
197}
198
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700199int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700200 struct audio_usecase *usecase,
201 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800202{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700203 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700204 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800205
206 if (usecase == NULL)
207 return -EINVAL;
208
209 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700210 if (usecase->type == PCM_CAPTURE)
211 snd_device = usecase->in_snd_device;
212 else
213 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800214 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700215 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700216 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700217 audio_route_reset_path(adev->audio_route, mixer_path);
218 if (update_mixer)
219 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800220
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800221 ALOGV("%s: exit", __func__);
222 return 0;
223}
224
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700225int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700226 snd_device_t snd_device,
227 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800228{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700229 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
230
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800231 if (snd_device < SND_DEVICE_MIN ||
232 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800233 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800234 return -EINVAL;
235 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700236
237 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700238
239 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
240 ALOGE("%s: Invalid sound device returned", __func__);
241 return -EINVAL;
242 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700243 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700244 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700245 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700246 return 0;
247 }
248
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700249 /* start usb playback thread */
250 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
251 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
252 audio_extn_usb_start_playback(adev);
253
254 /* start usb capture thread */
255 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
256 audio_extn_usb_start_capture(adev);
257
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700258 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
259 audio_extn_spkr_prot_is_enabled()) {
260 if (audio_extn_spkr_prot_start_processing(snd_device)) {
261 ALOGE("%s: spkr_start_processing failed", __func__);
262 return -EINVAL;
263 }
264 } else {
265 ALOGV("%s: snd_device(%d: %s)", __func__,
266 snd_device, device_name);
267 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
268 adev->snd_dev_ref_cnt[snd_device]--;
269 return -EINVAL;
270 }
271 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800272 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700273 if (update_mixer)
274 audio_route_update_mixer(adev->audio_route);
275
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800276 return 0;
277}
278
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700279int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700280 snd_device_t snd_device,
281 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800282{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700283 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
284
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800285 if (snd_device < SND_DEVICE_MIN ||
286 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800287 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800288 return -EINVAL;
289 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700290 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
291 ALOGE("%s: device ref cnt is already 0", __func__);
292 return -EINVAL;
293 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700294
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700295 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700296
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700297 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
298 ALOGE("%s: Invalid sound device returned", __func__);
299 return -EINVAL;
300 }
301
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700302 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700303 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700304 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800305 /* exit usb play back thread */
306 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
307 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
308 audio_extn_usb_stop_playback();
309
310 /* exit usb capture thread */
311 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
312 audio_extn_usb_stop_capture(adev);
313
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700314 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
315 audio_extn_spkr_prot_is_enabled()) {
316 audio_extn_spkr_prot_stop_processing();
317 } else
318 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800319
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700320 if (update_mixer)
321 audio_route_update_mixer(adev->audio_route);
322 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700323
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800324 return 0;
325}
326
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700327static void check_usecases_codec_backend(struct audio_device *adev,
328 struct audio_usecase *uc_info,
329 snd_device_t snd_device)
330{
331 struct listnode *node;
332 struct audio_usecase *usecase;
333 bool switch_device[AUDIO_USECASE_MAX];
334 int i, num_uc_to_switch = 0;
335
336 /*
337 * This function is to make sure that all the usecases that are active on
338 * the hardware codec backend are always routed to any one device that is
339 * handled by the hardware codec.
340 * For example, if low-latency and deep-buffer usecases are currently active
341 * on speaker and out_set_parameters(headset) is received on low-latency
342 * output, then we have to make sure deep-buffer is also switched to headset,
343 * because of the limitation that both the devices cannot be enabled
344 * at the same time as they share the same backend.
345 */
346 /* Disable all the usecases on the shared backend other than the
347 specified usecase */
348 for (i = 0; i < AUDIO_USECASE_MAX; i++)
349 switch_device[i] = false;
350
351 list_for_each(node, &adev->usecase_list) {
352 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700353 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700354 usecase != uc_info &&
355 usecase->out_snd_device != snd_device &&
356 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
357 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
358 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700359 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700360 disable_audio_route(adev, usecase, false);
361 switch_device[usecase->id] = true;
362 num_uc_to_switch++;
363 }
364 }
365
366 if (num_uc_to_switch) {
367 /* Make sure all the streams are de-routed before disabling the device */
368 audio_route_update_mixer(adev->audio_route);
369
370 list_for_each(node, &adev->usecase_list) {
371 usecase = node_to_item(node, struct audio_usecase, list);
372 if (switch_device[usecase->id]) {
373 disable_snd_device(adev, usecase->out_snd_device, false);
374 enable_snd_device(adev, snd_device, false);
375 }
376 }
377
378 /* Make sure new snd device is enabled before re-routing the streams */
379 audio_route_update_mixer(adev->audio_route);
380
381 /* Re-route all the usecases on the shared backend other than the
382 specified usecase to new snd devices */
383 list_for_each(node, &adev->usecase_list) {
384 usecase = node_to_item(node, struct audio_usecase, list);
385 /* Update the out_snd_device only before enabling the audio route */
386 if (switch_device[usecase->id] ) {
387 usecase->out_snd_device = snd_device;
388 enable_audio_route(adev, usecase, false);
389 }
390 }
391
392 audio_route_update_mixer(adev->audio_route);
393 }
394}
395
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700396static void check_and_route_capture_usecases(struct audio_device *adev,
397 struct audio_usecase *uc_info,
398 snd_device_t snd_device)
399{
400 struct listnode *node;
401 struct audio_usecase *usecase;
402 bool switch_device[AUDIO_USECASE_MAX];
403 int i, num_uc_to_switch = 0;
404
405 /*
406 * This function is to make sure that all the active capture usecases
407 * are always routed to the same input sound device.
408 * For example, if audio-record and voice-call usecases are currently
409 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
410 * is received for voice call then we have to make sure that audio-record
411 * usecase is also switched to earpiece i.e. voice-dmic-ef,
412 * because of the limitation that two devices cannot be enabled
413 * at the same time if they share the same backend.
414 */
415 for (i = 0; i < AUDIO_USECASE_MAX; i++)
416 switch_device[i] = false;
417
418 list_for_each(node, &adev->usecase_list) {
419 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700420 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700421 usecase != uc_info &&
422 usecase->in_snd_device != snd_device) {
423 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
424 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700425 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700426 disable_audio_route(adev, usecase, false);
427 switch_device[usecase->id] = true;
428 num_uc_to_switch++;
429 }
430 }
431
432 if (num_uc_to_switch) {
433 /* Make sure all the streams are de-routed before disabling the device */
434 audio_route_update_mixer(adev->audio_route);
435
436 list_for_each(node, &adev->usecase_list) {
437 usecase = node_to_item(node, struct audio_usecase, list);
438 if (switch_device[usecase->id]) {
439 disable_snd_device(adev, usecase->in_snd_device, false);
440 enable_snd_device(adev, snd_device, false);
441 }
442 }
443
444 /* Make sure new snd device is enabled before re-routing the streams */
445 audio_route_update_mixer(adev->audio_route);
446
447 /* Re-route all the usecases on the shared backend other than the
448 specified usecase to new snd devices */
449 list_for_each(node, &adev->usecase_list) {
450 usecase = node_to_item(node, struct audio_usecase, list);
451 /* Update the in_snd_device only before enabling the audio route */
452 if (switch_device[usecase->id] ) {
453 usecase->in_snd_device = snd_device;
454 enable_audio_route(adev, usecase, false);
455 }
456 }
457
458 audio_route_update_mixer(adev->audio_route);
459 }
460}
461
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700462static int disable_all_usecases_of_type(struct audio_device *adev,
463 usecase_type_t usecase_type,
464 bool update_mixer)
465{
466 struct audio_usecase *usecase;
467 struct listnode *node;
468 int ret = 0;
469
470 list_for_each(node, &adev->usecase_list) {
471 usecase = node_to_item(node, struct audio_usecase, list);
472 if (usecase->type == usecase_type) {
473 ALOGV("%s: usecase id %d", __func__, usecase->id);
474 ret = disable_audio_route(adev, usecase, update_mixer);
475 if (ret) {
476 ALOGE("%s: Failed to disable usecase id %d",
477 __func__, usecase->id);
478 }
479 }
480 }
481
482 return ret;
483}
484
485static int enable_all_usecases_of_type(struct audio_device *adev,
486 usecase_type_t usecase_type,
487 bool update_mixer)
488{
489 struct audio_usecase *usecase;
490 struct listnode *node;
491 int ret = 0;
492
493 list_for_each(node, &adev->usecase_list) {
494 usecase = node_to_item(node, struct audio_usecase, list);
495 if (usecase->type == usecase_type) {
496 ALOGV("%s: usecase id %d", __func__, usecase->id);
497 ret = enable_audio_route(adev, usecase, update_mixer);
498 if (ret) {
499 ALOGE("%s: Failed to enable usecase id %d",
500 __func__, usecase->id);
501 }
502 }
503 }
504
505 return ret;
506}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800507
508/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700509static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800510{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700511 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700512 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800513
514 switch (channels) {
515 /*
516 * Do not handle stereo output in Multi-channel cases
517 * Stereo case is handled in normal playback path
518 */
519 case 6:
520 ALOGV("%s: HDMI supports 5.1", __func__);
521 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
522 break;
523 case 8:
524 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
525 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
526 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
527 break;
528 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700529 ALOGE("HDMI does not support multi channel playback");
530 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800531 break;
532 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700533 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800534}
535
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700536static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
537{
538 struct audio_usecase *usecase;
539 struct listnode *node;
540
541 list_for_each(node, &adev->usecase_list) {
542 usecase = node_to_item(node, struct audio_usecase, list);
543 if (usecase->type == VOICE_CALL) {
544 ALOGV("%s: usecase id %d", __func__, usecase->id);
545 return usecase->id;
546 }
547 }
548 return USECASE_INVALID;
549}
550
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700551struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700552 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700553{
554 struct audio_usecase *usecase;
555 struct listnode *node;
556
557 list_for_each(node, &adev->usecase_list) {
558 usecase = node_to_item(node, struct audio_usecase, list);
559 if (usecase->id == uc_id)
560 return usecase;
561 }
562 return NULL;
563}
564
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700565int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800566{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800567 snd_device_t out_snd_device = SND_DEVICE_NONE;
568 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700569 struct audio_usecase *usecase = NULL;
570 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800571 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700572 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800573
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700574 usecase = get_usecase_from_list(adev, uc_id);
575 if (usecase == NULL) {
576 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
577 return -EINVAL;
578 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800579
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700580 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700581 out_snd_device = platform_get_output_snd_device(adev->platform,
582 usecase->stream.out->devices);
583 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700584 usecase->devices = usecase->stream.out->devices;
585 } else {
586 /*
587 * If the voice call is active, use the sound devices of voice call usecase
588 * so that it would not result any device switch. All the usecases will
589 * be switched to new device when select_devices() is called for voice call
590 * usecase. This is to avoid switching devices for voice call when
591 * check_usecases_codec_backend() is called below.
592 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700593 if (voice_is_in_call(adev)) {
594 vc_usecase = get_usecase_from_list(adev,
595 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700596 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
597 in_snd_device = vc_usecase->in_snd_device;
598 out_snd_device = vc_usecase->out_snd_device;
599 }
600 }
601 if (usecase->type == PCM_PLAYBACK) {
602 usecase->devices = usecase->stream.out->devices;
603 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700604 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700605 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700606 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700607 if (usecase->stream.out == adev->primary_output &&
608 adev->active_input &&
609 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
610 select_devices(adev, adev->active_input->usecase);
611 }
612 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700613 } else if (usecase->type == PCM_CAPTURE) {
614 usecase->devices = usecase->stream.in->device;
615 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700616 if (in_snd_device == SND_DEVICE_NONE) {
617 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
618 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700619 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700620 adev->primary_output->devices);
621 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700622 in_snd_device = platform_get_input_snd_device(adev->platform,
623 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700624 }
625 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700626 }
627 }
628
629 if (out_snd_device == usecase->out_snd_device &&
630 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800631 return 0;
632 }
633
sangwoobc677242013-08-08 16:53:43 +0900634 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700635 out_snd_device, platform_get_snd_device_name(out_snd_device),
636 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800637
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800638 /*
639 * Limitation: While in call, to do a device switch we need to disable
640 * and enable both RX and TX devices though one of them is same as current
641 * device.
642 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700643 if (usecase->type == VOICE_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700644 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700645 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800646 }
647
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700648 /* Disable current sound devices */
649 if (usecase->out_snd_device != SND_DEVICE_NONE) {
650 disable_audio_route(adev, usecase, true);
651 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800652 }
653
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700654 if (usecase->in_snd_device != SND_DEVICE_NONE) {
655 disable_audio_route(adev, usecase, true);
656 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800657 }
658
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700659 /* Enable new sound devices */
660 if (out_snd_device != SND_DEVICE_NONE) {
661 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
662 check_usecases_codec_backend(adev, usecase, out_snd_device);
663 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800664 }
665
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700666 if (in_snd_device != SND_DEVICE_NONE) {
667 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700668 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700669 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700670
Eric Laurentb23d5282013-05-14 15:27:20 -0700671 if (usecase->type == VOICE_CALL)
672 status = platform_switch_voice_call_device_post(adev->platform,
673 out_snd_device,
674 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800675
sangwoo170731f2013-06-08 15:36:36 +0900676 audio_route_update_mixer(adev->audio_route);
677
678 usecase->in_snd_device = in_snd_device;
679 usecase->out_snd_device = out_snd_device;
680
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700681 if (usecase->type == VOICE_CALL)
682 enable_all_usecases_of_type(adev, VOICE_CALL, true);
683 else
684 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900685
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800686 return status;
687}
688
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800689static int stop_input_stream(struct stream_in *in)
690{
691 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800692 struct audio_usecase *uc_info;
693 struct audio_device *adev = in->dev;
694
Eric Laurentc8400632013-02-14 19:04:54 -0800695 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800696
Eric Laurent994a6932013-07-17 11:51:42 -0700697 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700698 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800699 uc_info = get_usecase_from_list(adev, in->usecase);
700 if (uc_info == NULL) {
701 ALOGE("%s: Could not find the usecase (%d) in the list",
702 __func__, in->usecase);
703 return -EINVAL;
704 }
705
Eric Laurent150dbfe2013-02-27 14:31:02 -0800706 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700707 disable_audio_route(adev, uc_info, true);
708
709 /* 2. Disable the tx device */
710 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800711
Kiran Kandi910e1862013-10-29 13:29:42 -0700712 audio_extn_listen_update_status(uc_info,
713 LISTEN_EVENT_AUDIO_CAPTURE_INACTIVE);
714
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800715 list_remove(&uc_info->list);
716 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800717
Eric Laurent994a6932013-07-17 11:51:42 -0700718 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800719 return ret;
720}
721
722int start_input_stream(struct stream_in *in)
723{
724 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800725 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800726 struct audio_usecase *uc_info;
727 struct audio_device *adev = in->dev;
728
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700729 in->usecase = platform_get_usecase_from_source(in->source);
Eric Laurent994a6932013-07-17 11:51:42 -0700730 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700731
732 /* Check if source matches incall recording usecase criteria */
733 ret = voice_check_and_set_incall_rec_usecase(adev, in);
734 if (ret)
735 goto error_config;
736 else
737 ALOGV("%s: usecase(%d)", __func__, in->usecase);
738
Eric Laurentb23d5282013-05-14 15:27:20 -0700739 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800740 if (in->pcm_device_id < 0) {
741 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
742 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800743 ret = -EINVAL;
744 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700746
747 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800748 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
749 uc_info->id = in->usecase;
750 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800751 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700752 uc_info->devices = in->device;
753 uc_info->in_snd_device = SND_DEVICE_NONE;
754 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800755
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800756 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700757 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800758
Kiran Kandi910e1862013-10-29 13:29:42 -0700759 audio_extn_listen_update_status(uc_info,
760 LISTEN_EVENT_AUDIO_CAPTURE_ACTIVE);
761
Eric Laurentc8400632013-02-14 19:04:54 -0800762 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
763 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800764 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
765 PCM_IN, &in->config);
766 if (in->pcm && !pcm_is_ready(in->pcm)) {
767 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
768 pcm_close(in->pcm);
769 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800770 ret = -EIO;
771 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800772 }
Eric Laurent994a6932013-07-17 11:51:42 -0700773 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800774 return ret;
775
776error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800777 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800778
779error_config:
780 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700781 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800782
783 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800784}
785
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700786/* must be called with out->lock locked */
787static int send_offload_cmd_l(struct stream_out* out, int command)
788{
789 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
790
791 ALOGVV("%s %d", __func__, command);
792
793 cmd->cmd = command;
794 list_add_tail(&out->offload_cmd_list, &cmd->node);
795 pthread_cond_signal(&out->offload_cond);
796 return 0;
797}
798
799/* must be called iwth out->lock locked */
800static void stop_compressed_output_l(struct stream_out *out)
801{
802 out->offload_state = OFFLOAD_STATE_IDLE;
803 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700804 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700805 if (out->compr != NULL) {
806 compress_stop(out->compr);
807 while (out->offload_thread_blocked) {
808 pthread_cond_wait(&out->cond, &out->lock);
809 }
810 }
811}
812
813static void *offload_thread_loop(void *context)
814{
815 struct stream_out *out = (struct stream_out *) context;
816 struct listnode *item;
817
818 out->offload_state = OFFLOAD_STATE_IDLE;
819 out->playback_started = 0;
820
821 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
822 set_sched_policy(0, SP_FOREGROUND);
823 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
824
825 ALOGV("%s", __func__);
826 pthread_mutex_lock(&out->lock);
827 for (;;) {
828 struct offload_cmd *cmd = NULL;
829 stream_callback_event_t event;
830 bool send_callback = false;
831
832 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
833 __func__, list_empty(&out->offload_cmd_list),
834 out->offload_state);
835 if (list_empty(&out->offload_cmd_list)) {
836 ALOGV("%s SLEEPING", __func__);
837 pthread_cond_wait(&out->offload_cond, &out->lock);
838 ALOGV("%s RUNNING", __func__);
839 continue;
840 }
841
842 item = list_head(&out->offload_cmd_list);
843 cmd = node_to_item(item, struct offload_cmd, node);
844 list_remove(item);
845
846 ALOGVV("%s STATE %d CMD %d out->compr %p",
847 __func__, out->offload_state, cmd->cmd, out->compr);
848
849 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
850 free(cmd);
851 break;
852 }
853
854 if (out->compr == NULL) {
855 ALOGE("%s: Compress handle is NULL", __func__);
856 pthread_cond_signal(&out->cond);
857 continue;
858 }
859 out->offload_thread_blocked = true;
860 pthread_mutex_unlock(&out->lock);
861 send_callback = false;
862 switch(cmd->cmd) {
863 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
864 compress_wait(out->compr, -1);
865 send_callback = true;
866 event = STREAM_CBK_EVENT_WRITE_READY;
867 break;
868 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700869 compress_next_track(out->compr);
870 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700871 send_callback = true;
872 event = STREAM_CBK_EVENT_DRAIN_READY;
873 break;
874 case OFFLOAD_CMD_DRAIN:
875 compress_drain(out->compr);
876 send_callback = true;
877 event = STREAM_CBK_EVENT_DRAIN_READY;
878 break;
879 default:
880 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
881 break;
882 }
883 pthread_mutex_lock(&out->lock);
884 out->offload_thread_blocked = false;
885 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700886 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700887 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700888 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700889 free(cmd);
890 }
891
892 pthread_cond_signal(&out->cond);
893 while (!list_empty(&out->offload_cmd_list)) {
894 item = list_head(&out->offload_cmd_list);
895 list_remove(item);
896 free(node_to_item(item, struct offload_cmd, node));
897 }
898 pthread_mutex_unlock(&out->lock);
899
900 return NULL;
901}
902
903static int create_offload_callback_thread(struct stream_out *out)
904{
905 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
906 list_init(&out->offload_cmd_list);
907 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
908 offload_thread_loop, out);
909 return 0;
910}
911
912static int destroy_offload_callback_thread(struct stream_out *out)
913{
914 pthread_mutex_lock(&out->lock);
915 stop_compressed_output_l(out);
916 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
917
918 pthread_mutex_unlock(&out->lock);
919 pthread_join(out->offload_thread, (void **) NULL);
920 pthread_cond_destroy(&out->offload_cond);
921
922 return 0;
923}
924
Eric Laurent07eeafd2013-10-06 12:52:49 -0700925static bool allow_hdmi_channel_config(struct audio_device *adev)
926{
927 struct listnode *node;
928 struct audio_usecase *usecase;
929 bool ret = true;
930
931 list_for_each(node, &adev->usecase_list) {
932 usecase = node_to_item(node, struct audio_usecase, list);
933 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
934 /*
935 * If voice call is already existing, do not proceed further to avoid
936 * disabling/enabling both RX and TX devices, CSD calls, etc.
937 * Once the voice call done, the HDMI channels can be configured to
938 * max channels of remaining use cases.
939 */
940 if (usecase->id == USECASE_VOICE_CALL) {
941 ALOGD("%s: voice call is active, no change in HDMI channels",
942 __func__);
943 ret = false;
944 break;
945 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
946 ALOGD("%s: multi channel playback is active, "
947 "no change in HDMI channels", __func__);
948 ret = false;
949 break;
950 }
951 }
952 }
953 return ret;
954}
955
956static int check_and_set_hdmi_channels(struct audio_device *adev,
957 unsigned int channels)
958{
959 struct listnode *node;
960 struct audio_usecase *usecase;
961
962 /* Check if change in HDMI channel config is allowed */
963 if (!allow_hdmi_channel_config(adev))
964 return 0;
965
966 if (channels == adev->cur_hdmi_channels) {
967 ALOGD("%s: Requested channels are same as current", __func__);
968 return 0;
969 }
970
971 platform_set_hdmi_channels(adev->platform, channels);
972 adev->cur_hdmi_channels = channels;
973
974 /*
975 * Deroute all the playback streams routed to HDMI so that
976 * the back end is deactivated. Note that backend will not
977 * be deactivated if any one stream is connected to it.
978 */
979 list_for_each(node, &adev->usecase_list) {
980 usecase = node_to_item(node, struct audio_usecase, list);
981 if (usecase->type == PCM_PLAYBACK &&
982 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
983 disable_audio_route(adev, usecase, true);
984 }
985 }
986
987 /*
988 * Enable all the streams disabled above. Now the HDMI backend
989 * will be activated with new channel configuration
990 */
991 list_for_each(node, &adev->usecase_list) {
992 usecase = node_to_item(node, struct audio_usecase, list);
993 if (usecase->type == PCM_PLAYBACK &&
994 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
995 enable_audio_route(adev, usecase, true);
996 }
997 }
998
999 return 0;
1000}
1001
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001002static int stop_output_stream(struct stream_out *out)
1003{
1004 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001005 struct audio_usecase *uc_info;
1006 struct audio_device *adev = out->dev;
1007
Eric Laurent994a6932013-07-17 11:51:42 -07001008 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001009 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001010 uc_info = get_usecase_from_list(adev, out->usecase);
1011 if (uc_info == NULL) {
1012 ALOGE("%s: Could not find the usecase (%d) in the list",
1013 __func__, out->usecase);
1014 return -EINVAL;
1015 }
1016
Eric Laurentc4aef752013-09-12 17:45:53 -07001017 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
1018 adev->visualizer_stop_output != NULL)
1019 adev->visualizer_stop_output(out->handle);
1020
Eric Laurent150dbfe2013-02-27 14:31:02 -08001021 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001022 disable_audio_route(adev, uc_info, true);
1023
1024 /* 2. Disable the rx device */
1025 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001027 list_remove(&uc_info->list);
1028 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001029
Eric Laurent07eeafd2013-10-06 12:52:49 -07001030 /* Must be called after removing the usecase from list */
1031 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1032 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1033
Eric Laurent994a6932013-07-17 11:51:42 -07001034 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001035 return ret;
1036}
1037
1038int start_output_stream(struct stream_out *out)
1039{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001040 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001041 struct audio_usecase *uc_info;
1042 struct audio_device *adev = out->dev;
1043
Eric Laurent994a6932013-07-17 11:51:42 -07001044 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001045 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001046 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001047 if (out->pcm_device_id < 0) {
1048 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1049 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001050 ret = -EINVAL;
1051 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001052 }
1053
1054 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1055 uc_info->id = out->usecase;
1056 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001057 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001058 uc_info->devices = out->devices;
1059 uc_info->in_snd_device = SND_DEVICE_NONE;
1060 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001061
Eric Laurent07eeafd2013-10-06 12:52:49 -07001062 /* This must be called before adding this usecase to the list */
1063 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1064 check_and_set_hdmi_channels(adev, out->config.channels);
1065
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001066 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001067
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001068 select_devices(adev, out->usecase);
1069
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001070 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1071 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001072 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1073 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001074 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001075 if (out->pcm && !pcm_is_ready(out->pcm)) {
1076 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1077 pcm_close(out->pcm);
1078 out->pcm = NULL;
1079 ret = -EIO;
1080 goto error_open;
1081 }
1082 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001083 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001084 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1085 COMPRESS_IN, &out->compr_config);
1086 if (out->compr && !is_compress_ready(out->compr)) {
1087 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1088 compress_close(out->compr);
1089 out->compr = NULL;
1090 ret = -EIO;
1091 goto error_open;
1092 }
1093 if (out->offload_callback)
1094 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001095
1096 if (adev->visualizer_start_output != NULL)
1097 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001098 }
Eric Laurent994a6932013-07-17 11:51:42 -07001099 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001100 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001101error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001102 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001103error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001104 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001105}
1106
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001107static int check_input_parameters(uint32_t sample_rate,
1108 audio_format_t format,
1109 int channel_count)
1110{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001111 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001112
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001113 if (format != AUDIO_FORMAT_PCM_16_BIT) ret = -EINVAL;
1114
1115 switch (channel_count) {
1116 case 1:
1117 case 2:
1118 case 6:
1119 break;
1120 default:
1121 ret = -EINVAL;
1122 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001123
1124 switch (sample_rate) {
1125 case 8000:
1126 case 11025:
1127 case 12000:
1128 case 16000:
1129 case 22050:
1130 case 24000:
1131 case 32000:
1132 case 44100:
1133 case 48000:
1134 break;
1135 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001136 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001137 }
1138
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001139 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001140}
1141
1142static size_t get_input_buffer_size(uint32_t sample_rate,
1143 audio_format_t format,
1144 int channel_count)
1145{
1146 size_t size = 0;
1147
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001148 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1149 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001150
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001151 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1152 /* ToDo: should use frame_size computed based on the format and
1153 channel_count here. */
1154 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001155
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001156 /* make sure the size is multiple of 64 */
1157 size += 0x3f;
1158 size &= ~0x3f;
1159
1160 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001161}
1162
1163static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1164{
1165 struct stream_out *out = (struct stream_out *)stream;
1166
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001167 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001168}
1169
1170static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1171{
1172 return -ENOSYS;
1173}
1174
1175static size_t out_get_buffer_size(const struct audio_stream *stream)
1176{
1177 struct stream_out *out = (struct stream_out *)stream;
1178
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001179 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1180 return out->compr_config.fragment_size;
1181 }
1182
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001183 return out->config.period_size * audio_stream_frame_size(stream);
1184}
1185
1186static uint32_t out_get_channels(const struct audio_stream *stream)
1187{
1188 struct stream_out *out = (struct stream_out *)stream;
1189
1190 return out->channel_mask;
1191}
1192
1193static audio_format_t out_get_format(const struct audio_stream *stream)
1194{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001195 struct stream_out *out = (struct stream_out *)stream;
1196
1197 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001198}
1199
1200static int out_set_format(struct audio_stream *stream, audio_format_t format)
1201{
1202 return -ENOSYS;
1203}
1204
1205static int out_standby(struct audio_stream *stream)
1206{
1207 struct stream_out *out = (struct stream_out *)stream;
1208 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001209
Eric Laurent994a6932013-07-17 11:51:42 -07001210 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001211 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001212
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001213 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001214 if (!out->standby) {
1215 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001216 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1217 if (out->pcm) {
1218 pcm_close(out->pcm);
1219 out->pcm = NULL;
1220 }
1221 } else {
1222 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001223 out->gapless_mdata.encoder_delay = 0;
1224 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001225 if (out->compr != NULL) {
1226 compress_close(out->compr);
1227 out->compr = NULL;
1228 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001229 }
1230 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001231 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001232 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001233 }
1234 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001235 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001236 return 0;
1237}
1238
1239static int out_dump(const struct audio_stream *stream, int fd)
1240{
1241 return 0;
1242}
1243
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001244static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1245{
1246 int ret = 0;
1247 char value[32];
1248 struct compr_gapless_mdata tmp_mdata;
1249
1250 if (!out || !parms) {
1251 return -EINVAL;
1252 }
1253
1254 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1255 if (ret >= 0) {
1256 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1257 } else {
1258 return -EINVAL;
1259 }
1260
1261 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1262 if (ret >= 0) {
1263 tmp_mdata.encoder_padding = atoi(value);
1264 } else {
1265 return -EINVAL;
1266 }
1267
1268 out->gapless_mdata = tmp_mdata;
1269 out->send_new_metadata = 1;
1270 ALOGV("%s new encoder delay %u and padding %u", __func__,
1271 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1272
1273 return 0;
1274}
1275
1276
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1278{
1279 struct stream_out *out = (struct stream_out *)stream;
1280 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001281 struct audio_usecase *usecase;
1282 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001283 struct str_parms *parms;
1284 char value[32];
1285 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001286 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001287
sangwoobc677242013-08-08 16:53:43 +09001288 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001289 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001290 parms = str_parms_create_str(kvpairs);
1291 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1292 if (ret >= 0) {
1293 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001294 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001295 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001296
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001297 /*
1298 * When HDMI cable is unplugged the music playback is paused and
1299 * the policy manager sends routing=0. But the audioflinger
1300 * continues to write data until standby time (3sec).
1301 * As the HDMI core is turned off, the write gets blocked.
1302 * Avoid this by routing audio to speaker until standby.
1303 */
1304 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1305 val == AUDIO_DEVICE_NONE) {
1306 val = AUDIO_DEVICE_OUT_SPEAKER;
1307 }
1308
1309 /*
1310 * select_devices() call below switches all the usecases on the same
1311 * backend to the new device. Refer to check_usecases_codec_backend() in
1312 * the select_devices(). But how do we undo this?
1313 *
1314 * For example, music playback is active on headset (deep-buffer usecase)
1315 * and if we go to ringtones and select a ringtone, low-latency usecase
1316 * will be started on headset+speaker. As we can't enable headset+speaker
1317 * and headset devices at the same time, select_devices() switches the music
1318 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1319 * So when the ringtone playback is completed, how do we undo the same?
1320 *
1321 * We are relying on the out_set_parameters() call on deep-buffer output,
1322 * once the ringtone playback is ended.
1323 * NOTE: We should not check if the current devices are same as new devices.
1324 * Because select_devices() must be called to switch back the music
1325 * playback to headset.
1326 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001327 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001328 out->devices = val;
1329
1330 if (!out->standby)
1331 select_devices(adev, out->usecase);
1332
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001333 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1334 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001335 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001336 voice_start_call(adev);
1337 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1338 voice_is_in_call(adev) &&
1339 (out == adev->primary_output)) {
1340 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001341 }
1342 }
1343
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001344 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1345 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001346 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001347 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001348 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001349
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001350 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001351 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001352 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001353
1354 if (out == adev->primary_output) {
1355 pthread_mutex_lock(&adev->lock);
1356 audio_extn_set_parameters(adev, parms);
1357 pthread_mutex_unlock(&adev->lock);
1358 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001359 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1360 parse_compress_metadata(out, parms);
1361 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001362
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001363 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001364 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001365 return ret;
1366}
1367
1368static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1369{
1370 struct stream_out *out = (struct stream_out *)stream;
1371 struct str_parms *query = str_parms_create_str(keys);
1372 char *str;
1373 char value[256];
1374 struct str_parms *reply = str_parms_create();
1375 size_t i, j;
1376 int ret;
1377 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001378 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001379 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1380 if (ret >= 0) {
1381 value[0] = '\0';
1382 i = 0;
1383 while (out->supported_channel_masks[i] != 0) {
1384 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1385 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1386 if (!first) {
1387 strcat(value, "|");
1388 }
1389 strcat(value, out_channels_name_to_enum_table[j].name);
1390 first = false;
1391 break;
1392 }
1393 }
1394 i++;
1395 }
1396 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1397 str = str_parms_to_str(reply);
1398 } else {
1399 str = strdup(keys);
1400 }
1401 str_parms_destroy(query);
1402 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001403 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404 return str;
1405}
1406
1407static uint32_t out_get_latency(const struct audio_stream_out *stream)
1408{
1409 struct stream_out *out = (struct stream_out *)stream;
1410
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001411 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1412 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1413
1414 return (out->config.period_count * out->config.period_size * 1000) /
1415 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001416}
1417
1418static int out_set_volume(struct audio_stream_out *stream, float left,
1419 float right)
1420{
Eric Laurenta9024de2013-04-04 09:19:12 -07001421 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001422 int volume[2];
1423
Eric Laurenta9024de2013-04-04 09:19:12 -07001424 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1425 /* only take left channel into account: the API is for stereo anyway */
1426 out->muted = (left == 0.0f);
1427 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001428 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1429 const char *mixer_ctl_name = "Compress Playback Volume";
1430 struct audio_device *adev = out->dev;
1431 struct mixer_ctl *ctl;
1432
1433 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1434 if (!ctl) {
1435 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1436 __func__, mixer_ctl_name);
1437 return -EINVAL;
1438 }
1439 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1440 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1441 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1442 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001443 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001444
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001445 return -ENOSYS;
1446}
1447
1448static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1449 size_t bytes)
1450{
1451 struct stream_out *out = (struct stream_out *)stream;
1452 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001453 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001454
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001455 pthread_mutex_lock(&out->lock);
1456 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001457 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001458 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001459 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001460 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001461 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001462 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001463 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001464 goto exit;
1465 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001466 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001467
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001468 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001469 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1470 if (out->send_new_metadata) {
1471 ALOGVV("send new gapless metadata");
1472 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1473 out->send_new_metadata = 0;
1474 }
1475
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001476 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001477 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001478 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001479 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1480 }
1481 if (!out->playback_started) {
1482 compress_start(out->compr);
1483 out->playback_started = 1;
1484 out->offload_state = OFFLOAD_STATE_PLAYING;
1485 }
1486 pthread_mutex_unlock(&out->lock);
1487 return ret;
1488 } else {
1489 if (out->pcm) {
1490 if (out->muted)
1491 memset((void *)buffer, 0, bytes);
1492 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1493 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001494 if (ret == 0)
1495 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001496 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001497 }
1498
1499exit:
1500 pthread_mutex_unlock(&out->lock);
1501
1502 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001503 if (out->pcm)
1504 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001505 out_standby(&out->stream.common);
1506 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1507 out_get_sample_rate(&out->stream.common));
1508 }
1509 return bytes;
1510}
1511
1512static int out_get_render_position(const struct audio_stream_out *stream,
1513 uint32_t *dsp_frames)
1514{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001515 struct stream_out *out = (struct stream_out *)stream;
1516 *dsp_frames = 0;
1517 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1518 pthread_mutex_lock(&out->lock);
1519 if (out->compr != NULL) {
1520 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1521 &out->sample_rate);
1522 ALOGVV("%s rendered frames %d sample_rate %d",
1523 __func__, *dsp_frames, out->sample_rate);
1524 }
1525 pthread_mutex_unlock(&out->lock);
1526 return 0;
1527 } else
1528 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001529}
1530
1531static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1532{
1533 return 0;
1534}
1535
1536static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1537{
1538 return 0;
1539}
1540
1541static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1542 int64_t *timestamp)
1543{
1544 return -EINVAL;
1545}
1546
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001547static int out_get_presentation_position(const struct audio_stream_out *stream,
1548 uint64_t *frames, struct timespec *timestamp)
1549{
1550 struct stream_out *out = (struct stream_out *)stream;
1551 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001552 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001553
1554 pthread_mutex_lock(&out->lock);
1555
Eric Laurent949a0892013-09-20 09:20:13 -07001556 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1557 if (out->compr != NULL) {
1558 compress_get_tstamp(out->compr, &dsp_frames,
1559 &out->sample_rate);
1560 ALOGVV("%s rendered frames %ld sample_rate %d",
1561 __func__, dsp_frames, out->sample_rate);
1562 *frames = dsp_frames;
1563 ret = 0;
1564 /* this is the best we can do */
1565 clock_gettime(CLOCK_MONOTONIC, timestamp);
1566 }
1567 } else {
1568 if (out->pcm) {
1569 size_t avail;
1570 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1571 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001572 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001573 // This adjustment accounts for buffering after app processor.
1574 // It is based on estimated DSP latency per use case, rather than exact.
1575 signed_frames -=
1576 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1577
Eric Laurent949a0892013-09-20 09:20:13 -07001578 // It would be unusual for this value to be negative, but check just in case ...
1579 if (signed_frames >= 0) {
1580 *frames = signed_frames;
1581 ret = 0;
1582 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001583 }
1584 }
1585 }
1586
1587 pthread_mutex_unlock(&out->lock);
1588
1589 return ret;
1590}
1591
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001592static int out_set_callback(struct audio_stream_out *stream,
1593 stream_callback_t callback, void *cookie)
1594{
1595 struct stream_out *out = (struct stream_out *)stream;
1596
1597 ALOGV("%s", __func__);
1598 pthread_mutex_lock(&out->lock);
1599 out->offload_callback = callback;
1600 out->offload_cookie = cookie;
1601 pthread_mutex_unlock(&out->lock);
1602 return 0;
1603}
1604
1605static int out_pause(struct audio_stream_out* stream)
1606{
1607 struct stream_out *out = (struct stream_out *)stream;
1608 int status = -ENOSYS;
1609 ALOGV("%s", __func__);
1610 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1611 pthread_mutex_lock(&out->lock);
1612 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1613 status = compress_pause(out->compr);
1614 out->offload_state = OFFLOAD_STATE_PAUSED;
1615 }
1616 pthread_mutex_unlock(&out->lock);
1617 }
1618 return status;
1619}
1620
1621static int out_resume(struct audio_stream_out* stream)
1622{
1623 struct stream_out *out = (struct stream_out *)stream;
1624 int status = -ENOSYS;
1625 ALOGV("%s", __func__);
1626 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1627 status = 0;
1628 pthread_mutex_lock(&out->lock);
1629 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1630 status = compress_resume(out->compr);
1631 out->offload_state = OFFLOAD_STATE_PLAYING;
1632 }
1633 pthread_mutex_unlock(&out->lock);
1634 }
1635 return status;
1636}
1637
1638static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1639{
1640 struct stream_out *out = (struct stream_out *)stream;
1641 int status = -ENOSYS;
1642 ALOGV("%s", __func__);
1643 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1644 pthread_mutex_lock(&out->lock);
1645 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1646 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1647 else
1648 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1649 pthread_mutex_unlock(&out->lock);
1650 }
1651 return status;
1652}
1653
1654static int out_flush(struct audio_stream_out* stream)
1655{
1656 struct stream_out *out = (struct stream_out *)stream;
1657 ALOGV("%s", __func__);
1658 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1659 pthread_mutex_lock(&out->lock);
1660 stop_compressed_output_l(out);
1661 pthread_mutex_unlock(&out->lock);
1662 return 0;
1663 }
1664 return -ENOSYS;
1665}
1666
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001667/** audio_stream_in implementation **/
1668static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1669{
1670 struct stream_in *in = (struct stream_in *)stream;
1671
1672 return in->config.rate;
1673}
1674
1675static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1676{
1677 return -ENOSYS;
1678}
1679
1680static size_t in_get_buffer_size(const struct audio_stream *stream)
1681{
1682 struct stream_in *in = (struct stream_in *)stream;
1683
1684 return in->config.period_size * audio_stream_frame_size(stream);
1685}
1686
1687static uint32_t in_get_channels(const struct audio_stream *stream)
1688{
1689 struct stream_in *in = (struct stream_in *)stream;
1690
1691 return in->channel_mask;
1692}
1693
1694static audio_format_t in_get_format(const struct audio_stream *stream)
1695{
1696 return AUDIO_FORMAT_PCM_16_BIT;
1697}
1698
1699static int in_set_format(struct audio_stream *stream, audio_format_t format)
1700{
1701 return -ENOSYS;
1702}
1703
1704static int in_standby(struct audio_stream *stream)
1705{
1706 struct stream_in *in = (struct stream_in *)stream;
1707 struct audio_device *adev = in->dev;
1708 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001709 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001710 pthread_mutex_lock(&in->lock);
1711 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001712 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001713 if (in->pcm) {
1714 pcm_close(in->pcm);
1715 in->pcm = NULL;
1716 }
1717 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001718 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001719 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001720 }
1721 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001722 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001723 return status;
1724}
1725
1726static int in_dump(const struct audio_stream *stream, int fd)
1727{
1728 return 0;
1729}
1730
1731static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1732{
1733 struct stream_in *in = (struct stream_in *)stream;
1734 struct audio_device *adev = in->dev;
1735 struct str_parms *parms;
1736 char *str;
1737 char value[32];
1738 int ret, val = 0;
1739
Eric Laurent994a6932013-07-17 11:51:42 -07001740 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001741 parms = str_parms_create_str(kvpairs);
1742
1743 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1744
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001745 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001746 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001747 if (ret >= 0) {
1748 val = atoi(value);
1749 /* no audio source uses val == 0 */
1750 if ((in->source != val) && (val != 0)) {
1751 in->source = val;
1752 }
1753 }
1754
1755 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1756 if (ret >= 0) {
1757 val = atoi(value);
1758 if ((in->device != val) && (val != 0)) {
1759 in->device = val;
1760 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001761 if (!in->standby)
1762 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001763 }
1764 }
1765
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001766 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001767 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001768
1769 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001770 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001771 return ret;
1772}
1773
1774static char* in_get_parameters(const struct audio_stream *stream,
1775 const char *keys)
1776{
1777 return strdup("");
1778}
1779
1780static int in_set_gain(struct audio_stream_in *stream, float gain)
1781{
1782 return 0;
1783}
1784
1785static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1786 size_t bytes)
1787{
1788 struct stream_in *in = (struct stream_in *)stream;
1789 struct audio_device *adev = in->dev;
1790 int i, ret = -1;
1791
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001792 pthread_mutex_lock(&in->lock);
1793 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001794 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001795 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001796 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001797 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001798 goto exit;
1799 }
1800 in->standby = 0;
1801 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001802
1803 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001804 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1805 ret = audio_extn_ssr_read(stream, buffer, bytes);
1806 else
1807 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001808 }
1809
1810 /*
1811 * Instead of writing zeroes here, we could trust the hardware
1812 * to always provide zeroes when muted.
1813 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001814 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001815 memset(buffer, 0, bytes);
1816
1817exit:
1818 pthread_mutex_unlock(&in->lock);
1819
1820 if (ret != 0) {
1821 in_standby(&in->stream.common);
1822 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1823 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1824 in_get_sample_rate(&in->stream.common));
1825 }
1826 return bytes;
1827}
1828
1829static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1830{
1831 return 0;
1832}
1833
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001834static int add_remove_audio_effect(const struct audio_stream *stream,
1835 effect_handle_t effect,
1836 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001837{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001838 struct stream_in *in = (struct stream_in *)stream;
1839 int status = 0;
1840 effect_descriptor_t desc;
1841
1842 status = (*effect)->get_descriptor(effect, &desc);
1843 if (status != 0)
1844 return status;
1845
1846 pthread_mutex_lock(&in->lock);
1847 pthread_mutex_lock(&in->dev->lock);
1848 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1849 in->enable_aec != enable &&
1850 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1851 in->enable_aec = enable;
1852 if (!in->standby)
1853 select_devices(in->dev, in->usecase);
1854 }
1855 pthread_mutex_unlock(&in->dev->lock);
1856 pthread_mutex_unlock(&in->lock);
1857
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001858 return 0;
1859}
1860
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001861static int in_add_audio_effect(const struct audio_stream *stream,
1862 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001863{
Eric Laurent994a6932013-07-17 11:51:42 -07001864 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001865 return add_remove_audio_effect(stream, effect, true);
1866}
1867
1868static int in_remove_audio_effect(const struct audio_stream *stream,
1869 effect_handle_t effect)
1870{
Eric Laurent994a6932013-07-17 11:51:42 -07001871 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001872 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001873}
1874
1875static int adev_open_output_stream(struct audio_hw_device *dev,
1876 audio_io_handle_t handle,
1877 audio_devices_t devices,
1878 audio_output_flags_t flags,
1879 struct audio_config *config,
1880 struct audio_stream_out **stream_out)
1881{
1882 struct audio_device *adev = (struct audio_device *)dev;
1883 struct stream_out *out;
1884 int i, ret;
1885
Eric Laurent994a6932013-07-17 11:51:42 -07001886 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001887 __func__, config->sample_rate, config->channel_mask, devices, flags);
1888 *stream_out = NULL;
1889 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1890
1891 if (devices == AUDIO_DEVICE_NONE)
1892 devices = AUDIO_DEVICE_OUT_SPEAKER;
1893
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001894 out->flags = flags;
1895 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001896 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001897 out->format = config->format;
1898 out->sample_rate = config->sample_rate;
1899 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1900 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001901 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001902
1903 /* Init use case and pcm_config */
1904 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1905 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001906 pthread_mutex_lock(&adev->lock);
1907 ret = read_hdmi_channel_masks(out);
1908 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001909 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001910 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001911
1912 if (config->sample_rate == 0)
1913 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1914 if (config->channel_mask == 0)
1915 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1916
1917 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001918 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001919 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1920 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001921 out->config.rate = config->sample_rate;
1922 out->config.channels = popcount(out->channel_mask);
1923 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001924 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1925 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1926 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001927 out->sample_rate = out->config.rate;
1928 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1929 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1930 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1931 ALOGE("%s: Unsupported Offload information", __func__);
1932 ret = -EINVAL;
1933 goto error_open;
1934 }
1935 if (!is_supported_format(config->offload_info.format)) {
1936 ALOGE("%s: Unsupported audio format", __func__);
1937 ret = -EINVAL;
1938 goto error_open;
1939 }
1940
1941 out->compr_config.codec = (struct snd_codec *)
1942 calloc(1, sizeof(struct snd_codec));
1943
1944 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1945 if (config->offload_info.channel_mask)
1946 out->channel_mask = config->offload_info.channel_mask;
1947 else if (config->channel_mask)
1948 out->channel_mask = config->channel_mask;
1949 out->format = config->offload_info.format;
1950 out->sample_rate = config->offload_info.sample_rate;
1951
1952 out->stream.set_callback = out_set_callback;
1953 out->stream.pause = out_pause;
1954 out->stream.resume = out_resume;
1955 out->stream.drain = out_drain;
1956 out->stream.flush = out_flush;
1957
1958 out->compr_config.codec->id =
1959 get_snd_codec_id(config->offload_info.format);
1960 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1961 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1962 out->compr_config.codec->sample_rate =
1963 compress_get_alsa_rate(config->offload_info.sample_rate);
1964 out->compr_config.codec->bit_rate =
1965 config->offload_info.bit_rate;
1966 out->compr_config.codec->ch_in =
1967 popcount(config->channel_mask);
1968 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1969
1970 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1971 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001972
1973 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001974 create_offload_callback_thread(out);
1975 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1976 __func__, config->offload_info.version,
1977 config->offload_info.bit_rate);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07001978 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
1979 ret = voice_check_and_set_incall_music_usecase(adev, out);
1980 if (ret != 0) {
1981 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
1982 __func__, ret);
1983 goto error_open;
1984 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001985 } else {
1986 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1987 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001988 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001989 }
1990
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001991 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1992 if(adev->primary_output == NULL)
1993 adev->primary_output = out;
1994 else {
1995 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001996 ret = -EEXIST;
1997 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001998 }
1999 }
2000
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002001 /* Check if this usecase is already existing */
2002 pthread_mutex_lock(&adev->lock);
2003 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2004 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002005 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002006 ret = -EEXIST;
2007 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002008 }
2009 pthread_mutex_unlock(&adev->lock);
2010
2011 out->stream.common.get_sample_rate = out_get_sample_rate;
2012 out->stream.common.set_sample_rate = out_set_sample_rate;
2013 out->stream.common.get_buffer_size = out_get_buffer_size;
2014 out->stream.common.get_channels = out_get_channels;
2015 out->stream.common.get_format = out_get_format;
2016 out->stream.common.set_format = out_set_format;
2017 out->stream.common.standby = out_standby;
2018 out->stream.common.dump = out_dump;
2019 out->stream.common.set_parameters = out_set_parameters;
2020 out->stream.common.get_parameters = out_get_parameters;
2021 out->stream.common.add_audio_effect = out_add_audio_effect;
2022 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2023 out->stream.get_latency = out_get_latency;
2024 out->stream.set_volume = out_set_volume;
2025 out->stream.write = out_write;
2026 out->stream.get_render_position = out_get_render_position;
2027 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002028 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002029
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002030 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002031 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002032 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002033
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002034 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2035 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002036
2037 config->format = out->stream.common.get_format(&out->stream.common);
2038 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2039 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2040
2041 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002042 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002043 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002044
2045error_open:
2046 free(out);
2047 *stream_out = NULL;
2048 ALOGD("%s: exit: ret %d", __func__, ret);
2049 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002050}
2051
2052static void adev_close_output_stream(struct audio_hw_device *dev,
2053 struct audio_stream_out *stream)
2054{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002055 struct stream_out *out = (struct stream_out *)stream;
2056 struct audio_device *adev = out->dev;
2057
Eric Laurent994a6932013-07-17 11:51:42 -07002058 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002059 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002060 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2061 destroy_offload_callback_thread(out);
2062
2063 if (out->compr_config.codec != NULL)
2064 free(out->compr_config.codec);
2065 }
2066 pthread_cond_destroy(&out->cond);
2067 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002068 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002069 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002070}
2071
2072static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2073{
2074 struct audio_device *adev = (struct audio_device *)dev;
2075 struct str_parms *parms;
2076 char *str;
2077 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002078 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002079 int ret;
2080
Eric Laurent994a6932013-07-17 11:51:42 -07002081 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002082 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002083
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002084 voice_set_parameters(adev, parms);
2085 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002086
2087 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2088 if (ret >= 0) {
2089 /* When set to false, HAL should disable EC and NS
2090 * But it is currently not supported.
2091 */
2092 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2093 adev->bluetooth_nrec = true;
2094 else
2095 adev->bluetooth_nrec = false;
2096 }
2097
2098 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2099 if (ret >= 0) {
2100 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2101 adev->screen_off = false;
2102 else
2103 adev->screen_off = true;
2104 }
2105
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002106 ret = str_parms_get_int(parms, "rotation", &val);
2107 if (ret >= 0) {
2108 bool reverse_speakers = false;
2109 switch(val) {
2110 // FIXME: note that the code below assumes that the speakers are in the correct placement
2111 // relative to the user when the device is rotated 90deg from its default rotation. This
2112 // assumption is device-specific, not platform-specific like this code.
2113 case 270:
2114 reverse_speakers = true;
2115 break;
2116 case 0:
2117 case 90:
2118 case 180:
2119 break;
2120 default:
2121 ALOGE("%s: unexpected rotation of %d", __func__, val);
2122 }
2123 pthread_mutex_lock(&adev->lock);
2124 if (adev->speaker_lr_swap != reverse_speakers) {
2125 adev->speaker_lr_swap = reverse_speakers;
2126 // only update the selected device if there is active pcm playback
2127 struct audio_usecase *usecase;
2128 struct listnode *node;
2129 list_for_each(node, &adev->usecase_list) {
2130 usecase = node_to_item(node, struct audio_usecase, list);
2131 if (usecase->type == PCM_PLAYBACK) {
2132 select_devices(adev, usecase->id);
2133 break;
2134 }
2135 }
2136 }
2137 pthread_mutex_unlock(&adev->lock);
2138 }
2139
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002140 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002141 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002142 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002143 return ret;
2144}
2145
2146static char* adev_get_parameters(const struct audio_hw_device *dev,
2147 const char *keys)
2148{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002149 struct audio_device *adev = (struct audio_device *)dev;
2150 struct str_parms *reply = str_parms_create();
2151 struct str_parms *query = str_parms_create_str(keys);
2152 char *str;
2153
2154 audio_extn_get_parameters(adev, query, reply);
2155 platform_get_parameters(adev->platform, query, reply);
2156 str = str_parms_to_str(reply);
2157 str_parms_destroy(query);
2158 str_parms_destroy(reply);
2159
2160 ALOGV("%s: exit: returns - %s", __func__, str);
2161 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002162}
2163
2164static int adev_init_check(const struct audio_hw_device *dev)
2165{
2166 return 0;
2167}
2168
2169static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2170{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002171 int ret;
2172 struct audio_device *adev = (struct audio_device *)dev;
2173 pthread_mutex_lock(&adev->lock);
2174 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002175 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002176 pthread_mutex_unlock(&adev->lock);
2177 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002178}
2179
2180static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2181{
2182 return -ENOSYS;
2183}
2184
2185static int adev_get_master_volume(struct audio_hw_device *dev,
2186 float *volume)
2187{
2188 return -ENOSYS;
2189}
2190
2191static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2192{
2193 return -ENOSYS;
2194}
2195
2196static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2197{
2198 return -ENOSYS;
2199}
2200
2201static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2202{
2203 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002204 pthread_mutex_lock(&adev->lock);
2205 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002206 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002207 adev->mode = mode;
2208 }
2209 pthread_mutex_unlock(&adev->lock);
2210 return 0;
2211}
2212
2213static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2214{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002215 return voice_set_mic_mute((struct audio_device *)dev, state);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002216}
2217
2218static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2219{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002220 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002221 return 0;
2222}
2223
2224static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2225 const struct audio_config *config)
2226{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002227 int channel_count = popcount(config->channel_mask);
2228
2229 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2230}
2231
2232static int adev_open_input_stream(struct audio_hw_device *dev,
2233 audio_io_handle_t handle,
2234 audio_devices_t devices,
2235 struct audio_config *config,
2236 struct audio_stream_in **stream_in)
2237{
2238 struct audio_device *adev = (struct audio_device *)dev;
2239 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002240 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002241 int channel_count = popcount(config->channel_mask);
2242
Eric Laurent994a6932013-07-17 11:51:42 -07002243 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002244 *stream_in = NULL;
2245 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2246 return -EINVAL;
2247
2248 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2249
2250 in->stream.common.get_sample_rate = in_get_sample_rate;
2251 in->stream.common.set_sample_rate = in_set_sample_rate;
2252 in->stream.common.get_buffer_size = in_get_buffer_size;
2253 in->stream.common.get_channels = in_get_channels;
2254 in->stream.common.get_format = in_get_format;
2255 in->stream.common.set_format = in_set_format;
2256 in->stream.common.standby = in_standby;
2257 in->stream.common.dump = in_dump;
2258 in->stream.common.set_parameters = in_set_parameters;
2259 in->stream.common.get_parameters = in_get_parameters;
2260 in->stream.common.add_audio_effect = in_add_audio_effect;
2261 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2262 in->stream.set_gain = in_set_gain;
2263 in->stream.read = in_read;
2264 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2265
2266 in->device = devices;
2267 in->source = AUDIO_SOURCE_DEFAULT;
2268 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002269 in->standby = 1;
2270 in->channel_mask = config->channel_mask;
2271
2272 /* Update config params with the requested sample rate and channels */
2273 in->usecase = USECASE_AUDIO_RECORD;
2274 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002275 in->config.rate = config->sample_rate;
2276
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002277 if (channel_count == 6) {
2278 if(audio_extn_ssr_get_enabled()) {
2279 if(audio_extn_ssr_init(adev, in)) {
2280 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2281 ret = -EINVAL;
2282 goto err_open;
2283 }
2284 } else {
2285 ret = -EINVAL;
2286 goto err_open;
2287 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002288 } else {
2289 in->config.channels = channel_count;
2290 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2291 buffer_size = get_input_buffer_size(config->sample_rate,
2292 config->format,
2293 channel_count);
2294 in->config.period_size = buffer_size / frame_size;
2295 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002296
2297 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002298 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002299 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002300
2301err_open:
2302 free(in);
2303 *stream_in = NULL;
2304 return ret;
2305}
2306
2307static void adev_close_input_stream(struct audio_hw_device *dev,
2308 struct audio_stream_in *stream)
2309{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002310 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002311 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002312
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002313 in_standby(&stream->common);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002314 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2315 audio_extn_ssr_deinit();
2316 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002317 free(stream);
2318
2319 return;
2320}
2321
2322static int adev_dump(const audio_hw_device_t *device, int fd)
2323{
2324 return 0;
2325}
2326
2327static int adev_close(hw_device_t *device)
2328{
2329 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002330
2331 if (!adev)
2332 return 0;
2333
2334 pthread_mutex_lock(&adev_init_lock);
2335
2336 if ((--audio_device_ref_count) == 0) {
2337 audio_route_free(adev->audio_route);
2338 free(adev->snd_dev_ref_cnt);
2339 platform_deinit(adev->platform);
2340 audio_extn_listen_deinit(adev);
2341 free(device);
2342 adev = NULL;
2343 }
2344 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002345 return 0;
2346}
2347
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002348static int adev_open(const hw_module_t *module, const char *name,
2349 hw_device_t **device)
2350{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002351 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002352
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002353 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002354 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2355
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002356 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002357 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002358 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002359 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002360 ALOGD("%s: returning existing instance of adev", __func__);
2361 ALOGD("%s: exit", __func__);
2362 pthread_mutex_unlock(&adev_init_lock);
2363 return 0;
2364 }
2365
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002366 adev = calloc(1, sizeof(struct audio_device));
2367
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002368 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2369 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2370 adev->device.common.module = (struct hw_module_t *)module;
2371 adev->device.common.close = adev_close;
2372
2373 adev->device.init_check = adev_init_check;
2374 adev->device.set_voice_volume = adev_set_voice_volume;
2375 adev->device.set_master_volume = adev_set_master_volume;
2376 adev->device.get_master_volume = adev_get_master_volume;
2377 adev->device.set_master_mute = adev_set_master_mute;
2378 adev->device.get_master_mute = adev_get_master_mute;
2379 adev->device.set_mode = adev_set_mode;
2380 adev->device.set_mic_mute = adev_set_mic_mute;
2381 adev->device.get_mic_mute = adev_get_mic_mute;
2382 adev->device.set_parameters = adev_set_parameters;
2383 adev->device.get_parameters = adev_get_parameters;
2384 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2385 adev->device.open_output_stream = adev_open_output_stream;
2386 adev->device.close_output_stream = adev_close_output_stream;
2387 adev->device.open_input_stream = adev_open_input_stream;
2388 adev->device.close_input_stream = adev_close_input_stream;
2389 adev->device.dump = adev_dump;
2390
2391 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002392 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002393 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002394 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002395 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002396 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002397 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002398 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002399 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002400 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002401 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002402
2403 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002404 adev->platform = platform_init(adev);
2405 if (!adev->platform) {
2406 free(adev->snd_dev_ref_cnt);
2407 free(adev);
2408 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2409 *device = NULL;
2410 return -EINVAL;
2411 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002412
2413 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2414 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2415 if (adev->visualizer_lib == NULL) {
2416 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2417 } else {
2418 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2419 adev->visualizer_start_output =
2420 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2421 "visualizer_hal_start_output");
2422 adev->visualizer_stop_output =
2423 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2424 "visualizer_hal_stop_output");
2425 }
2426 }
Kiran Kandi910e1862013-10-29 13:29:42 -07002427 audio_extn_listen_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07002428
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002429 *device = &adev->device.common;
2430
Kiran Kandi910e1862013-10-29 13:29:42 -07002431 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002432 pthread_mutex_unlock(&adev_init_lock);
2433
Eric Laurent994a6932013-07-17 11:51:42 -07002434 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002435 return 0;
2436}
2437
2438static struct hw_module_methods_t hal_module_methods = {
2439 .open = adev_open,
2440};
2441
2442struct audio_module HAL_MODULE_INFO_SYM = {
2443 .common = {
2444 .tag = HARDWARE_MODULE_TAG,
2445 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2446 .hal_api_version = HARDWARE_HAL_API_VERSION,
2447 .id = AUDIO_HARDWARE_MODULE_ID,
2448 .name = "QCOM Audio HAL",
2449 .author = "Code Aurora Forum",
2450 .methods = &hal_module_methods,
2451 },
2452};