blob: 74217b75f5d0ac2f297b3c5a5ba315b856e465b3 [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);
361 enable_snd_device(adev, snd_device, false);
362 }
363 }
364
365 /* Make sure new snd device is enabled before re-routing the streams */
366 audio_route_update_mixer(adev->audio_route);
367
368 /* Re-route all the usecases on the shared backend other than the
369 specified usecase to new snd devices */
370 list_for_each(node, &adev->usecase_list) {
371 usecase = node_to_item(node, struct audio_usecase, list);
372 /* Update the out_snd_device only before enabling the audio route */
373 if (switch_device[usecase->id] ) {
374 usecase->out_snd_device = snd_device;
375 enable_audio_route(adev, usecase, false);
376 }
377 }
378
379 audio_route_update_mixer(adev->audio_route);
380 }
381}
382
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700383static void check_and_route_capture_usecases(struct audio_device *adev,
384 struct audio_usecase *uc_info,
385 snd_device_t snd_device)
386{
387 struct listnode *node;
388 struct audio_usecase *usecase;
389 bool switch_device[AUDIO_USECASE_MAX];
390 int i, num_uc_to_switch = 0;
391
392 /*
393 * This function is to make sure that all the active capture usecases
394 * are always routed to the same input sound device.
395 * For example, if audio-record and voice-call usecases are currently
396 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
397 * is received for voice call then we have to make sure that audio-record
398 * usecase is also switched to earpiece i.e. voice-dmic-ef,
399 * because of the limitation that two devices cannot be enabled
400 * at the same time if they share the same backend.
401 */
402 for (i = 0; i < AUDIO_USECASE_MAX; i++)
403 switch_device[i] = false;
404
405 list_for_each(node, &adev->usecase_list) {
406 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700407 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700408 usecase != uc_info &&
409 usecase->in_snd_device != snd_device) {
410 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
411 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700412 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700413 disable_audio_route(adev, usecase, false);
414 switch_device[usecase->id] = true;
415 num_uc_to_switch++;
416 }
417 }
418
419 if (num_uc_to_switch) {
420 /* Make sure all the streams are de-routed before disabling the device */
421 audio_route_update_mixer(adev->audio_route);
422
423 list_for_each(node, &adev->usecase_list) {
424 usecase = node_to_item(node, struct audio_usecase, list);
425 if (switch_device[usecase->id]) {
426 disable_snd_device(adev, usecase->in_snd_device, false);
427 enable_snd_device(adev, snd_device, false);
428 }
429 }
430
431 /* Make sure new snd device is enabled before re-routing the streams */
432 audio_route_update_mixer(adev->audio_route);
433
434 /* Re-route all the usecases on the shared backend other than the
435 specified usecase to new snd devices */
436 list_for_each(node, &adev->usecase_list) {
437 usecase = node_to_item(node, struct audio_usecase, list);
438 /* Update the in_snd_device only before enabling the audio route */
439 if (switch_device[usecase->id] ) {
440 usecase->in_snd_device = snd_device;
441 enable_audio_route(adev, usecase, false);
442 }
443 }
444
445 audio_route_update_mixer(adev->audio_route);
446 }
447}
448
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700449static int disable_all_usecases_of_type(struct audio_device *adev,
450 usecase_type_t usecase_type,
451 bool update_mixer)
452{
453 struct audio_usecase *usecase;
454 struct listnode *node;
455 int ret = 0;
456
457 list_for_each(node, &adev->usecase_list) {
458 usecase = node_to_item(node, struct audio_usecase, list);
459 if (usecase->type == usecase_type) {
460 ALOGV("%s: usecase id %d", __func__, usecase->id);
461 ret = disable_audio_route(adev, usecase, update_mixer);
462 if (ret) {
463 ALOGE("%s: Failed to disable usecase id %d",
464 __func__, usecase->id);
465 }
466 }
467 }
468
469 return ret;
470}
471
472static int enable_all_usecases_of_type(struct audio_device *adev,
473 usecase_type_t usecase_type,
474 bool update_mixer)
475{
476 struct audio_usecase *usecase;
477 struct listnode *node;
478 int ret = 0;
479
480 list_for_each(node, &adev->usecase_list) {
481 usecase = node_to_item(node, struct audio_usecase, list);
482 if (usecase->type == usecase_type) {
483 ALOGV("%s: usecase id %d", __func__, usecase->id);
484 ret = enable_audio_route(adev, usecase, update_mixer);
485 if (ret) {
486 ALOGE("%s: Failed to enable usecase id %d",
487 __func__, usecase->id);
488 }
489 }
490 }
491
492 return ret;
493}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800494
495/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700496static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800497{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700498 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700499 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800500
501 switch (channels) {
502 /*
503 * Do not handle stereo output in Multi-channel cases
504 * Stereo case is handled in normal playback path
505 */
506 case 6:
507 ALOGV("%s: HDMI supports 5.1", __func__);
508 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
509 break;
510 case 8:
511 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
512 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
513 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
514 break;
515 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700516 ALOGE("HDMI does not support multi channel playback");
517 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800518 break;
519 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700520 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800521}
522
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700523static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
524{
525 struct audio_usecase *usecase;
526 struct listnode *node;
527
528 list_for_each(node, &adev->usecase_list) {
529 usecase = node_to_item(node, struct audio_usecase, list);
530 if (usecase->type == VOICE_CALL) {
531 ALOGV("%s: usecase id %d", __func__, usecase->id);
532 return usecase->id;
533 }
534 }
535 return USECASE_INVALID;
536}
537
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700538struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700539 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700540{
541 struct audio_usecase *usecase;
542 struct listnode *node;
543
544 list_for_each(node, &adev->usecase_list) {
545 usecase = node_to_item(node, struct audio_usecase, list);
546 if (usecase->id == uc_id)
547 return usecase;
548 }
549 return NULL;
550}
551
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700552int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800553{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800554 snd_device_t out_snd_device = SND_DEVICE_NONE;
555 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700556 struct audio_usecase *usecase = NULL;
557 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800558 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700559 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800560
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700561 usecase = get_usecase_from_list(adev, uc_id);
562 if (usecase == NULL) {
563 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
564 return -EINVAL;
565 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800566
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700567 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700568 out_snd_device = platform_get_output_snd_device(adev->platform,
569 usecase->stream.out->devices);
570 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700571 usecase->devices = usecase->stream.out->devices;
572 } else {
573 /*
574 * If the voice call is active, use the sound devices of voice call usecase
575 * so that it would not result any device switch. All the usecases will
576 * be switched to new device when select_devices() is called for voice call
577 * usecase. This is to avoid switching devices for voice call when
578 * check_usecases_codec_backend() is called below.
579 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700580 if (voice_is_in_call(adev)) {
581 vc_usecase = get_usecase_from_list(adev,
582 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700583 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
584 in_snd_device = vc_usecase->in_snd_device;
585 out_snd_device = vc_usecase->out_snd_device;
586 }
587 }
588 if (usecase->type == PCM_PLAYBACK) {
589 usecase->devices = usecase->stream.out->devices;
590 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700591 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700592 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700593 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700594 if (usecase->stream.out == adev->primary_output &&
595 adev->active_input &&
596 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
597 select_devices(adev, adev->active_input->usecase);
598 }
599 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700600 } else if (usecase->type == PCM_CAPTURE) {
601 usecase->devices = usecase->stream.in->device;
602 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700603 if (in_snd_device == SND_DEVICE_NONE) {
604 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
605 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700606 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700607 adev->primary_output->devices);
608 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700609 in_snd_device = platform_get_input_snd_device(adev->platform,
610 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700611 }
612 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700613 }
614 }
615
616 if (out_snd_device == usecase->out_snd_device &&
617 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800618 return 0;
619 }
620
sangwoobc677242013-08-08 16:53:43 +0900621 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700622 out_snd_device, platform_get_snd_device_name(out_snd_device),
623 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800624
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800625 /*
626 * Limitation: While in call, to do a device switch we need to disable
627 * and enable both RX and TX devices though one of them is same as current
628 * device.
629 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700630 if (usecase->type == VOICE_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700631 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700632 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800633 }
634
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700635 /* Disable current sound devices */
636 if (usecase->out_snd_device != SND_DEVICE_NONE) {
637 disable_audio_route(adev, usecase, true);
638 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800639 }
640
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700641 if (usecase->in_snd_device != SND_DEVICE_NONE) {
642 disable_audio_route(adev, usecase, true);
643 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800644 }
645
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700646 /* Enable new sound devices */
647 if (out_snd_device != SND_DEVICE_NONE) {
648 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
649 check_usecases_codec_backend(adev, usecase, out_snd_device);
650 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800651 }
652
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700653 if (in_snd_device != SND_DEVICE_NONE) {
654 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700655 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700656 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700657
Eric Laurentb23d5282013-05-14 15:27:20 -0700658 if (usecase->type == VOICE_CALL)
659 status = platform_switch_voice_call_device_post(adev->platform,
660 out_snd_device,
661 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800662
sangwoo170731f2013-06-08 15:36:36 +0900663 audio_route_update_mixer(adev->audio_route);
664
665 usecase->in_snd_device = in_snd_device;
666 usecase->out_snd_device = out_snd_device;
667
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700668 if (usecase->type == VOICE_CALL)
669 enable_all_usecases_of_type(adev, VOICE_CALL, true);
670 else
671 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900672
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800673 return status;
674}
675
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800676static int stop_input_stream(struct stream_in *in)
677{
678 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800679 struct audio_usecase *uc_info;
680 struct audio_device *adev = in->dev;
681
Eric Laurentc8400632013-02-14 19:04:54 -0800682 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800683
Eric Laurent994a6932013-07-17 11:51:42 -0700684 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700685 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800686 uc_info = get_usecase_from_list(adev, in->usecase);
687 if (uc_info == NULL) {
688 ALOGE("%s: Could not find the usecase (%d) in the list",
689 __func__, in->usecase);
690 return -EINVAL;
691 }
692
Eric Laurent150dbfe2013-02-27 14:31:02 -0800693 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700694 disable_audio_route(adev, uc_info, true);
695
696 /* 2. Disable the tx device */
697 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800698
Kiran Kandi910e1862013-10-29 13:29:42 -0700699 audio_extn_listen_update_status(uc_info,
700 LISTEN_EVENT_AUDIO_CAPTURE_INACTIVE);
701
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800702 list_remove(&uc_info->list);
703 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800704
Eric Laurent994a6932013-07-17 11:51:42 -0700705 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800706 return ret;
707}
708
709int start_input_stream(struct stream_in *in)
710{
711 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800712 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800713 struct audio_usecase *uc_info;
714 struct audio_device *adev = in->dev;
715
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700716 in->usecase = platform_get_usecase_from_source(in->source);
Eric Laurent994a6932013-07-17 11:51:42 -0700717 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700718
719 /* Check if source matches incall recording usecase criteria */
720 ret = voice_check_and_set_incall_rec_usecase(adev, in);
721 if (ret)
722 goto error_config;
723 else
724 ALOGV("%s: usecase(%d)", __func__, in->usecase);
725
Eric Laurentb23d5282013-05-14 15:27:20 -0700726 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800727 if (in->pcm_device_id < 0) {
728 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
729 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800730 ret = -EINVAL;
731 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700733
734 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800735 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
736 uc_info->id = in->usecase;
737 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800738 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700739 uc_info->devices = in->device;
740 uc_info->in_snd_device = SND_DEVICE_NONE;
741 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800742
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800743 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700744 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745
Kiran Kandi910e1862013-10-29 13:29:42 -0700746 audio_extn_listen_update_status(uc_info,
747 LISTEN_EVENT_AUDIO_CAPTURE_ACTIVE);
748
Eric Laurentc8400632013-02-14 19:04:54 -0800749 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
750 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800751 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
752 PCM_IN, &in->config);
753 if (in->pcm && !pcm_is_ready(in->pcm)) {
754 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
755 pcm_close(in->pcm);
756 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800757 ret = -EIO;
758 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800759 }
Eric Laurent994a6932013-07-17 11:51:42 -0700760 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800761 return ret;
762
763error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800764 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800765
766error_config:
767 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700768 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800769
770 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800771}
772
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700773/* must be called with out->lock locked */
774static int send_offload_cmd_l(struct stream_out* out, int command)
775{
776 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
777
778 ALOGVV("%s %d", __func__, command);
779
780 cmd->cmd = command;
781 list_add_tail(&out->offload_cmd_list, &cmd->node);
782 pthread_cond_signal(&out->offload_cond);
783 return 0;
784}
785
786/* must be called iwth out->lock locked */
787static void stop_compressed_output_l(struct stream_out *out)
788{
789 out->offload_state = OFFLOAD_STATE_IDLE;
790 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700791 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700792 if (out->compr != NULL) {
793 compress_stop(out->compr);
794 while (out->offload_thread_blocked) {
795 pthread_cond_wait(&out->cond, &out->lock);
796 }
797 }
798}
799
800static void *offload_thread_loop(void *context)
801{
802 struct stream_out *out = (struct stream_out *) context;
803 struct listnode *item;
804
805 out->offload_state = OFFLOAD_STATE_IDLE;
806 out->playback_started = 0;
807
808 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
809 set_sched_policy(0, SP_FOREGROUND);
810 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
811
812 ALOGV("%s", __func__);
813 pthread_mutex_lock(&out->lock);
814 for (;;) {
815 struct offload_cmd *cmd = NULL;
816 stream_callback_event_t event;
817 bool send_callback = false;
818
819 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
820 __func__, list_empty(&out->offload_cmd_list),
821 out->offload_state);
822 if (list_empty(&out->offload_cmd_list)) {
823 ALOGV("%s SLEEPING", __func__);
824 pthread_cond_wait(&out->offload_cond, &out->lock);
825 ALOGV("%s RUNNING", __func__);
826 continue;
827 }
828
829 item = list_head(&out->offload_cmd_list);
830 cmd = node_to_item(item, struct offload_cmd, node);
831 list_remove(item);
832
833 ALOGVV("%s STATE %d CMD %d out->compr %p",
834 __func__, out->offload_state, cmd->cmd, out->compr);
835
836 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
837 free(cmd);
838 break;
839 }
840
841 if (out->compr == NULL) {
842 ALOGE("%s: Compress handle is NULL", __func__);
843 pthread_cond_signal(&out->cond);
844 continue;
845 }
846 out->offload_thread_blocked = true;
847 pthread_mutex_unlock(&out->lock);
848 send_callback = false;
849 switch(cmd->cmd) {
850 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
851 compress_wait(out->compr, -1);
852 send_callback = true;
853 event = STREAM_CBK_EVENT_WRITE_READY;
854 break;
855 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700856 compress_next_track(out->compr);
857 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700858 send_callback = true;
859 event = STREAM_CBK_EVENT_DRAIN_READY;
860 break;
861 case OFFLOAD_CMD_DRAIN:
862 compress_drain(out->compr);
863 send_callback = true;
864 event = STREAM_CBK_EVENT_DRAIN_READY;
865 break;
866 default:
867 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
868 break;
869 }
870 pthread_mutex_lock(&out->lock);
871 out->offload_thread_blocked = false;
872 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700873 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700874 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700875 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700876 free(cmd);
877 }
878
879 pthread_cond_signal(&out->cond);
880 while (!list_empty(&out->offload_cmd_list)) {
881 item = list_head(&out->offload_cmd_list);
882 list_remove(item);
883 free(node_to_item(item, struct offload_cmd, node));
884 }
885 pthread_mutex_unlock(&out->lock);
886
887 return NULL;
888}
889
890static int create_offload_callback_thread(struct stream_out *out)
891{
892 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
893 list_init(&out->offload_cmd_list);
894 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
895 offload_thread_loop, out);
896 return 0;
897}
898
899static int destroy_offload_callback_thread(struct stream_out *out)
900{
901 pthread_mutex_lock(&out->lock);
902 stop_compressed_output_l(out);
903 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
904
905 pthread_mutex_unlock(&out->lock);
906 pthread_join(out->offload_thread, (void **) NULL);
907 pthread_cond_destroy(&out->offload_cond);
908
909 return 0;
910}
911
Eric Laurent07eeafd2013-10-06 12:52:49 -0700912static bool allow_hdmi_channel_config(struct audio_device *adev)
913{
914 struct listnode *node;
915 struct audio_usecase *usecase;
916 bool ret = true;
917
918 list_for_each(node, &adev->usecase_list) {
919 usecase = node_to_item(node, struct audio_usecase, list);
920 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
921 /*
922 * If voice call is already existing, do not proceed further to avoid
923 * disabling/enabling both RX and TX devices, CSD calls, etc.
924 * Once the voice call done, the HDMI channels can be configured to
925 * max channels of remaining use cases.
926 */
927 if (usecase->id == USECASE_VOICE_CALL) {
928 ALOGD("%s: voice call is active, no change in HDMI channels",
929 __func__);
930 ret = false;
931 break;
932 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
933 ALOGD("%s: multi channel playback is active, "
934 "no change in HDMI channels", __func__);
935 ret = false;
936 break;
937 }
938 }
939 }
940 return ret;
941}
942
943static int check_and_set_hdmi_channels(struct audio_device *adev,
944 unsigned int channels)
945{
946 struct listnode *node;
947 struct audio_usecase *usecase;
948
949 /* Check if change in HDMI channel config is allowed */
950 if (!allow_hdmi_channel_config(adev))
951 return 0;
952
953 if (channels == adev->cur_hdmi_channels) {
954 ALOGD("%s: Requested channels are same as current", __func__);
955 return 0;
956 }
957
958 platform_set_hdmi_channels(adev->platform, channels);
959 adev->cur_hdmi_channels = channels;
960
961 /*
962 * Deroute all the playback streams routed to HDMI so that
963 * the back end is deactivated. Note that backend will not
964 * be deactivated if any one stream is connected to it.
965 */
966 list_for_each(node, &adev->usecase_list) {
967 usecase = node_to_item(node, struct audio_usecase, list);
968 if (usecase->type == PCM_PLAYBACK &&
969 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
970 disable_audio_route(adev, usecase, true);
971 }
972 }
973
974 /*
975 * Enable all the streams disabled above. Now the HDMI backend
976 * will be activated with new channel configuration
977 */
978 list_for_each(node, &adev->usecase_list) {
979 usecase = node_to_item(node, struct audio_usecase, list);
980 if (usecase->type == PCM_PLAYBACK &&
981 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
982 enable_audio_route(adev, usecase, true);
983 }
984 }
985
986 return 0;
987}
988
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800989static int stop_output_stream(struct stream_out *out)
990{
991 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800992 struct audio_usecase *uc_info;
993 struct audio_device *adev = out->dev;
994
Eric Laurent994a6932013-07-17 11:51:42 -0700995 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700996 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800997 uc_info = get_usecase_from_list(adev, out->usecase);
998 if (uc_info == NULL) {
999 ALOGE("%s: Could not find the usecase (%d) in the list",
1000 __func__, out->usecase);
1001 return -EINVAL;
1002 }
1003
Eric Laurentc4aef752013-09-12 17:45:53 -07001004 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
1005 adev->visualizer_stop_output != NULL)
1006 adev->visualizer_stop_output(out->handle);
1007
Eric Laurent150dbfe2013-02-27 14:31:02 -08001008 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001009 disable_audio_route(adev, uc_info, true);
1010
1011 /* 2. Disable the rx device */
1012 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001013
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001014 list_remove(&uc_info->list);
1015 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001016
Eric Laurent07eeafd2013-10-06 12:52:49 -07001017 /* Must be called after removing the usecase from list */
1018 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1019 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1020
Eric Laurent994a6932013-07-17 11:51:42 -07001021 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001022 return ret;
1023}
1024
1025int start_output_stream(struct stream_out *out)
1026{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001027 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001028 struct audio_usecase *uc_info;
1029 struct audio_device *adev = out->dev;
1030
Eric Laurent994a6932013-07-17 11:51:42 -07001031 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001032 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001033 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001034 if (out->pcm_device_id < 0) {
1035 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1036 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001037 ret = -EINVAL;
1038 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001039 }
1040
1041 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1042 uc_info->id = out->usecase;
1043 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001044 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001045 uc_info->devices = out->devices;
1046 uc_info->in_snd_device = SND_DEVICE_NONE;
1047 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001048
Eric Laurent07eeafd2013-10-06 12:52:49 -07001049 /* This must be called before adding this usecase to the list */
1050 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1051 check_and_set_hdmi_channels(adev, out->config.channels);
1052
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001053 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001054
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001055 select_devices(adev, out->usecase);
1056
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001057 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1058 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001059 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1060 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001061 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001062 if (out->pcm && !pcm_is_ready(out->pcm)) {
1063 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1064 pcm_close(out->pcm);
1065 out->pcm = NULL;
1066 ret = -EIO;
1067 goto error_open;
1068 }
1069 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001070 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001071 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1072 COMPRESS_IN, &out->compr_config);
1073 if (out->compr && !is_compress_ready(out->compr)) {
1074 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1075 compress_close(out->compr);
1076 out->compr = NULL;
1077 ret = -EIO;
1078 goto error_open;
1079 }
1080 if (out->offload_callback)
1081 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001082
1083 if (adev->visualizer_start_output != NULL)
1084 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001085 }
Eric Laurent994a6932013-07-17 11:51:42 -07001086 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001087 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001088error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001089 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001090error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001091 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001092}
1093
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001094static int check_input_parameters(uint32_t sample_rate,
1095 audio_format_t format,
1096 int channel_count)
1097{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001098 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001099
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001100 if (format != AUDIO_FORMAT_PCM_16_BIT) ret = -EINVAL;
1101
1102 switch (channel_count) {
1103 case 1:
1104 case 2:
1105 case 6:
1106 break;
1107 default:
1108 ret = -EINVAL;
1109 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001110
1111 switch (sample_rate) {
1112 case 8000:
1113 case 11025:
1114 case 12000:
1115 case 16000:
1116 case 22050:
1117 case 24000:
1118 case 32000:
1119 case 44100:
1120 case 48000:
1121 break;
1122 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001123 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001124 }
1125
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001126 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001127}
1128
1129static size_t get_input_buffer_size(uint32_t sample_rate,
1130 audio_format_t format,
1131 int channel_count)
1132{
1133 size_t size = 0;
1134
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001135 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1136 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001137
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001138 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1139 /* ToDo: should use frame_size computed based on the format and
1140 channel_count here. */
1141 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001142
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001143 /* make sure the size is multiple of 64 */
1144 size += 0x3f;
1145 size &= ~0x3f;
1146
1147 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001148}
1149
1150static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1151{
1152 struct stream_out *out = (struct stream_out *)stream;
1153
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001154 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001155}
1156
1157static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1158{
1159 return -ENOSYS;
1160}
1161
1162static size_t out_get_buffer_size(const struct audio_stream *stream)
1163{
1164 struct stream_out *out = (struct stream_out *)stream;
1165
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001166 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1167 return out->compr_config.fragment_size;
1168 }
1169
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001170 return out->config.period_size * audio_stream_frame_size(stream);
1171}
1172
1173static uint32_t out_get_channels(const struct audio_stream *stream)
1174{
1175 struct stream_out *out = (struct stream_out *)stream;
1176
1177 return out->channel_mask;
1178}
1179
1180static audio_format_t out_get_format(const struct audio_stream *stream)
1181{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001182 struct stream_out *out = (struct stream_out *)stream;
1183
1184 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001185}
1186
1187static int out_set_format(struct audio_stream *stream, audio_format_t format)
1188{
1189 return -ENOSYS;
1190}
1191
1192static int out_standby(struct audio_stream *stream)
1193{
1194 struct stream_out *out = (struct stream_out *)stream;
1195 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001196
Eric Laurent994a6932013-07-17 11:51:42 -07001197 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001198 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001199
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001200 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201 if (!out->standby) {
1202 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001203 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1204 if (out->pcm) {
1205 pcm_close(out->pcm);
1206 out->pcm = NULL;
1207 }
1208 } else {
1209 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001210 out->gapless_mdata.encoder_delay = 0;
1211 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001212 if (out->compr != NULL) {
1213 compress_close(out->compr);
1214 out->compr = NULL;
1215 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001216 }
1217 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001218 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001219 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001220 }
1221 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001222 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001223 return 0;
1224}
1225
1226static int out_dump(const struct audio_stream *stream, int fd)
1227{
1228 return 0;
1229}
1230
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001231static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1232{
1233 int ret = 0;
1234 char value[32];
1235 struct compr_gapless_mdata tmp_mdata;
1236
1237 if (!out || !parms) {
1238 return -EINVAL;
1239 }
1240
1241 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1242 if (ret >= 0) {
1243 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1244 } else {
1245 return -EINVAL;
1246 }
1247
1248 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1249 if (ret >= 0) {
1250 tmp_mdata.encoder_padding = atoi(value);
1251 } else {
1252 return -EINVAL;
1253 }
1254
1255 out->gapless_mdata = tmp_mdata;
1256 out->send_new_metadata = 1;
1257 ALOGV("%s new encoder delay %u and padding %u", __func__,
1258 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1259
1260 return 0;
1261}
1262
1263
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001264static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1265{
1266 struct stream_out *out = (struct stream_out *)stream;
1267 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001268 struct audio_usecase *usecase;
1269 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001270 struct str_parms *parms;
1271 char value[32];
1272 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001273 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001274
sangwoobc677242013-08-08 16:53:43 +09001275 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001276 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277 parms = str_parms_create_str(kvpairs);
1278 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1279 if (ret >= 0) {
1280 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001281 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001282 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001283
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001284 /*
1285 * When HDMI cable is unplugged the music playback is paused and
1286 * the policy manager sends routing=0. But the audioflinger
1287 * continues to write data until standby time (3sec).
1288 * As the HDMI core is turned off, the write gets blocked.
1289 * Avoid this by routing audio to speaker until standby.
1290 */
1291 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1292 val == AUDIO_DEVICE_NONE) {
1293 val = AUDIO_DEVICE_OUT_SPEAKER;
1294 }
1295
1296 /*
1297 * select_devices() call below switches all the usecases on the same
1298 * backend to the new device. Refer to check_usecases_codec_backend() in
1299 * the select_devices(). But how do we undo this?
1300 *
1301 * For example, music playback is active on headset (deep-buffer usecase)
1302 * and if we go to ringtones and select a ringtone, low-latency usecase
1303 * will be started on headset+speaker. As we can't enable headset+speaker
1304 * and headset devices at the same time, select_devices() switches the music
1305 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1306 * So when the ringtone playback is completed, how do we undo the same?
1307 *
1308 * We are relying on the out_set_parameters() call on deep-buffer output,
1309 * once the ringtone playback is ended.
1310 * NOTE: We should not check if the current devices are same as new devices.
1311 * Because select_devices() must be called to switch back the music
1312 * playback to headset.
1313 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001314 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001315 out->devices = val;
1316
1317 if (!out->standby)
1318 select_devices(adev, out->usecase);
1319
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001320 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1321 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001322 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001323 voice_start_call(adev);
1324 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1325 voice_is_in_call(adev) &&
1326 (out == adev->primary_output)) {
1327 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001328 }
1329 }
1330
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001331 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1332 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001333 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001334 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001335 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001336
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001338 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001339 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001340
1341 if (out == adev->primary_output) {
1342 pthread_mutex_lock(&adev->lock);
1343 audio_extn_set_parameters(adev, parms);
1344 pthread_mutex_unlock(&adev->lock);
1345 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001346 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1347 parse_compress_metadata(out, parms);
1348 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001349
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001350 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001351 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001352 return ret;
1353}
1354
1355static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1356{
1357 struct stream_out *out = (struct stream_out *)stream;
1358 struct str_parms *query = str_parms_create_str(keys);
1359 char *str;
1360 char value[256];
1361 struct str_parms *reply = str_parms_create();
1362 size_t i, j;
1363 int ret;
1364 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001365 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001366 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1367 if (ret >= 0) {
1368 value[0] = '\0';
1369 i = 0;
1370 while (out->supported_channel_masks[i] != 0) {
1371 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1372 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1373 if (!first) {
1374 strcat(value, "|");
1375 }
1376 strcat(value, out_channels_name_to_enum_table[j].name);
1377 first = false;
1378 break;
1379 }
1380 }
1381 i++;
1382 }
1383 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1384 str = str_parms_to_str(reply);
1385 } else {
1386 str = strdup(keys);
1387 }
1388 str_parms_destroy(query);
1389 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001390 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001391 return str;
1392}
1393
1394static uint32_t out_get_latency(const struct audio_stream_out *stream)
1395{
1396 struct stream_out *out = (struct stream_out *)stream;
1397
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001398 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1399 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1400
1401 return (out->config.period_count * out->config.period_size * 1000) /
1402 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001403}
1404
1405static int out_set_volume(struct audio_stream_out *stream, float left,
1406 float right)
1407{
Eric Laurenta9024de2013-04-04 09:19:12 -07001408 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001409 int volume[2];
1410
Eric Laurenta9024de2013-04-04 09:19:12 -07001411 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1412 /* only take left channel into account: the API is for stereo anyway */
1413 out->muted = (left == 0.0f);
1414 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001415 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1416 const char *mixer_ctl_name = "Compress Playback Volume";
1417 struct audio_device *adev = out->dev;
1418 struct mixer_ctl *ctl;
1419
1420 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1421 if (!ctl) {
1422 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1423 __func__, mixer_ctl_name);
1424 return -EINVAL;
1425 }
1426 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1427 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1428 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1429 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001430 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001431
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001432 return -ENOSYS;
1433}
1434
1435static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1436 size_t bytes)
1437{
1438 struct stream_out *out = (struct stream_out *)stream;
1439 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001440 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001441
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001442 pthread_mutex_lock(&out->lock);
1443 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001444 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001445 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001446 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001447 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001448 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001449 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001450 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001451 goto exit;
1452 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001453 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001454
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001455 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001456 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1457 if (out->send_new_metadata) {
1458 ALOGVV("send new gapless metadata");
1459 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1460 out->send_new_metadata = 0;
1461 }
1462
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001463 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001464 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001465 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001466 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1467 }
1468 if (!out->playback_started) {
1469 compress_start(out->compr);
1470 out->playback_started = 1;
1471 out->offload_state = OFFLOAD_STATE_PLAYING;
1472 }
1473 pthread_mutex_unlock(&out->lock);
1474 return ret;
1475 } else {
1476 if (out->pcm) {
1477 if (out->muted)
1478 memset((void *)buffer, 0, bytes);
1479 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1480 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001481 if (ret == 0)
1482 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001483 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001484 }
1485
1486exit:
1487 pthread_mutex_unlock(&out->lock);
1488
1489 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001490 if (out->pcm)
1491 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001492 out_standby(&out->stream.common);
1493 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1494 out_get_sample_rate(&out->stream.common));
1495 }
1496 return bytes;
1497}
1498
1499static int out_get_render_position(const struct audio_stream_out *stream,
1500 uint32_t *dsp_frames)
1501{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001502 struct stream_out *out = (struct stream_out *)stream;
1503 *dsp_frames = 0;
1504 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1505 pthread_mutex_lock(&out->lock);
1506 if (out->compr != NULL) {
1507 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1508 &out->sample_rate);
1509 ALOGVV("%s rendered frames %d sample_rate %d",
1510 __func__, *dsp_frames, out->sample_rate);
1511 }
1512 pthread_mutex_unlock(&out->lock);
1513 return 0;
1514 } else
1515 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001516}
1517
1518static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1519{
1520 return 0;
1521}
1522
1523static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1524{
1525 return 0;
1526}
1527
1528static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1529 int64_t *timestamp)
1530{
1531 return -EINVAL;
1532}
1533
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001534static int out_get_presentation_position(const struct audio_stream_out *stream,
1535 uint64_t *frames, struct timespec *timestamp)
1536{
1537 struct stream_out *out = (struct stream_out *)stream;
1538 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001539 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001540
1541 pthread_mutex_lock(&out->lock);
1542
Eric Laurent949a0892013-09-20 09:20:13 -07001543 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1544 if (out->compr != NULL) {
1545 compress_get_tstamp(out->compr, &dsp_frames,
1546 &out->sample_rate);
1547 ALOGVV("%s rendered frames %ld sample_rate %d",
1548 __func__, dsp_frames, out->sample_rate);
1549 *frames = dsp_frames;
1550 ret = 0;
1551 /* this is the best we can do */
1552 clock_gettime(CLOCK_MONOTONIC, timestamp);
1553 }
1554 } else {
1555 if (out->pcm) {
1556 size_t avail;
1557 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1558 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001559 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001560 // This adjustment accounts for buffering after app processor.
1561 // It is based on estimated DSP latency per use case, rather than exact.
1562 signed_frames -=
1563 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1564
Eric Laurent949a0892013-09-20 09:20:13 -07001565 // It would be unusual for this value to be negative, but check just in case ...
1566 if (signed_frames >= 0) {
1567 *frames = signed_frames;
1568 ret = 0;
1569 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001570 }
1571 }
1572 }
1573
1574 pthread_mutex_unlock(&out->lock);
1575
1576 return ret;
1577}
1578
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001579static int out_set_callback(struct audio_stream_out *stream,
1580 stream_callback_t callback, void *cookie)
1581{
1582 struct stream_out *out = (struct stream_out *)stream;
1583
1584 ALOGV("%s", __func__);
1585 pthread_mutex_lock(&out->lock);
1586 out->offload_callback = callback;
1587 out->offload_cookie = cookie;
1588 pthread_mutex_unlock(&out->lock);
1589 return 0;
1590}
1591
1592static int out_pause(struct audio_stream_out* stream)
1593{
1594 struct stream_out *out = (struct stream_out *)stream;
1595 int status = -ENOSYS;
1596 ALOGV("%s", __func__);
1597 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1598 pthread_mutex_lock(&out->lock);
1599 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1600 status = compress_pause(out->compr);
1601 out->offload_state = OFFLOAD_STATE_PAUSED;
1602 }
1603 pthread_mutex_unlock(&out->lock);
1604 }
1605 return status;
1606}
1607
1608static int out_resume(struct audio_stream_out* stream)
1609{
1610 struct stream_out *out = (struct stream_out *)stream;
1611 int status = -ENOSYS;
1612 ALOGV("%s", __func__);
1613 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1614 status = 0;
1615 pthread_mutex_lock(&out->lock);
1616 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1617 status = compress_resume(out->compr);
1618 out->offload_state = OFFLOAD_STATE_PLAYING;
1619 }
1620 pthread_mutex_unlock(&out->lock);
1621 }
1622 return status;
1623}
1624
1625static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1626{
1627 struct stream_out *out = (struct stream_out *)stream;
1628 int status = -ENOSYS;
1629 ALOGV("%s", __func__);
1630 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1631 pthread_mutex_lock(&out->lock);
1632 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1633 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1634 else
1635 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1636 pthread_mutex_unlock(&out->lock);
1637 }
1638 return status;
1639}
1640
1641static int out_flush(struct audio_stream_out* stream)
1642{
1643 struct stream_out *out = (struct stream_out *)stream;
1644 ALOGV("%s", __func__);
1645 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1646 pthread_mutex_lock(&out->lock);
1647 stop_compressed_output_l(out);
1648 pthread_mutex_unlock(&out->lock);
1649 return 0;
1650 }
1651 return -ENOSYS;
1652}
1653
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001654/** audio_stream_in implementation **/
1655static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1656{
1657 struct stream_in *in = (struct stream_in *)stream;
1658
1659 return in->config.rate;
1660}
1661
1662static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1663{
1664 return -ENOSYS;
1665}
1666
1667static size_t in_get_buffer_size(const struct audio_stream *stream)
1668{
1669 struct stream_in *in = (struct stream_in *)stream;
1670
1671 return in->config.period_size * audio_stream_frame_size(stream);
1672}
1673
1674static uint32_t in_get_channels(const struct audio_stream *stream)
1675{
1676 struct stream_in *in = (struct stream_in *)stream;
1677
1678 return in->channel_mask;
1679}
1680
1681static audio_format_t in_get_format(const struct audio_stream *stream)
1682{
1683 return AUDIO_FORMAT_PCM_16_BIT;
1684}
1685
1686static int in_set_format(struct audio_stream *stream, audio_format_t format)
1687{
1688 return -ENOSYS;
1689}
1690
1691static int in_standby(struct audio_stream *stream)
1692{
1693 struct stream_in *in = (struct stream_in *)stream;
1694 struct audio_device *adev = in->dev;
1695 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001696 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001697 pthread_mutex_lock(&in->lock);
1698 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001699 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001700 if (in->pcm) {
1701 pcm_close(in->pcm);
1702 in->pcm = NULL;
1703 }
1704 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001705 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001706 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001707 }
1708 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001709 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001710 return status;
1711}
1712
1713static int in_dump(const struct audio_stream *stream, int fd)
1714{
1715 return 0;
1716}
1717
1718static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1719{
1720 struct stream_in *in = (struct stream_in *)stream;
1721 struct audio_device *adev = in->dev;
1722 struct str_parms *parms;
1723 char *str;
1724 char value[32];
1725 int ret, val = 0;
1726
Eric Laurent994a6932013-07-17 11:51:42 -07001727 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001728 parms = str_parms_create_str(kvpairs);
1729
1730 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1731
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001732 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001733 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001734 if (ret >= 0) {
1735 val = atoi(value);
1736 /* no audio source uses val == 0 */
1737 if ((in->source != val) && (val != 0)) {
1738 in->source = val;
1739 }
1740 }
1741
1742 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1743 if (ret >= 0) {
1744 val = atoi(value);
1745 if ((in->device != val) && (val != 0)) {
1746 in->device = val;
1747 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001748 if (!in->standby)
1749 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001750 }
1751 }
1752
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001753 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001754 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001755
1756 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001757 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001758 return ret;
1759}
1760
1761static char* in_get_parameters(const struct audio_stream *stream,
1762 const char *keys)
1763{
1764 return strdup("");
1765}
1766
1767static int in_set_gain(struct audio_stream_in *stream, float gain)
1768{
1769 return 0;
1770}
1771
1772static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1773 size_t bytes)
1774{
1775 struct stream_in *in = (struct stream_in *)stream;
1776 struct audio_device *adev = in->dev;
1777 int i, ret = -1;
1778
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001779 pthread_mutex_lock(&in->lock);
1780 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001781 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001782 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001783 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001784 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001785 goto exit;
1786 }
1787 in->standby = 0;
1788 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001789
1790 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001791 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1792 ret = audio_extn_ssr_read(stream, buffer, bytes);
1793 else
1794 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001795 }
1796
1797 /*
1798 * Instead of writing zeroes here, we could trust the hardware
1799 * to always provide zeroes when muted.
1800 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001801 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001802 memset(buffer, 0, bytes);
1803
1804exit:
1805 pthread_mutex_unlock(&in->lock);
1806
1807 if (ret != 0) {
1808 in_standby(&in->stream.common);
1809 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1810 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1811 in_get_sample_rate(&in->stream.common));
1812 }
1813 return bytes;
1814}
1815
1816static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1817{
1818 return 0;
1819}
1820
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001821static int add_remove_audio_effect(const struct audio_stream *stream,
1822 effect_handle_t effect,
1823 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001824{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001825 struct stream_in *in = (struct stream_in *)stream;
1826 int status = 0;
1827 effect_descriptor_t desc;
1828
1829 status = (*effect)->get_descriptor(effect, &desc);
1830 if (status != 0)
1831 return status;
1832
1833 pthread_mutex_lock(&in->lock);
1834 pthread_mutex_lock(&in->dev->lock);
1835 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1836 in->enable_aec != enable &&
1837 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1838 in->enable_aec = enable;
1839 if (!in->standby)
1840 select_devices(in->dev, in->usecase);
1841 }
1842 pthread_mutex_unlock(&in->dev->lock);
1843 pthread_mutex_unlock(&in->lock);
1844
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001845 return 0;
1846}
1847
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001848static int in_add_audio_effect(const struct audio_stream *stream,
1849 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001850{
Eric Laurent994a6932013-07-17 11:51:42 -07001851 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001852 return add_remove_audio_effect(stream, effect, true);
1853}
1854
1855static int in_remove_audio_effect(const struct audio_stream *stream,
1856 effect_handle_t effect)
1857{
Eric Laurent994a6932013-07-17 11:51:42 -07001858 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001859 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001860}
1861
1862static int adev_open_output_stream(struct audio_hw_device *dev,
1863 audio_io_handle_t handle,
1864 audio_devices_t devices,
1865 audio_output_flags_t flags,
1866 struct audio_config *config,
1867 struct audio_stream_out **stream_out)
1868{
1869 struct audio_device *adev = (struct audio_device *)dev;
1870 struct stream_out *out;
1871 int i, ret;
1872
Eric Laurent994a6932013-07-17 11:51:42 -07001873 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001874 __func__, config->sample_rate, config->channel_mask, devices, flags);
1875 *stream_out = NULL;
1876 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1877
1878 if (devices == AUDIO_DEVICE_NONE)
1879 devices = AUDIO_DEVICE_OUT_SPEAKER;
1880
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001881 out->flags = flags;
1882 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001883 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001884 out->format = config->format;
1885 out->sample_rate = config->sample_rate;
1886 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1887 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001888 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001889
1890 /* Init use case and pcm_config */
1891 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1892 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001893 pthread_mutex_lock(&adev->lock);
1894 ret = read_hdmi_channel_masks(out);
1895 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001896 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001897 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001898
1899 if (config->sample_rate == 0)
1900 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1901 if (config->channel_mask == 0)
1902 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1903
1904 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001905 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001906 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1907 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001908 out->config.rate = config->sample_rate;
1909 out->config.channels = popcount(out->channel_mask);
1910 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001911 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1912 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1913 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001914 out->sample_rate = out->config.rate;
1915 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1916 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1917 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1918 ALOGE("%s: Unsupported Offload information", __func__);
1919 ret = -EINVAL;
1920 goto error_open;
1921 }
1922 if (!is_supported_format(config->offload_info.format)) {
1923 ALOGE("%s: Unsupported audio format", __func__);
1924 ret = -EINVAL;
1925 goto error_open;
1926 }
1927
1928 out->compr_config.codec = (struct snd_codec *)
1929 calloc(1, sizeof(struct snd_codec));
1930
1931 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1932 if (config->offload_info.channel_mask)
1933 out->channel_mask = config->offload_info.channel_mask;
1934 else if (config->channel_mask)
1935 out->channel_mask = config->channel_mask;
1936 out->format = config->offload_info.format;
1937 out->sample_rate = config->offload_info.sample_rate;
1938
1939 out->stream.set_callback = out_set_callback;
1940 out->stream.pause = out_pause;
1941 out->stream.resume = out_resume;
1942 out->stream.drain = out_drain;
1943 out->stream.flush = out_flush;
1944
1945 out->compr_config.codec->id =
1946 get_snd_codec_id(config->offload_info.format);
1947 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1948 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1949 out->compr_config.codec->sample_rate =
1950 compress_get_alsa_rate(config->offload_info.sample_rate);
1951 out->compr_config.codec->bit_rate =
1952 config->offload_info.bit_rate;
1953 out->compr_config.codec->ch_in =
1954 popcount(config->channel_mask);
1955 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1956
1957 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1958 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001959
1960 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001961 create_offload_callback_thread(out);
1962 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1963 __func__, config->offload_info.version,
1964 config->offload_info.bit_rate);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07001965 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
1966 ret = voice_check_and_set_incall_music_usecase(adev, out);
1967 if (ret != 0) {
1968 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
1969 __func__, ret);
1970 goto error_open;
1971 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001972 } else {
1973 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1974 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001975 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001976 }
1977
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001978 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1979 if(adev->primary_output == NULL)
1980 adev->primary_output = out;
1981 else {
1982 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001983 ret = -EEXIST;
1984 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001985 }
1986 }
1987
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001988 /* Check if this usecase is already existing */
1989 pthread_mutex_lock(&adev->lock);
1990 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1991 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001992 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001993 ret = -EEXIST;
1994 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001995 }
1996 pthread_mutex_unlock(&adev->lock);
1997
1998 out->stream.common.get_sample_rate = out_get_sample_rate;
1999 out->stream.common.set_sample_rate = out_set_sample_rate;
2000 out->stream.common.get_buffer_size = out_get_buffer_size;
2001 out->stream.common.get_channels = out_get_channels;
2002 out->stream.common.get_format = out_get_format;
2003 out->stream.common.set_format = out_set_format;
2004 out->stream.common.standby = out_standby;
2005 out->stream.common.dump = out_dump;
2006 out->stream.common.set_parameters = out_set_parameters;
2007 out->stream.common.get_parameters = out_get_parameters;
2008 out->stream.common.add_audio_effect = out_add_audio_effect;
2009 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2010 out->stream.get_latency = out_get_latency;
2011 out->stream.set_volume = out_set_volume;
2012 out->stream.write = out_write;
2013 out->stream.get_render_position = out_get_render_position;
2014 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002015 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002016
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002017 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002018 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002019 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002020
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002021 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2022 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002023
2024 config->format = out->stream.common.get_format(&out->stream.common);
2025 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2026 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2027
2028 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002029 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002030 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002031
2032error_open:
2033 free(out);
2034 *stream_out = NULL;
2035 ALOGD("%s: exit: ret %d", __func__, ret);
2036 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002037}
2038
2039static void adev_close_output_stream(struct audio_hw_device *dev,
2040 struct audio_stream_out *stream)
2041{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002042 struct stream_out *out = (struct stream_out *)stream;
2043 struct audio_device *adev = out->dev;
2044
Eric Laurent994a6932013-07-17 11:51:42 -07002045 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002046 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002047 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2048 destroy_offload_callback_thread(out);
2049
2050 if (out->compr_config.codec != NULL)
2051 free(out->compr_config.codec);
2052 }
2053 pthread_cond_destroy(&out->cond);
2054 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002055 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002056 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002057}
2058
2059static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2060{
2061 struct audio_device *adev = (struct audio_device *)dev;
2062 struct str_parms *parms;
2063 char *str;
2064 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002065 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002066 int ret;
2067
Eric Laurent994a6932013-07-17 11:51:42 -07002068 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002069 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002070
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002071 voice_set_parameters(adev, parms);
2072 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002073
2074 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2075 if (ret >= 0) {
2076 /* When set to false, HAL should disable EC and NS
2077 * But it is currently not supported.
2078 */
2079 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2080 adev->bluetooth_nrec = true;
2081 else
2082 adev->bluetooth_nrec = false;
2083 }
2084
2085 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2086 if (ret >= 0) {
2087 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2088 adev->screen_off = false;
2089 else
2090 adev->screen_off = true;
2091 }
2092
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002093 ret = str_parms_get_int(parms, "rotation", &val);
2094 if (ret >= 0) {
2095 bool reverse_speakers = false;
2096 switch(val) {
2097 // FIXME: note that the code below assumes that the speakers are in the correct placement
2098 // relative to the user when the device is rotated 90deg from its default rotation. This
2099 // assumption is device-specific, not platform-specific like this code.
2100 case 270:
2101 reverse_speakers = true;
2102 break;
2103 case 0:
2104 case 90:
2105 case 180:
2106 break;
2107 default:
2108 ALOGE("%s: unexpected rotation of %d", __func__, val);
2109 }
2110 pthread_mutex_lock(&adev->lock);
2111 if (adev->speaker_lr_swap != reverse_speakers) {
2112 adev->speaker_lr_swap = reverse_speakers;
2113 // only update the selected device if there is active pcm playback
2114 struct audio_usecase *usecase;
2115 struct listnode *node;
2116 list_for_each(node, &adev->usecase_list) {
2117 usecase = node_to_item(node, struct audio_usecase, list);
2118 if (usecase->type == PCM_PLAYBACK) {
2119 select_devices(adev, usecase->id);
2120 break;
2121 }
2122 }
2123 }
2124 pthread_mutex_unlock(&adev->lock);
2125 }
2126
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002127 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002128 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002129 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002130 return ret;
2131}
2132
2133static char* adev_get_parameters(const struct audio_hw_device *dev,
2134 const char *keys)
2135{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002136 struct audio_device *adev = (struct audio_device *)dev;
2137 struct str_parms *reply = str_parms_create();
2138 struct str_parms *query = str_parms_create_str(keys);
2139 char *str;
2140
2141 audio_extn_get_parameters(adev, query, reply);
2142 platform_get_parameters(adev->platform, query, reply);
2143 str = str_parms_to_str(reply);
2144 str_parms_destroy(query);
2145 str_parms_destroy(reply);
2146
2147 ALOGV("%s: exit: returns - %s", __func__, str);
2148 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002149}
2150
2151static int adev_init_check(const struct audio_hw_device *dev)
2152{
2153 return 0;
2154}
2155
2156static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2157{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002158 int ret;
2159 struct audio_device *adev = (struct audio_device *)dev;
2160 pthread_mutex_lock(&adev->lock);
2161 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002162 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002163 pthread_mutex_unlock(&adev->lock);
2164 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002165}
2166
2167static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2168{
2169 return -ENOSYS;
2170}
2171
2172static int adev_get_master_volume(struct audio_hw_device *dev,
2173 float *volume)
2174{
2175 return -ENOSYS;
2176}
2177
2178static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2179{
2180 return -ENOSYS;
2181}
2182
2183static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2184{
2185 return -ENOSYS;
2186}
2187
2188static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2189{
2190 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002191 pthread_mutex_lock(&adev->lock);
2192 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002193 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002194 adev->mode = mode;
2195 }
2196 pthread_mutex_unlock(&adev->lock);
2197 return 0;
2198}
2199
2200static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2201{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002202 return voice_set_mic_mute((struct audio_device *)dev, state);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002203}
2204
2205static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2206{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002207 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002208 return 0;
2209}
2210
2211static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2212 const struct audio_config *config)
2213{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002214 int channel_count = popcount(config->channel_mask);
2215
2216 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2217}
2218
2219static int adev_open_input_stream(struct audio_hw_device *dev,
2220 audio_io_handle_t handle,
2221 audio_devices_t devices,
2222 struct audio_config *config,
2223 struct audio_stream_in **stream_in)
2224{
2225 struct audio_device *adev = (struct audio_device *)dev;
2226 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002227 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002228 int channel_count = popcount(config->channel_mask);
2229
Eric Laurent994a6932013-07-17 11:51:42 -07002230 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002231 *stream_in = NULL;
2232 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2233 return -EINVAL;
2234
2235 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2236
2237 in->stream.common.get_sample_rate = in_get_sample_rate;
2238 in->stream.common.set_sample_rate = in_set_sample_rate;
2239 in->stream.common.get_buffer_size = in_get_buffer_size;
2240 in->stream.common.get_channels = in_get_channels;
2241 in->stream.common.get_format = in_get_format;
2242 in->stream.common.set_format = in_set_format;
2243 in->stream.common.standby = in_standby;
2244 in->stream.common.dump = in_dump;
2245 in->stream.common.set_parameters = in_set_parameters;
2246 in->stream.common.get_parameters = in_get_parameters;
2247 in->stream.common.add_audio_effect = in_add_audio_effect;
2248 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2249 in->stream.set_gain = in_set_gain;
2250 in->stream.read = in_read;
2251 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2252
2253 in->device = devices;
2254 in->source = AUDIO_SOURCE_DEFAULT;
2255 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002256 in->standby = 1;
2257 in->channel_mask = config->channel_mask;
2258
2259 /* Update config params with the requested sample rate and channels */
2260 in->usecase = USECASE_AUDIO_RECORD;
2261 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002262 in->config.rate = config->sample_rate;
2263
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002264 if (channel_count == 6) {
2265 if(audio_extn_ssr_get_enabled()) {
2266 if(audio_extn_ssr_init(adev, in)) {
2267 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2268 ret = -EINVAL;
2269 goto err_open;
2270 }
2271 } else {
2272 ret = -EINVAL;
2273 goto err_open;
2274 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002275 } else {
2276 in->config.channels = channel_count;
2277 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2278 buffer_size = get_input_buffer_size(config->sample_rate,
2279 config->format,
2280 channel_count);
2281 in->config.period_size = buffer_size / frame_size;
2282 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002283
2284 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002285 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002286 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002287
2288err_open:
2289 free(in);
2290 *stream_in = NULL;
2291 return ret;
2292}
2293
2294static void adev_close_input_stream(struct audio_hw_device *dev,
2295 struct audio_stream_in *stream)
2296{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002297 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002298 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002299
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002300 in_standby(&stream->common);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002301 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2302 audio_extn_ssr_deinit();
2303 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002304 free(stream);
2305
2306 return;
2307}
2308
2309static int adev_dump(const audio_hw_device_t *device, int fd)
2310{
2311 return 0;
2312}
2313
2314static int adev_close(hw_device_t *device)
2315{
2316 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002317
2318 if (!adev)
2319 return 0;
2320
2321 pthread_mutex_lock(&adev_init_lock);
2322
2323 if ((--audio_device_ref_count) == 0) {
2324 audio_route_free(adev->audio_route);
2325 free(adev->snd_dev_ref_cnt);
2326 platform_deinit(adev->platform);
2327 audio_extn_listen_deinit(adev);
2328 free(device);
2329 adev = NULL;
2330 }
2331 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002332 return 0;
2333}
2334
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002335static int adev_open(const hw_module_t *module, const char *name,
2336 hw_device_t **device)
2337{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002338 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002339
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002340 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002341 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2342
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002343 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002344 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002345 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002346 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002347 ALOGD("%s: returning existing instance of adev", __func__);
2348 ALOGD("%s: exit", __func__);
2349 pthread_mutex_unlock(&adev_init_lock);
2350 return 0;
2351 }
2352
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002353 adev = calloc(1, sizeof(struct audio_device));
2354
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002355 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2356 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2357 adev->device.common.module = (struct hw_module_t *)module;
2358 adev->device.common.close = adev_close;
2359
2360 adev->device.init_check = adev_init_check;
2361 adev->device.set_voice_volume = adev_set_voice_volume;
2362 adev->device.set_master_volume = adev_set_master_volume;
2363 adev->device.get_master_volume = adev_get_master_volume;
2364 adev->device.set_master_mute = adev_set_master_mute;
2365 adev->device.get_master_mute = adev_get_master_mute;
2366 adev->device.set_mode = adev_set_mode;
2367 adev->device.set_mic_mute = adev_set_mic_mute;
2368 adev->device.get_mic_mute = adev_get_mic_mute;
2369 adev->device.set_parameters = adev_set_parameters;
2370 adev->device.get_parameters = adev_get_parameters;
2371 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2372 adev->device.open_output_stream = adev_open_output_stream;
2373 adev->device.close_output_stream = adev_close_output_stream;
2374 adev->device.open_input_stream = adev_open_input_stream;
2375 adev->device.close_input_stream = adev_close_input_stream;
2376 adev->device.dump = adev_dump;
2377
2378 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002379 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002380 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002381 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002382 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002383 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002384 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002385 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002386 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002387 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002388 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002389
2390 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002391 adev->platform = platform_init(adev);
2392 if (!adev->platform) {
2393 free(adev->snd_dev_ref_cnt);
2394 free(adev);
2395 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2396 *device = NULL;
2397 return -EINVAL;
2398 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002399
2400 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2401 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2402 if (adev->visualizer_lib == NULL) {
2403 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2404 } else {
2405 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2406 adev->visualizer_start_output =
2407 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2408 "visualizer_hal_start_output");
2409 adev->visualizer_stop_output =
2410 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2411 "visualizer_hal_stop_output");
2412 }
2413 }
Kiran Kandi910e1862013-10-29 13:29:42 -07002414 audio_extn_listen_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07002415
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002416 *device = &adev->device.common;
2417
Kiran Kandi910e1862013-10-29 13:29:42 -07002418 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002419 pthread_mutex_unlock(&adev_init_lock);
2420
Eric Laurent994a6932013-07-17 11:51:42 -07002421 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002422 return 0;
2423}
2424
2425static struct hw_module_methods_t hal_module_methods = {
2426 .open = adev_open,
2427};
2428
2429struct audio_module HAL_MODULE_INFO_SYM = {
2430 .common = {
2431 .tag = HARDWARE_MODULE_TAG,
2432 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2433 .hal_api_version = HARDWARE_HAL_API_VERSION,
2434 .id = AUDIO_HARDWARE_MODULE_ID,
2435 .name = "QCOM Audio HAL",
2436 .author = "Code Aurora Forum",
2437 .methods = &hal_module_methods,
2438 },
2439};