blob: b661dd1942c6c9e58ee025967c3ba76cb97536d6 [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);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700374 }
375 }
376
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700377 list_for_each(node, &adev->usecase_list) {
378 usecase = node_to_item(node, struct audio_usecase, list);
379 if (switch_device[usecase->id]) {
380 enable_snd_device(adev, snd_device, false);
381 }
382 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700383 /* Make sure new snd device is enabled before re-routing the streams */
384 audio_route_update_mixer(adev->audio_route);
385
386 /* Re-route all the usecases on the shared backend other than the
387 specified usecase to new snd devices */
388 list_for_each(node, &adev->usecase_list) {
389 usecase = node_to_item(node, struct audio_usecase, list);
390 /* Update the out_snd_device only before enabling the audio route */
391 if (switch_device[usecase->id] ) {
392 usecase->out_snd_device = snd_device;
393 enable_audio_route(adev, usecase, false);
394 }
395 }
396
397 audio_route_update_mixer(adev->audio_route);
398 }
399}
400
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700401static void check_and_route_capture_usecases(struct audio_device *adev,
402 struct audio_usecase *uc_info,
403 snd_device_t snd_device)
404{
405 struct listnode *node;
406 struct audio_usecase *usecase;
407 bool switch_device[AUDIO_USECASE_MAX];
408 int i, num_uc_to_switch = 0;
409
410 /*
411 * This function is to make sure that all the active capture usecases
412 * are always routed to the same input sound device.
413 * For example, if audio-record and voice-call usecases are currently
414 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
415 * is received for voice call then we have to make sure that audio-record
416 * usecase is also switched to earpiece i.e. voice-dmic-ef,
417 * because of the limitation that two devices cannot be enabled
418 * at the same time if they share the same backend.
419 */
420 for (i = 0; i < AUDIO_USECASE_MAX; i++)
421 switch_device[i] = false;
422
423 list_for_each(node, &adev->usecase_list) {
424 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700425 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700426 usecase != uc_info &&
427 usecase->in_snd_device != snd_device) {
428 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
429 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700430 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700431 disable_audio_route(adev, usecase, false);
432 switch_device[usecase->id] = true;
433 num_uc_to_switch++;
434 }
435 }
436
437 if (num_uc_to_switch) {
438 /* Make sure all the streams are de-routed before disabling the device */
439 audio_route_update_mixer(adev->audio_route);
440
441 list_for_each(node, &adev->usecase_list) {
442 usecase = node_to_item(node, struct audio_usecase, list);
443 if (switch_device[usecase->id]) {
444 disable_snd_device(adev, usecase->in_snd_device, false);
445 enable_snd_device(adev, snd_device, false);
446 }
447 }
448
449 /* Make sure new snd device is enabled before re-routing the streams */
450 audio_route_update_mixer(adev->audio_route);
451
452 /* Re-route all the usecases on the shared backend other than the
453 specified usecase to new snd devices */
454 list_for_each(node, &adev->usecase_list) {
455 usecase = node_to_item(node, struct audio_usecase, list);
456 /* Update the in_snd_device only before enabling the audio route */
457 if (switch_device[usecase->id] ) {
458 usecase->in_snd_device = snd_device;
459 enable_audio_route(adev, usecase, false);
460 }
461 }
462
463 audio_route_update_mixer(adev->audio_route);
464 }
465}
466
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700467static int disable_all_usecases_of_type(struct audio_device *adev,
468 usecase_type_t usecase_type,
469 bool update_mixer)
470{
471 struct audio_usecase *usecase;
472 struct listnode *node;
473 int ret = 0;
474
475 list_for_each(node, &adev->usecase_list) {
476 usecase = node_to_item(node, struct audio_usecase, list);
477 if (usecase->type == usecase_type) {
478 ALOGV("%s: usecase id %d", __func__, usecase->id);
479 ret = disable_audio_route(adev, usecase, update_mixer);
480 if (ret) {
481 ALOGE("%s: Failed to disable usecase id %d",
482 __func__, usecase->id);
483 }
484 }
485 }
486
487 return ret;
488}
489
490static int enable_all_usecases_of_type(struct audio_device *adev,
491 usecase_type_t usecase_type,
492 bool update_mixer)
493{
494 struct audio_usecase *usecase;
495 struct listnode *node;
496 int ret = 0;
497
498 list_for_each(node, &adev->usecase_list) {
499 usecase = node_to_item(node, struct audio_usecase, list);
500 if (usecase->type == usecase_type) {
501 ALOGV("%s: usecase id %d", __func__, usecase->id);
502 ret = enable_audio_route(adev, usecase, update_mixer);
503 if (ret) {
504 ALOGE("%s: Failed to enable usecase id %d",
505 __func__, usecase->id);
506 }
507 }
508 }
509
510 return ret;
511}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800512
513/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700514static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800515{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700516 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700517 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800518
519 switch (channels) {
520 /*
521 * Do not handle stereo output in Multi-channel cases
522 * Stereo case is handled in normal playback path
523 */
524 case 6:
525 ALOGV("%s: HDMI supports 5.1", __func__);
526 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
527 break;
528 case 8:
529 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
530 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
531 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
532 break;
533 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700534 ALOGE("HDMI does not support multi channel playback");
535 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800536 break;
537 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700538 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800539}
540
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700541static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
542{
543 struct audio_usecase *usecase;
544 struct listnode *node;
545
546 list_for_each(node, &adev->usecase_list) {
547 usecase = node_to_item(node, struct audio_usecase, list);
548 if (usecase->type == VOICE_CALL) {
549 ALOGV("%s: usecase id %d", __func__, usecase->id);
550 return usecase->id;
551 }
552 }
553 return USECASE_INVALID;
554}
555
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700556struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700557 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700558{
559 struct audio_usecase *usecase;
560 struct listnode *node;
561
562 list_for_each(node, &adev->usecase_list) {
563 usecase = node_to_item(node, struct audio_usecase, list);
564 if (usecase->id == uc_id)
565 return usecase;
566 }
567 return NULL;
568}
569
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700570int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800571{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800572 snd_device_t out_snd_device = SND_DEVICE_NONE;
573 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700574 struct audio_usecase *usecase = NULL;
575 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800576 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700577 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800578
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700579 usecase = get_usecase_from_list(adev, uc_id);
580 if (usecase == NULL) {
581 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
582 return -EINVAL;
583 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800584
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700585 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700586 out_snd_device = platform_get_output_snd_device(adev->platform,
587 usecase->stream.out->devices);
588 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700589 usecase->devices = usecase->stream.out->devices;
590 } else {
591 /*
592 * If the voice call is active, use the sound devices of voice call usecase
593 * so that it would not result any device switch. All the usecases will
594 * be switched to new device when select_devices() is called for voice call
595 * usecase. This is to avoid switching devices for voice call when
596 * check_usecases_codec_backend() is called below.
597 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700598 if (voice_is_in_call(adev)) {
599 vc_usecase = get_usecase_from_list(adev,
600 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700601 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
602 in_snd_device = vc_usecase->in_snd_device;
603 out_snd_device = vc_usecase->out_snd_device;
604 }
605 }
606 if (usecase->type == PCM_PLAYBACK) {
607 usecase->devices = usecase->stream.out->devices;
608 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700609 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700610 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700611 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700612 if (usecase->stream.out == adev->primary_output &&
613 adev->active_input &&
614 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
615 select_devices(adev, adev->active_input->usecase);
616 }
617 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700618 } else if (usecase->type == PCM_CAPTURE) {
619 usecase->devices = usecase->stream.in->device;
620 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700621 if (in_snd_device == SND_DEVICE_NONE) {
622 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
623 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700624 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700625 adev->primary_output->devices);
626 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700627 in_snd_device = platform_get_input_snd_device(adev->platform,
628 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700629 }
630 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700631 }
632 }
633
634 if (out_snd_device == usecase->out_snd_device &&
635 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800636 return 0;
637 }
638
sangwoobc677242013-08-08 16:53:43 +0900639 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700640 out_snd_device, platform_get_snd_device_name(out_snd_device),
641 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800642
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800643 /*
644 * Limitation: While in call, to do a device switch we need to disable
645 * and enable both RX and TX devices though one of them is same as current
646 * device.
647 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700648 if (usecase->type == VOICE_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700649 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700650 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800651 }
652
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700653 /* Disable current sound devices */
654 if (usecase->out_snd_device != SND_DEVICE_NONE) {
655 disable_audio_route(adev, usecase, true);
656 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800657 }
658
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700659 if (usecase->in_snd_device != SND_DEVICE_NONE) {
660 disable_audio_route(adev, usecase, true);
661 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800662 }
663
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700664 /* Enable new sound devices */
665 if (out_snd_device != SND_DEVICE_NONE) {
666 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
667 check_usecases_codec_backend(adev, usecase, out_snd_device);
668 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800669 }
670
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700671 if (in_snd_device != SND_DEVICE_NONE) {
672 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700673 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700674 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700675
Eric Laurentb23d5282013-05-14 15:27:20 -0700676 if (usecase->type == VOICE_CALL)
677 status = platform_switch_voice_call_device_post(adev->platform,
678 out_snd_device,
679 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800680
sangwoo170731f2013-06-08 15:36:36 +0900681 audio_route_update_mixer(adev->audio_route);
682
683 usecase->in_snd_device = in_snd_device;
684 usecase->out_snd_device = out_snd_device;
685
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700686 if (usecase->type == VOICE_CALL)
687 enable_all_usecases_of_type(adev, VOICE_CALL, true);
688 else
689 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900690
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691 return status;
692}
693
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800694static int stop_input_stream(struct stream_in *in)
695{
696 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800697 struct audio_usecase *uc_info;
698 struct audio_device *adev = in->dev;
699
Eric Laurentc8400632013-02-14 19:04:54 -0800700 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800701
Eric Laurent994a6932013-07-17 11:51:42 -0700702 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700703 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800704 uc_info = get_usecase_from_list(adev, in->usecase);
705 if (uc_info == NULL) {
706 ALOGE("%s: Could not find the usecase (%d) in the list",
707 __func__, in->usecase);
708 return -EINVAL;
709 }
710
Eric Laurent150dbfe2013-02-27 14:31:02 -0800711 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700712 disable_audio_route(adev, uc_info, true);
713
714 /* 2. Disable the tx device */
715 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800716
Kiran Kandi910e1862013-10-29 13:29:42 -0700717 audio_extn_listen_update_status(uc_info,
718 LISTEN_EVENT_AUDIO_CAPTURE_INACTIVE);
719
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800720 list_remove(&uc_info->list);
721 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800722
Eric Laurent994a6932013-07-17 11:51:42 -0700723 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800724 return ret;
725}
726
727int start_input_stream(struct stream_in *in)
728{
729 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800730 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800731 struct audio_usecase *uc_info;
732 struct audio_device *adev = in->dev;
733
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700734 in->usecase = platform_get_usecase_from_source(in->source);
Eric Laurent994a6932013-07-17 11:51:42 -0700735 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700736
737 /* Check if source matches incall recording usecase criteria */
738 ret = voice_check_and_set_incall_rec_usecase(adev, in);
739 if (ret)
740 goto error_config;
741 else
742 ALOGV("%s: usecase(%d)", __func__, in->usecase);
743
Eric Laurentb23d5282013-05-14 15:27:20 -0700744 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745 if (in->pcm_device_id < 0) {
746 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
747 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800748 ret = -EINVAL;
749 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800750 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700751
752 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800753 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
754 uc_info->id = in->usecase;
755 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800756 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700757 uc_info->devices = in->device;
758 uc_info->in_snd_device = SND_DEVICE_NONE;
759 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800760
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800761 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700762 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800763
Kiran Kandi910e1862013-10-29 13:29:42 -0700764 audio_extn_listen_update_status(uc_info,
765 LISTEN_EVENT_AUDIO_CAPTURE_ACTIVE);
766
Eric Laurentc8400632013-02-14 19:04:54 -0800767 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
768 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800769 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
770 PCM_IN, &in->config);
771 if (in->pcm && !pcm_is_ready(in->pcm)) {
772 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
773 pcm_close(in->pcm);
774 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800775 ret = -EIO;
776 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800777 }
Eric Laurent994a6932013-07-17 11:51:42 -0700778 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800779 return ret;
780
781error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800782 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800783
784error_config:
785 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700786 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800787
788 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800789}
790
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700791/* must be called with out->lock locked */
792static int send_offload_cmd_l(struct stream_out* out, int command)
793{
794 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
795
796 ALOGVV("%s %d", __func__, command);
797
798 cmd->cmd = command;
799 list_add_tail(&out->offload_cmd_list, &cmd->node);
800 pthread_cond_signal(&out->offload_cond);
801 return 0;
802}
803
804/* must be called iwth out->lock locked */
805static void stop_compressed_output_l(struct stream_out *out)
806{
807 out->offload_state = OFFLOAD_STATE_IDLE;
808 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700809 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700810 if (out->compr != NULL) {
811 compress_stop(out->compr);
812 while (out->offload_thread_blocked) {
813 pthread_cond_wait(&out->cond, &out->lock);
814 }
815 }
816}
817
818static void *offload_thread_loop(void *context)
819{
820 struct stream_out *out = (struct stream_out *) context;
821 struct listnode *item;
822
823 out->offload_state = OFFLOAD_STATE_IDLE;
824 out->playback_started = 0;
825
826 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
827 set_sched_policy(0, SP_FOREGROUND);
828 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
829
830 ALOGV("%s", __func__);
831 pthread_mutex_lock(&out->lock);
832 for (;;) {
833 struct offload_cmd *cmd = NULL;
834 stream_callback_event_t event;
835 bool send_callback = false;
836
837 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
838 __func__, list_empty(&out->offload_cmd_list),
839 out->offload_state);
840 if (list_empty(&out->offload_cmd_list)) {
841 ALOGV("%s SLEEPING", __func__);
842 pthread_cond_wait(&out->offload_cond, &out->lock);
843 ALOGV("%s RUNNING", __func__);
844 continue;
845 }
846
847 item = list_head(&out->offload_cmd_list);
848 cmd = node_to_item(item, struct offload_cmd, node);
849 list_remove(item);
850
851 ALOGVV("%s STATE %d CMD %d out->compr %p",
852 __func__, out->offload_state, cmd->cmd, out->compr);
853
854 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
855 free(cmd);
856 break;
857 }
858
859 if (out->compr == NULL) {
860 ALOGE("%s: Compress handle is NULL", __func__);
861 pthread_cond_signal(&out->cond);
862 continue;
863 }
864 out->offload_thread_blocked = true;
865 pthread_mutex_unlock(&out->lock);
866 send_callback = false;
867 switch(cmd->cmd) {
868 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
869 compress_wait(out->compr, -1);
870 send_callback = true;
871 event = STREAM_CBK_EVENT_WRITE_READY;
872 break;
873 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700874 compress_next_track(out->compr);
875 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700876 send_callback = true;
877 event = STREAM_CBK_EVENT_DRAIN_READY;
878 break;
879 case OFFLOAD_CMD_DRAIN:
880 compress_drain(out->compr);
881 send_callback = true;
882 event = STREAM_CBK_EVENT_DRAIN_READY;
883 break;
884 default:
885 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
886 break;
887 }
888 pthread_mutex_lock(&out->lock);
889 out->offload_thread_blocked = false;
890 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700891 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700892 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700893 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700894 free(cmd);
895 }
896
897 pthread_cond_signal(&out->cond);
898 while (!list_empty(&out->offload_cmd_list)) {
899 item = list_head(&out->offload_cmd_list);
900 list_remove(item);
901 free(node_to_item(item, struct offload_cmd, node));
902 }
903 pthread_mutex_unlock(&out->lock);
904
905 return NULL;
906}
907
908static int create_offload_callback_thread(struct stream_out *out)
909{
910 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
911 list_init(&out->offload_cmd_list);
912 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
913 offload_thread_loop, out);
914 return 0;
915}
916
917static int destroy_offload_callback_thread(struct stream_out *out)
918{
919 pthread_mutex_lock(&out->lock);
920 stop_compressed_output_l(out);
921 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
922
923 pthread_mutex_unlock(&out->lock);
924 pthread_join(out->offload_thread, (void **) NULL);
925 pthread_cond_destroy(&out->offload_cond);
926
927 return 0;
928}
929
Eric Laurent07eeafd2013-10-06 12:52:49 -0700930static bool allow_hdmi_channel_config(struct audio_device *adev)
931{
932 struct listnode *node;
933 struct audio_usecase *usecase;
934 bool ret = true;
935
936 list_for_each(node, &adev->usecase_list) {
937 usecase = node_to_item(node, struct audio_usecase, list);
938 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
939 /*
940 * If voice call is already existing, do not proceed further to avoid
941 * disabling/enabling both RX and TX devices, CSD calls, etc.
942 * Once the voice call done, the HDMI channels can be configured to
943 * max channels of remaining use cases.
944 */
945 if (usecase->id == USECASE_VOICE_CALL) {
946 ALOGD("%s: voice call is active, no change in HDMI channels",
947 __func__);
948 ret = false;
949 break;
950 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
951 ALOGD("%s: multi channel playback is active, "
952 "no change in HDMI channels", __func__);
953 ret = false;
954 break;
955 }
956 }
957 }
958 return ret;
959}
960
961static int check_and_set_hdmi_channels(struct audio_device *adev,
962 unsigned int channels)
963{
964 struct listnode *node;
965 struct audio_usecase *usecase;
966
967 /* Check if change in HDMI channel config is allowed */
968 if (!allow_hdmi_channel_config(adev))
969 return 0;
970
971 if (channels == adev->cur_hdmi_channels) {
972 ALOGD("%s: Requested channels are same as current", __func__);
973 return 0;
974 }
975
976 platform_set_hdmi_channels(adev->platform, channels);
977 adev->cur_hdmi_channels = channels;
978
979 /*
980 * Deroute all the playback streams routed to HDMI so that
981 * the back end is deactivated. Note that backend will not
982 * be deactivated if any one stream is connected to it.
983 */
984 list_for_each(node, &adev->usecase_list) {
985 usecase = node_to_item(node, struct audio_usecase, list);
986 if (usecase->type == PCM_PLAYBACK &&
987 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
988 disable_audio_route(adev, usecase, true);
989 }
990 }
991
992 /*
993 * Enable all the streams disabled above. Now the HDMI backend
994 * will be activated with new channel configuration
995 */
996 list_for_each(node, &adev->usecase_list) {
997 usecase = node_to_item(node, struct audio_usecase, list);
998 if (usecase->type == PCM_PLAYBACK &&
999 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1000 enable_audio_route(adev, usecase, true);
1001 }
1002 }
1003
1004 return 0;
1005}
1006
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001007static int stop_output_stream(struct stream_out *out)
1008{
1009 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001010 struct audio_usecase *uc_info;
1011 struct audio_device *adev = out->dev;
1012
Eric Laurent994a6932013-07-17 11:51:42 -07001013 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001014 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001015 uc_info = get_usecase_from_list(adev, out->usecase);
1016 if (uc_info == NULL) {
1017 ALOGE("%s: Could not find the usecase (%d) in the list",
1018 __func__, out->usecase);
1019 return -EINVAL;
1020 }
1021
Eric Laurentc4aef752013-09-12 17:45:53 -07001022 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
1023 adev->visualizer_stop_output != NULL)
1024 adev->visualizer_stop_output(out->handle);
1025
Eric Laurent150dbfe2013-02-27 14:31:02 -08001026 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001027 disable_audio_route(adev, uc_info, true);
1028
1029 /* 2. Disable the rx device */
1030 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001032 list_remove(&uc_info->list);
1033 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001034
Eric Laurent07eeafd2013-10-06 12:52:49 -07001035 /* Must be called after removing the usecase from list */
1036 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1037 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1038
Eric Laurent994a6932013-07-17 11:51:42 -07001039 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001040 return ret;
1041}
1042
1043int start_output_stream(struct stream_out *out)
1044{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001045 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001046 struct audio_usecase *uc_info;
1047 struct audio_device *adev = out->dev;
1048
Eric Laurent994a6932013-07-17 11:51:42 -07001049 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001050 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001051 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001052 if (out->pcm_device_id < 0) {
1053 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1054 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001055 ret = -EINVAL;
1056 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001057 }
1058
1059 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1060 uc_info->id = out->usecase;
1061 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001062 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001063 uc_info->devices = out->devices;
1064 uc_info->in_snd_device = SND_DEVICE_NONE;
1065 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001066
Eric Laurent07eeafd2013-10-06 12:52:49 -07001067 /* This must be called before adding this usecase to the list */
1068 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1069 check_and_set_hdmi_channels(adev, out->config.channels);
1070
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001071 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001072
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001073 select_devices(adev, out->usecase);
1074
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001075 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1076 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001077 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1078 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001079 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001080 if (out->pcm && !pcm_is_ready(out->pcm)) {
1081 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1082 pcm_close(out->pcm);
1083 out->pcm = NULL;
1084 ret = -EIO;
1085 goto error_open;
1086 }
1087 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001088 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001089 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1090 COMPRESS_IN, &out->compr_config);
1091 if (out->compr && !is_compress_ready(out->compr)) {
1092 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1093 compress_close(out->compr);
1094 out->compr = NULL;
1095 ret = -EIO;
1096 goto error_open;
1097 }
1098 if (out->offload_callback)
1099 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001100
1101 if (adev->visualizer_start_output != NULL)
1102 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001103 }
Eric Laurent994a6932013-07-17 11:51:42 -07001104 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001105 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001106error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001107 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001108error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001109 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001110}
1111
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001112static int check_input_parameters(uint32_t sample_rate,
1113 audio_format_t format,
1114 int channel_count)
1115{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001116 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001117
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001118 if (format != AUDIO_FORMAT_PCM_16_BIT) ret = -EINVAL;
1119
1120 switch (channel_count) {
1121 case 1:
1122 case 2:
1123 case 6:
1124 break;
1125 default:
1126 ret = -EINVAL;
1127 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001128
1129 switch (sample_rate) {
1130 case 8000:
1131 case 11025:
1132 case 12000:
1133 case 16000:
1134 case 22050:
1135 case 24000:
1136 case 32000:
1137 case 44100:
1138 case 48000:
1139 break;
1140 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001141 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001142 }
1143
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001144 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001145}
1146
1147static size_t get_input_buffer_size(uint32_t sample_rate,
1148 audio_format_t format,
1149 int channel_count)
1150{
1151 size_t size = 0;
1152
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001153 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1154 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001155
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001156 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1157 /* ToDo: should use frame_size computed based on the format and
1158 channel_count here. */
1159 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001160
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001161 /* make sure the size is multiple of 64 */
1162 size += 0x3f;
1163 size &= ~0x3f;
1164
1165 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001166}
1167
1168static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1169{
1170 struct stream_out *out = (struct stream_out *)stream;
1171
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001172 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001173}
1174
1175static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1176{
1177 return -ENOSYS;
1178}
1179
1180static size_t out_get_buffer_size(const struct audio_stream *stream)
1181{
1182 struct stream_out *out = (struct stream_out *)stream;
1183
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001184 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1185 return out->compr_config.fragment_size;
1186 }
1187
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001188 return out->config.period_size * audio_stream_frame_size(stream);
1189}
1190
1191static uint32_t out_get_channels(const struct audio_stream *stream)
1192{
1193 struct stream_out *out = (struct stream_out *)stream;
1194
1195 return out->channel_mask;
1196}
1197
1198static audio_format_t out_get_format(const struct audio_stream *stream)
1199{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001200 struct stream_out *out = (struct stream_out *)stream;
1201
1202 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001203}
1204
1205static int out_set_format(struct audio_stream *stream, audio_format_t format)
1206{
1207 return -ENOSYS;
1208}
1209
1210static int out_standby(struct audio_stream *stream)
1211{
1212 struct stream_out *out = (struct stream_out *)stream;
1213 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001214
Eric Laurent994a6932013-07-17 11:51:42 -07001215 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001216 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001217
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001218 pthread_mutex_lock(&out->lock);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001219 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001220 if (!out->standby) {
1221 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001222 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1223 if (out->pcm) {
1224 pcm_close(out->pcm);
1225 out->pcm = NULL;
1226 }
1227 } else {
1228 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001229 out->gapless_mdata.encoder_delay = 0;
1230 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001231 if (out->compr != NULL) {
1232 compress_close(out->compr);
1233 out->compr = NULL;
1234 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001235 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001236 stop_output_stream(out);
1237 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001238 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001239 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001240 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001241 return 0;
1242}
1243
1244static int out_dump(const struct audio_stream *stream, int fd)
1245{
1246 return 0;
1247}
1248
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001249static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1250{
1251 int ret = 0;
1252 char value[32];
1253 struct compr_gapless_mdata tmp_mdata;
1254
1255 if (!out || !parms) {
1256 return -EINVAL;
1257 }
1258
1259 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1260 if (ret >= 0) {
1261 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1262 } else {
1263 return -EINVAL;
1264 }
1265
1266 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1267 if (ret >= 0) {
1268 tmp_mdata.encoder_padding = atoi(value);
1269 } else {
1270 return -EINVAL;
1271 }
1272
1273 out->gapless_mdata = tmp_mdata;
1274 out->send_new_metadata = 1;
1275 ALOGV("%s new encoder delay %u and padding %u", __func__,
1276 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1277
1278 return 0;
1279}
1280
1281
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001282static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1283{
1284 struct stream_out *out = (struct stream_out *)stream;
1285 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001286 struct audio_usecase *usecase;
1287 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001288 struct str_parms *parms;
1289 char value[32];
1290 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001291 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001292
sangwoobc677242013-08-08 16:53:43 +09001293 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001294 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001295 parms = str_parms_create_str(kvpairs);
1296 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1297 if (ret >= 0) {
1298 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001299 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001300 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001301
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001302 /*
1303 * When HDMI cable is unplugged the music playback is paused and
1304 * the policy manager sends routing=0. But the audioflinger
1305 * continues to write data until standby time (3sec).
1306 * As the HDMI core is turned off, the write gets blocked.
1307 * Avoid this by routing audio to speaker until standby.
1308 */
1309 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1310 val == AUDIO_DEVICE_NONE) {
1311 val = AUDIO_DEVICE_OUT_SPEAKER;
1312 }
1313
1314 /*
1315 * select_devices() call below switches all the usecases on the same
1316 * backend to the new device. Refer to check_usecases_codec_backend() in
1317 * the select_devices(). But how do we undo this?
1318 *
1319 * For example, music playback is active on headset (deep-buffer usecase)
1320 * and if we go to ringtones and select a ringtone, low-latency usecase
1321 * will be started on headset+speaker. As we can't enable headset+speaker
1322 * and headset devices at the same time, select_devices() switches the music
1323 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1324 * So when the ringtone playback is completed, how do we undo the same?
1325 *
1326 * We are relying on the out_set_parameters() call on deep-buffer output,
1327 * once the ringtone playback is ended.
1328 * NOTE: We should not check if the current devices are same as new devices.
1329 * Because select_devices() must be called to switch back the music
1330 * playback to headset.
1331 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001332 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001333 out->devices = val;
1334
1335 if (!out->standby)
1336 select_devices(adev, out->usecase);
1337
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001338 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1339 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001340 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001341 voice_start_call(adev);
1342 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1343 voice_is_in_call(adev) &&
1344 (out == adev->primary_output)) {
1345 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001346 }
1347 }
1348
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001349 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1350 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001351 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001352 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001353 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001354
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001355 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001356 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001357 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001358
1359 if (out == adev->primary_output) {
1360 pthread_mutex_lock(&adev->lock);
1361 audio_extn_set_parameters(adev, parms);
1362 pthread_mutex_unlock(&adev->lock);
1363 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001364 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1365 parse_compress_metadata(out, parms);
1366 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001367
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001368 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001369 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001370 return ret;
1371}
1372
1373static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1374{
1375 struct stream_out *out = (struct stream_out *)stream;
1376 struct str_parms *query = str_parms_create_str(keys);
1377 char *str;
1378 char value[256];
1379 struct str_parms *reply = str_parms_create();
1380 size_t i, j;
1381 int ret;
1382 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001383 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001384 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1385 if (ret >= 0) {
1386 value[0] = '\0';
1387 i = 0;
1388 while (out->supported_channel_masks[i] != 0) {
1389 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1390 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1391 if (!first) {
1392 strcat(value, "|");
1393 }
1394 strcat(value, out_channels_name_to_enum_table[j].name);
1395 first = false;
1396 break;
1397 }
1398 }
1399 i++;
1400 }
1401 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1402 str = str_parms_to_str(reply);
1403 } else {
1404 str = strdup(keys);
1405 }
1406 str_parms_destroy(query);
1407 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001408 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409 return str;
1410}
1411
1412static uint32_t out_get_latency(const struct audio_stream_out *stream)
1413{
1414 struct stream_out *out = (struct stream_out *)stream;
1415
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001416 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1417 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1418
1419 return (out->config.period_count * out->config.period_size * 1000) /
1420 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001421}
1422
1423static int out_set_volume(struct audio_stream_out *stream, float left,
1424 float right)
1425{
Eric Laurenta9024de2013-04-04 09:19:12 -07001426 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001427 int volume[2];
1428
Eric Laurenta9024de2013-04-04 09:19:12 -07001429 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1430 /* only take left channel into account: the API is for stereo anyway */
1431 out->muted = (left == 0.0f);
1432 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001433 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1434 const char *mixer_ctl_name = "Compress Playback Volume";
1435 struct audio_device *adev = out->dev;
1436 struct mixer_ctl *ctl;
1437
1438 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1439 if (!ctl) {
1440 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1441 __func__, mixer_ctl_name);
1442 return -EINVAL;
1443 }
1444 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1445 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1446 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1447 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001448 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001449
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001450 return -ENOSYS;
1451}
1452
1453static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1454 size_t bytes)
1455{
1456 struct stream_out *out = (struct stream_out *)stream;
1457 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001458 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001459
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001460 pthread_mutex_lock(&out->lock);
1461 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001462 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001463 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001464 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001465 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001466 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001467 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001468 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001469 goto exit;
1470 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001471 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001472
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001473 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001474 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1475 if (out->send_new_metadata) {
1476 ALOGVV("send new gapless metadata");
1477 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1478 out->send_new_metadata = 0;
1479 }
1480
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001481 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001482 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001483 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001484 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1485 }
1486 if (!out->playback_started) {
1487 compress_start(out->compr);
1488 out->playback_started = 1;
1489 out->offload_state = OFFLOAD_STATE_PLAYING;
1490 }
1491 pthread_mutex_unlock(&out->lock);
1492 return ret;
1493 } else {
1494 if (out->pcm) {
1495 if (out->muted)
1496 memset((void *)buffer, 0, bytes);
1497 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1498 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001499 if (ret == 0)
1500 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001501 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001502 }
1503
1504exit:
1505 pthread_mutex_unlock(&out->lock);
1506
1507 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001508 if (out->pcm)
1509 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001510 out_standby(&out->stream.common);
1511 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1512 out_get_sample_rate(&out->stream.common));
1513 }
1514 return bytes;
1515}
1516
1517static int out_get_render_position(const struct audio_stream_out *stream,
1518 uint32_t *dsp_frames)
1519{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001520 struct stream_out *out = (struct stream_out *)stream;
1521 *dsp_frames = 0;
1522 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1523 pthread_mutex_lock(&out->lock);
1524 if (out->compr != NULL) {
1525 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1526 &out->sample_rate);
1527 ALOGVV("%s rendered frames %d sample_rate %d",
1528 __func__, *dsp_frames, out->sample_rate);
1529 }
1530 pthread_mutex_unlock(&out->lock);
1531 return 0;
1532 } else
1533 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001534}
1535
1536static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1537{
1538 return 0;
1539}
1540
1541static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1542{
1543 return 0;
1544}
1545
1546static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1547 int64_t *timestamp)
1548{
1549 return -EINVAL;
1550}
1551
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001552static int out_get_presentation_position(const struct audio_stream_out *stream,
1553 uint64_t *frames, struct timespec *timestamp)
1554{
1555 struct stream_out *out = (struct stream_out *)stream;
1556 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001557 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001558
1559 pthread_mutex_lock(&out->lock);
1560
Eric Laurent949a0892013-09-20 09:20:13 -07001561 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1562 if (out->compr != NULL) {
1563 compress_get_tstamp(out->compr, &dsp_frames,
1564 &out->sample_rate);
1565 ALOGVV("%s rendered frames %ld sample_rate %d",
1566 __func__, dsp_frames, out->sample_rate);
1567 *frames = dsp_frames;
1568 ret = 0;
1569 /* this is the best we can do */
1570 clock_gettime(CLOCK_MONOTONIC, timestamp);
1571 }
1572 } else {
1573 if (out->pcm) {
1574 size_t avail;
1575 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1576 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001577 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001578 // This adjustment accounts for buffering after app processor.
1579 // It is based on estimated DSP latency per use case, rather than exact.
1580 signed_frames -=
1581 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1582
Eric Laurent949a0892013-09-20 09:20:13 -07001583 // It would be unusual for this value to be negative, but check just in case ...
1584 if (signed_frames >= 0) {
1585 *frames = signed_frames;
1586 ret = 0;
1587 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001588 }
1589 }
1590 }
1591
1592 pthread_mutex_unlock(&out->lock);
1593
1594 return ret;
1595}
1596
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001597static int out_set_callback(struct audio_stream_out *stream,
1598 stream_callback_t callback, void *cookie)
1599{
1600 struct stream_out *out = (struct stream_out *)stream;
1601
1602 ALOGV("%s", __func__);
1603 pthread_mutex_lock(&out->lock);
1604 out->offload_callback = callback;
1605 out->offload_cookie = cookie;
1606 pthread_mutex_unlock(&out->lock);
1607 return 0;
1608}
1609
1610static int out_pause(struct audio_stream_out* stream)
1611{
1612 struct stream_out *out = (struct stream_out *)stream;
1613 int status = -ENOSYS;
1614 ALOGV("%s", __func__);
1615 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1616 pthread_mutex_lock(&out->lock);
1617 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1618 status = compress_pause(out->compr);
1619 out->offload_state = OFFLOAD_STATE_PAUSED;
1620 }
1621 pthread_mutex_unlock(&out->lock);
1622 }
1623 return status;
1624}
1625
1626static int out_resume(struct audio_stream_out* stream)
1627{
1628 struct stream_out *out = (struct stream_out *)stream;
1629 int status = -ENOSYS;
1630 ALOGV("%s", __func__);
1631 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1632 status = 0;
1633 pthread_mutex_lock(&out->lock);
1634 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1635 status = compress_resume(out->compr);
1636 out->offload_state = OFFLOAD_STATE_PLAYING;
1637 }
1638 pthread_mutex_unlock(&out->lock);
1639 }
1640 return status;
1641}
1642
1643static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1644{
1645 struct stream_out *out = (struct stream_out *)stream;
1646 int status = -ENOSYS;
1647 ALOGV("%s", __func__);
1648 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1649 pthread_mutex_lock(&out->lock);
1650 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1651 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1652 else
1653 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1654 pthread_mutex_unlock(&out->lock);
1655 }
1656 return status;
1657}
1658
1659static int out_flush(struct audio_stream_out* stream)
1660{
1661 struct stream_out *out = (struct stream_out *)stream;
1662 ALOGV("%s", __func__);
1663 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1664 pthread_mutex_lock(&out->lock);
1665 stop_compressed_output_l(out);
1666 pthread_mutex_unlock(&out->lock);
1667 return 0;
1668 }
1669 return -ENOSYS;
1670}
1671
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001672/** audio_stream_in implementation **/
1673static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1674{
1675 struct stream_in *in = (struct stream_in *)stream;
1676
1677 return in->config.rate;
1678}
1679
1680static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1681{
1682 return -ENOSYS;
1683}
1684
1685static size_t in_get_buffer_size(const struct audio_stream *stream)
1686{
1687 struct stream_in *in = (struct stream_in *)stream;
1688
1689 return in->config.period_size * audio_stream_frame_size(stream);
1690}
1691
1692static uint32_t in_get_channels(const struct audio_stream *stream)
1693{
1694 struct stream_in *in = (struct stream_in *)stream;
1695
1696 return in->channel_mask;
1697}
1698
1699static audio_format_t in_get_format(const struct audio_stream *stream)
1700{
1701 return AUDIO_FORMAT_PCM_16_BIT;
1702}
1703
1704static int in_set_format(struct audio_stream *stream, audio_format_t format)
1705{
1706 return -ENOSYS;
1707}
1708
1709static int in_standby(struct audio_stream *stream)
1710{
1711 struct stream_in *in = (struct stream_in *)stream;
1712 struct audio_device *adev = in->dev;
1713 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001714 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001715 pthread_mutex_lock(&in->lock);
1716 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001717 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001718 if (in->pcm) {
1719 pcm_close(in->pcm);
1720 in->pcm = NULL;
1721 }
1722 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001723 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001724 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001725 }
1726 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001727 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001728 return status;
1729}
1730
1731static int in_dump(const struct audio_stream *stream, int fd)
1732{
1733 return 0;
1734}
1735
1736static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1737{
1738 struct stream_in *in = (struct stream_in *)stream;
1739 struct audio_device *adev = in->dev;
1740 struct str_parms *parms;
1741 char *str;
1742 char value[32];
1743 int ret, val = 0;
1744
Eric Laurent994a6932013-07-17 11:51:42 -07001745 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001746 parms = str_parms_create_str(kvpairs);
1747
1748 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1749
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001750 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001751 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001752 if (ret >= 0) {
1753 val = atoi(value);
1754 /* no audio source uses val == 0 */
1755 if ((in->source != val) && (val != 0)) {
1756 in->source = val;
1757 }
1758 }
1759
1760 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1761 if (ret >= 0) {
1762 val = atoi(value);
1763 if ((in->device != val) && (val != 0)) {
1764 in->device = val;
1765 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001766 if (!in->standby)
1767 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001768 }
1769 }
1770
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001771 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001772 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001773
1774 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001775 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001776 return ret;
1777}
1778
1779static char* in_get_parameters(const struct audio_stream *stream,
1780 const char *keys)
1781{
1782 return strdup("");
1783}
1784
1785static int in_set_gain(struct audio_stream_in *stream, float gain)
1786{
1787 return 0;
1788}
1789
1790static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1791 size_t bytes)
1792{
1793 struct stream_in *in = (struct stream_in *)stream;
1794 struct audio_device *adev = in->dev;
1795 int i, ret = -1;
1796
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001797 pthread_mutex_lock(&in->lock);
1798 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001799 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001800 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001801 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001802 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001803 goto exit;
1804 }
1805 in->standby = 0;
1806 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001807
1808 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001809 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1810 ret = audio_extn_ssr_read(stream, buffer, bytes);
1811 else
1812 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001813 }
1814
1815 /*
1816 * Instead of writing zeroes here, we could trust the hardware
1817 * to always provide zeroes when muted.
1818 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001819 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001820 memset(buffer, 0, bytes);
1821
1822exit:
1823 pthread_mutex_unlock(&in->lock);
1824
1825 if (ret != 0) {
1826 in_standby(&in->stream.common);
1827 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1828 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1829 in_get_sample_rate(&in->stream.common));
1830 }
1831 return bytes;
1832}
1833
1834static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1835{
1836 return 0;
1837}
1838
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001839static int add_remove_audio_effect(const struct audio_stream *stream,
1840 effect_handle_t effect,
1841 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001842{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001843 struct stream_in *in = (struct stream_in *)stream;
1844 int status = 0;
1845 effect_descriptor_t desc;
1846
1847 status = (*effect)->get_descriptor(effect, &desc);
1848 if (status != 0)
1849 return status;
1850
1851 pthread_mutex_lock(&in->lock);
1852 pthread_mutex_lock(&in->dev->lock);
1853 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1854 in->enable_aec != enable &&
1855 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1856 in->enable_aec = enable;
1857 if (!in->standby)
1858 select_devices(in->dev, in->usecase);
1859 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08001860 if (in->enable_ns != enable &&
1861 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1862 in->enable_ns = enable;
1863 if (!in->standby)
1864 select_devices(in->dev, in->usecase);
1865 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001866 pthread_mutex_unlock(&in->dev->lock);
1867 pthread_mutex_unlock(&in->lock);
1868
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001869 return 0;
1870}
1871
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001872static int in_add_audio_effect(const struct audio_stream *stream,
1873 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001874{
Eric Laurent994a6932013-07-17 11:51:42 -07001875 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001876 return add_remove_audio_effect(stream, effect, true);
1877}
1878
1879static int in_remove_audio_effect(const struct audio_stream *stream,
1880 effect_handle_t effect)
1881{
Eric Laurent994a6932013-07-17 11:51:42 -07001882 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001883 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001884}
1885
1886static int adev_open_output_stream(struct audio_hw_device *dev,
1887 audio_io_handle_t handle,
1888 audio_devices_t devices,
1889 audio_output_flags_t flags,
1890 struct audio_config *config,
1891 struct audio_stream_out **stream_out)
1892{
1893 struct audio_device *adev = (struct audio_device *)dev;
1894 struct stream_out *out;
1895 int i, ret;
1896
Eric Laurent994a6932013-07-17 11:51:42 -07001897 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001898 __func__, config->sample_rate, config->channel_mask, devices, flags);
1899 *stream_out = NULL;
1900 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1901
1902 if (devices == AUDIO_DEVICE_NONE)
1903 devices = AUDIO_DEVICE_OUT_SPEAKER;
1904
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001905 out->flags = flags;
1906 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001907 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001908 out->format = config->format;
1909 out->sample_rate = config->sample_rate;
1910 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1911 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001912 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001913
1914 /* Init use case and pcm_config */
1915 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1916 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001917 pthread_mutex_lock(&adev->lock);
1918 ret = read_hdmi_channel_masks(out);
1919 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001920 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001921 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001922
1923 if (config->sample_rate == 0)
1924 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1925 if (config->channel_mask == 0)
1926 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1927
1928 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001929 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001930 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1931 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001932 out->config.rate = config->sample_rate;
1933 out->config.channels = popcount(out->channel_mask);
1934 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001935 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1936 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1937 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001938 out->sample_rate = out->config.rate;
1939 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1940 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1941 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1942 ALOGE("%s: Unsupported Offload information", __func__);
1943 ret = -EINVAL;
1944 goto error_open;
1945 }
1946 if (!is_supported_format(config->offload_info.format)) {
1947 ALOGE("%s: Unsupported audio format", __func__);
1948 ret = -EINVAL;
1949 goto error_open;
1950 }
1951
1952 out->compr_config.codec = (struct snd_codec *)
1953 calloc(1, sizeof(struct snd_codec));
1954
1955 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1956 if (config->offload_info.channel_mask)
1957 out->channel_mask = config->offload_info.channel_mask;
1958 else if (config->channel_mask)
1959 out->channel_mask = config->channel_mask;
1960 out->format = config->offload_info.format;
1961 out->sample_rate = config->offload_info.sample_rate;
1962
1963 out->stream.set_callback = out_set_callback;
1964 out->stream.pause = out_pause;
1965 out->stream.resume = out_resume;
1966 out->stream.drain = out_drain;
1967 out->stream.flush = out_flush;
1968
1969 out->compr_config.codec->id =
1970 get_snd_codec_id(config->offload_info.format);
1971 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1972 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1973 out->compr_config.codec->sample_rate =
1974 compress_get_alsa_rate(config->offload_info.sample_rate);
1975 out->compr_config.codec->bit_rate =
1976 config->offload_info.bit_rate;
1977 out->compr_config.codec->ch_in =
1978 popcount(config->channel_mask);
1979 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1980
1981 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1982 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001983
1984 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001985 create_offload_callback_thread(out);
1986 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1987 __func__, config->offload_info.version,
1988 config->offload_info.bit_rate);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07001989 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
1990 ret = voice_check_and_set_incall_music_usecase(adev, out);
1991 if (ret != 0) {
1992 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
1993 __func__, ret);
1994 goto error_open;
1995 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001996 } else {
1997 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1998 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001999 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 }
2001
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002002 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2003 if(adev->primary_output == NULL)
2004 adev->primary_output = out;
2005 else {
2006 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002007 ret = -EEXIST;
2008 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002009 }
2010 }
2011
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002012 /* Check if this usecase is already existing */
2013 pthread_mutex_lock(&adev->lock);
2014 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2015 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002016 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002017 ret = -EEXIST;
2018 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002019 }
2020 pthread_mutex_unlock(&adev->lock);
2021
2022 out->stream.common.get_sample_rate = out_get_sample_rate;
2023 out->stream.common.set_sample_rate = out_set_sample_rate;
2024 out->stream.common.get_buffer_size = out_get_buffer_size;
2025 out->stream.common.get_channels = out_get_channels;
2026 out->stream.common.get_format = out_get_format;
2027 out->stream.common.set_format = out_set_format;
2028 out->stream.common.standby = out_standby;
2029 out->stream.common.dump = out_dump;
2030 out->stream.common.set_parameters = out_set_parameters;
2031 out->stream.common.get_parameters = out_get_parameters;
2032 out->stream.common.add_audio_effect = out_add_audio_effect;
2033 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2034 out->stream.get_latency = out_get_latency;
2035 out->stream.set_volume = out_set_volume;
2036 out->stream.write = out_write;
2037 out->stream.get_render_position = out_get_render_position;
2038 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002039 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002040
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002041 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002042 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002043 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002044
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002045 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2046 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002047
2048 config->format = out->stream.common.get_format(&out->stream.common);
2049 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2050 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2051
2052 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002053 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002054 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002055
2056error_open:
2057 free(out);
2058 *stream_out = NULL;
2059 ALOGD("%s: exit: ret %d", __func__, ret);
2060 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002061}
2062
2063static void adev_close_output_stream(struct audio_hw_device *dev,
2064 struct audio_stream_out *stream)
2065{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002066 struct stream_out *out = (struct stream_out *)stream;
2067 struct audio_device *adev = out->dev;
2068
Eric Laurent994a6932013-07-17 11:51:42 -07002069 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002070 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002071 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2072 destroy_offload_callback_thread(out);
2073
2074 if (out->compr_config.codec != NULL)
2075 free(out->compr_config.codec);
2076 }
2077 pthread_cond_destroy(&out->cond);
2078 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002079 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002080 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002081}
2082
2083static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2084{
2085 struct audio_device *adev = (struct audio_device *)dev;
2086 struct str_parms *parms;
2087 char *str;
2088 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002089 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002090 int ret;
2091
Eric Laurent994a6932013-07-17 11:51:42 -07002092 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002093 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002094
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002095 voice_set_parameters(adev, parms);
2096 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002097
2098 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2099 if (ret >= 0) {
2100 /* When set to false, HAL should disable EC and NS
2101 * But it is currently not supported.
2102 */
2103 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2104 adev->bluetooth_nrec = true;
2105 else
2106 adev->bluetooth_nrec = false;
2107 }
2108
2109 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2110 if (ret >= 0) {
2111 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2112 adev->screen_off = false;
2113 else
2114 adev->screen_off = true;
2115 }
2116
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002117 ret = str_parms_get_int(parms, "rotation", &val);
2118 if (ret >= 0) {
2119 bool reverse_speakers = false;
2120 switch(val) {
2121 // FIXME: note that the code below assumes that the speakers are in the correct placement
2122 // relative to the user when the device is rotated 90deg from its default rotation. This
2123 // assumption is device-specific, not platform-specific like this code.
2124 case 270:
2125 reverse_speakers = true;
2126 break;
2127 case 0:
2128 case 90:
2129 case 180:
2130 break;
2131 default:
2132 ALOGE("%s: unexpected rotation of %d", __func__, val);
2133 }
2134 pthread_mutex_lock(&adev->lock);
2135 if (adev->speaker_lr_swap != reverse_speakers) {
2136 adev->speaker_lr_swap = reverse_speakers;
2137 // only update the selected device if there is active pcm playback
2138 struct audio_usecase *usecase;
2139 struct listnode *node;
2140 list_for_each(node, &adev->usecase_list) {
2141 usecase = node_to_item(node, struct audio_usecase, list);
2142 if (usecase->type == PCM_PLAYBACK) {
2143 select_devices(adev, usecase->id);
2144 break;
2145 }
2146 }
2147 }
2148 pthread_mutex_unlock(&adev->lock);
2149 }
2150
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002151 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002152 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002153 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002154 return ret;
2155}
2156
2157static char* adev_get_parameters(const struct audio_hw_device *dev,
2158 const char *keys)
2159{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002160 struct audio_device *adev = (struct audio_device *)dev;
2161 struct str_parms *reply = str_parms_create();
2162 struct str_parms *query = str_parms_create_str(keys);
2163 char *str;
2164
2165 audio_extn_get_parameters(adev, query, reply);
2166 platform_get_parameters(adev->platform, query, reply);
2167 str = str_parms_to_str(reply);
2168 str_parms_destroy(query);
2169 str_parms_destroy(reply);
2170
2171 ALOGV("%s: exit: returns - %s", __func__, str);
2172 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002173}
2174
2175static int adev_init_check(const struct audio_hw_device *dev)
2176{
2177 return 0;
2178}
2179
2180static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2181{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002182 int ret;
2183 struct audio_device *adev = (struct audio_device *)dev;
2184 pthread_mutex_lock(&adev->lock);
2185 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002186 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002187 pthread_mutex_unlock(&adev->lock);
2188 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002189}
2190
2191static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2192{
2193 return -ENOSYS;
2194}
2195
2196static int adev_get_master_volume(struct audio_hw_device *dev,
2197 float *volume)
2198{
2199 return -ENOSYS;
2200}
2201
2202static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2203{
2204 return -ENOSYS;
2205}
2206
2207static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2208{
2209 return -ENOSYS;
2210}
2211
2212static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2213{
2214 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002215 pthread_mutex_lock(&adev->lock);
2216 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002217 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002218 adev->mode = mode;
2219 }
2220 pthread_mutex_unlock(&adev->lock);
2221 return 0;
2222}
2223
2224static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2225{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002226 return voice_set_mic_mute((struct audio_device *)dev, state);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002227}
2228
2229static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2230{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002231 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002232 return 0;
2233}
2234
2235static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2236 const struct audio_config *config)
2237{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002238 int channel_count = popcount(config->channel_mask);
2239
2240 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2241}
2242
2243static int adev_open_input_stream(struct audio_hw_device *dev,
2244 audio_io_handle_t handle,
2245 audio_devices_t devices,
2246 struct audio_config *config,
2247 struct audio_stream_in **stream_in)
2248{
2249 struct audio_device *adev = (struct audio_device *)dev;
2250 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002251 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002252 int channel_count = popcount(config->channel_mask);
2253
Eric Laurent994a6932013-07-17 11:51:42 -07002254 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002255 *stream_in = NULL;
2256 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2257 return -EINVAL;
2258
2259 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2260
2261 in->stream.common.get_sample_rate = in_get_sample_rate;
2262 in->stream.common.set_sample_rate = in_set_sample_rate;
2263 in->stream.common.get_buffer_size = in_get_buffer_size;
2264 in->stream.common.get_channels = in_get_channels;
2265 in->stream.common.get_format = in_get_format;
2266 in->stream.common.set_format = in_set_format;
2267 in->stream.common.standby = in_standby;
2268 in->stream.common.dump = in_dump;
2269 in->stream.common.set_parameters = in_set_parameters;
2270 in->stream.common.get_parameters = in_get_parameters;
2271 in->stream.common.add_audio_effect = in_add_audio_effect;
2272 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2273 in->stream.set_gain = in_set_gain;
2274 in->stream.read = in_read;
2275 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2276
2277 in->device = devices;
2278 in->source = AUDIO_SOURCE_DEFAULT;
2279 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002280 in->standby = 1;
2281 in->channel_mask = config->channel_mask;
2282
2283 /* Update config params with the requested sample rate and channels */
2284 in->usecase = USECASE_AUDIO_RECORD;
2285 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002286 in->config.rate = config->sample_rate;
2287
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002288 if (channel_count == 6) {
2289 if(audio_extn_ssr_get_enabled()) {
2290 if(audio_extn_ssr_init(adev, in)) {
2291 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2292 ret = -EINVAL;
2293 goto err_open;
2294 }
2295 } else {
2296 ret = -EINVAL;
2297 goto err_open;
2298 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002299 } else {
2300 in->config.channels = channel_count;
2301 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2302 buffer_size = get_input_buffer_size(config->sample_rate,
2303 config->format,
2304 channel_count);
2305 in->config.period_size = buffer_size / frame_size;
2306 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002307
2308 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002309 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002310 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002311
2312err_open:
2313 free(in);
2314 *stream_in = NULL;
2315 return ret;
2316}
2317
2318static void adev_close_input_stream(struct audio_hw_device *dev,
2319 struct audio_stream_in *stream)
2320{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002321 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002322 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002323
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002324 in_standby(&stream->common);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002325 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2326 audio_extn_ssr_deinit();
2327 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002328 free(stream);
2329
2330 return;
2331}
2332
2333static int adev_dump(const audio_hw_device_t *device, int fd)
2334{
2335 return 0;
2336}
2337
2338static int adev_close(hw_device_t *device)
2339{
2340 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002341
2342 if (!adev)
2343 return 0;
2344
2345 pthread_mutex_lock(&adev_init_lock);
2346
2347 if ((--audio_device_ref_count) == 0) {
2348 audio_route_free(adev->audio_route);
2349 free(adev->snd_dev_ref_cnt);
2350 platform_deinit(adev->platform);
2351 audio_extn_listen_deinit(adev);
2352 free(device);
2353 adev = NULL;
2354 }
2355 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002356 return 0;
2357}
2358
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002359static int adev_open(const hw_module_t *module, const char *name,
2360 hw_device_t **device)
2361{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002362 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002363
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002364 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002365 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2366
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002367 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002368 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002369 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002370 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002371 ALOGD("%s: returning existing instance of adev", __func__);
2372 ALOGD("%s: exit", __func__);
2373 pthread_mutex_unlock(&adev_init_lock);
2374 return 0;
2375 }
2376
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002377 adev = calloc(1, sizeof(struct audio_device));
2378
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002379 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2380 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2381 adev->device.common.module = (struct hw_module_t *)module;
2382 adev->device.common.close = adev_close;
2383
2384 adev->device.init_check = adev_init_check;
2385 adev->device.set_voice_volume = adev_set_voice_volume;
2386 adev->device.set_master_volume = adev_set_master_volume;
2387 adev->device.get_master_volume = adev_get_master_volume;
2388 adev->device.set_master_mute = adev_set_master_mute;
2389 adev->device.get_master_mute = adev_get_master_mute;
2390 adev->device.set_mode = adev_set_mode;
2391 adev->device.set_mic_mute = adev_set_mic_mute;
2392 adev->device.get_mic_mute = adev_get_mic_mute;
2393 adev->device.set_parameters = adev_set_parameters;
2394 adev->device.get_parameters = adev_get_parameters;
2395 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2396 adev->device.open_output_stream = adev_open_output_stream;
2397 adev->device.close_output_stream = adev_close_output_stream;
2398 adev->device.open_input_stream = adev_open_input_stream;
2399 adev->device.close_input_stream = adev_close_input_stream;
2400 adev->device.dump = adev_dump;
2401
2402 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002403 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002404 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002405 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002406 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002407 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002408 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002409 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002410 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002411 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002412 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002413
2414 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002415 adev->platform = platform_init(adev);
2416 if (!adev->platform) {
2417 free(adev->snd_dev_ref_cnt);
2418 free(adev);
2419 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2420 *device = NULL;
2421 return -EINVAL;
2422 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002423
2424 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2425 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2426 if (adev->visualizer_lib == NULL) {
2427 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2428 } else {
2429 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2430 adev->visualizer_start_output =
2431 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2432 "visualizer_hal_start_output");
2433 adev->visualizer_stop_output =
2434 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2435 "visualizer_hal_stop_output");
2436 }
2437 }
Kiran Kandi910e1862013-10-29 13:29:42 -07002438 audio_extn_listen_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07002439
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002440 *device = &adev->device.common;
2441
Kiran Kandi910e1862013-10-29 13:29:42 -07002442 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002443 pthread_mutex_unlock(&adev_init_lock);
2444
Eric Laurent994a6932013-07-17 11:51:42 -07002445 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002446 return 0;
2447}
2448
2449static struct hw_module_methods_t hal_module_methods = {
2450 .open = adev_open,
2451};
2452
2453struct audio_module HAL_MODULE_INFO_SYM = {
2454 .common = {
2455 .tag = HARDWARE_MODULE_TAG,
2456 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2457 .hal_api_version = HARDWARE_HAL_API_VERSION,
2458 .id = AUDIO_HARDWARE_MODULE_ID,
2459 .name = "QCOM Audio HAL",
2460 .author = "Code Aurora Forum",
2461 .methods = &hal_module_methods,
2462 },
2463};