blob: 29997fec403993b060a6029abc0c8f753fe3566b [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
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700288 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
289 ALOGE("%s: Invalid sound device returned", __func__);
290 return -EINVAL;
291 }
292
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700293 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700294 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700295 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800296 /* exit usb play back thread */
297 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
298 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
299 audio_extn_usb_stop_playback();
300
301 /* exit usb capture thread */
302 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
303 audio_extn_usb_stop_capture(adev);
304
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700305 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800306
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700307 if (update_mixer)
308 audio_route_update_mixer(adev->audio_route);
309 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700310
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800311 return 0;
312}
313
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700314static void check_usecases_codec_backend(struct audio_device *adev,
315 struct audio_usecase *uc_info,
316 snd_device_t snd_device)
317{
318 struct listnode *node;
319 struct audio_usecase *usecase;
320 bool switch_device[AUDIO_USECASE_MAX];
321 int i, num_uc_to_switch = 0;
322
323 /*
324 * This function is to make sure that all the usecases that are active on
325 * the hardware codec backend are always routed to any one device that is
326 * handled by the hardware codec.
327 * For example, if low-latency and deep-buffer usecases are currently active
328 * on speaker and out_set_parameters(headset) is received on low-latency
329 * output, then we have to make sure deep-buffer is also switched to headset,
330 * because of the limitation that both the devices cannot be enabled
331 * at the same time as they share the same backend.
332 */
333 /* Disable all the usecases on the shared backend other than the
334 specified usecase */
335 for (i = 0; i < AUDIO_USECASE_MAX; i++)
336 switch_device[i] = false;
337
338 list_for_each(node, &adev->usecase_list) {
339 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700340 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700341 usecase != uc_info &&
342 usecase->out_snd_device != snd_device &&
343 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
344 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
345 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700346 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700347 disable_audio_route(adev, usecase, false);
348 switch_device[usecase->id] = true;
349 num_uc_to_switch++;
350 }
351 }
352
353 if (num_uc_to_switch) {
354 /* Make sure all the streams are de-routed before disabling the device */
355 audio_route_update_mixer(adev->audio_route);
356
357 list_for_each(node, &adev->usecase_list) {
358 usecase = node_to_item(node, struct audio_usecase, list);
359 if (switch_device[usecase->id]) {
360 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700361 }
362 }
363
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700364 list_for_each(node, &adev->usecase_list) {
365 usecase = node_to_item(node, struct audio_usecase, list);
366 if (switch_device[usecase->id]) {
367 enable_snd_device(adev, snd_device, false);
368 }
369 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700370 /* Make sure new snd device is enabled before re-routing the streams */
371 audio_route_update_mixer(adev->audio_route);
372
373 /* Re-route all the usecases on the shared backend other than the
374 specified usecase to new snd devices */
375 list_for_each(node, &adev->usecase_list) {
376 usecase = node_to_item(node, struct audio_usecase, list);
377 /* Update the out_snd_device only before enabling the audio route */
378 if (switch_device[usecase->id] ) {
379 usecase->out_snd_device = snd_device;
380 enable_audio_route(adev, usecase, false);
381 }
382 }
383
384 audio_route_update_mixer(adev->audio_route);
385 }
386}
387
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700388static void check_and_route_capture_usecases(struct audio_device *adev,
389 struct audio_usecase *uc_info,
390 snd_device_t snd_device)
391{
392 struct listnode *node;
393 struct audio_usecase *usecase;
394 bool switch_device[AUDIO_USECASE_MAX];
395 int i, num_uc_to_switch = 0;
396
397 /*
398 * This function is to make sure that all the active capture usecases
399 * are always routed to the same input sound device.
400 * For example, if audio-record and voice-call usecases are currently
401 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
402 * is received for voice call then we have to make sure that audio-record
403 * usecase is also switched to earpiece i.e. voice-dmic-ef,
404 * because of the limitation that two devices cannot be enabled
405 * at the same time if they share the same backend.
406 */
407 for (i = 0; i < AUDIO_USECASE_MAX; i++)
408 switch_device[i] = false;
409
410 list_for_each(node, &adev->usecase_list) {
411 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700412 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700413 usecase != uc_info &&
414 usecase->in_snd_device != snd_device) {
415 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
416 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700417 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700418 disable_audio_route(adev, usecase, false);
419 switch_device[usecase->id] = true;
420 num_uc_to_switch++;
421 }
422 }
423
424 if (num_uc_to_switch) {
425 /* Make sure all the streams are de-routed before disabling the device */
426 audio_route_update_mixer(adev->audio_route);
427
428 list_for_each(node, &adev->usecase_list) {
429 usecase = node_to_item(node, struct audio_usecase, list);
430 if (switch_device[usecase->id]) {
431 disable_snd_device(adev, usecase->in_snd_device, false);
432 enable_snd_device(adev, snd_device, false);
433 }
434 }
435
436 /* Make sure new snd device is enabled before re-routing the streams */
437 audio_route_update_mixer(adev->audio_route);
438
439 /* Re-route all the usecases on the shared backend other than the
440 specified usecase to new snd devices */
441 list_for_each(node, &adev->usecase_list) {
442 usecase = node_to_item(node, struct audio_usecase, list);
443 /* Update the in_snd_device only before enabling the audio route */
444 if (switch_device[usecase->id] ) {
445 usecase->in_snd_device = snd_device;
446 enable_audio_route(adev, usecase, false);
447 }
448 }
449
450 audio_route_update_mixer(adev->audio_route);
451 }
452}
453
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700454static int disable_all_usecases_of_type(struct audio_device *adev,
455 usecase_type_t usecase_type,
456 bool update_mixer)
457{
458 struct audio_usecase *usecase;
459 struct listnode *node;
460 int ret = 0;
461
462 list_for_each(node, &adev->usecase_list) {
463 usecase = node_to_item(node, struct audio_usecase, list);
464 if (usecase->type == usecase_type) {
465 ALOGV("%s: usecase id %d", __func__, usecase->id);
466 ret = disable_audio_route(adev, usecase, update_mixer);
467 if (ret) {
468 ALOGE("%s: Failed to disable usecase id %d",
469 __func__, usecase->id);
470 }
471 }
472 }
473
474 return ret;
475}
476
477static int enable_all_usecases_of_type(struct audio_device *adev,
478 usecase_type_t usecase_type,
479 bool update_mixer)
480{
481 struct audio_usecase *usecase;
482 struct listnode *node;
483 int ret = 0;
484
485 list_for_each(node, &adev->usecase_list) {
486 usecase = node_to_item(node, struct audio_usecase, list);
487 if (usecase->type == usecase_type) {
488 ALOGV("%s: usecase id %d", __func__, usecase->id);
489 ret = enable_audio_route(adev, usecase, update_mixer);
490 if (ret) {
491 ALOGE("%s: Failed to enable usecase id %d",
492 __func__, usecase->id);
493 }
494 }
495 }
496
497 return ret;
498}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800499
500/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700501static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800502{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700503 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700504 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800505
506 switch (channels) {
507 /*
508 * Do not handle stereo output in Multi-channel cases
509 * Stereo case is handled in normal playback path
510 */
511 case 6:
512 ALOGV("%s: HDMI supports 5.1", __func__);
513 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
514 break;
515 case 8:
516 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
517 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
518 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
519 break;
520 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700521 ALOGE("HDMI does not support multi channel playback");
522 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800523 break;
524 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700525 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800526}
527
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700528static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
529{
530 struct audio_usecase *usecase;
531 struct listnode *node;
532
533 list_for_each(node, &adev->usecase_list) {
534 usecase = node_to_item(node, struct audio_usecase, list);
535 if (usecase->type == VOICE_CALL) {
536 ALOGV("%s: usecase id %d", __func__, usecase->id);
537 return usecase->id;
538 }
539 }
540 return USECASE_INVALID;
541}
542
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700543struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700544 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700545{
546 struct audio_usecase *usecase;
547 struct listnode *node;
548
549 list_for_each(node, &adev->usecase_list) {
550 usecase = node_to_item(node, struct audio_usecase, list);
551 if (usecase->id == uc_id)
552 return usecase;
553 }
554 return NULL;
555}
556
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700557int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800558{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800559 snd_device_t out_snd_device = SND_DEVICE_NONE;
560 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700561 struct audio_usecase *usecase = NULL;
562 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800563 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700564 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800565
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700566 usecase = get_usecase_from_list(adev, uc_id);
567 if (usecase == NULL) {
568 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
569 return -EINVAL;
570 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800571
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700572 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700573 out_snd_device = platform_get_output_snd_device(adev->platform,
574 usecase->stream.out->devices);
575 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700576 usecase->devices = usecase->stream.out->devices;
577 } else {
578 /*
579 * If the voice call is active, use the sound devices of voice call usecase
580 * so that it would not result any device switch. All the usecases will
581 * be switched to new device when select_devices() is called for voice call
582 * usecase. This is to avoid switching devices for voice call when
583 * check_usecases_codec_backend() is called below.
584 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700585 if (voice_is_in_call(adev)) {
586 vc_usecase = get_usecase_from_list(adev,
587 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700588 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
589 in_snd_device = vc_usecase->in_snd_device;
590 out_snd_device = vc_usecase->out_snd_device;
591 }
592 }
593 if (usecase->type == PCM_PLAYBACK) {
594 usecase->devices = usecase->stream.out->devices;
595 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700596 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700597 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700598 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700599 if (usecase->stream.out == adev->primary_output &&
600 adev->active_input &&
601 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
602 select_devices(adev, adev->active_input->usecase);
603 }
604 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700605 } else if (usecase->type == PCM_CAPTURE) {
606 usecase->devices = usecase->stream.in->device;
607 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700608 if (in_snd_device == SND_DEVICE_NONE) {
609 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
610 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700611 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700612 adev->primary_output->devices);
613 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700614 in_snd_device = platform_get_input_snd_device(adev->platform,
615 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700616 }
617 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700618 }
619 }
620
621 if (out_snd_device == usecase->out_snd_device &&
622 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800623 return 0;
624 }
625
sangwoobc677242013-08-08 16:53:43 +0900626 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700627 out_snd_device, platform_get_snd_device_name(out_snd_device),
628 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800629
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800630 /*
631 * Limitation: While in call, to do a device switch we need to disable
632 * and enable both RX and TX devices though one of them is same as current
633 * device.
634 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700635 if (usecase->type == VOICE_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700636 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700637 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800638 }
639
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700640 /* Disable current sound devices */
641 if (usecase->out_snd_device != SND_DEVICE_NONE) {
642 disable_audio_route(adev, usecase, true);
643 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800644 }
645
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700646 if (usecase->in_snd_device != SND_DEVICE_NONE) {
647 disable_audio_route(adev, usecase, true);
648 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800649 }
650
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700651 /* Enable new sound devices */
652 if (out_snd_device != SND_DEVICE_NONE) {
653 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
654 check_usecases_codec_backend(adev, usecase, out_snd_device);
655 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800656 }
657
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700658 if (in_snd_device != SND_DEVICE_NONE) {
659 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700660 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700661 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700662
Eric Laurentb23d5282013-05-14 15:27:20 -0700663 if (usecase->type == VOICE_CALL)
664 status = platform_switch_voice_call_device_post(adev->platform,
665 out_snd_device,
666 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800667
sangwoo170731f2013-06-08 15:36:36 +0900668 audio_route_update_mixer(adev->audio_route);
669
670 usecase->in_snd_device = in_snd_device;
671 usecase->out_snd_device = out_snd_device;
672
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700673 if (usecase->type == VOICE_CALL)
674 enable_all_usecases_of_type(adev, VOICE_CALL, true);
675 else
676 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900677
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800678 return status;
679}
680
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800681static int stop_input_stream(struct stream_in *in)
682{
683 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800684 struct audio_usecase *uc_info;
685 struct audio_device *adev = in->dev;
686
Eric Laurentc8400632013-02-14 19:04:54 -0800687 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800688
Eric Laurent994a6932013-07-17 11:51:42 -0700689 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700690 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691 uc_info = get_usecase_from_list(adev, in->usecase);
692 if (uc_info == NULL) {
693 ALOGE("%s: Could not find the usecase (%d) in the list",
694 __func__, in->usecase);
695 return -EINVAL;
696 }
697
Eric Laurent150dbfe2013-02-27 14:31:02 -0800698 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700699 disable_audio_route(adev, uc_info, true);
700
701 /* 2. Disable the tx device */
702 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800703
Kiran Kandi910e1862013-10-29 13:29:42 -0700704 audio_extn_listen_update_status(uc_info,
705 LISTEN_EVENT_AUDIO_CAPTURE_INACTIVE);
706
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800707 list_remove(&uc_info->list);
708 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800709
Eric Laurent994a6932013-07-17 11:51:42 -0700710 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800711 return ret;
712}
713
714int start_input_stream(struct stream_in *in)
715{
716 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800717 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800718 struct audio_usecase *uc_info;
719 struct audio_device *adev = in->dev;
720
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700721 in->usecase = platform_get_usecase_from_source(in->source);
Eric Laurent994a6932013-07-17 11:51:42 -0700722 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700723
724 /* Check if source matches incall recording usecase criteria */
725 ret = voice_check_and_set_incall_rec_usecase(adev, in);
726 if (ret)
727 goto error_config;
728 else
729 ALOGV("%s: usecase(%d)", __func__, in->usecase);
730
Eric Laurentb23d5282013-05-14 15:27:20 -0700731 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732 if (in->pcm_device_id < 0) {
733 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
734 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800735 ret = -EINVAL;
736 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800737 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700738
739 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800740 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
741 uc_info->id = in->usecase;
742 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800743 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700744 uc_info->devices = in->device;
745 uc_info->in_snd_device = SND_DEVICE_NONE;
746 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800747
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800748 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700749 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800750
Kiran Kandi910e1862013-10-29 13:29:42 -0700751 audio_extn_listen_update_status(uc_info,
752 LISTEN_EVENT_AUDIO_CAPTURE_ACTIVE);
753
Eric Laurentc8400632013-02-14 19:04:54 -0800754 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
755 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800756 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
757 PCM_IN, &in->config);
758 if (in->pcm && !pcm_is_ready(in->pcm)) {
759 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
760 pcm_close(in->pcm);
761 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800762 ret = -EIO;
763 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800764 }
Eric Laurent994a6932013-07-17 11:51:42 -0700765 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800766 return ret;
767
768error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800769 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800770
771error_config:
772 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700773 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800774
775 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800776}
777
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700778/* must be called with out->lock locked */
779static int send_offload_cmd_l(struct stream_out* out, int command)
780{
781 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
782
783 ALOGVV("%s %d", __func__, command);
784
785 cmd->cmd = command;
786 list_add_tail(&out->offload_cmd_list, &cmd->node);
787 pthread_cond_signal(&out->offload_cond);
788 return 0;
789}
790
791/* must be called iwth out->lock locked */
792static void stop_compressed_output_l(struct stream_out *out)
793{
794 out->offload_state = OFFLOAD_STATE_IDLE;
795 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700796 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700797 if (out->compr != NULL) {
798 compress_stop(out->compr);
799 while (out->offload_thread_blocked) {
800 pthread_cond_wait(&out->cond, &out->lock);
801 }
802 }
803}
804
805static void *offload_thread_loop(void *context)
806{
807 struct stream_out *out = (struct stream_out *) context;
808 struct listnode *item;
809
810 out->offload_state = OFFLOAD_STATE_IDLE;
811 out->playback_started = 0;
812
813 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
814 set_sched_policy(0, SP_FOREGROUND);
815 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
816
817 ALOGV("%s", __func__);
818 pthread_mutex_lock(&out->lock);
819 for (;;) {
820 struct offload_cmd *cmd = NULL;
821 stream_callback_event_t event;
822 bool send_callback = false;
823
824 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
825 __func__, list_empty(&out->offload_cmd_list),
826 out->offload_state);
827 if (list_empty(&out->offload_cmd_list)) {
828 ALOGV("%s SLEEPING", __func__);
829 pthread_cond_wait(&out->offload_cond, &out->lock);
830 ALOGV("%s RUNNING", __func__);
831 continue;
832 }
833
834 item = list_head(&out->offload_cmd_list);
835 cmd = node_to_item(item, struct offload_cmd, node);
836 list_remove(item);
837
838 ALOGVV("%s STATE %d CMD %d out->compr %p",
839 __func__, out->offload_state, cmd->cmd, out->compr);
840
841 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
842 free(cmd);
843 break;
844 }
845
846 if (out->compr == NULL) {
847 ALOGE("%s: Compress handle is NULL", __func__);
848 pthread_cond_signal(&out->cond);
849 continue;
850 }
851 out->offload_thread_blocked = true;
852 pthread_mutex_unlock(&out->lock);
853 send_callback = false;
854 switch(cmd->cmd) {
855 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
856 compress_wait(out->compr, -1);
857 send_callback = true;
858 event = STREAM_CBK_EVENT_WRITE_READY;
859 break;
860 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700861 compress_next_track(out->compr);
862 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700863 send_callback = true;
864 event = STREAM_CBK_EVENT_DRAIN_READY;
865 break;
866 case OFFLOAD_CMD_DRAIN:
867 compress_drain(out->compr);
868 send_callback = true;
869 event = STREAM_CBK_EVENT_DRAIN_READY;
870 break;
871 default:
872 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
873 break;
874 }
875 pthread_mutex_lock(&out->lock);
876 out->offload_thread_blocked = false;
877 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700878 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700879 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700880 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700881 free(cmd);
882 }
883
884 pthread_cond_signal(&out->cond);
885 while (!list_empty(&out->offload_cmd_list)) {
886 item = list_head(&out->offload_cmd_list);
887 list_remove(item);
888 free(node_to_item(item, struct offload_cmd, node));
889 }
890 pthread_mutex_unlock(&out->lock);
891
892 return NULL;
893}
894
895static int create_offload_callback_thread(struct stream_out *out)
896{
897 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
898 list_init(&out->offload_cmd_list);
899 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
900 offload_thread_loop, out);
901 return 0;
902}
903
904static int destroy_offload_callback_thread(struct stream_out *out)
905{
906 pthread_mutex_lock(&out->lock);
907 stop_compressed_output_l(out);
908 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
909
910 pthread_mutex_unlock(&out->lock);
911 pthread_join(out->offload_thread, (void **) NULL);
912 pthread_cond_destroy(&out->offload_cond);
913
914 return 0;
915}
916
Eric Laurent07eeafd2013-10-06 12:52:49 -0700917static bool allow_hdmi_channel_config(struct audio_device *adev)
918{
919 struct listnode *node;
920 struct audio_usecase *usecase;
921 bool ret = true;
922
923 list_for_each(node, &adev->usecase_list) {
924 usecase = node_to_item(node, struct audio_usecase, list);
925 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
926 /*
927 * If voice call is already existing, do not proceed further to avoid
928 * disabling/enabling both RX and TX devices, CSD calls, etc.
929 * Once the voice call done, the HDMI channels can be configured to
930 * max channels of remaining use cases.
931 */
932 if (usecase->id == USECASE_VOICE_CALL) {
933 ALOGD("%s: voice call is active, no change in HDMI channels",
934 __func__);
935 ret = false;
936 break;
937 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
938 ALOGD("%s: multi channel playback is active, "
939 "no change in HDMI channels", __func__);
940 ret = false;
941 break;
942 }
943 }
944 }
945 return ret;
946}
947
948static int check_and_set_hdmi_channels(struct audio_device *adev,
949 unsigned int channels)
950{
951 struct listnode *node;
952 struct audio_usecase *usecase;
953
954 /* Check if change in HDMI channel config is allowed */
955 if (!allow_hdmi_channel_config(adev))
956 return 0;
957
958 if (channels == adev->cur_hdmi_channels) {
959 ALOGD("%s: Requested channels are same as current", __func__);
960 return 0;
961 }
962
963 platform_set_hdmi_channels(adev->platform, channels);
964 adev->cur_hdmi_channels = channels;
965
966 /*
967 * Deroute all the playback streams routed to HDMI so that
968 * the back end is deactivated. Note that backend will not
969 * be deactivated if any one stream is connected to it.
970 */
971 list_for_each(node, &adev->usecase_list) {
972 usecase = node_to_item(node, struct audio_usecase, list);
973 if (usecase->type == PCM_PLAYBACK &&
974 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
975 disable_audio_route(adev, usecase, true);
976 }
977 }
978
979 /*
980 * Enable all the streams disabled above. Now the HDMI backend
981 * will be activated with new channel configuration
982 */
983 list_for_each(node, &adev->usecase_list) {
984 usecase = node_to_item(node, struct audio_usecase, list);
985 if (usecase->type == PCM_PLAYBACK &&
986 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
987 enable_audio_route(adev, usecase, true);
988 }
989 }
990
991 return 0;
992}
993
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800994static int stop_output_stream(struct stream_out *out)
995{
996 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800997 struct audio_usecase *uc_info;
998 struct audio_device *adev = out->dev;
999
Eric Laurent994a6932013-07-17 11:51:42 -07001000 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001001 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001002 uc_info = get_usecase_from_list(adev, out->usecase);
1003 if (uc_info == NULL) {
1004 ALOGE("%s: Could not find the usecase (%d) in the list",
1005 __func__, out->usecase);
1006 return -EINVAL;
1007 }
1008
Eric Laurentc4aef752013-09-12 17:45:53 -07001009 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
1010 adev->visualizer_stop_output != NULL)
1011 adev->visualizer_stop_output(out->handle);
1012
Eric Laurent150dbfe2013-02-27 14:31:02 -08001013 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001014 disable_audio_route(adev, uc_info, true);
1015
1016 /* 2. Disable the rx device */
1017 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001018
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001019 list_remove(&uc_info->list);
1020 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001021
Eric Laurent07eeafd2013-10-06 12:52:49 -07001022 /* Must be called after removing the usecase from list */
1023 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1024 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1025
Eric Laurent994a6932013-07-17 11:51:42 -07001026 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001027 return ret;
1028}
1029
1030int start_output_stream(struct stream_out *out)
1031{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001032 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001033 struct audio_usecase *uc_info;
1034 struct audio_device *adev = out->dev;
1035
Eric Laurent994a6932013-07-17 11:51:42 -07001036 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001037 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001038 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001039 if (out->pcm_device_id < 0) {
1040 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1041 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001042 ret = -EINVAL;
1043 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001044 }
1045
1046 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1047 uc_info->id = out->usecase;
1048 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001049 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001050 uc_info->devices = out->devices;
1051 uc_info->in_snd_device = SND_DEVICE_NONE;
1052 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001053
Eric Laurent07eeafd2013-10-06 12:52:49 -07001054 /* This must be called before adding this usecase to the list */
1055 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1056 check_and_set_hdmi_channels(adev, out->config.channels);
1057
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001058 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001059
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001060 select_devices(adev, out->usecase);
1061
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001062 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1063 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001064 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1065 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001066 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001067 if (out->pcm && !pcm_is_ready(out->pcm)) {
1068 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1069 pcm_close(out->pcm);
1070 out->pcm = NULL;
1071 ret = -EIO;
1072 goto error_open;
1073 }
1074 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001075 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001076 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1077 COMPRESS_IN, &out->compr_config);
1078 if (out->compr && !is_compress_ready(out->compr)) {
1079 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1080 compress_close(out->compr);
1081 out->compr = NULL;
1082 ret = -EIO;
1083 goto error_open;
1084 }
1085 if (out->offload_callback)
1086 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001087
1088 if (adev->visualizer_start_output != NULL)
1089 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001090 }
Eric Laurent994a6932013-07-17 11:51:42 -07001091 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001092 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001093error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001094 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001095error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001096 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001097}
1098
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001099static int check_input_parameters(uint32_t sample_rate,
1100 audio_format_t format,
1101 int channel_count)
1102{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001103 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001104
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001105 if (format != AUDIO_FORMAT_PCM_16_BIT) ret = -EINVAL;
1106
1107 switch (channel_count) {
1108 case 1:
1109 case 2:
1110 case 6:
1111 break;
1112 default:
1113 ret = -EINVAL;
1114 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001115
1116 switch (sample_rate) {
1117 case 8000:
1118 case 11025:
1119 case 12000:
1120 case 16000:
1121 case 22050:
1122 case 24000:
1123 case 32000:
1124 case 44100:
1125 case 48000:
1126 break;
1127 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001128 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001129 }
1130
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001131 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001132}
1133
1134static size_t get_input_buffer_size(uint32_t sample_rate,
1135 audio_format_t format,
1136 int channel_count)
1137{
1138 size_t size = 0;
1139
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001140 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1141 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001142
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001143 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1144 /* ToDo: should use frame_size computed based on the format and
1145 channel_count here. */
1146 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001147
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001148 /* make sure the size is multiple of 64 */
1149 size += 0x3f;
1150 size &= ~0x3f;
1151
1152 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001153}
1154
1155static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1156{
1157 struct stream_out *out = (struct stream_out *)stream;
1158
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001159 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001160}
1161
1162static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1163{
1164 return -ENOSYS;
1165}
1166
1167static size_t out_get_buffer_size(const struct audio_stream *stream)
1168{
1169 struct stream_out *out = (struct stream_out *)stream;
1170
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001171 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1172 return out->compr_config.fragment_size;
1173 }
1174
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001175 return out->config.period_size * audio_stream_frame_size(stream);
1176}
1177
1178static uint32_t out_get_channels(const struct audio_stream *stream)
1179{
1180 struct stream_out *out = (struct stream_out *)stream;
1181
1182 return out->channel_mask;
1183}
1184
1185static audio_format_t out_get_format(const struct audio_stream *stream)
1186{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001187 struct stream_out *out = (struct stream_out *)stream;
1188
1189 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001190}
1191
1192static int out_set_format(struct audio_stream *stream, audio_format_t format)
1193{
1194 return -ENOSYS;
1195}
1196
1197static int out_standby(struct audio_stream *stream)
1198{
1199 struct stream_out *out = (struct stream_out *)stream;
1200 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001201
Eric Laurent994a6932013-07-17 11:51:42 -07001202 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001203 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001204
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001205 pthread_mutex_lock(&out->lock);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001206 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001207 if (!out->standby) {
1208 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001209 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1210 if (out->pcm) {
1211 pcm_close(out->pcm);
1212 out->pcm = NULL;
1213 }
1214 } else {
1215 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001216 out->gapless_mdata.encoder_delay = 0;
1217 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001218 if (out->compr != NULL) {
1219 compress_close(out->compr);
1220 out->compr = NULL;
1221 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001222 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001223 stop_output_stream(out);
1224 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001225 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001226 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001227 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001228 return 0;
1229}
1230
1231static int out_dump(const struct audio_stream *stream, int fd)
1232{
1233 return 0;
1234}
1235
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001236static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1237{
1238 int ret = 0;
1239 char value[32];
1240 struct compr_gapless_mdata tmp_mdata;
1241
1242 if (!out || !parms) {
1243 return -EINVAL;
1244 }
1245
1246 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1247 if (ret >= 0) {
1248 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1249 } else {
1250 return -EINVAL;
1251 }
1252
1253 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1254 if (ret >= 0) {
1255 tmp_mdata.encoder_padding = atoi(value);
1256 } else {
1257 return -EINVAL;
1258 }
1259
1260 out->gapless_mdata = tmp_mdata;
1261 out->send_new_metadata = 1;
1262 ALOGV("%s new encoder delay %u and padding %u", __func__,
1263 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1264
1265 return 0;
1266}
1267
1268
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001269static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1270{
1271 struct stream_out *out = (struct stream_out *)stream;
1272 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001273 struct audio_usecase *usecase;
1274 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001275 struct str_parms *parms;
1276 char value[32];
1277 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001278 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001279
sangwoobc677242013-08-08 16:53:43 +09001280 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001281 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001282 parms = str_parms_create_str(kvpairs);
1283 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1284 if (ret >= 0) {
1285 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001286 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001287 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001288
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001289 /*
1290 * When HDMI cable is unplugged the music playback is paused and
1291 * the policy manager sends routing=0. But the audioflinger
1292 * continues to write data until standby time (3sec).
1293 * As the HDMI core is turned off, the write gets blocked.
1294 * Avoid this by routing audio to speaker until standby.
1295 */
1296 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1297 val == AUDIO_DEVICE_NONE) {
1298 val = AUDIO_DEVICE_OUT_SPEAKER;
1299 }
1300
1301 /*
1302 * select_devices() call below switches all the usecases on the same
1303 * backend to the new device. Refer to check_usecases_codec_backend() in
1304 * the select_devices(). But how do we undo this?
1305 *
1306 * For example, music playback is active on headset (deep-buffer usecase)
1307 * and if we go to ringtones and select a ringtone, low-latency usecase
1308 * will be started on headset+speaker. As we can't enable headset+speaker
1309 * and headset devices at the same time, select_devices() switches the music
1310 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1311 * So when the ringtone playback is completed, how do we undo the same?
1312 *
1313 * We are relying on the out_set_parameters() call on deep-buffer output,
1314 * once the ringtone playback is ended.
1315 * NOTE: We should not check if the current devices are same as new devices.
1316 * Because select_devices() must be called to switch back the music
1317 * playback to headset.
1318 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001319 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001320 out->devices = val;
1321
1322 if (!out->standby)
1323 select_devices(adev, out->usecase);
1324
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001325 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1326 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001327 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001328 voice_start_call(adev);
1329 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1330 voice_is_in_call(adev) &&
1331 (out == adev->primary_output)) {
1332 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001333 }
1334 }
1335
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001336 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1337 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001338 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001339 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001340 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001341
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001342 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001343 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001344 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001345
1346 if (out == adev->primary_output) {
1347 pthread_mutex_lock(&adev->lock);
1348 audio_extn_set_parameters(adev, parms);
1349 pthread_mutex_unlock(&adev->lock);
1350 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001351 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1352 parse_compress_metadata(out, parms);
1353 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001354
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001355 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001356 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001357 return ret;
1358}
1359
1360static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1361{
1362 struct stream_out *out = (struct stream_out *)stream;
1363 struct str_parms *query = str_parms_create_str(keys);
1364 char *str;
1365 char value[256];
1366 struct str_parms *reply = str_parms_create();
1367 size_t i, j;
1368 int ret;
1369 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001370 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001371 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1372 if (ret >= 0) {
1373 value[0] = '\0';
1374 i = 0;
1375 while (out->supported_channel_masks[i] != 0) {
1376 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1377 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1378 if (!first) {
1379 strcat(value, "|");
1380 }
1381 strcat(value, out_channels_name_to_enum_table[j].name);
1382 first = false;
1383 break;
1384 }
1385 }
1386 i++;
1387 }
1388 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1389 str = str_parms_to_str(reply);
1390 } else {
1391 str = strdup(keys);
1392 }
1393 str_parms_destroy(query);
1394 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001395 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001396 return str;
1397}
1398
1399static uint32_t out_get_latency(const struct audio_stream_out *stream)
1400{
1401 struct stream_out *out = (struct stream_out *)stream;
1402
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001403 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1404 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1405
1406 return (out->config.period_count * out->config.period_size * 1000) /
1407 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001408}
1409
1410static int out_set_volume(struct audio_stream_out *stream, float left,
1411 float right)
1412{
Eric Laurenta9024de2013-04-04 09:19:12 -07001413 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001414 int volume[2];
1415
Eric Laurenta9024de2013-04-04 09:19:12 -07001416 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1417 /* only take left channel into account: the API is for stereo anyway */
1418 out->muted = (left == 0.0f);
1419 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001420 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1421 const char *mixer_ctl_name = "Compress Playback Volume";
1422 struct audio_device *adev = out->dev;
1423 struct mixer_ctl *ctl;
1424
1425 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1426 if (!ctl) {
1427 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1428 __func__, mixer_ctl_name);
1429 return -EINVAL;
1430 }
1431 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1432 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1433 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1434 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001435 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001436
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001437 return -ENOSYS;
1438}
1439
1440static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1441 size_t bytes)
1442{
1443 struct stream_out *out = (struct stream_out *)stream;
1444 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001445 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001446
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001447 pthread_mutex_lock(&out->lock);
1448 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001449 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001450 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001451 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001452 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001453 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001454 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001455 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001456 goto exit;
1457 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001458 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001459
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001460 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001461 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1462 if (out->send_new_metadata) {
1463 ALOGVV("send new gapless metadata");
1464 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1465 out->send_new_metadata = 0;
1466 }
1467
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001468 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001469 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001470 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001471 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1472 }
1473 if (!out->playback_started) {
1474 compress_start(out->compr);
1475 out->playback_started = 1;
1476 out->offload_state = OFFLOAD_STATE_PLAYING;
1477 }
1478 pthread_mutex_unlock(&out->lock);
1479 return ret;
1480 } else {
1481 if (out->pcm) {
1482 if (out->muted)
1483 memset((void *)buffer, 0, bytes);
1484 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1485 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001486 if (ret == 0)
1487 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001488 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001489 }
1490
1491exit:
1492 pthread_mutex_unlock(&out->lock);
1493
1494 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001495 if (out->pcm)
1496 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001497 out_standby(&out->stream.common);
1498 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1499 out_get_sample_rate(&out->stream.common));
1500 }
1501 return bytes;
1502}
1503
1504static int out_get_render_position(const struct audio_stream_out *stream,
1505 uint32_t *dsp_frames)
1506{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001507 struct stream_out *out = (struct stream_out *)stream;
1508 *dsp_frames = 0;
1509 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1510 pthread_mutex_lock(&out->lock);
1511 if (out->compr != NULL) {
1512 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1513 &out->sample_rate);
1514 ALOGVV("%s rendered frames %d sample_rate %d",
1515 __func__, *dsp_frames, out->sample_rate);
1516 }
1517 pthread_mutex_unlock(&out->lock);
1518 return 0;
1519 } else
1520 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001521}
1522
1523static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1524{
1525 return 0;
1526}
1527
1528static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1529{
1530 return 0;
1531}
1532
1533static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1534 int64_t *timestamp)
1535{
1536 return -EINVAL;
1537}
1538
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001539static int out_get_presentation_position(const struct audio_stream_out *stream,
1540 uint64_t *frames, struct timespec *timestamp)
1541{
1542 struct stream_out *out = (struct stream_out *)stream;
1543 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001544 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001545
1546 pthread_mutex_lock(&out->lock);
1547
Eric Laurent949a0892013-09-20 09:20:13 -07001548 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1549 if (out->compr != NULL) {
1550 compress_get_tstamp(out->compr, &dsp_frames,
1551 &out->sample_rate);
1552 ALOGVV("%s rendered frames %ld sample_rate %d",
1553 __func__, dsp_frames, out->sample_rate);
1554 *frames = dsp_frames;
1555 ret = 0;
1556 /* this is the best we can do */
1557 clock_gettime(CLOCK_MONOTONIC, timestamp);
1558 }
1559 } else {
1560 if (out->pcm) {
1561 size_t avail;
1562 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1563 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001564 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001565 // This adjustment accounts for buffering after app processor.
1566 // It is based on estimated DSP latency per use case, rather than exact.
1567 signed_frames -=
1568 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1569
Eric Laurent949a0892013-09-20 09:20:13 -07001570 // It would be unusual for this value to be negative, but check just in case ...
1571 if (signed_frames >= 0) {
1572 *frames = signed_frames;
1573 ret = 0;
1574 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001575 }
1576 }
1577 }
1578
1579 pthread_mutex_unlock(&out->lock);
1580
1581 return ret;
1582}
1583
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001584static int out_set_callback(struct audio_stream_out *stream,
1585 stream_callback_t callback, void *cookie)
1586{
1587 struct stream_out *out = (struct stream_out *)stream;
1588
1589 ALOGV("%s", __func__);
1590 pthread_mutex_lock(&out->lock);
1591 out->offload_callback = callback;
1592 out->offload_cookie = cookie;
1593 pthread_mutex_unlock(&out->lock);
1594 return 0;
1595}
1596
1597static int out_pause(struct audio_stream_out* stream)
1598{
1599 struct stream_out *out = (struct stream_out *)stream;
1600 int status = -ENOSYS;
1601 ALOGV("%s", __func__);
1602 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1603 pthread_mutex_lock(&out->lock);
1604 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1605 status = compress_pause(out->compr);
1606 out->offload_state = OFFLOAD_STATE_PAUSED;
1607 }
1608 pthread_mutex_unlock(&out->lock);
1609 }
1610 return status;
1611}
1612
1613static int out_resume(struct audio_stream_out* stream)
1614{
1615 struct stream_out *out = (struct stream_out *)stream;
1616 int status = -ENOSYS;
1617 ALOGV("%s", __func__);
1618 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1619 status = 0;
1620 pthread_mutex_lock(&out->lock);
1621 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1622 status = compress_resume(out->compr);
1623 out->offload_state = OFFLOAD_STATE_PLAYING;
1624 }
1625 pthread_mutex_unlock(&out->lock);
1626 }
1627 return status;
1628}
1629
1630static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1631{
1632 struct stream_out *out = (struct stream_out *)stream;
1633 int status = -ENOSYS;
1634 ALOGV("%s", __func__);
1635 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1636 pthread_mutex_lock(&out->lock);
1637 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1638 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1639 else
1640 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1641 pthread_mutex_unlock(&out->lock);
1642 }
1643 return status;
1644}
1645
1646static int out_flush(struct audio_stream_out* stream)
1647{
1648 struct stream_out *out = (struct stream_out *)stream;
1649 ALOGV("%s", __func__);
1650 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1651 pthread_mutex_lock(&out->lock);
1652 stop_compressed_output_l(out);
1653 pthread_mutex_unlock(&out->lock);
1654 return 0;
1655 }
1656 return -ENOSYS;
1657}
1658
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001659/** audio_stream_in implementation **/
1660static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1661{
1662 struct stream_in *in = (struct stream_in *)stream;
1663
1664 return in->config.rate;
1665}
1666
1667static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1668{
1669 return -ENOSYS;
1670}
1671
1672static size_t in_get_buffer_size(const struct audio_stream *stream)
1673{
1674 struct stream_in *in = (struct stream_in *)stream;
1675
1676 return in->config.period_size * audio_stream_frame_size(stream);
1677}
1678
1679static uint32_t in_get_channels(const struct audio_stream *stream)
1680{
1681 struct stream_in *in = (struct stream_in *)stream;
1682
1683 return in->channel_mask;
1684}
1685
1686static audio_format_t in_get_format(const struct audio_stream *stream)
1687{
1688 return AUDIO_FORMAT_PCM_16_BIT;
1689}
1690
1691static int in_set_format(struct audio_stream *stream, audio_format_t format)
1692{
1693 return -ENOSYS;
1694}
1695
1696static int in_standby(struct audio_stream *stream)
1697{
1698 struct stream_in *in = (struct stream_in *)stream;
1699 struct audio_device *adev = in->dev;
1700 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001701 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001702 pthread_mutex_lock(&in->lock);
1703 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001704 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001705 if (in->pcm) {
1706 pcm_close(in->pcm);
1707 in->pcm = NULL;
1708 }
1709 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001710 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001711 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001712 }
1713 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001714 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001715 return status;
1716}
1717
1718static int in_dump(const struct audio_stream *stream, int fd)
1719{
1720 return 0;
1721}
1722
1723static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1724{
1725 struct stream_in *in = (struct stream_in *)stream;
1726 struct audio_device *adev = in->dev;
1727 struct str_parms *parms;
1728 char *str;
1729 char value[32];
1730 int ret, val = 0;
1731
Eric Laurent994a6932013-07-17 11:51:42 -07001732 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001733 parms = str_parms_create_str(kvpairs);
1734
1735 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1736
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001737 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001738 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001739 if (ret >= 0) {
1740 val = atoi(value);
1741 /* no audio source uses val == 0 */
1742 if ((in->source != val) && (val != 0)) {
1743 in->source = val;
1744 }
1745 }
1746
1747 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1748 if (ret >= 0) {
1749 val = atoi(value);
1750 if ((in->device != val) && (val != 0)) {
1751 in->device = val;
1752 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001753 if (!in->standby)
1754 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001755 }
1756 }
1757
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001758 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001759 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001760
1761 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001762 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001763 return ret;
1764}
1765
1766static char* in_get_parameters(const struct audio_stream *stream,
1767 const char *keys)
1768{
1769 return strdup("");
1770}
1771
1772static int in_set_gain(struct audio_stream_in *stream, float gain)
1773{
1774 return 0;
1775}
1776
1777static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1778 size_t bytes)
1779{
1780 struct stream_in *in = (struct stream_in *)stream;
1781 struct audio_device *adev = in->dev;
1782 int i, ret = -1;
1783
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001784 pthread_mutex_lock(&in->lock);
1785 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001786 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001787 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001788 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001789 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790 goto exit;
1791 }
1792 in->standby = 0;
1793 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001794
1795 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001796 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1797 ret = audio_extn_ssr_read(stream, buffer, bytes);
1798 else
1799 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001800 }
1801
1802 /*
1803 * Instead of writing zeroes here, we could trust the hardware
1804 * to always provide zeroes when muted.
1805 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001806 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001807 memset(buffer, 0, bytes);
1808
1809exit:
1810 pthread_mutex_unlock(&in->lock);
1811
1812 if (ret != 0) {
1813 in_standby(&in->stream.common);
1814 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1815 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1816 in_get_sample_rate(&in->stream.common));
1817 }
1818 return bytes;
1819}
1820
1821static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1822{
1823 return 0;
1824}
1825
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001826static int add_remove_audio_effect(const struct audio_stream *stream,
1827 effect_handle_t effect,
1828 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001829{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001830 struct stream_in *in = (struct stream_in *)stream;
1831 int status = 0;
1832 effect_descriptor_t desc;
1833
1834 status = (*effect)->get_descriptor(effect, &desc);
1835 if (status != 0)
1836 return status;
1837
1838 pthread_mutex_lock(&in->lock);
1839 pthread_mutex_lock(&in->dev->lock);
1840 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1841 in->enable_aec != enable &&
1842 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1843 in->enable_aec = enable;
1844 if (!in->standby)
1845 select_devices(in->dev, in->usecase);
1846 }
1847 pthread_mutex_unlock(&in->dev->lock);
1848 pthread_mutex_unlock(&in->lock);
1849
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001850 return 0;
1851}
1852
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001853static int in_add_audio_effect(const struct audio_stream *stream,
1854 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001855{
Eric Laurent994a6932013-07-17 11:51:42 -07001856 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001857 return add_remove_audio_effect(stream, effect, true);
1858}
1859
1860static int in_remove_audio_effect(const struct audio_stream *stream,
1861 effect_handle_t effect)
1862{
Eric Laurent994a6932013-07-17 11:51:42 -07001863 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001864 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001865}
1866
1867static int adev_open_output_stream(struct audio_hw_device *dev,
1868 audio_io_handle_t handle,
1869 audio_devices_t devices,
1870 audio_output_flags_t flags,
1871 struct audio_config *config,
1872 struct audio_stream_out **stream_out)
1873{
1874 struct audio_device *adev = (struct audio_device *)dev;
1875 struct stream_out *out;
1876 int i, ret;
1877
Eric Laurent994a6932013-07-17 11:51:42 -07001878 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001879 __func__, config->sample_rate, config->channel_mask, devices, flags);
1880 *stream_out = NULL;
1881 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1882
1883 if (devices == AUDIO_DEVICE_NONE)
1884 devices = AUDIO_DEVICE_OUT_SPEAKER;
1885
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001886 out->flags = flags;
1887 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001888 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001889 out->format = config->format;
1890 out->sample_rate = config->sample_rate;
1891 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1892 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001893 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001894
1895 /* Init use case and pcm_config */
1896 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1897 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001898 pthread_mutex_lock(&adev->lock);
1899 ret = read_hdmi_channel_masks(out);
1900 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001901 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001902 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001903
1904 if (config->sample_rate == 0)
1905 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1906 if (config->channel_mask == 0)
1907 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1908
1909 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001910 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001911 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1912 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001913 out->config.rate = config->sample_rate;
1914 out->config.channels = popcount(out->channel_mask);
1915 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001916 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1917 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1918 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001919 out->sample_rate = out->config.rate;
1920 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1921 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1922 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1923 ALOGE("%s: Unsupported Offload information", __func__);
1924 ret = -EINVAL;
1925 goto error_open;
1926 }
1927 if (!is_supported_format(config->offload_info.format)) {
1928 ALOGE("%s: Unsupported audio format", __func__);
1929 ret = -EINVAL;
1930 goto error_open;
1931 }
1932
1933 out->compr_config.codec = (struct snd_codec *)
1934 calloc(1, sizeof(struct snd_codec));
1935
1936 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1937 if (config->offload_info.channel_mask)
1938 out->channel_mask = config->offload_info.channel_mask;
1939 else if (config->channel_mask)
1940 out->channel_mask = config->channel_mask;
1941 out->format = config->offload_info.format;
1942 out->sample_rate = config->offload_info.sample_rate;
1943
1944 out->stream.set_callback = out_set_callback;
1945 out->stream.pause = out_pause;
1946 out->stream.resume = out_resume;
1947 out->stream.drain = out_drain;
1948 out->stream.flush = out_flush;
1949
1950 out->compr_config.codec->id =
1951 get_snd_codec_id(config->offload_info.format);
1952 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1953 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1954 out->compr_config.codec->sample_rate =
1955 compress_get_alsa_rate(config->offload_info.sample_rate);
1956 out->compr_config.codec->bit_rate =
1957 config->offload_info.bit_rate;
1958 out->compr_config.codec->ch_in =
1959 popcount(config->channel_mask);
1960 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1961
1962 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1963 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001964
1965 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001966 create_offload_callback_thread(out);
1967 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1968 __func__, config->offload_info.version,
1969 config->offload_info.bit_rate);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07001970 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
1971 ret = voice_check_and_set_incall_music_usecase(adev, out);
1972 if (ret != 0) {
1973 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
1974 __func__, ret);
1975 goto error_open;
1976 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001977 } else {
1978 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1979 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001980 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001981 }
1982
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001983 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1984 if(adev->primary_output == NULL)
1985 adev->primary_output = out;
1986 else {
1987 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001988 ret = -EEXIST;
1989 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001990 }
1991 }
1992
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001993 /* Check if this usecase is already existing */
1994 pthread_mutex_lock(&adev->lock);
1995 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1996 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001997 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001998 ret = -EEXIST;
1999 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 }
2001 pthread_mutex_unlock(&adev->lock);
2002
2003 out->stream.common.get_sample_rate = out_get_sample_rate;
2004 out->stream.common.set_sample_rate = out_set_sample_rate;
2005 out->stream.common.get_buffer_size = out_get_buffer_size;
2006 out->stream.common.get_channels = out_get_channels;
2007 out->stream.common.get_format = out_get_format;
2008 out->stream.common.set_format = out_set_format;
2009 out->stream.common.standby = out_standby;
2010 out->stream.common.dump = out_dump;
2011 out->stream.common.set_parameters = out_set_parameters;
2012 out->stream.common.get_parameters = out_get_parameters;
2013 out->stream.common.add_audio_effect = out_add_audio_effect;
2014 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2015 out->stream.get_latency = out_get_latency;
2016 out->stream.set_volume = out_set_volume;
2017 out->stream.write = out_write;
2018 out->stream.get_render_position = out_get_render_position;
2019 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002020 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002021
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002022 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002023 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002024 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002025
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002026 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2027 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002028
2029 config->format = out->stream.common.get_format(&out->stream.common);
2030 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2031 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2032
2033 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002034 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002035 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002036
2037error_open:
2038 free(out);
2039 *stream_out = NULL;
2040 ALOGD("%s: exit: ret %d", __func__, ret);
2041 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002042}
2043
2044static void adev_close_output_stream(struct audio_hw_device *dev,
2045 struct audio_stream_out *stream)
2046{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002047 struct stream_out *out = (struct stream_out *)stream;
2048 struct audio_device *adev = out->dev;
2049
Eric Laurent994a6932013-07-17 11:51:42 -07002050 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002051 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002052 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2053 destroy_offload_callback_thread(out);
2054
2055 if (out->compr_config.codec != NULL)
2056 free(out->compr_config.codec);
2057 }
2058 pthread_cond_destroy(&out->cond);
2059 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002060 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002061 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002062}
2063
2064static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2065{
2066 struct audio_device *adev = (struct audio_device *)dev;
2067 struct str_parms *parms;
2068 char *str;
2069 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002070 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002071 int ret;
2072
Eric Laurent994a6932013-07-17 11:51:42 -07002073 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002074 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002075
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002076 voice_set_parameters(adev, parms);
2077 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002078
2079 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2080 if (ret >= 0) {
2081 /* When set to false, HAL should disable EC and NS
2082 * But it is currently not supported.
2083 */
2084 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2085 adev->bluetooth_nrec = true;
2086 else
2087 adev->bluetooth_nrec = false;
2088 }
2089
2090 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2091 if (ret >= 0) {
2092 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2093 adev->screen_off = false;
2094 else
2095 adev->screen_off = true;
2096 }
2097
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002098 ret = str_parms_get_int(parms, "rotation", &val);
2099 if (ret >= 0) {
2100 bool reverse_speakers = false;
2101 switch(val) {
2102 // FIXME: note that the code below assumes that the speakers are in the correct placement
2103 // relative to the user when the device is rotated 90deg from its default rotation. This
2104 // assumption is device-specific, not platform-specific like this code.
2105 case 270:
2106 reverse_speakers = true;
2107 break;
2108 case 0:
2109 case 90:
2110 case 180:
2111 break;
2112 default:
2113 ALOGE("%s: unexpected rotation of %d", __func__, val);
2114 }
2115 pthread_mutex_lock(&adev->lock);
2116 if (adev->speaker_lr_swap != reverse_speakers) {
2117 adev->speaker_lr_swap = reverse_speakers;
2118 // only update the selected device if there is active pcm playback
2119 struct audio_usecase *usecase;
2120 struct listnode *node;
2121 list_for_each(node, &adev->usecase_list) {
2122 usecase = node_to_item(node, struct audio_usecase, list);
2123 if (usecase->type == PCM_PLAYBACK) {
2124 select_devices(adev, usecase->id);
2125 break;
2126 }
2127 }
2128 }
2129 pthread_mutex_unlock(&adev->lock);
2130 }
2131
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002132 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002133 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002134 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002135 return ret;
2136}
2137
2138static char* adev_get_parameters(const struct audio_hw_device *dev,
2139 const char *keys)
2140{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002141 struct audio_device *adev = (struct audio_device *)dev;
2142 struct str_parms *reply = str_parms_create();
2143 struct str_parms *query = str_parms_create_str(keys);
2144 char *str;
2145
2146 audio_extn_get_parameters(adev, query, reply);
2147 platform_get_parameters(adev->platform, query, reply);
2148 str = str_parms_to_str(reply);
2149 str_parms_destroy(query);
2150 str_parms_destroy(reply);
2151
2152 ALOGV("%s: exit: returns - %s", __func__, str);
2153 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002154}
2155
2156static int adev_init_check(const struct audio_hw_device *dev)
2157{
2158 return 0;
2159}
2160
2161static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2162{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002163 int ret;
2164 struct audio_device *adev = (struct audio_device *)dev;
2165 pthread_mutex_lock(&adev->lock);
2166 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002167 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002168 pthread_mutex_unlock(&adev->lock);
2169 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002170}
2171
2172static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2173{
2174 return -ENOSYS;
2175}
2176
2177static int adev_get_master_volume(struct audio_hw_device *dev,
2178 float *volume)
2179{
2180 return -ENOSYS;
2181}
2182
2183static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2184{
2185 return -ENOSYS;
2186}
2187
2188static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2189{
2190 return -ENOSYS;
2191}
2192
2193static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2194{
2195 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002196 pthread_mutex_lock(&adev->lock);
2197 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002198 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002199 adev->mode = mode;
2200 }
2201 pthread_mutex_unlock(&adev->lock);
2202 return 0;
2203}
2204
2205static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2206{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002207 return voice_set_mic_mute((struct audio_device *)dev, state);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002208}
2209
2210static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2211{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002212 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002213 return 0;
2214}
2215
2216static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2217 const struct audio_config *config)
2218{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002219 int channel_count = popcount(config->channel_mask);
2220
2221 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2222}
2223
2224static int adev_open_input_stream(struct audio_hw_device *dev,
2225 audio_io_handle_t handle,
2226 audio_devices_t devices,
2227 struct audio_config *config,
2228 struct audio_stream_in **stream_in)
2229{
2230 struct audio_device *adev = (struct audio_device *)dev;
2231 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002232 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002233 int channel_count = popcount(config->channel_mask);
2234
Eric Laurent994a6932013-07-17 11:51:42 -07002235 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002236 *stream_in = NULL;
2237 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2238 return -EINVAL;
2239
2240 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2241
2242 in->stream.common.get_sample_rate = in_get_sample_rate;
2243 in->stream.common.set_sample_rate = in_set_sample_rate;
2244 in->stream.common.get_buffer_size = in_get_buffer_size;
2245 in->stream.common.get_channels = in_get_channels;
2246 in->stream.common.get_format = in_get_format;
2247 in->stream.common.set_format = in_set_format;
2248 in->stream.common.standby = in_standby;
2249 in->stream.common.dump = in_dump;
2250 in->stream.common.set_parameters = in_set_parameters;
2251 in->stream.common.get_parameters = in_get_parameters;
2252 in->stream.common.add_audio_effect = in_add_audio_effect;
2253 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2254 in->stream.set_gain = in_set_gain;
2255 in->stream.read = in_read;
2256 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2257
2258 in->device = devices;
2259 in->source = AUDIO_SOURCE_DEFAULT;
2260 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002261 in->standby = 1;
2262 in->channel_mask = config->channel_mask;
2263
2264 /* Update config params with the requested sample rate and channels */
2265 in->usecase = USECASE_AUDIO_RECORD;
2266 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002267 in->config.rate = config->sample_rate;
2268
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002269 if (channel_count == 6) {
2270 if(audio_extn_ssr_get_enabled()) {
2271 if(audio_extn_ssr_init(adev, in)) {
2272 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2273 ret = -EINVAL;
2274 goto err_open;
2275 }
2276 } else {
2277 ret = -EINVAL;
2278 goto err_open;
2279 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002280 } else {
2281 in->config.channels = channel_count;
2282 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2283 buffer_size = get_input_buffer_size(config->sample_rate,
2284 config->format,
2285 channel_count);
2286 in->config.period_size = buffer_size / frame_size;
2287 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002288
2289 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002290 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002291 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002292
2293err_open:
2294 free(in);
2295 *stream_in = NULL;
2296 return ret;
2297}
2298
2299static void adev_close_input_stream(struct audio_hw_device *dev,
2300 struct audio_stream_in *stream)
2301{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002302 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002303 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002304
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002305 in_standby(&stream->common);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002306 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2307 audio_extn_ssr_deinit();
2308 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002309 free(stream);
2310
2311 return;
2312}
2313
2314static int adev_dump(const audio_hw_device_t *device, int fd)
2315{
2316 return 0;
2317}
2318
2319static int adev_close(hw_device_t *device)
2320{
2321 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002322
2323 if (!adev)
2324 return 0;
2325
2326 pthread_mutex_lock(&adev_init_lock);
2327
2328 if ((--audio_device_ref_count) == 0) {
2329 audio_route_free(adev->audio_route);
2330 free(adev->snd_dev_ref_cnt);
2331 platform_deinit(adev->platform);
2332 audio_extn_listen_deinit(adev);
2333 free(device);
2334 adev = NULL;
2335 }
2336 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002337 return 0;
2338}
2339
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002340static int adev_open(const hw_module_t *module, const char *name,
2341 hw_device_t **device)
2342{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002343 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002344
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002345 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002346 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2347
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002348 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002349 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002350 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002351 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002352 ALOGD("%s: returning existing instance of adev", __func__);
2353 ALOGD("%s: exit", __func__);
2354 pthread_mutex_unlock(&adev_init_lock);
2355 return 0;
2356 }
2357
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002358 adev = calloc(1, sizeof(struct audio_device));
2359
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002360 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2361 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2362 adev->device.common.module = (struct hw_module_t *)module;
2363 adev->device.common.close = adev_close;
2364
2365 adev->device.init_check = adev_init_check;
2366 adev->device.set_voice_volume = adev_set_voice_volume;
2367 adev->device.set_master_volume = adev_set_master_volume;
2368 adev->device.get_master_volume = adev_get_master_volume;
2369 adev->device.set_master_mute = adev_set_master_mute;
2370 adev->device.get_master_mute = adev_get_master_mute;
2371 adev->device.set_mode = adev_set_mode;
2372 adev->device.set_mic_mute = adev_set_mic_mute;
2373 adev->device.get_mic_mute = adev_get_mic_mute;
2374 adev->device.set_parameters = adev_set_parameters;
2375 adev->device.get_parameters = adev_get_parameters;
2376 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2377 adev->device.open_output_stream = adev_open_output_stream;
2378 adev->device.close_output_stream = adev_close_output_stream;
2379 adev->device.open_input_stream = adev_open_input_stream;
2380 adev->device.close_input_stream = adev_close_input_stream;
2381 adev->device.dump = adev_dump;
2382
2383 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002384 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002385 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002386 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002387 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002388 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002389 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002390 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002391 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002392 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002393 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002394
2395 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002396 adev->platform = platform_init(adev);
2397 if (!adev->platform) {
2398 free(adev->snd_dev_ref_cnt);
2399 free(adev);
2400 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2401 *device = NULL;
2402 return -EINVAL;
2403 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002404
2405 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2406 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2407 if (adev->visualizer_lib == NULL) {
2408 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2409 } else {
2410 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2411 adev->visualizer_start_output =
2412 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2413 "visualizer_hal_start_output");
2414 adev->visualizer_stop_output =
2415 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2416 "visualizer_hal_stop_output");
2417 }
2418 }
Kiran Kandi910e1862013-10-29 13:29:42 -07002419 audio_extn_listen_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07002420
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002421 *device = &adev->device.common;
2422
Kiran Kandi910e1862013-10-29 13:29:42 -07002423 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002424 pthread_mutex_unlock(&adev_init_lock);
2425
Eric Laurent994a6932013-07-17 11:51:42 -07002426 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002427 return 0;
2428}
2429
2430static struct hw_module_methods_t hal_module_methods = {
2431 .open = adev_open,
2432};
2433
2434struct audio_module HAL_MODULE_INFO_SYM = {
2435 .common = {
2436 .tag = HARDWARE_MODULE_TAG,
2437 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2438 .hal_api_version = HARDWARE_HAL_API_VERSION,
2439 .id = AUDIO_HARDWARE_MODULE_ID,
2440 .name = "QCOM Audio HAL",
2441 .author = "Code Aurora Forum",
2442 .methods = &hal_module_methods,
2443 },
2444};