blob: b53892fad3efe8899247663d40fd2f24a8ae1111 [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;
138static bool is_adev_initialised = false;
Haynes Mathew George5191a852013-09-11 14:19:36 -0700139static int set_voice_volume_l(struct audio_device *adev, float volume);
140
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700141static bool is_supported_format(audio_format_t format)
142{
Eric Laurent86e17132013-09-12 17:49:30 -0700143 if (format == AUDIO_FORMAT_MP3 ||
144 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700145 return true;
146
147 return false;
148}
149
150static int get_snd_codec_id(audio_format_t format)
151{
152 int id = 0;
153
154 switch (format) {
155 case AUDIO_FORMAT_MP3:
156 id = SND_AUDIOCODEC_MP3;
157 break;
158 case AUDIO_FORMAT_AAC:
159 id = SND_AUDIOCODEC_AAC;
160 break;
161 default:
162 ALOGE("%s: Unsupported audio format", __func__);
163 }
164
165 return id;
166}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800167
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700168static int enable_audio_route(struct audio_device *adev,
169 struct audio_usecase *usecase,
170 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800171{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700172 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700173 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800174
175 if (usecase == NULL)
176 return -EINVAL;
177
178 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
179
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800180 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700181 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800182 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700183 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800184
185 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700186 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700187 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700188 audio_route_apply_path(adev->audio_route, mixer_path);
189 if (update_mixer)
190 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800191
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800192 ALOGV("%s: exit", __func__);
193 return 0;
194}
195
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700196int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700197 struct audio_usecase *usecase,
198 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800199{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700200 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700201 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800202
203 if (usecase == NULL)
204 return -EINVAL;
205
206 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700207 if (usecase->type == PCM_CAPTURE)
208 snd_device = usecase->in_snd_device;
209 else
210 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800211 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700212 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700213 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700214 audio_route_reset_path(adev->audio_route, mixer_path);
215 if (update_mixer)
216 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800217
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800218 ALOGV("%s: exit", __func__);
219 return 0;
220}
221
222static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700223 snd_device_t snd_device,
224 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800225{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700226 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
227
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800228 if (snd_device < SND_DEVICE_MIN ||
229 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800230 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800231 return -EINVAL;
232 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700233
234 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700235
236 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
237 ALOGE("%s: Invalid sound device returned", __func__);
238 return -EINVAL;
239 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700240 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700241 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700242 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700243 return 0;
244 }
245
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700246 /* start usb playback thread */
247 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
248 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
249 audio_extn_usb_start_playback(adev);
250
251 /* start usb capture thread */
252 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
253 audio_extn_usb_start_capture(adev);
254
Eric Laurentb23d5282013-05-14 15:27:20 -0700255 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700256 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800257 return -EINVAL;
258 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800259
Eric Laurent994a6932013-07-17 11:51:42 -0700260 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700261 snd_device, device_name);
262 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700263 if (update_mixer)
264 audio_route_update_mixer(adev->audio_route);
265
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800266 return 0;
267}
268
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700269int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700270 snd_device_t snd_device,
271 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800272{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700273 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
274
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800275 if (snd_device < SND_DEVICE_MIN ||
276 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800277 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800278 return -EINVAL;
279 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700280 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
281 ALOGE("%s: device ref cnt is already 0", __func__);
282 return -EINVAL;
283 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700284
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700285 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700286
287 /* exit usb play back thread */
288 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
289 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
290 audio_extn_usb_stop_playback();
291
292 /* exit usb capture thread */
293 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
294 audio_extn_usb_stop_capture(adev);
295
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700296 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
297 ALOGE("%s: Invalid sound device returned", __func__);
298 return -EINVAL;
299 }
300
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700301 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700302 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700303 snd_device, device_name);
304 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700305 if (update_mixer)
306 audio_route_update_mixer(adev->audio_route);
307 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700308
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800309 return 0;
310}
311
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700312static void check_usecases_codec_backend(struct audio_device *adev,
313 struct audio_usecase *uc_info,
314 snd_device_t snd_device)
315{
316 struct listnode *node;
317 struct audio_usecase *usecase;
318 bool switch_device[AUDIO_USECASE_MAX];
319 int i, num_uc_to_switch = 0;
320
321 /*
322 * This function is to make sure that all the usecases that are active on
323 * the hardware codec backend are always routed to any one device that is
324 * handled by the hardware codec.
325 * For example, if low-latency and deep-buffer usecases are currently active
326 * on speaker and out_set_parameters(headset) is received on low-latency
327 * output, then we have to make sure deep-buffer is also switched to headset,
328 * because of the limitation that both the devices cannot be enabled
329 * at the same time as they share the same backend.
330 */
331 /* Disable all the usecases on the shared backend other than the
332 specified usecase */
333 for (i = 0; i < AUDIO_USECASE_MAX; i++)
334 switch_device[i] = false;
335
336 list_for_each(node, &adev->usecase_list) {
337 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700338 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700339 usecase != uc_info &&
340 usecase->out_snd_device != snd_device &&
341 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
342 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
343 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700344 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700345 disable_audio_route(adev, usecase, false);
346 switch_device[usecase->id] = true;
347 num_uc_to_switch++;
348 }
349 }
350
351 if (num_uc_to_switch) {
352 /* Make sure all the streams are de-routed before disabling the device */
353 audio_route_update_mixer(adev->audio_route);
354
355 list_for_each(node, &adev->usecase_list) {
356 usecase = node_to_item(node, struct audio_usecase, list);
357 if (switch_device[usecase->id]) {
358 disable_snd_device(adev, usecase->out_snd_device, false);
359 enable_snd_device(adev, snd_device, false);
360 }
361 }
362
363 /* Make sure new snd device is enabled before re-routing the streams */
364 audio_route_update_mixer(adev->audio_route);
365
366 /* Re-route all the usecases on the shared backend other than the
367 specified usecase to new snd devices */
368 list_for_each(node, &adev->usecase_list) {
369 usecase = node_to_item(node, struct audio_usecase, list);
370 /* Update the out_snd_device only before enabling the audio route */
371 if (switch_device[usecase->id] ) {
372 usecase->out_snd_device = snd_device;
373 enable_audio_route(adev, usecase, false);
374 }
375 }
376
377 audio_route_update_mixer(adev->audio_route);
378 }
379}
380
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700381static void check_and_route_capture_usecases(struct audio_device *adev,
382 struct audio_usecase *uc_info,
383 snd_device_t snd_device)
384{
385 struct listnode *node;
386 struct audio_usecase *usecase;
387 bool switch_device[AUDIO_USECASE_MAX];
388 int i, num_uc_to_switch = 0;
389
390 /*
391 * This function is to make sure that all the active capture usecases
392 * are always routed to the same input sound device.
393 * For example, if audio-record and voice-call usecases are currently
394 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
395 * is received for voice call then we have to make sure that audio-record
396 * usecase is also switched to earpiece i.e. voice-dmic-ef,
397 * because of the limitation that two devices cannot be enabled
398 * at the same time if they share the same backend.
399 */
400 for (i = 0; i < AUDIO_USECASE_MAX; i++)
401 switch_device[i] = false;
402
403 list_for_each(node, &adev->usecase_list) {
404 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700405 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700406 usecase != uc_info &&
407 usecase->in_snd_device != snd_device) {
408 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
409 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700410 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700411 disable_audio_route(adev, usecase, false);
412 switch_device[usecase->id] = true;
413 num_uc_to_switch++;
414 }
415 }
416
417 if (num_uc_to_switch) {
418 /* Make sure all the streams are de-routed before disabling the device */
419 audio_route_update_mixer(adev->audio_route);
420
421 list_for_each(node, &adev->usecase_list) {
422 usecase = node_to_item(node, struct audio_usecase, list);
423 if (switch_device[usecase->id]) {
424 disable_snd_device(adev, usecase->in_snd_device, false);
425 enable_snd_device(adev, snd_device, false);
426 }
427 }
428
429 /* Make sure new snd device is enabled before re-routing the streams */
430 audio_route_update_mixer(adev->audio_route);
431
432 /* Re-route all the usecases on the shared backend other than the
433 specified usecase to new snd devices */
434 list_for_each(node, &adev->usecase_list) {
435 usecase = node_to_item(node, struct audio_usecase, list);
436 /* Update the in_snd_device only before enabling the audio route */
437 if (switch_device[usecase->id] ) {
438 usecase->in_snd_device = snd_device;
439 enable_audio_route(adev, usecase, false);
440 }
441 }
442
443 audio_route_update_mixer(adev->audio_route);
444 }
445}
446
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700447static int disable_all_usecases_of_type(struct audio_device *adev,
448 usecase_type_t usecase_type,
449 bool update_mixer)
450{
451 struct audio_usecase *usecase;
452 struct listnode *node;
453 int ret = 0;
454
455 list_for_each(node, &adev->usecase_list) {
456 usecase = node_to_item(node, struct audio_usecase, list);
457 if (usecase->type == usecase_type) {
458 ALOGV("%s: usecase id %d", __func__, usecase->id);
459 ret = disable_audio_route(adev, usecase, update_mixer);
460 if (ret) {
461 ALOGE("%s: Failed to disable usecase id %d",
462 __func__, usecase->id);
463 }
464 }
465 }
466
467 return ret;
468}
469
470static int enable_all_usecases_of_type(struct audio_device *adev,
471 usecase_type_t usecase_type,
472 bool update_mixer)
473{
474 struct audio_usecase *usecase;
475 struct listnode *node;
476 int ret = 0;
477
478 list_for_each(node, &adev->usecase_list) {
479 usecase = node_to_item(node, struct audio_usecase, list);
480 if (usecase->type == usecase_type) {
481 ALOGV("%s: usecase id %d", __func__, usecase->id);
482 ret = enable_audio_route(adev, usecase, update_mixer);
483 if (ret) {
484 ALOGE("%s: Failed to enable usecase id %d",
485 __func__, usecase->id);
486 }
487 }
488 }
489
490 return ret;
491}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800492
493/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700494static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800495{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700496 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700497 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800498
499 switch (channels) {
500 /*
501 * Do not handle stereo output in Multi-channel cases
502 * Stereo case is handled in normal playback path
503 */
504 case 6:
505 ALOGV("%s: HDMI supports 5.1", __func__);
506 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
507 break;
508 case 8:
509 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
510 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
511 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
512 break;
513 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700514 ALOGE("HDMI does not support multi channel playback");
515 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800516 break;
517 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700518 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800519}
520
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700521static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
522{
523 struct audio_usecase *usecase;
524 struct listnode *node;
525
526 list_for_each(node, &adev->usecase_list) {
527 usecase = node_to_item(node, struct audio_usecase, list);
528 if (usecase->type == VOICE_CALL) {
529 ALOGV("%s: usecase id %d", __func__, usecase->id);
530 return usecase->id;
531 }
532 }
533 return USECASE_INVALID;
534}
535
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700536struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700537 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700538{
539 struct audio_usecase *usecase;
540 struct listnode *node;
541
542 list_for_each(node, &adev->usecase_list) {
543 usecase = node_to_item(node, struct audio_usecase, list);
544 if (usecase->id == uc_id)
545 return usecase;
546 }
547 return NULL;
548}
549
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700550int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800551{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800552 snd_device_t out_snd_device = SND_DEVICE_NONE;
553 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700554 struct audio_usecase *usecase = NULL;
555 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800556 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700557 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800558
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700559 usecase = get_usecase_from_list(adev, uc_id);
560 if (usecase == NULL) {
561 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
562 return -EINVAL;
563 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800564
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700565 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700566 out_snd_device = platform_get_output_snd_device(adev->platform,
567 usecase->stream.out->devices);
568 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700569 usecase->devices = usecase->stream.out->devices;
570 } else {
571 /*
572 * If the voice call is active, use the sound devices of voice call usecase
573 * so that it would not result any device switch. All the usecases will
574 * be switched to new device when select_devices() is called for voice call
575 * usecase. This is to avoid switching devices for voice call when
576 * check_usecases_codec_backend() is called below.
577 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700578 if (voice_is_in_call(adev)) {
579 vc_usecase = get_usecase_from_list(adev,
580 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700581 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
582 in_snd_device = vc_usecase->in_snd_device;
583 out_snd_device = vc_usecase->out_snd_device;
584 }
585 }
586 if (usecase->type == PCM_PLAYBACK) {
587 usecase->devices = usecase->stream.out->devices;
588 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700589 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700590 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700591 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700592 if (usecase->stream.out == adev->primary_output &&
593 adev->active_input &&
594 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
595 select_devices(adev, adev->active_input->usecase);
596 }
597 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700598 } else if (usecase->type == PCM_CAPTURE) {
599 usecase->devices = usecase->stream.in->device;
600 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700601 if (in_snd_device == SND_DEVICE_NONE) {
602 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
603 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700604 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700605 adev->primary_output->devices);
606 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700607 in_snd_device = platform_get_input_snd_device(adev->platform,
608 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700609 }
610 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700611 }
612 }
613
614 if (out_snd_device == usecase->out_snd_device &&
615 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800616 return 0;
617 }
618
sangwoobc677242013-08-08 16:53:43 +0900619 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700620 out_snd_device, platform_get_snd_device_name(out_snd_device),
621 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800622
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800623 /*
624 * Limitation: While in call, to do a device switch we need to disable
625 * and enable both RX and TX devices though one of them is same as current
626 * device.
627 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700628 if (usecase->type == VOICE_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700629 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700630 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800631 }
632
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700633 /* Disable current sound devices */
634 if (usecase->out_snd_device != SND_DEVICE_NONE) {
635 disable_audio_route(adev, usecase, true);
636 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800637 }
638
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700639 if (usecase->in_snd_device != SND_DEVICE_NONE) {
640 disable_audio_route(adev, usecase, true);
641 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800642 }
643
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700644 /* Enable new sound devices */
645 if (out_snd_device != SND_DEVICE_NONE) {
646 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
647 check_usecases_codec_backend(adev, usecase, out_snd_device);
648 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800649 }
650
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700651 if (in_snd_device != SND_DEVICE_NONE) {
652 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700653 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700654 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700655
Eric Laurentb23d5282013-05-14 15:27:20 -0700656 if (usecase->type == VOICE_CALL)
657 status = platform_switch_voice_call_device_post(adev->platform,
658 out_snd_device,
659 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800660
sangwoo170731f2013-06-08 15:36:36 +0900661 audio_route_update_mixer(adev->audio_route);
662
663 usecase->in_snd_device = in_snd_device;
664 usecase->out_snd_device = out_snd_device;
665
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700666 if (usecase->type == VOICE_CALL)
667 enable_all_usecases_of_type(adev, VOICE_CALL, true);
668 else
669 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900670
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800671 return status;
672}
673
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800674static int stop_input_stream(struct stream_in *in)
675{
676 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800677 struct audio_usecase *uc_info;
678 struct audio_device *adev = in->dev;
679
Eric Laurentc8400632013-02-14 19:04:54 -0800680 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800681
Eric Laurent994a6932013-07-17 11:51:42 -0700682 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700683 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800684 uc_info = get_usecase_from_list(adev, in->usecase);
685 if (uc_info == NULL) {
686 ALOGE("%s: Could not find the usecase (%d) in the list",
687 __func__, in->usecase);
688 return -EINVAL;
689 }
690
Eric Laurent150dbfe2013-02-27 14:31:02 -0800691 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700692 disable_audio_route(adev, uc_info, true);
693
694 /* 2. Disable the tx device */
695 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800696
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800697 list_remove(&uc_info->list);
698 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800699
Eric Laurent994a6932013-07-17 11:51:42 -0700700 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800701 return ret;
702}
703
704int start_input_stream(struct stream_in *in)
705{
706 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800707 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800708 struct audio_usecase *uc_info;
709 struct audio_device *adev = in->dev;
710
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700711 in->usecase = platform_get_usecase_from_source(in->source);
Eric Laurent994a6932013-07-17 11:51:42 -0700712 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700713
714 /* Check if source matches incall recording usecase criteria */
715 ret = voice_check_and_set_incall_rec_usecase(adev, in);
716 if (ret)
717 goto error_config;
718 else
719 ALOGV("%s: usecase(%d)", __func__, in->usecase);
720
Eric Laurentb23d5282013-05-14 15:27:20 -0700721 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800722 if (in->pcm_device_id < 0) {
723 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
724 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800725 ret = -EINVAL;
726 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800727 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700728
729 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800730 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
731 uc_info->id = in->usecase;
732 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800733 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700734 uc_info->devices = in->device;
735 uc_info->in_snd_device = SND_DEVICE_NONE;
736 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800737
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800738 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700739 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800740
Eric Laurentc8400632013-02-14 19:04:54 -0800741 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
742 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800743 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
744 PCM_IN, &in->config);
745 if (in->pcm && !pcm_is_ready(in->pcm)) {
746 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
747 pcm_close(in->pcm);
748 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800749 ret = -EIO;
750 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800751 }
Eric Laurent994a6932013-07-17 11:51:42 -0700752 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800753 return ret;
754
755error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800756 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800757
758error_config:
759 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700760 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800761
762 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800763}
764
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700765/* must be called with out->lock locked */
766static int send_offload_cmd_l(struct stream_out* out, int command)
767{
768 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
769
770 ALOGVV("%s %d", __func__, command);
771
772 cmd->cmd = command;
773 list_add_tail(&out->offload_cmd_list, &cmd->node);
774 pthread_cond_signal(&out->offload_cond);
775 return 0;
776}
777
778/* must be called iwth out->lock locked */
779static void stop_compressed_output_l(struct stream_out *out)
780{
781 out->offload_state = OFFLOAD_STATE_IDLE;
782 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700783 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700784 if (out->compr != NULL) {
785 compress_stop(out->compr);
786 while (out->offload_thread_blocked) {
787 pthread_cond_wait(&out->cond, &out->lock);
788 }
789 }
790}
791
792static void *offload_thread_loop(void *context)
793{
794 struct stream_out *out = (struct stream_out *) context;
795 struct listnode *item;
796
797 out->offload_state = OFFLOAD_STATE_IDLE;
798 out->playback_started = 0;
799
800 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
801 set_sched_policy(0, SP_FOREGROUND);
802 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
803
804 ALOGV("%s", __func__);
805 pthread_mutex_lock(&out->lock);
806 for (;;) {
807 struct offload_cmd *cmd = NULL;
808 stream_callback_event_t event;
809 bool send_callback = false;
810
811 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
812 __func__, list_empty(&out->offload_cmd_list),
813 out->offload_state);
814 if (list_empty(&out->offload_cmd_list)) {
815 ALOGV("%s SLEEPING", __func__);
816 pthread_cond_wait(&out->offload_cond, &out->lock);
817 ALOGV("%s RUNNING", __func__);
818 continue;
819 }
820
821 item = list_head(&out->offload_cmd_list);
822 cmd = node_to_item(item, struct offload_cmd, node);
823 list_remove(item);
824
825 ALOGVV("%s STATE %d CMD %d out->compr %p",
826 __func__, out->offload_state, cmd->cmd, out->compr);
827
828 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
829 free(cmd);
830 break;
831 }
832
833 if (out->compr == NULL) {
834 ALOGE("%s: Compress handle is NULL", __func__);
835 pthread_cond_signal(&out->cond);
836 continue;
837 }
838 out->offload_thread_blocked = true;
839 pthread_mutex_unlock(&out->lock);
840 send_callback = false;
841 switch(cmd->cmd) {
842 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
843 compress_wait(out->compr, -1);
844 send_callback = true;
845 event = STREAM_CBK_EVENT_WRITE_READY;
846 break;
847 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700848 compress_next_track(out->compr);
849 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700850 send_callback = true;
851 event = STREAM_CBK_EVENT_DRAIN_READY;
852 break;
853 case OFFLOAD_CMD_DRAIN:
854 compress_drain(out->compr);
855 send_callback = true;
856 event = STREAM_CBK_EVENT_DRAIN_READY;
857 break;
858 default:
859 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
860 break;
861 }
862 pthread_mutex_lock(&out->lock);
863 out->offload_thread_blocked = false;
864 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700865 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700866 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700867 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700868 free(cmd);
869 }
870
871 pthread_cond_signal(&out->cond);
872 while (!list_empty(&out->offload_cmd_list)) {
873 item = list_head(&out->offload_cmd_list);
874 list_remove(item);
875 free(node_to_item(item, struct offload_cmd, node));
876 }
877 pthread_mutex_unlock(&out->lock);
878
879 return NULL;
880}
881
882static int create_offload_callback_thread(struct stream_out *out)
883{
884 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
885 list_init(&out->offload_cmd_list);
886 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
887 offload_thread_loop, out);
888 return 0;
889}
890
891static int destroy_offload_callback_thread(struct stream_out *out)
892{
893 pthread_mutex_lock(&out->lock);
894 stop_compressed_output_l(out);
895 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
896
897 pthread_mutex_unlock(&out->lock);
898 pthread_join(out->offload_thread, (void **) NULL);
899 pthread_cond_destroy(&out->offload_cond);
900
901 return 0;
902}
903
Eric Laurent07eeafd2013-10-06 12:52:49 -0700904static bool allow_hdmi_channel_config(struct audio_device *adev)
905{
906 struct listnode *node;
907 struct audio_usecase *usecase;
908 bool ret = true;
909
910 list_for_each(node, &adev->usecase_list) {
911 usecase = node_to_item(node, struct audio_usecase, list);
912 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
913 /*
914 * If voice call is already existing, do not proceed further to avoid
915 * disabling/enabling both RX and TX devices, CSD calls, etc.
916 * Once the voice call done, the HDMI channels can be configured to
917 * max channels of remaining use cases.
918 */
919 if (usecase->id == USECASE_VOICE_CALL) {
920 ALOGD("%s: voice call is active, no change in HDMI channels",
921 __func__);
922 ret = false;
923 break;
924 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
925 ALOGD("%s: multi channel playback is active, "
926 "no change in HDMI channels", __func__);
927 ret = false;
928 break;
929 }
930 }
931 }
932 return ret;
933}
934
935static int check_and_set_hdmi_channels(struct audio_device *adev,
936 unsigned int channels)
937{
938 struct listnode *node;
939 struct audio_usecase *usecase;
940
941 /* Check if change in HDMI channel config is allowed */
942 if (!allow_hdmi_channel_config(adev))
943 return 0;
944
945 if (channels == adev->cur_hdmi_channels) {
946 ALOGD("%s: Requested channels are same as current", __func__);
947 return 0;
948 }
949
950 platform_set_hdmi_channels(adev->platform, channels);
951 adev->cur_hdmi_channels = channels;
952
953 /*
954 * Deroute all the playback streams routed to HDMI so that
955 * the back end is deactivated. Note that backend will not
956 * be deactivated if any one stream is connected to it.
957 */
958 list_for_each(node, &adev->usecase_list) {
959 usecase = node_to_item(node, struct audio_usecase, list);
960 if (usecase->type == PCM_PLAYBACK &&
961 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
962 disable_audio_route(adev, usecase, true);
963 }
964 }
965
966 /*
967 * Enable all the streams disabled above. Now the HDMI backend
968 * will be activated with new channel configuration
969 */
970 list_for_each(node, &adev->usecase_list) {
971 usecase = node_to_item(node, struct audio_usecase, list);
972 if (usecase->type == PCM_PLAYBACK &&
973 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
974 enable_audio_route(adev, usecase, true);
975 }
976 }
977
978 return 0;
979}
980
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800981static int stop_output_stream(struct stream_out *out)
982{
983 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800984 struct audio_usecase *uc_info;
985 struct audio_device *adev = out->dev;
986
Eric Laurent994a6932013-07-17 11:51:42 -0700987 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700988 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800989 uc_info = get_usecase_from_list(adev, out->usecase);
990 if (uc_info == NULL) {
991 ALOGE("%s: Could not find the usecase (%d) in the list",
992 __func__, out->usecase);
993 return -EINVAL;
994 }
995
Eric Laurentc4aef752013-09-12 17:45:53 -0700996 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
997 adev->visualizer_stop_output != NULL)
998 adev->visualizer_stop_output(out->handle);
999
Eric Laurent150dbfe2013-02-27 14:31:02 -08001000 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001001 disable_audio_route(adev, uc_info, true);
1002
1003 /* 2. Disable the rx device */
1004 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001005
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001006 list_remove(&uc_info->list);
1007 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001008
Eric Laurent07eeafd2013-10-06 12:52:49 -07001009 /* Must be called after removing the usecase from list */
1010 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1011 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1012
Eric Laurent994a6932013-07-17 11:51:42 -07001013 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001014 return ret;
1015}
1016
1017int start_output_stream(struct stream_out *out)
1018{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001019 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001020 struct audio_usecase *uc_info;
1021 struct audio_device *adev = out->dev;
1022
Eric Laurent994a6932013-07-17 11:51:42 -07001023 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001024 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001025 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026 if (out->pcm_device_id < 0) {
1027 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1028 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001029 ret = -EINVAL;
1030 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031 }
1032
1033 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1034 uc_info->id = out->usecase;
1035 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001036 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001037 uc_info->devices = out->devices;
1038 uc_info->in_snd_device = SND_DEVICE_NONE;
1039 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001040
Eric Laurent07eeafd2013-10-06 12:52:49 -07001041 /* This must be called before adding this usecase to the list */
1042 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1043 check_and_set_hdmi_channels(adev, out->config.channels);
1044
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001045 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001046
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001047 select_devices(adev, out->usecase);
1048
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001049 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1050 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001051 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1052 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001053 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001054 if (out->pcm && !pcm_is_ready(out->pcm)) {
1055 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1056 pcm_close(out->pcm);
1057 out->pcm = NULL;
1058 ret = -EIO;
1059 goto error_open;
1060 }
1061 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001062 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001063 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1064 COMPRESS_IN, &out->compr_config);
1065 if (out->compr && !is_compress_ready(out->compr)) {
1066 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1067 compress_close(out->compr);
1068 out->compr = NULL;
1069 ret = -EIO;
1070 goto error_open;
1071 }
1072 if (out->offload_callback)
1073 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001074
1075 if (adev->visualizer_start_output != NULL)
1076 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001077 }
Eric Laurent994a6932013-07-17 11:51:42 -07001078 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001079 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001080error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001081 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001082error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001083 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001084}
1085
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001086static int check_input_parameters(uint32_t sample_rate,
1087 audio_format_t format,
1088 int channel_count)
1089{
1090 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1091
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001092 if ((channel_count < 1) || (channel_count > 6)) return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001093
1094 switch (sample_rate) {
1095 case 8000:
1096 case 11025:
1097 case 12000:
1098 case 16000:
1099 case 22050:
1100 case 24000:
1101 case 32000:
1102 case 44100:
1103 case 48000:
1104 break;
1105 default:
1106 return -EINVAL;
1107 }
1108
1109 return 0;
1110}
1111
1112static size_t get_input_buffer_size(uint32_t sample_rate,
1113 audio_format_t format,
1114 int channel_count)
1115{
1116 size_t size = 0;
1117
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001118 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1119 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001120
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001121 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1122 /* ToDo: should use frame_size computed based on the format and
1123 channel_count here. */
1124 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001125
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001126 /* make sure the size is multiple of 64 */
1127 size += 0x3f;
1128 size &= ~0x3f;
1129
1130 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001131}
1132
1133static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1134{
1135 struct stream_out *out = (struct stream_out *)stream;
1136
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001137 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001138}
1139
1140static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1141{
1142 return -ENOSYS;
1143}
1144
1145static size_t out_get_buffer_size(const struct audio_stream *stream)
1146{
1147 struct stream_out *out = (struct stream_out *)stream;
1148
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001149 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1150 return out->compr_config.fragment_size;
1151 }
1152
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001153 return out->config.period_size * audio_stream_frame_size(stream);
1154}
1155
1156static uint32_t out_get_channels(const struct audio_stream *stream)
1157{
1158 struct stream_out *out = (struct stream_out *)stream;
1159
1160 return out->channel_mask;
1161}
1162
1163static audio_format_t out_get_format(const struct audio_stream *stream)
1164{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001165 struct stream_out *out = (struct stream_out *)stream;
1166
1167 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001168}
1169
1170static int out_set_format(struct audio_stream *stream, audio_format_t format)
1171{
1172 return -ENOSYS;
1173}
1174
1175static int out_standby(struct audio_stream *stream)
1176{
1177 struct stream_out *out = (struct stream_out *)stream;
1178 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001179
Eric Laurent994a6932013-07-17 11:51:42 -07001180 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001181 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001182
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001183 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001184 if (!out->standby) {
1185 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001186 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1187 if (out->pcm) {
1188 pcm_close(out->pcm);
1189 out->pcm = NULL;
1190 }
1191 } else {
1192 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001193 out->gapless_mdata.encoder_delay = 0;
1194 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001195 if (out->compr != NULL) {
1196 compress_close(out->compr);
1197 out->compr = NULL;
1198 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001199 }
1200 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001202 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001203 }
1204 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001205 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001206 return 0;
1207}
1208
1209static int out_dump(const struct audio_stream *stream, int fd)
1210{
1211 return 0;
1212}
1213
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001214static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1215{
1216 int ret = 0;
1217 char value[32];
1218 struct compr_gapless_mdata tmp_mdata;
1219
1220 if (!out || !parms) {
1221 return -EINVAL;
1222 }
1223
1224 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1225 if (ret >= 0) {
1226 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1227 } else {
1228 return -EINVAL;
1229 }
1230
1231 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1232 if (ret >= 0) {
1233 tmp_mdata.encoder_padding = atoi(value);
1234 } else {
1235 return -EINVAL;
1236 }
1237
1238 out->gapless_mdata = tmp_mdata;
1239 out->send_new_metadata = 1;
1240 ALOGV("%s new encoder delay %u and padding %u", __func__,
1241 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1242
1243 return 0;
1244}
1245
1246
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001247static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1248{
1249 struct stream_out *out = (struct stream_out *)stream;
1250 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001251 struct audio_usecase *usecase;
1252 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001253 struct str_parms *parms;
1254 char value[32];
1255 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001256 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001257
sangwoobc677242013-08-08 16:53:43 +09001258 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001259 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001260 parms = str_parms_create_str(kvpairs);
1261 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1262 if (ret >= 0) {
1263 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001264 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001265 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001266
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001267 /*
1268 * When HDMI cable is unplugged the music playback is paused and
1269 * the policy manager sends routing=0. But the audioflinger
1270 * continues to write data until standby time (3sec).
1271 * As the HDMI core is turned off, the write gets blocked.
1272 * Avoid this by routing audio to speaker until standby.
1273 */
1274 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1275 val == AUDIO_DEVICE_NONE) {
1276 val = AUDIO_DEVICE_OUT_SPEAKER;
1277 }
1278
1279 /*
1280 * select_devices() call below switches all the usecases on the same
1281 * backend to the new device. Refer to check_usecases_codec_backend() in
1282 * the select_devices(). But how do we undo this?
1283 *
1284 * For example, music playback is active on headset (deep-buffer usecase)
1285 * and if we go to ringtones and select a ringtone, low-latency usecase
1286 * will be started on headset+speaker. As we can't enable headset+speaker
1287 * and headset devices at the same time, select_devices() switches the music
1288 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1289 * So when the ringtone playback is completed, how do we undo the same?
1290 *
1291 * We are relying on the out_set_parameters() call on deep-buffer output,
1292 * once the ringtone playback is ended.
1293 * NOTE: We should not check if the current devices are same as new devices.
1294 * Because select_devices() must be called to switch back the music
1295 * playback to headset.
1296 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001297 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001298 out->devices = val;
1299
1300 if (!out->standby)
1301 select_devices(adev, out->usecase);
1302
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001303 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1304 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001305 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001306 voice_start_call(adev);
1307 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1308 voice_is_in_call(adev) &&
1309 (out == adev->primary_output)) {
1310 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001311 }
1312 }
1313
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001314 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1315 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001316 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001317 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001318 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001319
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001320 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001321 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001322 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001323
1324 if (out == adev->primary_output) {
1325 pthread_mutex_lock(&adev->lock);
1326 audio_extn_set_parameters(adev, parms);
1327 pthread_mutex_unlock(&adev->lock);
1328 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001329 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1330 parse_compress_metadata(out, parms);
1331 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001332
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001333 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001334 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001335 return ret;
1336}
1337
1338static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1339{
1340 struct stream_out *out = (struct stream_out *)stream;
1341 struct str_parms *query = str_parms_create_str(keys);
1342 char *str;
1343 char value[256];
1344 struct str_parms *reply = str_parms_create();
1345 size_t i, j;
1346 int ret;
1347 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001348 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001349 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1350 if (ret >= 0) {
1351 value[0] = '\0';
1352 i = 0;
1353 while (out->supported_channel_masks[i] != 0) {
1354 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1355 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1356 if (!first) {
1357 strcat(value, "|");
1358 }
1359 strcat(value, out_channels_name_to_enum_table[j].name);
1360 first = false;
1361 break;
1362 }
1363 }
1364 i++;
1365 }
1366 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1367 str = str_parms_to_str(reply);
1368 } else {
1369 str = strdup(keys);
1370 }
1371 str_parms_destroy(query);
1372 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001373 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001374 return str;
1375}
1376
1377static uint32_t out_get_latency(const struct audio_stream_out *stream)
1378{
1379 struct stream_out *out = (struct stream_out *)stream;
1380
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001381 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1382 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1383
1384 return (out->config.period_count * out->config.period_size * 1000) /
1385 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001386}
1387
1388static int out_set_volume(struct audio_stream_out *stream, float left,
1389 float right)
1390{
Eric Laurenta9024de2013-04-04 09:19:12 -07001391 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001392 int volume[2];
1393
Eric Laurenta9024de2013-04-04 09:19:12 -07001394 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1395 /* only take left channel into account: the API is for stereo anyway */
1396 out->muted = (left == 0.0f);
1397 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001398 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1399 const char *mixer_ctl_name = "Compress Playback Volume";
1400 struct audio_device *adev = out->dev;
1401 struct mixer_ctl *ctl;
1402
1403 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1404 if (!ctl) {
1405 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1406 __func__, mixer_ctl_name);
1407 return -EINVAL;
1408 }
1409 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1410 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1411 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1412 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001413 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001414
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001415 return -ENOSYS;
1416}
1417
1418static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1419 size_t bytes)
1420{
1421 struct stream_out *out = (struct stream_out *)stream;
1422 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001423 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001424
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001425 pthread_mutex_lock(&out->lock);
1426 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001427 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001428 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001429 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001430 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001431 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001432 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001433 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001434 goto exit;
1435 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001436 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001437
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001438 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001439 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1440 if (out->send_new_metadata) {
1441 ALOGVV("send new gapless metadata");
1442 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1443 out->send_new_metadata = 0;
1444 }
1445
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001446 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001447 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001448 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001449 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1450 }
1451 if (!out->playback_started) {
1452 compress_start(out->compr);
1453 out->playback_started = 1;
1454 out->offload_state = OFFLOAD_STATE_PLAYING;
1455 }
1456 pthread_mutex_unlock(&out->lock);
1457 return ret;
1458 } else {
1459 if (out->pcm) {
1460 if (out->muted)
1461 memset((void *)buffer, 0, bytes);
1462 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1463 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001464 if (ret == 0)
1465 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001466 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001467 }
1468
1469exit:
1470 pthread_mutex_unlock(&out->lock);
1471
1472 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001473 if (out->pcm)
1474 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001475 out_standby(&out->stream.common);
1476 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1477 out_get_sample_rate(&out->stream.common));
1478 }
1479 return bytes;
1480}
1481
1482static int out_get_render_position(const struct audio_stream_out *stream,
1483 uint32_t *dsp_frames)
1484{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001485 struct stream_out *out = (struct stream_out *)stream;
1486 *dsp_frames = 0;
1487 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1488 pthread_mutex_lock(&out->lock);
1489 if (out->compr != NULL) {
1490 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1491 &out->sample_rate);
1492 ALOGVV("%s rendered frames %d sample_rate %d",
1493 __func__, *dsp_frames, out->sample_rate);
1494 }
1495 pthread_mutex_unlock(&out->lock);
1496 return 0;
1497 } else
1498 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001499}
1500
1501static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1502{
1503 return 0;
1504}
1505
1506static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1507{
1508 return 0;
1509}
1510
1511static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1512 int64_t *timestamp)
1513{
1514 return -EINVAL;
1515}
1516
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001517static int out_get_presentation_position(const struct audio_stream_out *stream,
1518 uint64_t *frames, struct timespec *timestamp)
1519{
1520 struct stream_out *out = (struct stream_out *)stream;
1521 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001522 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001523
1524 pthread_mutex_lock(&out->lock);
1525
Eric Laurent949a0892013-09-20 09:20:13 -07001526 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1527 if (out->compr != NULL) {
1528 compress_get_tstamp(out->compr, &dsp_frames,
1529 &out->sample_rate);
1530 ALOGVV("%s rendered frames %ld sample_rate %d",
1531 __func__, dsp_frames, out->sample_rate);
1532 *frames = dsp_frames;
1533 ret = 0;
1534 /* this is the best we can do */
1535 clock_gettime(CLOCK_MONOTONIC, timestamp);
1536 }
1537 } else {
1538 if (out->pcm) {
1539 size_t avail;
1540 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1541 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001542 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001543 // This adjustment accounts for buffering after app processor.
1544 // It is based on estimated DSP latency per use case, rather than exact.
1545 signed_frames -=
1546 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1547
Eric Laurent949a0892013-09-20 09:20:13 -07001548 // It would be unusual for this value to be negative, but check just in case ...
1549 if (signed_frames >= 0) {
1550 *frames = signed_frames;
1551 ret = 0;
1552 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001553 }
1554 }
1555 }
1556
1557 pthread_mutex_unlock(&out->lock);
1558
1559 return ret;
1560}
1561
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001562static int out_set_callback(struct audio_stream_out *stream,
1563 stream_callback_t callback, void *cookie)
1564{
1565 struct stream_out *out = (struct stream_out *)stream;
1566
1567 ALOGV("%s", __func__);
1568 pthread_mutex_lock(&out->lock);
1569 out->offload_callback = callback;
1570 out->offload_cookie = cookie;
1571 pthread_mutex_unlock(&out->lock);
1572 return 0;
1573}
1574
1575static int out_pause(struct audio_stream_out* stream)
1576{
1577 struct stream_out *out = (struct stream_out *)stream;
1578 int status = -ENOSYS;
1579 ALOGV("%s", __func__);
1580 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1581 pthread_mutex_lock(&out->lock);
1582 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1583 status = compress_pause(out->compr);
1584 out->offload_state = OFFLOAD_STATE_PAUSED;
1585 }
1586 pthread_mutex_unlock(&out->lock);
1587 }
1588 return status;
1589}
1590
1591static int out_resume(struct audio_stream_out* stream)
1592{
1593 struct stream_out *out = (struct stream_out *)stream;
1594 int status = -ENOSYS;
1595 ALOGV("%s", __func__);
1596 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1597 status = 0;
1598 pthread_mutex_lock(&out->lock);
1599 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1600 status = compress_resume(out->compr);
1601 out->offload_state = OFFLOAD_STATE_PLAYING;
1602 }
1603 pthread_mutex_unlock(&out->lock);
1604 }
1605 return status;
1606}
1607
1608static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
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 pthread_mutex_lock(&out->lock);
1615 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1616 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1617 else
1618 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1619 pthread_mutex_unlock(&out->lock);
1620 }
1621 return status;
1622}
1623
1624static int out_flush(struct audio_stream_out* stream)
1625{
1626 struct stream_out *out = (struct stream_out *)stream;
1627 ALOGV("%s", __func__);
1628 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1629 pthread_mutex_lock(&out->lock);
1630 stop_compressed_output_l(out);
1631 pthread_mutex_unlock(&out->lock);
1632 return 0;
1633 }
1634 return -ENOSYS;
1635}
1636
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001637/** audio_stream_in implementation **/
1638static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1639{
1640 struct stream_in *in = (struct stream_in *)stream;
1641
1642 return in->config.rate;
1643}
1644
1645static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1646{
1647 return -ENOSYS;
1648}
1649
1650static size_t in_get_buffer_size(const struct audio_stream *stream)
1651{
1652 struct stream_in *in = (struct stream_in *)stream;
1653
1654 return in->config.period_size * audio_stream_frame_size(stream);
1655}
1656
1657static uint32_t in_get_channels(const struct audio_stream *stream)
1658{
1659 struct stream_in *in = (struct stream_in *)stream;
1660
1661 return in->channel_mask;
1662}
1663
1664static audio_format_t in_get_format(const struct audio_stream *stream)
1665{
1666 return AUDIO_FORMAT_PCM_16_BIT;
1667}
1668
1669static int in_set_format(struct audio_stream *stream, audio_format_t format)
1670{
1671 return -ENOSYS;
1672}
1673
1674static int in_standby(struct audio_stream *stream)
1675{
1676 struct stream_in *in = (struct stream_in *)stream;
1677 struct audio_device *adev = in->dev;
1678 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001679 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001680 pthread_mutex_lock(&in->lock);
1681 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001682 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001683 if (in->pcm) {
1684 pcm_close(in->pcm);
1685 in->pcm = NULL;
1686 }
1687 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001688 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001689 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001690 }
1691 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001692 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001693 return status;
1694}
1695
1696static int in_dump(const struct audio_stream *stream, int fd)
1697{
1698 return 0;
1699}
1700
1701static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1702{
1703 struct stream_in *in = (struct stream_in *)stream;
1704 struct audio_device *adev = in->dev;
1705 struct str_parms *parms;
1706 char *str;
1707 char value[32];
1708 int ret, val = 0;
1709
Eric Laurent994a6932013-07-17 11:51:42 -07001710 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001711 parms = str_parms_create_str(kvpairs);
1712
1713 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1714
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001715 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001716 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001717 if (ret >= 0) {
1718 val = atoi(value);
1719 /* no audio source uses val == 0 */
1720 if ((in->source != val) && (val != 0)) {
1721 in->source = val;
1722 }
1723 }
1724
1725 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1726 if (ret >= 0) {
1727 val = atoi(value);
1728 if ((in->device != val) && (val != 0)) {
1729 in->device = val;
1730 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001731 if (!in->standby)
1732 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001733 }
1734 }
1735
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001736 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001737 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001738
1739 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001740 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001741 return ret;
1742}
1743
1744static char* in_get_parameters(const struct audio_stream *stream,
1745 const char *keys)
1746{
1747 return strdup("");
1748}
1749
1750static int in_set_gain(struct audio_stream_in *stream, float gain)
1751{
1752 return 0;
1753}
1754
1755static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1756 size_t bytes)
1757{
1758 struct stream_in *in = (struct stream_in *)stream;
1759 struct audio_device *adev = in->dev;
1760 int i, ret = -1;
1761
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001762 pthread_mutex_lock(&in->lock);
1763 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001764 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001765 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001766 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001767 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001768 goto exit;
1769 }
1770 in->standby = 0;
1771 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001772
1773 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001774 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1775 ret = audio_extn_ssr_read(stream, buffer, bytes);
1776 else
1777 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001778 }
1779
1780 /*
1781 * Instead of writing zeroes here, we could trust the hardware
1782 * to always provide zeroes when muted.
1783 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001784 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001785 memset(buffer, 0, bytes);
1786
1787exit:
1788 pthread_mutex_unlock(&in->lock);
1789
1790 if (ret != 0) {
1791 in_standby(&in->stream.common);
1792 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1793 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1794 in_get_sample_rate(&in->stream.common));
1795 }
1796 return bytes;
1797}
1798
1799static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1800{
1801 return 0;
1802}
1803
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001804static int add_remove_audio_effect(const struct audio_stream *stream,
1805 effect_handle_t effect,
1806 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001807{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001808 struct stream_in *in = (struct stream_in *)stream;
1809 int status = 0;
1810 effect_descriptor_t desc;
1811
1812 status = (*effect)->get_descriptor(effect, &desc);
1813 if (status != 0)
1814 return status;
1815
1816 pthread_mutex_lock(&in->lock);
1817 pthread_mutex_lock(&in->dev->lock);
1818 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1819 in->enable_aec != enable &&
1820 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1821 in->enable_aec = enable;
1822 if (!in->standby)
1823 select_devices(in->dev, in->usecase);
1824 }
1825 pthread_mutex_unlock(&in->dev->lock);
1826 pthread_mutex_unlock(&in->lock);
1827
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001828 return 0;
1829}
1830
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001831static int in_add_audio_effect(const struct audio_stream *stream,
1832 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001833{
Eric Laurent994a6932013-07-17 11:51:42 -07001834 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001835 return add_remove_audio_effect(stream, effect, true);
1836}
1837
1838static int in_remove_audio_effect(const struct audio_stream *stream,
1839 effect_handle_t effect)
1840{
Eric Laurent994a6932013-07-17 11:51:42 -07001841 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001842 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001843}
1844
1845static int adev_open_output_stream(struct audio_hw_device *dev,
1846 audio_io_handle_t handle,
1847 audio_devices_t devices,
1848 audio_output_flags_t flags,
1849 struct audio_config *config,
1850 struct audio_stream_out **stream_out)
1851{
1852 struct audio_device *adev = (struct audio_device *)dev;
1853 struct stream_out *out;
1854 int i, ret;
1855
Eric Laurent994a6932013-07-17 11:51:42 -07001856 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001857 __func__, config->sample_rate, config->channel_mask, devices, flags);
1858 *stream_out = NULL;
1859 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1860
1861 if (devices == AUDIO_DEVICE_NONE)
1862 devices = AUDIO_DEVICE_OUT_SPEAKER;
1863
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001864 out->flags = flags;
1865 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001866 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001867 out->format = config->format;
1868 out->sample_rate = config->sample_rate;
1869 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1870 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001871 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001872
1873 /* Init use case and pcm_config */
1874 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1875 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001876 pthread_mutex_lock(&adev->lock);
1877 ret = read_hdmi_channel_masks(out);
1878 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001879 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001880 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001881
1882 if (config->sample_rate == 0)
1883 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1884 if (config->channel_mask == 0)
1885 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1886
1887 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001888 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001889 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1890 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001891 out->config.rate = config->sample_rate;
1892 out->config.channels = popcount(out->channel_mask);
1893 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001894 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1895 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1896 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001897 out->sample_rate = out->config.rate;
1898 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1899 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1900 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1901 ALOGE("%s: Unsupported Offload information", __func__);
1902 ret = -EINVAL;
1903 goto error_open;
1904 }
1905 if (!is_supported_format(config->offload_info.format)) {
1906 ALOGE("%s: Unsupported audio format", __func__);
1907 ret = -EINVAL;
1908 goto error_open;
1909 }
1910
1911 out->compr_config.codec = (struct snd_codec *)
1912 calloc(1, sizeof(struct snd_codec));
1913
1914 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1915 if (config->offload_info.channel_mask)
1916 out->channel_mask = config->offload_info.channel_mask;
1917 else if (config->channel_mask)
1918 out->channel_mask = config->channel_mask;
1919 out->format = config->offload_info.format;
1920 out->sample_rate = config->offload_info.sample_rate;
1921
1922 out->stream.set_callback = out_set_callback;
1923 out->stream.pause = out_pause;
1924 out->stream.resume = out_resume;
1925 out->stream.drain = out_drain;
1926 out->stream.flush = out_flush;
1927
1928 out->compr_config.codec->id =
1929 get_snd_codec_id(config->offload_info.format);
1930 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1931 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1932 out->compr_config.codec->sample_rate =
1933 compress_get_alsa_rate(config->offload_info.sample_rate);
1934 out->compr_config.codec->bit_rate =
1935 config->offload_info.bit_rate;
1936 out->compr_config.codec->ch_in =
1937 popcount(config->channel_mask);
1938 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1939
1940 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1941 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001942
1943 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001944 create_offload_callback_thread(out);
1945 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1946 __func__, config->offload_info.version,
1947 config->offload_info.bit_rate);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07001948 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
1949 ret = voice_check_and_set_incall_music_usecase(adev, out);
1950 if (ret != 0) {
1951 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
1952 __func__, ret);
1953 goto error_open;
1954 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001955 } else {
1956 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1957 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001958 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001959 }
1960
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001961 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1962 if(adev->primary_output == NULL)
1963 adev->primary_output = out;
1964 else {
1965 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001966 ret = -EEXIST;
1967 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001968 }
1969 }
1970
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001971 /* Check if this usecase is already existing */
1972 pthread_mutex_lock(&adev->lock);
1973 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1974 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001975 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001976 ret = -EEXIST;
1977 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001978 }
1979 pthread_mutex_unlock(&adev->lock);
1980
1981 out->stream.common.get_sample_rate = out_get_sample_rate;
1982 out->stream.common.set_sample_rate = out_set_sample_rate;
1983 out->stream.common.get_buffer_size = out_get_buffer_size;
1984 out->stream.common.get_channels = out_get_channels;
1985 out->stream.common.get_format = out_get_format;
1986 out->stream.common.set_format = out_set_format;
1987 out->stream.common.standby = out_standby;
1988 out->stream.common.dump = out_dump;
1989 out->stream.common.set_parameters = out_set_parameters;
1990 out->stream.common.get_parameters = out_get_parameters;
1991 out->stream.common.add_audio_effect = out_add_audio_effect;
1992 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1993 out->stream.get_latency = out_get_latency;
1994 out->stream.set_volume = out_set_volume;
1995 out->stream.write = out_write;
1996 out->stream.get_render_position = out_get_render_position;
1997 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001998 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001999
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002001 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002002 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002003
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002004 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2005 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002006
2007 config->format = out->stream.common.get_format(&out->stream.common);
2008 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2009 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2010
2011 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002012 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002013 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002014
2015error_open:
2016 free(out);
2017 *stream_out = NULL;
2018 ALOGD("%s: exit: ret %d", __func__, ret);
2019 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002020}
2021
2022static void adev_close_output_stream(struct audio_hw_device *dev,
2023 struct audio_stream_out *stream)
2024{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002025 struct stream_out *out = (struct stream_out *)stream;
2026 struct audio_device *adev = out->dev;
2027
Eric Laurent994a6932013-07-17 11:51:42 -07002028 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002029 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002030 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2031 destroy_offload_callback_thread(out);
2032
2033 if (out->compr_config.codec != NULL)
2034 free(out->compr_config.codec);
2035 }
2036 pthread_cond_destroy(&out->cond);
2037 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002038 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002039 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002040}
2041
2042static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2043{
2044 struct audio_device *adev = (struct audio_device *)dev;
2045 struct str_parms *parms;
2046 char *str;
2047 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002048 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002049 int ret;
2050
Eric Laurent994a6932013-07-17 11:51:42 -07002051 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002052 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002053
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002054 voice_set_parameters(adev, parms);
2055 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002056
2057 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2058 if (ret >= 0) {
2059 /* When set to false, HAL should disable EC and NS
2060 * But it is currently not supported.
2061 */
2062 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2063 adev->bluetooth_nrec = true;
2064 else
2065 adev->bluetooth_nrec = false;
2066 }
2067
2068 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2069 if (ret >= 0) {
2070 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2071 adev->screen_off = false;
2072 else
2073 adev->screen_off = true;
2074 }
2075
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002076 ret = str_parms_get_int(parms, "rotation", &val);
2077 if (ret >= 0) {
2078 bool reverse_speakers = false;
2079 switch(val) {
2080 // FIXME: note that the code below assumes that the speakers are in the correct placement
2081 // relative to the user when the device is rotated 90deg from its default rotation. This
2082 // assumption is device-specific, not platform-specific like this code.
2083 case 270:
2084 reverse_speakers = true;
2085 break;
2086 case 0:
2087 case 90:
2088 case 180:
2089 break;
2090 default:
2091 ALOGE("%s: unexpected rotation of %d", __func__, val);
2092 }
2093 pthread_mutex_lock(&adev->lock);
2094 if (adev->speaker_lr_swap != reverse_speakers) {
2095 adev->speaker_lr_swap = reverse_speakers;
2096 // only update the selected device if there is active pcm playback
2097 struct audio_usecase *usecase;
2098 struct listnode *node;
2099 list_for_each(node, &adev->usecase_list) {
2100 usecase = node_to_item(node, struct audio_usecase, list);
2101 if (usecase->type == PCM_PLAYBACK) {
2102 select_devices(adev, usecase->id);
2103 break;
2104 }
2105 }
2106 }
2107 pthread_mutex_unlock(&adev->lock);
2108 }
2109
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002110 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002111 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002112 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002113 return ret;
2114}
2115
2116static char* adev_get_parameters(const struct audio_hw_device *dev,
2117 const char *keys)
2118{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002119 struct audio_device *adev = (struct audio_device *)dev;
2120 struct str_parms *reply = str_parms_create();
2121 struct str_parms *query = str_parms_create_str(keys);
2122 char *str;
2123
2124 audio_extn_get_parameters(adev, query, reply);
2125 platform_get_parameters(adev->platform, query, reply);
2126 str = str_parms_to_str(reply);
2127 str_parms_destroy(query);
2128 str_parms_destroy(reply);
2129
2130 ALOGV("%s: exit: returns - %s", __func__, str);
2131 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002132}
2133
2134static int adev_init_check(const struct audio_hw_device *dev)
2135{
2136 return 0;
2137}
2138
2139static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2140{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002141 int ret;
2142 struct audio_device *adev = (struct audio_device *)dev;
2143 pthread_mutex_lock(&adev->lock);
2144 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002145 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002146 pthread_mutex_unlock(&adev->lock);
2147 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002148}
2149
2150static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2151{
2152 return -ENOSYS;
2153}
2154
2155static int adev_get_master_volume(struct audio_hw_device *dev,
2156 float *volume)
2157{
2158 return -ENOSYS;
2159}
2160
2161static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2162{
2163 return -ENOSYS;
2164}
2165
2166static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2167{
2168 return -ENOSYS;
2169}
2170
2171static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2172{
2173 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002174 pthread_mutex_lock(&adev->lock);
2175 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002176 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002177 adev->mode = mode;
2178 }
2179 pthread_mutex_unlock(&adev->lock);
2180 return 0;
2181}
2182
2183static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2184{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002185 return voice_set_mic_mute((struct audio_device *)dev, state);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002186}
2187
2188static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2189{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002190 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002191 return 0;
2192}
2193
2194static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2195 const struct audio_config *config)
2196{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002197 int channel_count = popcount(config->channel_mask);
2198
2199 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2200}
2201
2202static int adev_open_input_stream(struct audio_hw_device *dev,
2203 audio_io_handle_t handle,
2204 audio_devices_t devices,
2205 struct audio_config *config,
2206 struct audio_stream_in **stream_in)
2207{
2208 struct audio_device *adev = (struct audio_device *)dev;
2209 struct stream_in *in;
2210 int ret, buffer_size, frame_size;
2211 int channel_count = popcount(config->channel_mask);
2212
Eric Laurent994a6932013-07-17 11:51:42 -07002213 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002214 *stream_in = NULL;
2215 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2216 return -EINVAL;
2217
2218 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2219
2220 in->stream.common.get_sample_rate = in_get_sample_rate;
2221 in->stream.common.set_sample_rate = in_set_sample_rate;
2222 in->stream.common.get_buffer_size = in_get_buffer_size;
2223 in->stream.common.get_channels = in_get_channels;
2224 in->stream.common.get_format = in_get_format;
2225 in->stream.common.set_format = in_set_format;
2226 in->stream.common.standby = in_standby;
2227 in->stream.common.dump = in_dump;
2228 in->stream.common.set_parameters = in_set_parameters;
2229 in->stream.common.get_parameters = in_get_parameters;
2230 in->stream.common.add_audio_effect = in_add_audio_effect;
2231 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2232 in->stream.set_gain = in_set_gain;
2233 in->stream.read = in_read;
2234 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2235
2236 in->device = devices;
2237 in->source = AUDIO_SOURCE_DEFAULT;
2238 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002239 in->standby = 1;
2240 in->channel_mask = config->channel_mask;
2241
2242 /* Update config params with the requested sample rate and channels */
2243 in->usecase = USECASE_AUDIO_RECORD;
2244 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002245 in->config.rate = config->sample_rate;
2246
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002247 if (audio_extn_ssr_get_enabled()&& channel_count == 6) {
2248 if(audio_extn_ssr_init(adev, in))
2249 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2250 } else {
2251 in->config.channels = channel_count;
2252 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2253 buffer_size = get_input_buffer_size(config->sample_rate,
2254 config->format,
2255 channel_count);
2256 in->config.period_size = buffer_size / frame_size;
2257 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002258
2259 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002260 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002261 return 0;
2262
2263err_open:
2264 free(in);
2265 *stream_in = NULL;
2266 return ret;
2267}
2268
2269static void adev_close_input_stream(struct audio_hw_device *dev,
2270 struct audio_stream_in *stream)
2271{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002272 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002273 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002274
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002275 in_standby(&stream->common);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002276 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2277 audio_extn_ssr_deinit();
2278 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002279 free(stream);
2280
2281 return;
2282}
2283
2284static int adev_dump(const audio_hw_device_t *device, int fd)
2285{
2286 return 0;
2287}
2288
2289static int adev_close(hw_device_t *device)
2290{
2291 struct audio_device *adev = (struct audio_device *)device;
2292 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002293 free(adev->snd_dev_ref_cnt);
2294 platform_deinit(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002295 free(device);
2296 return 0;
2297}
2298
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002299static int adev_open(const hw_module_t *module, const char *name,
2300 hw_device_t **device)
2301{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002302 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002303
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002304 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002305 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2306
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002307 pthread_mutex_lock(&adev_init_lock);
2308 if (is_adev_initialised == true){
2309 *device = &adev->device.common;
2310 ALOGD("%s: returning existing instance of adev", __func__);
2311 ALOGD("%s: exit", __func__);
2312 pthread_mutex_unlock(&adev_init_lock);
2313 return 0;
2314 }
2315
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002316 adev = calloc(1, sizeof(struct audio_device));
2317
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002318 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2319 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2320 adev->device.common.module = (struct hw_module_t *)module;
2321 adev->device.common.close = adev_close;
2322
2323 adev->device.init_check = adev_init_check;
2324 adev->device.set_voice_volume = adev_set_voice_volume;
2325 adev->device.set_master_volume = adev_set_master_volume;
2326 adev->device.get_master_volume = adev_get_master_volume;
2327 adev->device.set_master_mute = adev_set_master_mute;
2328 adev->device.get_master_mute = adev_get_master_mute;
2329 adev->device.set_mode = adev_set_mode;
2330 adev->device.set_mic_mute = adev_set_mic_mute;
2331 adev->device.get_mic_mute = adev_get_mic_mute;
2332 adev->device.set_parameters = adev_set_parameters;
2333 adev->device.get_parameters = adev_get_parameters;
2334 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2335 adev->device.open_output_stream = adev_open_output_stream;
2336 adev->device.close_output_stream = adev_close_output_stream;
2337 adev->device.open_input_stream = adev_open_input_stream;
2338 adev->device.close_input_stream = adev_close_input_stream;
2339 adev->device.dump = adev_dump;
2340
2341 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002342 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002343 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002344 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002345 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002346 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002347 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002348 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002349 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002350 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002351 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002352
2353 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002354 adev->platform = platform_init(adev);
2355 if (!adev->platform) {
2356 free(adev->snd_dev_ref_cnt);
2357 free(adev);
2358 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2359 *device = NULL;
2360 return -EINVAL;
2361 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002362
2363 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2364 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2365 if (adev->visualizer_lib == NULL) {
2366 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2367 } else {
2368 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2369 adev->visualizer_start_output =
2370 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2371 "visualizer_hal_start_output");
2372 adev->visualizer_stop_output =
2373 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2374 "visualizer_hal_stop_output");
2375 }
2376 }
2377
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002378 *device = &adev->device.common;
2379
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002380 /* update init flag*/
2381 is_adev_initialised = true;
2382 pthread_mutex_unlock(&adev_init_lock);
2383
Eric Laurent994a6932013-07-17 11:51:42 -07002384 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002385 return 0;
2386}
2387
2388static struct hw_module_methods_t hal_module_methods = {
2389 .open = adev_open,
2390};
2391
2392struct audio_module HAL_MODULE_INFO_SYM = {
2393 .common = {
2394 .tag = HARDWARE_MODULE_TAG,
2395 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2396 .hal_api_version = HARDWARE_HAL_API_VERSION,
2397 .id = AUDIO_HARDWARE_MODULE_ID,
2398 .name = "QCOM Audio HAL",
2399 .author = "Code Aurora Forum",
2400 .methods = &hal_module_methods,
2401 },
2402};