blob: 59e7c862231793a84e106d40a052b78945ffb2be [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",
Eric Laurentb23d5282013-05-14 15:27:20 -0700120};
121
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800122
123#define STRING_TO_ENUM(string) { #string, string }
124
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800125struct string_to_enum {
126 const char *name;
127 uint32_t value;
128};
129
130static const struct string_to_enum out_channels_name_to_enum_table[] = {
131 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
132 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
133 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
134};
135
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700136static struct audio_device *adev = NULL;
137static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700138static unsigned int audio_device_ref_count;
139
Haynes Mathew George5191a852013-09-11 14:19:36 -0700140static int set_voice_volume_l(struct audio_device *adev, float volume);
141
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700142static bool is_supported_format(audio_format_t format)
143{
Eric Laurent86e17132013-09-12 17:49:30 -0700144 if (format == AUDIO_FORMAT_MP3 ||
145 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700146 return true;
147
148 return false;
149}
150
151static int get_snd_codec_id(audio_format_t format)
152{
153 int id = 0;
154
155 switch (format) {
156 case AUDIO_FORMAT_MP3:
157 id = SND_AUDIOCODEC_MP3;
158 break;
159 case AUDIO_FORMAT_AAC:
160 id = SND_AUDIOCODEC_AAC;
161 break;
162 default:
163 ALOGE("%s: Unsupported audio format", __func__);
164 }
165
166 return id;
167}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800168
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700169static int enable_audio_route(struct audio_device *adev,
170 struct audio_usecase *usecase,
171 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800172{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700173 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700174 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800175
176 if (usecase == NULL)
177 return -EINVAL;
178
179 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
180
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800181 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700182 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800183 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700184 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800185
186 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700187 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700188 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700189 audio_route_apply_path(adev->audio_route, mixer_path);
190 if (update_mixer)
191 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800192
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800193 ALOGV("%s: exit", __func__);
194 return 0;
195}
196
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700197int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700198 struct audio_usecase *usecase,
199 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800200{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700201 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700202 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800203
204 if (usecase == NULL)
205 return -EINVAL;
206
207 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700208 if (usecase->type == PCM_CAPTURE)
209 snd_device = usecase->in_snd_device;
210 else
211 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800212 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700213 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700214 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700215 audio_route_reset_path(adev->audio_route, mixer_path);
216 if (update_mixer)
217 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800218
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800219 ALOGV("%s: exit", __func__);
220 return 0;
221}
222
223static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700224 snd_device_t snd_device,
225 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800226{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700227 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
228
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800229 if (snd_device < SND_DEVICE_MIN ||
230 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800231 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800232 return -EINVAL;
233 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700234
235 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700236
237 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
238 ALOGE("%s: Invalid sound device returned", __func__);
239 return -EINVAL;
240 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700241 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700242 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700243 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700244 return 0;
245 }
246
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700247 /* start usb playback thread */
248 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
249 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
250 audio_extn_usb_start_playback(adev);
251
252 /* start usb capture thread */
253 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
254 audio_extn_usb_start_capture(adev);
255
Eric Laurentb23d5282013-05-14 15:27:20 -0700256 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700257 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800258 return -EINVAL;
259 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800260
Eric Laurent994a6932013-07-17 11:51:42 -0700261 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700262 snd_device, device_name);
263 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700264 if (update_mixer)
265 audio_route_update_mixer(adev->audio_route);
266
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800267 return 0;
268}
269
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700270int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700271 snd_device_t snd_device,
272 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800273{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700274 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
275
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800276 if (snd_device < SND_DEVICE_MIN ||
277 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800278 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800279 return -EINVAL;
280 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700281 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
282 ALOGE("%s: device ref cnt is already 0", __func__);
283 return -EINVAL;
284 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700285
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700286 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700287
288 /* exit usb play back thread */
289 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
290 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
291 audio_extn_usb_stop_playback();
292
293 /* exit usb capture thread */
294 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
295 audio_extn_usb_stop_capture(adev);
296
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);
305 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700306 if (update_mixer)
307 audio_route_update_mixer(adev->audio_route);
308 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700309
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800310 return 0;
311}
312
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700313static void check_usecases_codec_backend(struct audio_device *adev,
314 struct audio_usecase *uc_info,
315 snd_device_t snd_device)
316{
317 struct listnode *node;
318 struct audio_usecase *usecase;
319 bool switch_device[AUDIO_USECASE_MAX];
320 int i, num_uc_to_switch = 0;
321
322 /*
323 * This function is to make sure that all the usecases that are active on
324 * the hardware codec backend are always routed to any one device that is
325 * handled by the hardware codec.
326 * For example, if low-latency and deep-buffer usecases are currently active
327 * on speaker and out_set_parameters(headset) is received on low-latency
328 * output, then we have to make sure deep-buffer is also switched to headset,
329 * because of the limitation that both the devices cannot be enabled
330 * at the same time as they share the same backend.
331 */
332 /* Disable all the usecases on the shared backend other than the
333 specified usecase */
334 for (i = 0; i < AUDIO_USECASE_MAX; i++)
335 switch_device[i] = false;
336
337 list_for_each(node, &adev->usecase_list) {
338 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700339 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700340 usecase != uc_info &&
341 usecase->out_snd_device != snd_device &&
342 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
343 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
344 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700345 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700346 disable_audio_route(adev, usecase, false);
347 switch_device[usecase->id] = true;
348 num_uc_to_switch++;
349 }
350 }
351
352 if (num_uc_to_switch) {
353 /* Make sure all the streams are de-routed before disabling the device */
354 audio_route_update_mixer(adev->audio_route);
355
356 list_for_each(node, &adev->usecase_list) {
357 usecase = node_to_item(node, struct audio_usecase, list);
358 if (switch_device[usecase->id]) {
359 disable_snd_device(adev, usecase->out_snd_device, false);
360 enable_snd_device(adev, snd_device, false);
361 }
362 }
363
364 /* Make sure new snd device is enabled before re-routing the streams */
365 audio_route_update_mixer(adev->audio_route);
366
367 /* Re-route all the usecases on the shared backend other than the
368 specified usecase to new snd devices */
369 list_for_each(node, &adev->usecase_list) {
370 usecase = node_to_item(node, struct audio_usecase, list);
371 /* Update the out_snd_device only before enabling the audio route */
372 if (switch_device[usecase->id] ) {
373 usecase->out_snd_device = snd_device;
374 enable_audio_route(adev, usecase, false);
375 }
376 }
377
378 audio_route_update_mixer(adev->audio_route);
379 }
380}
381
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700382static void check_and_route_capture_usecases(struct audio_device *adev,
383 struct audio_usecase *uc_info,
384 snd_device_t snd_device)
385{
386 struct listnode *node;
387 struct audio_usecase *usecase;
388 bool switch_device[AUDIO_USECASE_MAX];
389 int i, num_uc_to_switch = 0;
390
391 /*
392 * This function is to make sure that all the active capture usecases
393 * are always routed to the same input sound device.
394 * For example, if audio-record and voice-call usecases are currently
395 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
396 * is received for voice call then we have to make sure that audio-record
397 * usecase is also switched to earpiece i.e. voice-dmic-ef,
398 * because of the limitation that two devices cannot be enabled
399 * at the same time if they share the same backend.
400 */
401 for (i = 0; i < AUDIO_USECASE_MAX; i++)
402 switch_device[i] = false;
403
404 list_for_each(node, &adev->usecase_list) {
405 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700406 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700407 usecase != uc_info &&
408 usecase->in_snd_device != snd_device) {
409 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
410 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700411 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700412 disable_audio_route(adev, usecase, false);
413 switch_device[usecase->id] = true;
414 num_uc_to_switch++;
415 }
416 }
417
418 if (num_uc_to_switch) {
419 /* Make sure all the streams are de-routed before disabling the device */
420 audio_route_update_mixer(adev->audio_route);
421
422 list_for_each(node, &adev->usecase_list) {
423 usecase = node_to_item(node, struct audio_usecase, list);
424 if (switch_device[usecase->id]) {
425 disable_snd_device(adev, usecase->in_snd_device, false);
426 enable_snd_device(adev, snd_device, false);
427 }
428 }
429
430 /* Make sure new snd device is enabled before re-routing the streams */
431 audio_route_update_mixer(adev->audio_route);
432
433 /* Re-route all the usecases on the shared backend other than the
434 specified usecase to new snd devices */
435 list_for_each(node, &adev->usecase_list) {
436 usecase = node_to_item(node, struct audio_usecase, list);
437 /* Update the in_snd_device only before enabling the audio route */
438 if (switch_device[usecase->id] ) {
439 usecase->in_snd_device = snd_device;
440 enable_audio_route(adev, usecase, false);
441 }
442 }
443
444 audio_route_update_mixer(adev->audio_route);
445 }
446}
447
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700448static int disable_all_usecases_of_type(struct audio_device *adev,
449 usecase_type_t usecase_type,
450 bool update_mixer)
451{
452 struct audio_usecase *usecase;
453 struct listnode *node;
454 int ret = 0;
455
456 list_for_each(node, &adev->usecase_list) {
457 usecase = node_to_item(node, struct audio_usecase, list);
458 if (usecase->type == usecase_type) {
459 ALOGV("%s: usecase id %d", __func__, usecase->id);
460 ret = disable_audio_route(adev, usecase, update_mixer);
461 if (ret) {
462 ALOGE("%s: Failed to disable usecase id %d",
463 __func__, usecase->id);
464 }
465 }
466 }
467
468 return ret;
469}
470
471static int enable_all_usecases_of_type(struct audio_device *adev,
472 usecase_type_t usecase_type,
473 bool update_mixer)
474{
475 struct audio_usecase *usecase;
476 struct listnode *node;
477 int ret = 0;
478
479 list_for_each(node, &adev->usecase_list) {
480 usecase = node_to_item(node, struct audio_usecase, list);
481 if (usecase->type == usecase_type) {
482 ALOGV("%s: usecase id %d", __func__, usecase->id);
483 ret = enable_audio_route(adev, usecase, update_mixer);
484 if (ret) {
485 ALOGE("%s: Failed to enable usecase id %d",
486 __func__, usecase->id);
487 }
488 }
489 }
490
491 return ret;
492}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800493
494/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700495static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800496{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700497 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700498 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800499
500 switch (channels) {
501 /*
502 * Do not handle stereo output in Multi-channel cases
503 * Stereo case is handled in normal playback path
504 */
505 case 6:
506 ALOGV("%s: HDMI supports 5.1", __func__);
507 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
508 break;
509 case 8:
510 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
511 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
512 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
513 break;
514 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700515 ALOGE("HDMI does not support multi channel playback");
516 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800517 break;
518 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700519 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800520}
521
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700522static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
523{
524 struct audio_usecase *usecase;
525 struct listnode *node;
526
527 list_for_each(node, &adev->usecase_list) {
528 usecase = node_to_item(node, struct audio_usecase, list);
529 if (usecase->type == VOICE_CALL) {
530 ALOGV("%s: usecase id %d", __func__, usecase->id);
531 return usecase->id;
532 }
533 }
534 return USECASE_INVALID;
535}
536
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700537struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700538 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700539{
540 struct audio_usecase *usecase;
541 struct listnode *node;
542
543 list_for_each(node, &adev->usecase_list) {
544 usecase = node_to_item(node, struct audio_usecase, list);
545 if (usecase->id == uc_id)
546 return usecase;
547 }
548 return NULL;
549}
550
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700551int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800552{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800553 snd_device_t out_snd_device = SND_DEVICE_NONE;
554 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700555 struct audio_usecase *usecase = NULL;
556 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800557 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700558 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800559
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700560 usecase = get_usecase_from_list(adev, uc_id);
561 if (usecase == NULL) {
562 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
563 return -EINVAL;
564 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800565
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700566 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700567 out_snd_device = platform_get_output_snd_device(adev->platform,
568 usecase->stream.out->devices);
569 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700570 usecase->devices = usecase->stream.out->devices;
571 } else {
572 /*
573 * If the voice call is active, use the sound devices of voice call usecase
574 * so that it would not result any device switch. All the usecases will
575 * be switched to new device when select_devices() is called for voice call
576 * usecase. This is to avoid switching devices for voice call when
577 * check_usecases_codec_backend() is called below.
578 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700579 if (voice_is_in_call(adev)) {
580 vc_usecase = get_usecase_from_list(adev,
581 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700582 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
583 in_snd_device = vc_usecase->in_snd_device;
584 out_snd_device = vc_usecase->out_snd_device;
585 }
586 }
587 if (usecase->type == PCM_PLAYBACK) {
588 usecase->devices = usecase->stream.out->devices;
589 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700590 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700591 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700592 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700593 if (usecase->stream.out == adev->primary_output &&
594 adev->active_input &&
595 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
596 select_devices(adev, adev->active_input->usecase);
597 }
598 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700599 } else if (usecase->type == PCM_CAPTURE) {
600 usecase->devices = usecase->stream.in->device;
601 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700602 if (in_snd_device == SND_DEVICE_NONE) {
603 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
604 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700605 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700606 adev->primary_output->devices);
607 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700608 in_snd_device = platform_get_input_snd_device(adev->platform,
609 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700610 }
611 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700612 }
613 }
614
615 if (out_snd_device == usecase->out_snd_device &&
616 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800617 return 0;
618 }
619
sangwoobc677242013-08-08 16:53:43 +0900620 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700621 out_snd_device, platform_get_snd_device_name(out_snd_device),
622 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800623
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800624 /*
625 * Limitation: While in call, to do a device switch we need to disable
626 * and enable both RX and TX devices though one of them is same as current
627 * device.
628 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700629 if (usecase->type == VOICE_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700630 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700631 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800632 }
633
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700634 /* Disable current sound devices */
635 if (usecase->out_snd_device != SND_DEVICE_NONE) {
636 disable_audio_route(adev, usecase, true);
637 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800638 }
639
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700640 if (usecase->in_snd_device != SND_DEVICE_NONE) {
641 disable_audio_route(adev, usecase, true);
642 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800643 }
644
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700645 /* Enable new sound devices */
646 if (out_snd_device != SND_DEVICE_NONE) {
647 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
648 check_usecases_codec_backend(adev, usecase, out_snd_device);
649 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800650 }
651
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700652 if (in_snd_device != SND_DEVICE_NONE) {
653 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700654 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700655 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700656
Eric Laurentb23d5282013-05-14 15:27:20 -0700657 if (usecase->type == VOICE_CALL)
658 status = platform_switch_voice_call_device_post(adev->platform,
659 out_snd_device,
660 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800661
sangwoo170731f2013-06-08 15:36:36 +0900662 audio_route_update_mixer(adev->audio_route);
663
664 usecase->in_snd_device = in_snd_device;
665 usecase->out_snd_device = out_snd_device;
666
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700667 if (usecase->type == VOICE_CALL)
668 enable_all_usecases_of_type(adev, VOICE_CALL, true);
669 else
670 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900671
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800672 return status;
673}
674
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800675static int stop_input_stream(struct stream_in *in)
676{
677 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800678 struct audio_usecase *uc_info;
679 struct audio_device *adev = in->dev;
680
Eric Laurentc8400632013-02-14 19:04:54 -0800681 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800682
Eric Laurent994a6932013-07-17 11:51:42 -0700683 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700684 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800685 uc_info = get_usecase_from_list(adev, in->usecase);
686 if (uc_info == NULL) {
687 ALOGE("%s: Could not find the usecase (%d) in the list",
688 __func__, in->usecase);
689 return -EINVAL;
690 }
691
Eric Laurent150dbfe2013-02-27 14:31:02 -0800692 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700693 disable_audio_route(adev, uc_info, true);
694
695 /* 2. Disable the tx device */
696 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800697
Kiran Kandi910e1862013-10-29 13:29:42 -0700698 audio_extn_listen_update_status(uc_info,
699 LISTEN_EVENT_AUDIO_CAPTURE_INACTIVE);
700
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800701 list_remove(&uc_info->list);
702 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800703
Eric Laurent994a6932013-07-17 11:51:42 -0700704 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800705 return ret;
706}
707
708int start_input_stream(struct stream_in *in)
709{
710 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800711 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800712 struct audio_usecase *uc_info;
713 struct audio_device *adev = in->dev;
714
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700715 in->usecase = platform_get_usecase_from_source(in->source);
Eric Laurent994a6932013-07-17 11:51:42 -0700716 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700717
718 /* Check if source matches incall recording usecase criteria */
719 ret = voice_check_and_set_incall_rec_usecase(adev, in);
720 if (ret)
721 goto error_config;
722 else
723 ALOGV("%s: usecase(%d)", __func__, in->usecase);
724
Eric Laurentb23d5282013-05-14 15:27:20 -0700725 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800726 if (in->pcm_device_id < 0) {
727 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
728 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800729 ret = -EINVAL;
730 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800731 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700732
733 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800734 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
735 uc_info->id = in->usecase;
736 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800737 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700738 uc_info->devices = in->device;
739 uc_info->in_snd_device = SND_DEVICE_NONE;
740 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800741
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800742 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700743 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800744
Kiran Kandi910e1862013-10-29 13:29:42 -0700745 audio_extn_listen_update_status(uc_info,
746 LISTEN_EVENT_AUDIO_CAPTURE_ACTIVE);
747
Eric Laurentc8400632013-02-14 19:04:54 -0800748 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
749 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800750 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
751 PCM_IN, &in->config);
752 if (in->pcm && !pcm_is_ready(in->pcm)) {
753 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
754 pcm_close(in->pcm);
755 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800756 ret = -EIO;
757 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800758 }
Eric Laurent994a6932013-07-17 11:51:42 -0700759 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800760 return ret;
761
762error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800763 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800764
765error_config:
766 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700767 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800768
769 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800770}
771
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700772/* must be called with out->lock locked */
773static int send_offload_cmd_l(struct stream_out* out, int command)
774{
775 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
776
777 ALOGVV("%s %d", __func__, command);
778
779 cmd->cmd = command;
780 list_add_tail(&out->offload_cmd_list, &cmd->node);
781 pthread_cond_signal(&out->offload_cond);
782 return 0;
783}
784
785/* must be called iwth out->lock locked */
786static void stop_compressed_output_l(struct stream_out *out)
787{
788 out->offload_state = OFFLOAD_STATE_IDLE;
789 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700790 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700791 if (out->compr != NULL) {
792 compress_stop(out->compr);
793 while (out->offload_thread_blocked) {
794 pthread_cond_wait(&out->cond, &out->lock);
795 }
796 }
797}
798
799static void *offload_thread_loop(void *context)
800{
801 struct stream_out *out = (struct stream_out *) context;
802 struct listnode *item;
803
804 out->offload_state = OFFLOAD_STATE_IDLE;
805 out->playback_started = 0;
806
807 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
808 set_sched_policy(0, SP_FOREGROUND);
809 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
810
811 ALOGV("%s", __func__);
812 pthread_mutex_lock(&out->lock);
813 for (;;) {
814 struct offload_cmd *cmd = NULL;
815 stream_callback_event_t event;
816 bool send_callback = false;
817
818 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
819 __func__, list_empty(&out->offload_cmd_list),
820 out->offload_state);
821 if (list_empty(&out->offload_cmd_list)) {
822 ALOGV("%s SLEEPING", __func__);
823 pthread_cond_wait(&out->offload_cond, &out->lock);
824 ALOGV("%s RUNNING", __func__);
825 continue;
826 }
827
828 item = list_head(&out->offload_cmd_list);
829 cmd = node_to_item(item, struct offload_cmd, node);
830 list_remove(item);
831
832 ALOGVV("%s STATE %d CMD %d out->compr %p",
833 __func__, out->offload_state, cmd->cmd, out->compr);
834
835 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
836 free(cmd);
837 break;
838 }
839
840 if (out->compr == NULL) {
841 ALOGE("%s: Compress handle is NULL", __func__);
842 pthread_cond_signal(&out->cond);
843 continue;
844 }
845 out->offload_thread_blocked = true;
846 pthread_mutex_unlock(&out->lock);
847 send_callback = false;
848 switch(cmd->cmd) {
849 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
850 compress_wait(out->compr, -1);
851 send_callback = true;
852 event = STREAM_CBK_EVENT_WRITE_READY;
853 break;
854 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700855 compress_next_track(out->compr);
856 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700857 send_callback = true;
858 event = STREAM_CBK_EVENT_DRAIN_READY;
859 break;
860 case OFFLOAD_CMD_DRAIN:
861 compress_drain(out->compr);
862 send_callback = true;
863 event = STREAM_CBK_EVENT_DRAIN_READY;
864 break;
865 default:
866 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
867 break;
868 }
869 pthread_mutex_lock(&out->lock);
870 out->offload_thread_blocked = false;
871 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700872 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700873 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700874 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700875 free(cmd);
876 }
877
878 pthread_cond_signal(&out->cond);
879 while (!list_empty(&out->offload_cmd_list)) {
880 item = list_head(&out->offload_cmd_list);
881 list_remove(item);
882 free(node_to_item(item, struct offload_cmd, node));
883 }
884 pthread_mutex_unlock(&out->lock);
885
886 return NULL;
887}
888
889static int create_offload_callback_thread(struct stream_out *out)
890{
891 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
892 list_init(&out->offload_cmd_list);
893 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
894 offload_thread_loop, out);
895 return 0;
896}
897
898static int destroy_offload_callback_thread(struct stream_out *out)
899{
900 pthread_mutex_lock(&out->lock);
901 stop_compressed_output_l(out);
902 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
903
904 pthread_mutex_unlock(&out->lock);
905 pthread_join(out->offload_thread, (void **) NULL);
906 pthread_cond_destroy(&out->offload_cond);
907
908 return 0;
909}
910
Eric Laurent07eeafd2013-10-06 12:52:49 -0700911static bool allow_hdmi_channel_config(struct audio_device *adev)
912{
913 struct listnode *node;
914 struct audio_usecase *usecase;
915 bool ret = true;
916
917 list_for_each(node, &adev->usecase_list) {
918 usecase = node_to_item(node, struct audio_usecase, list);
919 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
920 /*
921 * If voice call is already existing, do not proceed further to avoid
922 * disabling/enabling both RX and TX devices, CSD calls, etc.
923 * Once the voice call done, the HDMI channels can be configured to
924 * max channels of remaining use cases.
925 */
926 if (usecase->id == USECASE_VOICE_CALL) {
927 ALOGD("%s: voice call is active, no change in HDMI channels",
928 __func__);
929 ret = false;
930 break;
931 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
932 ALOGD("%s: multi channel playback is active, "
933 "no change in HDMI channels", __func__);
934 ret = false;
935 break;
936 }
937 }
938 }
939 return ret;
940}
941
942static int check_and_set_hdmi_channels(struct audio_device *adev,
943 unsigned int channels)
944{
945 struct listnode *node;
946 struct audio_usecase *usecase;
947
948 /* Check if change in HDMI channel config is allowed */
949 if (!allow_hdmi_channel_config(adev))
950 return 0;
951
952 if (channels == adev->cur_hdmi_channels) {
953 ALOGD("%s: Requested channels are same as current", __func__);
954 return 0;
955 }
956
957 platform_set_hdmi_channels(adev->platform, channels);
958 adev->cur_hdmi_channels = channels;
959
960 /*
961 * Deroute all the playback streams routed to HDMI so that
962 * the back end is deactivated. Note that backend will not
963 * be deactivated if any one stream is connected to it.
964 */
965 list_for_each(node, &adev->usecase_list) {
966 usecase = node_to_item(node, struct audio_usecase, list);
967 if (usecase->type == PCM_PLAYBACK &&
968 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
969 disable_audio_route(adev, usecase, true);
970 }
971 }
972
973 /*
974 * Enable all the streams disabled above. Now the HDMI backend
975 * will be activated with new channel configuration
976 */
977 list_for_each(node, &adev->usecase_list) {
978 usecase = node_to_item(node, struct audio_usecase, list);
979 if (usecase->type == PCM_PLAYBACK &&
980 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
981 enable_audio_route(adev, usecase, true);
982 }
983 }
984
985 return 0;
986}
987
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800988static int stop_output_stream(struct stream_out *out)
989{
990 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800991 struct audio_usecase *uc_info;
992 struct audio_device *adev = out->dev;
993
Eric Laurent994a6932013-07-17 11:51:42 -0700994 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700995 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800996 uc_info = get_usecase_from_list(adev, out->usecase);
997 if (uc_info == NULL) {
998 ALOGE("%s: Could not find the usecase (%d) in the list",
999 __func__, out->usecase);
1000 return -EINVAL;
1001 }
1002
Eric Laurentc4aef752013-09-12 17:45:53 -07001003 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
1004 adev->visualizer_stop_output != NULL)
1005 adev->visualizer_stop_output(out->handle);
1006
Eric Laurent150dbfe2013-02-27 14:31:02 -08001007 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001008 disable_audio_route(adev, uc_info, true);
1009
1010 /* 2. Disable the rx device */
1011 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001012
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001013 list_remove(&uc_info->list);
1014 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001015
Eric Laurent07eeafd2013-10-06 12:52:49 -07001016 /* Must be called after removing the usecase from list */
1017 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1018 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1019
Eric Laurent994a6932013-07-17 11:51:42 -07001020 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001021 return ret;
1022}
1023
1024int start_output_stream(struct stream_out *out)
1025{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001027 struct audio_usecase *uc_info;
1028 struct audio_device *adev = out->dev;
1029
Eric Laurent994a6932013-07-17 11:51:42 -07001030 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001031 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001032 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001033 if (out->pcm_device_id < 0) {
1034 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1035 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001036 ret = -EINVAL;
1037 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001038 }
1039
1040 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1041 uc_info->id = out->usecase;
1042 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001043 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001044 uc_info->devices = out->devices;
1045 uc_info->in_snd_device = SND_DEVICE_NONE;
1046 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001047
Eric Laurent07eeafd2013-10-06 12:52:49 -07001048 /* This must be called before adding this usecase to the list */
1049 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1050 check_and_set_hdmi_channels(adev, out->config.channels);
1051
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001052 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001053
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001054 select_devices(adev, out->usecase);
1055
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001056 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1057 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001058 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1059 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001060 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001061 if (out->pcm && !pcm_is_ready(out->pcm)) {
1062 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1063 pcm_close(out->pcm);
1064 out->pcm = NULL;
1065 ret = -EIO;
1066 goto error_open;
1067 }
1068 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001069 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001070 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1071 COMPRESS_IN, &out->compr_config);
1072 if (out->compr && !is_compress_ready(out->compr)) {
1073 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1074 compress_close(out->compr);
1075 out->compr = NULL;
1076 ret = -EIO;
1077 goto error_open;
1078 }
1079 if (out->offload_callback)
1080 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001081
1082 if (adev->visualizer_start_output != NULL)
1083 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001084 }
Eric Laurent994a6932013-07-17 11:51:42 -07001085 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001086 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001087error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001088 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001089error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001090 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001091}
1092
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001093static int check_input_parameters(uint32_t sample_rate,
1094 audio_format_t format,
1095 int channel_count)
1096{
1097 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1098
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001099 if ((channel_count < 1) || (channel_count > 6)) return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001100
1101 switch (sample_rate) {
1102 case 8000:
1103 case 11025:
1104 case 12000:
1105 case 16000:
1106 case 22050:
1107 case 24000:
1108 case 32000:
1109 case 44100:
1110 case 48000:
1111 break;
1112 default:
1113 return -EINVAL;
1114 }
1115
1116 return 0;
1117}
1118
1119static size_t get_input_buffer_size(uint32_t sample_rate,
1120 audio_format_t format,
1121 int channel_count)
1122{
1123 size_t size = 0;
1124
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001125 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1126 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001127
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001128 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1129 /* ToDo: should use frame_size computed based on the format and
1130 channel_count here. */
1131 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001132
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001133 /* make sure the size is multiple of 64 */
1134 size += 0x3f;
1135 size &= ~0x3f;
1136
1137 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001138}
1139
1140static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1141{
1142 struct stream_out *out = (struct stream_out *)stream;
1143
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001144 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001145}
1146
1147static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1148{
1149 return -ENOSYS;
1150}
1151
1152static size_t out_get_buffer_size(const struct audio_stream *stream)
1153{
1154 struct stream_out *out = (struct stream_out *)stream;
1155
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001156 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1157 return out->compr_config.fragment_size;
1158 }
1159
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001160 return out->config.period_size * audio_stream_frame_size(stream);
1161}
1162
1163static uint32_t out_get_channels(const struct audio_stream *stream)
1164{
1165 struct stream_out *out = (struct stream_out *)stream;
1166
1167 return out->channel_mask;
1168}
1169
1170static audio_format_t out_get_format(const struct audio_stream *stream)
1171{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001172 struct stream_out *out = (struct stream_out *)stream;
1173
1174 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001175}
1176
1177static int out_set_format(struct audio_stream *stream, audio_format_t format)
1178{
1179 return -ENOSYS;
1180}
1181
1182static int out_standby(struct audio_stream *stream)
1183{
1184 struct stream_out *out = (struct stream_out *)stream;
1185 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001186
Eric Laurent994a6932013-07-17 11:51:42 -07001187 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001188 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001189
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001190 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001191 if (!out->standby) {
1192 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001193 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1194 if (out->pcm) {
1195 pcm_close(out->pcm);
1196 out->pcm = NULL;
1197 }
1198 } else {
1199 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001200 out->gapless_mdata.encoder_delay = 0;
1201 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001202 if (out->compr != NULL) {
1203 compress_close(out->compr);
1204 out->compr = NULL;
1205 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001206 }
1207 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001208 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001209 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001210 }
1211 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001212 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001213 return 0;
1214}
1215
1216static int out_dump(const struct audio_stream *stream, int fd)
1217{
1218 return 0;
1219}
1220
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001221static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1222{
1223 int ret = 0;
1224 char value[32];
1225 struct compr_gapless_mdata tmp_mdata;
1226
1227 if (!out || !parms) {
1228 return -EINVAL;
1229 }
1230
1231 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1232 if (ret >= 0) {
1233 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1234 } else {
1235 return -EINVAL;
1236 }
1237
1238 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1239 if (ret >= 0) {
1240 tmp_mdata.encoder_padding = atoi(value);
1241 } else {
1242 return -EINVAL;
1243 }
1244
1245 out->gapless_mdata = tmp_mdata;
1246 out->send_new_metadata = 1;
1247 ALOGV("%s new encoder delay %u and padding %u", __func__,
1248 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1249
1250 return 0;
1251}
1252
1253
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001254static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1255{
1256 struct stream_out *out = (struct stream_out *)stream;
1257 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001258 struct audio_usecase *usecase;
1259 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001260 struct str_parms *parms;
1261 char value[32];
1262 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001263 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001264
sangwoobc677242013-08-08 16:53:43 +09001265 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001266 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001267 parms = str_parms_create_str(kvpairs);
1268 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1269 if (ret >= 0) {
1270 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001271 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001272 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001274 /*
1275 * When HDMI cable is unplugged the music playback is paused and
1276 * the policy manager sends routing=0. But the audioflinger
1277 * continues to write data until standby time (3sec).
1278 * As the HDMI core is turned off, the write gets blocked.
1279 * Avoid this by routing audio to speaker until standby.
1280 */
1281 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1282 val == AUDIO_DEVICE_NONE) {
1283 val = AUDIO_DEVICE_OUT_SPEAKER;
1284 }
1285
1286 /*
1287 * select_devices() call below switches all the usecases on the same
1288 * backend to the new device. Refer to check_usecases_codec_backend() in
1289 * the select_devices(). But how do we undo this?
1290 *
1291 * For example, music playback is active on headset (deep-buffer usecase)
1292 * and if we go to ringtones and select a ringtone, low-latency usecase
1293 * will be started on headset+speaker. As we can't enable headset+speaker
1294 * and headset devices at the same time, select_devices() switches the music
1295 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1296 * So when the ringtone playback is completed, how do we undo the same?
1297 *
1298 * We are relying on the out_set_parameters() call on deep-buffer output,
1299 * once the ringtone playback is ended.
1300 * NOTE: We should not check if the current devices are same as new devices.
1301 * Because select_devices() must be called to switch back the music
1302 * playback to headset.
1303 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001304 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001305 out->devices = val;
1306
1307 if (!out->standby)
1308 select_devices(adev, out->usecase);
1309
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001310 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1311 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001312 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001313 voice_start_call(adev);
1314 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1315 voice_is_in_call(adev) &&
1316 (out == adev->primary_output)) {
1317 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001318 }
1319 }
1320
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001321 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1322 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001323 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001324 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001325 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001326
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001327 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001328 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001329 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001330
1331 if (out == adev->primary_output) {
1332 pthread_mutex_lock(&adev->lock);
1333 audio_extn_set_parameters(adev, parms);
1334 pthread_mutex_unlock(&adev->lock);
1335 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001336 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1337 parse_compress_metadata(out, parms);
1338 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001339
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001340 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001341 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001342 return ret;
1343}
1344
1345static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1346{
1347 struct stream_out *out = (struct stream_out *)stream;
1348 struct str_parms *query = str_parms_create_str(keys);
1349 char *str;
1350 char value[256];
1351 struct str_parms *reply = str_parms_create();
1352 size_t i, j;
1353 int ret;
1354 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001355 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001356 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1357 if (ret >= 0) {
1358 value[0] = '\0';
1359 i = 0;
1360 while (out->supported_channel_masks[i] != 0) {
1361 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1362 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1363 if (!first) {
1364 strcat(value, "|");
1365 }
1366 strcat(value, out_channels_name_to_enum_table[j].name);
1367 first = false;
1368 break;
1369 }
1370 }
1371 i++;
1372 }
1373 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1374 str = str_parms_to_str(reply);
1375 } else {
1376 str = strdup(keys);
1377 }
1378 str_parms_destroy(query);
1379 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001380 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001381 return str;
1382}
1383
1384static uint32_t out_get_latency(const struct audio_stream_out *stream)
1385{
1386 struct stream_out *out = (struct stream_out *)stream;
1387
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001388 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1389 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1390
1391 return (out->config.period_count * out->config.period_size * 1000) /
1392 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001393}
1394
1395static int out_set_volume(struct audio_stream_out *stream, float left,
1396 float right)
1397{
Eric Laurenta9024de2013-04-04 09:19:12 -07001398 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001399 int volume[2];
1400
Eric Laurenta9024de2013-04-04 09:19:12 -07001401 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1402 /* only take left channel into account: the API is for stereo anyway */
1403 out->muted = (left == 0.0f);
1404 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001405 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1406 const char *mixer_ctl_name = "Compress Playback Volume";
1407 struct audio_device *adev = out->dev;
1408 struct mixer_ctl *ctl;
1409
1410 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1411 if (!ctl) {
1412 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1413 __func__, mixer_ctl_name);
1414 return -EINVAL;
1415 }
1416 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1417 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1418 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1419 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001420 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001421
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001422 return -ENOSYS;
1423}
1424
1425static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1426 size_t bytes)
1427{
1428 struct stream_out *out = (struct stream_out *)stream;
1429 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001430 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001431
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001432 pthread_mutex_lock(&out->lock);
1433 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001434 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001435 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001436 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001437 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001438 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001439 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001440 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001441 goto exit;
1442 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001443 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001444
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001445 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001446 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1447 if (out->send_new_metadata) {
1448 ALOGVV("send new gapless metadata");
1449 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1450 out->send_new_metadata = 0;
1451 }
1452
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001453 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001454 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001455 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001456 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1457 }
1458 if (!out->playback_started) {
1459 compress_start(out->compr);
1460 out->playback_started = 1;
1461 out->offload_state = OFFLOAD_STATE_PLAYING;
1462 }
1463 pthread_mutex_unlock(&out->lock);
1464 return ret;
1465 } else {
1466 if (out->pcm) {
1467 if (out->muted)
1468 memset((void *)buffer, 0, bytes);
1469 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1470 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001471 if (ret == 0)
1472 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001473 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001474 }
1475
1476exit:
1477 pthread_mutex_unlock(&out->lock);
1478
1479 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001480 if (out->pcm)
1481 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001482 out_standby(&out->stream.common);
1483 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1484 out_get_sample_rate(&out->stream.common));
1485 }
1486 return bytes;
1487}
1488
1489static int out_get_render_position(const struct audio_stream_out *stream,
1490 uint32_t *dsp_frames)
1491{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001492 struct stream_out *out = (struct stream_out *)stream;
1493 *dsp_frames = 0;
1494 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1495 pthread_mutex_lock(&out->lock);
1496 if (out->compr != NULL) {
1497 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1498 &out->sample_rate);
1499 ALOGVV("%s rendered frames %d sample_rate %d",
1500 __func__, *dsp_frames, out->sample_rate);
1501 }
1502 pthread_mutex_unlock(&out->lock);
1503 return 0;
1504 } else
1505 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001506}
1507
1508static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1509{
1510 return 0;
1511}
1512
1513static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1514{
1515 return 0;
1516}
1517
1518static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1519 int64_t *timestamp)
1520{
1521 return -EINVAL;
1522}
1523
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001524static int out_get_presentation_position(const struct audio_stream_out *stream,
1525 uint64_t *frames, struct timespec *timestamp)
1526{
1527 struct stream_out *out = (struct stream_out *)stream;
1528 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001529 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001530
1531 pthread_mutex_lock(&out->lock);
1532
Eric Laurent949a0892013-09-20 09:20:13 -07001533 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1534 if (out->compr != NULL) {
1535 compress_get_tstamp(out->compr, &dsp_frames,
1536 &out->sample_rate);
1537 ALOGVV("%s rendered frames %ld sample_rate %d",
1538 __func__, dsp_frames, out->sample_rate);
1539 *frames = dsp_frames;
1540 ret = 0;
1541 /* this is the best we can do */
1542 clock_gettime(CLOCK_MONOTONIC, timestamp);
1543 }
1544 } else {
1545 if (out->pcm) {
1546 size_t avail;
1547 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1548 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001549 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001550 // This adjustment accounts for buffering after app processor.
1551 // It is based on estimated DSP latency per use case, rather than exact.
1552 signed_frames -=
1553 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1554
Eric Laurent949a0892013-09-20 09:20:13 -07001555 // It would be unusual for this value to be negative, but check just in case ...
1556 if (signed_frames >= 0) {
1557 *frames = signed_frames;
1558 ret = 0;
1559 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001560 }
1561 }
1562 }
1563
1564 pthread_mutex_unlock(&out->lock);
1565
1566 return ret;
1567}
1568
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001569static int out_set_callback(struct audio_stream_out *stream,
1570 stream_callback_t callback, void *cookie)
1571{
1572 struct stream_out *out = (struct stream_out *)stream;
1573
1574 ALOGV("%s", __func__);
1575 pthread_mutex_lock(&out->lock);
1576 out->offload_callback = callback;
1577 out->offload_cookie = cookie;
1578 pthread_mutex_unlock(&out->lock);
1579 return 0;
1580}
1581
1582static int out_pause(struct audio_stream_out* stream)
1583{
1584 struct stream_out *out = (struct stream_out *)stream;
1585 int status = -ENOSYS;
1586 ALOGV("%s", __func__);
1587 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1588 pthread_mutex_lock(&out->lock);
1589 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1590 status = compress_pause(out->compr);
1591 out->offload_state = OFFLOAD_STATE_PAUSED;
1592 }
1593 pthread_mutex_unlock(&out->lock);
1594 }
1595 return status;
1596}
1597
1598static int out_resume(struct audio_stream_out* stream)
1599{
1600 struct stream_out *out = (struct stream_out *)stream;
1601 int status = -ENOSYS;
1602 ALOGV("%s", __func__);
1603 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1604 status = 0;
1605 pthread_mutex_lock(&out->lock);
1606 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1607 status = compress_resume(out->compr);
1608 out->offload_state = OFFLOAD_STATE_PLAYING;
1609 }
1610 pthread_mutex_unlock(&out->lock);
1611 }
1612 return status;
1613}
1614
1615static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1616{
1617 struct stream_out *out = (struct stream_out *)stream;
1618 int status = -ENOSYS;
1619 ALOGV("%s", __func__);
1620 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1621 pthread_mutex_lock(&out->lock);
1622 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1623 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1624 else
1625 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1626 pthread_mutex_unlock(&out->lock);
1627 }
1628 return status;
1629}
1630
1631static int out_flush(struct audio_stream_out* stream)
1632{
1633 struct stream_out *out = (struct stream_out *)stream;
1634 ALOGV("%s", __func__);
1635 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1636 pthread_mutex_lock(&out->lock);
1637 stop_compressed_output_l(out);
1638 pthread_mutex_unlock(&out->lock);
1639 return 0;
1640 }
1641 return -ENOSYS;
1642}
1643
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001644/** audio_stream_in implementation **/
1645static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1646{
1647 struct stream_in *in = (struct stream_in *)stream;
1648
1649 return in->config.rate;
1650}
1651
1652static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1653{
1654 return -ENOSYS;
1655}
1656
1657static size_t in_get_buffer_size(const struct audio_stream *stream)
1658{
1659 struct stream_in *in = (struct stream_in *)stream;
1660
1661 return in->config.period_size * audio_stream_frame_size(stream);
1662}
1663
1664static uint32_t in_get_channels(const struct audio_stream *stream)
1665{
1666 struct stream_in *in = (struct stream_in *)stream;
1667
1668 return in->channel_mask;
1669}
1670
1671static audio_format_t in_get_format(const struct audio_stream *stream)
1672{
1673 return AUDIO_FORMAT_PCM_16_BIT;
1674}
1675
1676static int in_set_format(struct audio_stream *stream, audio_format_t format)
1677{
1678 return -ENOSYS;
1679}
1680
1681static int in_standby(struct audio_stream *stream)
1682{
1683 struct stream_in *in = (struct stream_in *)stream;
1684 struct audio_device *adev = in->dev;
1685 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001686 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001687 pthread_mutex_lock(&in->lock);
1688 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001689 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001690 if (in->pcm) {
1691 pcm_close(in->pcm);
1692 in->pcm = NULL;
1693 }
1694 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001695 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001696 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001697 }
1698 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001699 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001700 return status;
1701}
1702
1703static int in_dump(const struct audio_stream *stream, int fd)
1704{
1705 return 0;
1706}
1707
1708static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1709{
1710 struct stream_in *in = (struct stream_in *)stream;
1711 struct audio_device *adev = in->dev;
1712 struct str_parms *parms;
1713 char *str;
1714 char value[32];
1715 int ret, val = 0;
1716
Eric Laurent994a6932013-07-17 11:51:42 -07001717 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001718 parms = str_parms_create_str(kvpairs);
1719
1720 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1721
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001722 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001723 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001724 if (ret >= 0) {
1725 val = atoi(value);
1726 /* no audio source uses val == 0 */
1727 if ((in->source != val) && (val != 0)) {
1728 in->source = val;
1729 }
1730 }
1731
1732 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1733 if (ret >= 0) {
1734 val = atoi(value);
1735 if ((in->device != val) && (val != 0)) {
1736 in->device = val;
1737 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001738 if (!in->standby)
1739 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001740 }
1741 }
1742
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001743 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001744 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001745
1746 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001747 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001748 return ret;
1749}
1750
1751static char* in_get_parameters(const struct audio_stream *stream,
1752 const char *keys)
1753{
1754 return strdup("");
1755}
1756
1757static int in_set_gain(struct audio_stream_in *stream, float gain)
1758{
1759 return 0;
1760}
1761
1762static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1763 size_t bytes)
1764{
1765 struct stream_in *in = (struct stream_in *)stream;
1766 struct audio_device *adev = in->dev;
1767 int i, ret = -1;
1768
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001769 pthread_mutex_lock(&in->lock);
1770 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001771 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001772 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001773 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001774 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001775 goto exit;
1776 }
1777 in->standby = 0;
1778 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001779
1780 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001781 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1782 ret = audio_extn_ssr_read(stream, buffer, bytes);
1783 else
1784 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001785 }
1786
1787 /*
1788 * Instead of writing zeroes here, we could trust the hardware
1789 * to always provide zeroes when muted.
1790 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001791 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001792 memset(buffer, 0, bytes);
1793
1794exit:
1795 pthread_mutex_unlock(&in->lock);
1796
1797 if (ret != 0) {
1798 in_standby(&in->stream.common);
1799 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1800 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1801 in_get_sample_rate(&in->stream.common));
1802 }
1803 return bytes;
1804}
1805
1806static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1807{
1808 return 0;
1809}
1810
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001811static int add_remove_audio_effect(const struct audio_stream *stream,
1812 effect_handle_t effect,
1813 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001814{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001815 struct stream_in *in = (struct stream_in *)stream;
1816 int status = 0;
1817 effect_descriptor_t desc;
1818
1819 status = (*effect)->get_descriptor(effect, &desc);
1820 if (status != 0)
1821 return status;
1822
1823 pthread_mutex_lock(&in->lock);
1824 pthread_mutex_lock(&in->dev->lock);
1825 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1826 in->enable_aec != enable &&
1827 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1828 in->enable_aec = enable;
1829 if (!in->standby)
1830 select_devices(in->dev, in->usecase);
1831 }
1832 pthread_mutex_unlock(&in->dev->lock);
1833 pthread_mutex_unlock(&in->lock);
1834
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001835 return 0;
1836}
1837
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001838static int in_add_audio_effect(const struct audio_stream *stream,
1839 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001840{
Eric Laurent994a6932013-07-17 11:51:42 -07001841 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001842 return add_remove_audio_effect(stream, effect, true);
1843}
1844
1845static int in_remove_audio_effect(const struct audio_stream *stream,
1846 effect_handle_t effect)
1847{
Eric Laurent994a6932013-07-17 11:51:42 -07001848 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001849 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001850}
1851
1852static int adev_open_output_stream(struct audio_hw_device *dev,
1853 audio_io_handle_t handle,
1854 audio_devices_t devices,
1855 audio_output_flags_t flags,
1856 struct audio_config *config,
1857 struct audio_stream_out **stream_out)
1858{
1859 struct audio_device *adev = (struct audio_device *)dev;
1860 struct stream_out *out;
1861 int i, ret;
1862
Eric Laurent994a6932013-07-17 11:51:42 -07001863 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001864 __func__, config->sample_rate, config->channel_mask, devices, flags);
1865 *stream_out = NULL;
1866 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1867
1868 if (devices == AUDIO_DEVICE_NONE)
1869 devices = AUDIO_DEVICE_OUT_SPEAKER;
1870
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001871 out->flags = flags;
1872 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001873 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001874 out->format = config->format;
1875 out->sample_rate = config->sample_rate;
1876 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1877 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001878 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001879
1880 /* Init use case and pcm_config */
1881 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1882 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001883 pthread_mutex_lock(&adev->lock);
1884 ret = read_hdmi_channel_masks(out);
1885 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001886 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001887 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001888
1889 if (config->sample_rate == 0)
1890 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1891 if (config->channel_mask == 0)
1892 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1893
1894 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001895 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001896 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1897 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001898 out->config.rate = config->sample_rate;
1899 out->config.channels = popcount(out->channel_mask);
1900 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001901 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1902 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1903 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001904 out->sample_rate = out->config.rate;
1905 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1906 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1907 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1908 ALOGE("%s: Unsupported Offload information", __func__);
1909 ret = -EINVAL;
1910 goto error_open;
1911 }
1912 if (!is_supported_format(config->offload_info.format)) {
1913 ALOGE("%s: Unsupported audio format", __func__);
1914 ret = -EINVAL;
1915 goto error_open;
1916 }
1917
1918 out->compr_config.codec = (struct snd_codec *)
1919 calloc(1, sizeof(struct snd_codec));
1920
1921 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1922 if (config->offload_info.channel_mask)
1923 out->channel_mask = config->offload_info.channel_mask;
1924 else if (config->channel_mask)
1925 out->channel_mask = config->channel_mask;
1926 out->format = config->offload_info.format;
1927 out->sample_rate = config->offload_info.sample_rate;
1928
1929 out->stream.set_callback = out_set_callback;
1930 out->stream.pause = out_pause;
1931 out->stream.resume = out_resume;
1932 out->stream.drain = out_drain;
1933 out->stream.flush = out_flush;
1934
1935 out->compr_config.codec->id =
1936 get_snd_codec_id(config->offload_info.format);
1937 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1938 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1939 out->compr_config.codec->sample_rate =
1940 compress_get_alsa_rate(config->offload_info.sample_rate);
1941 out->compr_config.codec->bit_rate =
1942 config->offload_info.bit_rate;
1943 out->compr_config.codec->ch_in =
1944 popcount(config->channel_mask);
1945 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1946
1947 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1948 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001949
1950 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001951 create_offload_callback_thread(out);
1952 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1953 __func__, config->offload_info.version,
1954 config->offload_info.bit_rate);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07001955 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
1956 ret = voice_check_and_set_incall_music_usecase(adev, out);
1957 if (ret != 0) {
1958 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
1959 __func__, ret);
1960 goto error_open;
1961 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001962 } else {
1963 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1964 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001965 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001966 }
1967
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001968 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1969 if(adev->primary_output == NULL)
1970 adev->primary_output = out;
1971 else {
1972 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001973 ret = -EEXIST;
1974 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001975 }
1976 }
1977
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001978 /* Check if this usecase is already existing */
1979 pthread_mutex_lock(&adev->lock);
1980 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1981 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001982 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001983 ret = -EEXIST;
1984 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001985 }
1986 pthread_mutex_unlock(&adev->lock);
1987
1988 out->stream.common.get_sample_rate = out_get_sample_rate;
1989 out->stream.common.set_sample_rate = out_set_sample_rate;
1990 out->stream.common.get_buffer_size = out_get_buffer_size;
1991 out->stream.common.get_channels = out_get_channels;
1992 out->stream.common.get_format = out_get_format;
1993 out->stream.common.set_format = out_set_format;
1994 out->stream.common.standby = out_standby;
1995 out->stream.common.dump = out_dump;
1996 out->stream.common.set_parameters = out_set_parameters;
1997 out->stream.common.get_parameters = out_get_parameters;
1998 out->stream.common.add_audio_effect = out_add_audio_effect;
1999 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2000 out->stream.get_latency = out_get_latency;
2001 out->stream.set_volume = out_set_volume;
2002 out->stream.write = out_write;
2003 out->stream.get_render_position = out_get_render_position;
2004 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002005 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002006
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002007 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002008 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002009 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002010
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002011 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2012 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002013
2014 config->format = out->stream.common.get_format(&out->stream.common);
2015 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2016 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2017
2018 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002019 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002020 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002021
2022error_open:
2023 free(out);
2024 *stream_out = NULL;
2025 ALOGD("%s: exit: ret %d", __func__, ret);
2026 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002027}
2028
2029static void adev_close_output_stream(struct audio_hw_device *dev,
2030 struct audio_stream_out *stream)
2031{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002032 struct stream_out *out = (struct stream_out *)stream;
2033 struct audio_device *adev = out->dev;
2034
Eric Laurent994a6932013-07-17 11:51:42 -07002035 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002036 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002037 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2038 destroy_offload_callback_thread(out);
2039
2040 if (out->compr_config.codec != NULL)
2041 free(out->compr_config.codec);
2042 }
2043 pthread_cond_destroy(&out->cond);
2044 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002045 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002046 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002047}
2048
2049static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2050{
2051 struct audio_device *adev = (struct audio_device *)dev;
2052 struct str_parms *parms;
2053 char *str;
2054 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002055 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002056 int ret;
2057
Eric Laurent994a6932013-07-17 11:51:42 -07002058 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002059 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002060
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002061 voice_set_parameters(adev, parms);
2062 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002063
2064 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2065 if (ret >= 0) {
2066 /* When set to false, HAL should disable EC and NS
2067 * But it is currently not supported.
2068 */
2069 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2070 adev->bluetooth_nrec = true;
2071 else
2072 adev->bluetooth_nrec = false;
2073 }
2074
2075 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2076 if (ret >= 0) {
2077 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2078 adev->screen_off = false;
2079 else
2080 adev->screen_off = true;
2081 }
2082
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002083 ret = str_parms_get_int(parms, "rotation", &val);
2084 if (ret >= 0) {
2085 bool reverse_speakers = false;
2086 switch(val) {
2087 // FIXME: note that the code below assumes that the speakers are in the correct placement
2088 // relative to the user when the device is rotated 90deg from its default rotation. This
2089 // assumption is device-specific, not platform-specific like this code.
2090 case 270:
2091 reverse_speakers = true;
2092 break;
2093 case 0:
2094 case 90:
2095 case 180:
2096 break;
2097 default:
2098 ALOGE("%s: unexpected rotation of %d", __func__, val);
2099 }
2100 pthread_mutex_lock(&adev->lock);
2101 if (adev->speaker_lr_swap != reverse_speakers) {
2102 adev->speaker_lr_swap = reverse_speakers;
2103 // only update the selected device if there is active pcm playback
2104 struct audio_usecase *usecase;
2105 struct listnode *node;
2106 list_for_each(node, &adev->usecase_list) {
2107 usecase = node_to_item(node, struct audio_usecase, list);
2108 if (usecase->type == PCM_PLAYBACK) {
2109 select_devices(adev, usecase->id);
2110 break;
2111 }
2112 }
2113 }
2114 pthread_mutex_unlock(&adev->lock);
2115 }
2116
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002117 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002118 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002119 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002120 return ret;
2121}
2122
2123static char* adev_get_parameters(const struct audio_hw_device *dev,
2124 const char *keys)
2125{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002126 struct audio_device *adev = (struct audio_device *)dev;
2127 struct str_parms *reply = str_parms_create();
2128 struct str_parms *query = str_parms_create_str(keys);
2129 char *str;
2130
2131 audio_extn_get_parameters(adev, query, reply);
2132 platform_get_parameters(adev->platform, query, reply);
2133 str = str_parms_to_str(reply);
2134 str_parms_destroy(query);
2135 str_parms_destroy(reply);
2136
2137 ALOGV("%s: exit: returns - %s", __func__, str);
2138 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002139}
2140
2141static int adev_init_check(const struct audio_hw_device *dev)
2142{
2143 return 0;
2144}
2145
2146static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2147{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002148 int ret;
2149 struct audio_device *adev = (struct audio_device *)dev;
2150 pthread_mutex_lock(&adev->lock);
2151 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002152 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002153 pthread_mutex_unlock(&adev->lock);
2154 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002155}
2156
2157static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2158{
2159 return -ENOSYS;
2160}
2161
2162static int adev_get_master_volume(struct audio_hw_device *dev,
2163 float *volume)
2164{
2165 return -ENOSYS;
2166}
2167
2168static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2169{
2170 return -ENOSYS;
2171}
2172
2173static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2174{
2175 return -ENOSYS;
2176}
2177
2178static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2179{
2180 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002181 pthread_mutex_lock(&adev->lock);
2182 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002183 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002184 adev->mode = mode;
2185 }
2186 pthread_mutex_unlock(&adev->lock);
2187 return 0;
2188}
2189
2190static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2191{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002192 return voice_set_mic_mute((struct audio_device *)dev, state);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002193}
2194
2195static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2196{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002197 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002198 return 0;
2199}
2200
2201static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2202 const struct audio_config *config)
2203{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002204 int channel_count = popcount(config->channel_mask);
2205
2206 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2207}
2208
2209static int adev_open_input_stream(struct audio_hw_device *dev,
2210 audio_io_handle_t handle,
2211 audio_devices_t devices,
2212 struct audio_config *config,
2213 struct audio_stream_in **stream_in)
2214{
2215 struct audio_device *adev = (struct audio_device *)dev;
2216 struct stream_in *in;
2217 int ret, buffer_size, frame_size;
2218 int channel_count = popcount(config->channel_mask);
2219
Eric Laurent994a6932013-07-17 11:51:42 -07002220 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002221 *stream_in = NULL;
2222 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2223 return -EINVAL;
2224
2225 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2226
2227 in->stream.common.get_sample_rate = in_get_sample_rate;
2228 in->stream.common.set_sample_rate = in_set_sample_rate;
2229 in->stream.common.get_buffer_size = in_get_buffer_size;
2230 in->stream.common.get_channels = in_get_channels;
2231 in->stream.common.get_format = in_get_format;
2232 in->stream.common.set_format = in_set_format;
2233 in->stream.common.standby = in_standby;
2234 in->stream.common.dump = in_dump;
2235 in->stream.common.set_parameters = in_set_parameters;
2236 in->stream.common.get_parameters = in_get_parameters;
2237 in->stream.common.add_audio_effect = in_add_audio_effect;
2238 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2239 in->stream.set_gain = in_set_gain;
2240 in->stream.read = in_read;
2241 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2242
2243 in->device = devices;
2244 in->source = AUDIO_SOURCE_DEFAULT;
2245 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002246 in->standby = 1;
2247 in->channel_mask = config->channel_mask;
2248
2249 /* Update config params with the requested sample rate and channels */
2250 in->usecase = USECASE_AUDIO_RECORD;
2251 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002252 in->config.rate = config->sample_rate;
2253
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002254 if (audio_extn_ssr_get_enabled()&& channel_count == 6) {
2255 if(audio_extn_ssr_init(adev, in))
2256 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2257 } else {
2258 in->config.channels = channel_count;
2259 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2260 buffer_size = get_input_buffer_size(config->sample_rate,
2261 config->format,
2262 channel_count);
2263 in->config.period_size = buffer_size / frame_size;
2264 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002265
2266 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002267 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002268 return 0;
2269
2270err_open:
2271 free(in);
2272 *stream_in = NULL;
2273 return ret;
2274}
2275
2276static void adev_close_input_stream(struct audio_hw_device *dev,
2277 struct audio_stream_in *stream)
2278{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002279 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002280 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002281
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002282 in_standby(&stream->common);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002283 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2284 audio_extn_ssr_deinit();
2285 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002286 free(stream);
2287
2288 return;
2289}
2290
2291static int adev_dump(const audio_hw_device_t *device, int fd)
2292{
2293 return 0;
2294}
2295
2296static int adev_close(hw_device_t *device)
2297{
2298 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002299
2300 if (!adev)
2301 return 0;
2302
2303 pthread_mutex_lock(&adev_init_lock);
2304
2305 if ((--audio_device_ref_count) == 0) {
2306 audio_route_free(adev->audio_route);
2307 free(adev->snd_dev_ref_cnt);
2308 platform_deinit(adev->platform);
2309 audio_extn_listen_deinit(adev);
2310 free(device);
2311 adev = NULL;
2312 }
2313 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002314 return 0;
2315}
2316
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002317static int adev_open(const hw_module_t *module, const char *name,
2318 hw_device_t **device)
2319{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002320 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002321
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002322 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002323 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2324
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002325 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002326 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002327 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002328 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002329 ALOGD("%s: returning existing instance of adev", __func__);
2330 ALOGD("%s: exit", __func__);
2331 pthread_mutex_unlock(&adev_init_lock);
2332 return 0;
2333 }
2334
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002335 adev = calloc(1, sizeof(struct audio_device));
2336
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002337 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2338 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2339 adev->device.common.module = (struct hw_module_t *)module;
2340 adev->device.common.close = adev_close;
2341
2342 adev->device.init_check = adev_init_check;
2343 adev->device.set_voice_volume = adev_set_voice_volume;
2344 adev->device.set_master_volume = adev_set_master_volume;
2345 adev->device.get_master_volume = adev_get_master_volume;
2346 adev->device.set_master_mute = adev_set_master_mute;
2347 adev->device.get_master_mute = adev_get_master_mute;
2348 adev->device.set_mode = adev_set_mode;
2349 adev->device.set_mic_mute = adev_set_mic_mute;
2350 adev->device.get_mic_mute = adev_get_mic_mute;
2351 adev->device.set_parameters = adev_set_parameters;
2352 adev->device.get_parameters = adev_get_parameters;
2353 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2354 adev->device.open_output_stream = adev_open_output_stream;
2355 adev->device.close_output_stream = adev_close_output_stream;
2356 adev->device.open_input_stream = adev_open_input_stream;
2357 adev->device.close_input_stream = adev_close_input_stream;
2358 adev->device.dump = adev_dump;
2359
2360 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002361 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002362 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002363 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002364 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002365 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002366 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002367 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002368 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002369 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002370 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002371
2372 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002373 adev->platform = platform_init(adev);
2374 if (!adev->platform) {
2375 free(adev->snd_dev_ref_cnt);
2376 free(adev);
2377 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2378 *device = NULL;
2379 return -EINVAL;
2380 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002381
2382 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2383 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2384 if (adev->visualizer_lib == NULL) {
2385 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2386 } else {
2387 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2388 adev->visualizer_start_output =
2389 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2390 "visualizer_hal_start_output");
2391 adev->visualizer_stop_output =
2392 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2393 "visualizer_hal_stop_output");
2394 }
2395 }
Kiran Kandi910e1862013-10-29 13:29:42 -07002396 audio_extn_listen_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07002397
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002398 *device = &adev->device.common;
2399
Kiran Kandi910e1862013-10-29 13:29:42 -07002400 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002401 pthread_mutex_unlock(&adev_init_lock);
2402
Eric Laurent994a6932013-07-17 11:51:42 -07002403 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002404 return 0;
2405}
2406
2407static struct hw_module_methods_t hal_module_methods = {
2408 .open = adev_open,
2409};
2410
2411struct audio_module HAL_MODULE_INFO_SYM = {
2412 .common = {
2413 .tag = HARDWARE_MODULE_TAG,
2414 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2415 .hal_api_version = HARDWARE_HAL_API_VERSION,
2416 .id = AUDIO_HARDWARE_MODULE_ID,
2417 .name = "QCOM Audio HAL",
2418 .author = "Code Aurora Forum",
2419 .methods = &hal_module_methods,
2420 },
2421};