blob: c2997c66ecc7a853942c9333b4bf6f8141e49447 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_primary"
18/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070019/*#define VERY_VERY_VERBOSE_LOGGING*/
20#ifdef VERY_VERY_VERBOSE_LOGGING
21#define ALOGVV ALOGV
22#else
23#define ALOGVV(a...) do { } while(0)
24#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080025
26#include <errno.h>
27#include <pthread.h>
28#include <stdint.h>
29#include <sys/time.h>
30#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080031#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070032#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070033#include <sys/resource.h>
34#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080035
36#include <cutils/log.h>
37#include <cutils/str_parms.h>
38#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070039#include <cutils/atomic.h>
40#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080041
Eric Laurentb23d5282013-05-14 15:27:20 -070042#include <hardware/audio_effect.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070043#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070044#include <audio_effects/effect_aec.h>
45#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080046#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include "platform_api.h"
48#include <platform.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070050#include "sound/compress_params.h"
51
52#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
53#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
54/* ToDo: Check and update a proper value in msec */
55#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
56#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
57
Eric Laurentb23d5282013-05-14 15:27:20 -070058struct pcm_config pcm_config_deep_buffer = {
59 .channels = 2,
60 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
61 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
62 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
63 .format = PCM_FORMAT_S16_LE,
64 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
65 .stop_threshold = INT_MAX,
66 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
67};
68
69struct pcm_config pcm_config_low_latency = {
70 .channels = 2,
71 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
72 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
73 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
74 .format = PCM_FORMAT_S16_LE,
75 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
76 .stop_threshold = INT_MAX,
77 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
78};
79
80struct pcm_config pcm_config_hdmi_multi = {
81 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
82 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
83 .period_size = HDMI_MULTI_PERIOD_SIZE,
84 .period_count = HDMI_MULTI_PERIOD_COUNT,
85 .format = PCM_FORMAT_S16_LE,
86 .start_threshold = 0,
87 .stop_threshold = INT_MAX,
88 .avail_min = 0,
89};
90
91struct pcm_config pcm_config_audio_capture = {
92 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070093 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
94 .format = PCM_FORMAT_S16_LE,
95};
96
97struct pcm_config pcm_config_voice_call = {
98 .channels = 1,
99 .rate = 8000,
100 .period_size = 160,
101 .period_count = 2,
102 .format = PCM_FORMAT_S16_LE,
103};
104
105static const char * const use_case_table[AUDIO_USECASE_MAX] = {
106 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
107 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
108 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
109 [USECASE_AUDIO_RECORD] = "audio-record",
110 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
111 [USECASE_VOICE_CALL] = "voice-call",
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700112 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700113};
114
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800115
116#define STRING_TO_ENUM(string) { #string, string }
117
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800118struct string_to_enum {
119 const char *name;
120 uint32_t value;
121};
122
123static const struct string_to_enum out_channels_name_to_enum_table[] = {
124 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
125 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
126 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
127};
128
Haynes Mathew George5191a852013-09-11 14:19:36 -0700129static int set_voice_volume_l(struct audio_device *adev, float volume);
130
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700131static bool is_supported_format(audio_format_t format)
132{
Eric Laurent86e17132013-09-12 17:49:30 -0700133 if (format == AUDIO_FORMAT_MP3 ||
134 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700135 return true;
136
137 return false;
138}
139
140static int get_snd_codec_id(audio_format_t format)
141{
142 int id = 0;
143
144 switch (format) {
145 case AUDIO_FORMAT_MP3:
146 id = SND_AUDIOCODEC_MP3;
147 break;
148 case AUDIO_FORMAT_AAC:
149 id = SND_AUDIOCODEC_AAC;
150 break;
151 default:
152 ALOGE("%s: Unsupported audio format", __func__);
153 }
154
155 return id;
156}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800157
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700158static int enable_audio_route(struct audio_device *adev,
159 struct audio_usecase *usecase,
160 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800161{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700162 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800164
165 if (usecase == NULL)
166 return -EINVAL;
167
168 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
169
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800170 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700171 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800172 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700173 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800174
175 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700176 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700177 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700178 audio_route_apply_path(adev->audio_route, mixer_path);
179 if (update_mixer)
180 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800181
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800182 ALOGV("%s: exit", __func__);
183 return 0;
184}
185
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700186static int disable_audio_route(struct audio_device *adev,
187 struct audio_usecase *usecase,
188 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800189{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700190 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800191 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800192
193 if (usecase == NULL)
194 return -EINVAL;
195
196 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700197 if (usecase->type == PCM_CAPTURE)
198 snd_device = usecase->in_snd_device;
199 else
200 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800201 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700202 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700203 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700204 audio_route_reset_path(adev->audio_route, mixer_path);
205 if (update_mixer)
206 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800207
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800208 ALOGV("%s: exit", __func__);
209 return 0;
210}
211
212static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700213 snd_device_t snd_device,
214 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800215{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800216 if (snd_device < SND_DEVICE_MIN ||
217 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800218 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800219 return -EINVAL;
220 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700221
222 adev->snd_dev_ref_cnt[snd_device]++;
223 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700224 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700225 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700226 return 0;
227 }
228
Eric Laurentb23d5282013-05-14 15:27:20 -0700229 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700230 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800231 return -EINVAL;
232 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800233
Eric Laurent994a6932013-07-17 11:51:42 -0700234 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700235 snd_device, platform_get_snd_device_name(snd_device));
236 audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700237 if (update_mixer)
238 audio_route_update_mixer(adev->audio_route);
239
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800240 return 0;
241}
242
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700243static int disable_snd_device(struct audio_device *adev,
244 snd_device_t snd_device,
245 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800246{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800247 if (snd_device < SND_DEVICE_MIN ||
248 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800249 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800250 return -EINVAL;
251 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700252 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
253 ALOGE("%s: device ref cnt is already 0", __func__);
254 return -EINVAL;
255 }
256 adev->snd_dev_ref_cnt[snd_device]--;
257 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700258 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700259 snd_device, platform_get_snd_device_name(snd_device));
260 audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700261 if (update_mixer)
262 audio_route_update_mixer(adev->audio_route);
263 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800264 return 0;
265}
266
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700267static void check_usecases_codec_backend(struct audio_device *adev,
268 struct audio_usecase *uc_info,
269 snd_device_t snd_device)
270{
271 struct listnode *node;
272 struct audio_usecase *usecase;
273 bool switch_device[AUDIO_USECASE_MAX];
274 int i, num_uc_to_switch = 0;
275
276 /*
277 * This function is to make sure that all the usecases that are active on
278 * the hardware codec backend are always routed to any one device that is
279 * handled by the hardware codec.
280 * For example, if low-latency and deep-buffer usecases are currently active
281 * on speaker and out_set_parameters(headset) is received on low-latency
282 * output, then we have to make sure deep-buffer is also switched to headset,
283 * because of the limitation that both the devices cannot be enabled
284 * at the same time as they share the same backend.
285 */
286 /* Disable all the usecases on the shared backend other than the
287 specified usecase */
288 for (i = 0; i < AUDIO_USECASE_MAX; i++)
289 switch_device[i] = false;
290
291 list_for_each(node, &adev->usecase_list) {
292 usecase = node_to_item(node, struct audio_usecase, list);
293 if (usecase->type != PCM_CAPTURE &&
294 usecase != uc_info &&
295 usecase->out_snd_device != snd_device &&
296 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
297 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
298 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700299 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700300 disable_audio_route(adev, usecase, false);
301 switch_device[usecase->id] = true;
302 num_uc_to_switch++;
303 }
304 }
305
306 if (num_uc_to_switch) {
307 /* Make sure all the streams are de-routed before disabling the device */
308 audio_route_update_mixer(adev->audio_route);
309
310 list_for_each(node, &adev->usecase_list) {
311 usecase = node_to_item(node, struct audio_usecase, list);
312 if (switch_device[usecase->id]) {
313 disable_snd_device(adev, usecase->out_snd_device, false);
314 enable_snd_device(adev, snd_device, false);
315 }
316 }
317
318 /* Make sure new snd device is enabled before re-routing the streams */
319 audio_route_update_mixer(adev->audio_route);
320
321 /* Re-route all the usecases on the shared backend other than the
322 specified usecase to new snd devices */
323 list_for_each(node, &adev->usecase_list) {
324 usecase = node_to_item(node, struct audio_usecase, list);
325 /* Update the out_snd_device only before enabling the audio route */
326 if (switch_device[usecase->id] ) {
327 usecase->out_snd_device = snd_device;
328 enable_audio_route(adev, usecase, false);
329 }
330 }
331
332 audio_route_update_mixer(adev->audio_route);
333 }
334}
335
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700336static void check_and_route_capture_usecases(struct audio_device *adev,
337 struct audio_usecase *uc_info,
338 snd_device_t snd_device)
339{
340 struct listnode *node;
341 struct audio_usecase *usecase;
342 bool switch_device[AUDIO_USECASE_MAX];
343 int i, num_uc_to_switch = 0;
344
345 /*
346 * This function is to make sure that all the active capture usecases
347 * are always routed to the same input sound device.
348 * For example, if audio-record and voice-call usecases are currently
349 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
350 * is received for voice call then we have to make sure that audio-record
351 * usecase is also switched to earpiece i.e. voice-dmic-ef,
352 * because of the limitation that two devices cannot be enabled
353 * at the same time if they share the same backend.
354 */
355 for (i = 0; i < AUDIO_USECASE_MAX; i++)
356 switch_device[i] = false;
357
358 list_for_each(node, &adev->usecase_list) {
359 usecase = node_to_item(node, struct audio_usecase, list);
360 if (usecase->type != PCM_PLAYBACK &&
361 usecase != uc_info &&
362 usecase->in_snd_device != snd_device) {
363 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
364 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700365 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700366 disable_audio_route(adev, usecase, false);
367 switch_device[usecase->id] = true;
368 num_uc_to_switch++;
369 }
370 }
371
372 if (num_uc_to_switch) {
373 /* Make sure all the streams are de-routed before disabling the device */
374 audio_route_update_mixer(adev->audio_route);
375
376 list_for_each(node, &adev->usecase_list) {
377 usecase = node_to_item(node, struct audio_usecase, list);
378 if (switch_device[usecase->id]) {
379 disable_snd_device(adev, usecase->in_snd_device, false);
380 enable_snd_device(adev, snd_device, false);
381 }
382 }
383
384 /* Make sure new snd device is enabled before re-routing the streams */
385 audio_route_update_mixer(adev->audio_route);
386
387 /* Re-route all the usecases on the shared backend other than the
388 specified usecase to new snd devices */
389 list_for_each(node, &adev->usecase_list) {
390 usecase = node_to_item(node, struct audio_usecase, list);
391 /* Update the in_snd_device only before enabling the audio route */
392 if (switch_device[usecase->id] ) {
393 usecase->in_snd_device = snd_device;
394 enable_audio_route(adev, usecase, false);
395 }
396 }
397
398 audio_route_update_mixer(adev->audio_route);
399 }
400}
401
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800402
403/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700404static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800405{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700406 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700407 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800408
409 switch (channels) {
410 /*
411 * Do not handle stereo output in Multi-channel cases
412 * Stereo case is handled in normal playback path
413 */
414 case 6:
415 ALOGV("%s: HDMI supports 5.1", __func__);
416 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
417 break;
418 case 8:
419 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
420 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
421 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
422 break;
423 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700424 ALOGE("HDMI does not support multi channel playback");
425 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800426 break;
427 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700428 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800429}
430
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700431static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
432 audio_usecase_t uc_id)
433{
434 struct audio_usecase *usecase;
435 struct listnode *node;
436
437 list_for_each(node, &adev->usecase_list) {
438 usecase = node_to_item(node, struct audio_usecase, list);
439 if (usecase->id == uc_id)
440 return usecase;
441 }
442 return NULL;
443}
444
445static int select_devices(struct audio_device *adev,
446 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800447{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800448 snd_device_t out_snd_device = SND_DEVICE_NONE;
449 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700450 struct audio_usecase *usecase = NULL;
451 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800452 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700453 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800454
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700455 usecase = get_usecase_from_list(adev, uc_id);
456 if (usecase == NULL) {
457 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
458 return -EINVAL;
459 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800460
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700461 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700462 out_snd_device = platform_get_output_snd_device(adev->platform,
463 usecase->stream.out->devices);
464 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700465 usecase->devices = usecase->stream.out->devices;
466 } else {
467 /*
468 * If the voice call is active, use the sound devices of voice call usecase
469 * so that it would not result any device switch. All the usecases will
470 * be switched to new device when select_devices() is called for voice call
471 * usecase. This is to avoid switching devices for voice call when
472 * check_usecases_codec_backend() is called below.
473 */
474 if (adev->in_call) {
475 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
476 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
477 in_snd_device = vc_usecase->in_snd_device;
478 out_snd_device = vc_usecase->out_snd_device;
479 }
480 }
481 if (usecase->type == PCM_PLAYBACK) {
482 usecase->devices = usecase->stream.out->devices;
483 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700484 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700485 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700486 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700487 if (usecase->stream.out == adev->primary_output &&
488 adev->active_input &&
489 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
490 select_devices(adev, adev->active_input->usecase);
491 }
492 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700493 } else if (usecase->type == PCM_CAPTURE) {
494 usecase->devices = usecase->stream.in->device;
495 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700496 if (in_snd_device == SND_DEVICE_NONE) {
497 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
498 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700499 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700500 adev->primary_output->devices);
501 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700502 in_snd_device = platform_get_input_snd_device(adev->platform,
503 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700504 }
505 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700506 }
507 }
508
509 if (out_snd_device == usecase->out_snd_device &&
510 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800511 return 0;
512 }
513
sangwoobc677242013-08-08 16:53:43 +0900514 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700515 out_snd_device, platform_get_snd_device_name(out_snd_device),
516 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800517
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800518 /*
519 * Limitation: While in call, to do a device switch we need to disable
520 * and enable both RX and TX devices though one of them is same as current
521 * device.
522 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700523 if (usecase->type == VOICE_CALL) {
524 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800525 }
526
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700527 /* Disable current sound devices */
528 if (usecase->out_snd_device != SND_DEVICE_NONE) {
529 disable_audio_route(adev, usecase, true);
530 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800531 }
532
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700533 if (usecase->in_snd_device != SND_DEVICE_NONE) {
534 disable_audio_route(adev, usecase, true);
535 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800536 }
537
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700538 /* Enable new sound devices */
539 if (out_snd_device != SND_DEVICE_NONE) {
540 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
541 check_usecases_codec_backend(adev, usecase, out_snd_device);
542 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800543 }
544
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700545 if (in_snd_device != SND_DEVICE_NONE) {
546 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700547 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700548 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700549
Eric Laurentb23d5282013-05-14 15:27:20 -0700550 if (usecase->type == VOICE_CALL)
551 status = platform_switch_voice_call_device_post(adev->platform,
552 out_snd_device,
553 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800554
sangwoo170731f2013-06-08 15:36:36 +0900555 audio_route_update_mixer(adev->audio_route);
556
557 usecase->in_snd_device = in_snd_device;
558 usecase->out_snd_device = out_snd_device;
559
560 enable_audio_route(adev, usecase, true);
561
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800562 return status;
563}
564
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800565static int stop_input_stream(struct stream_in *in)
566{
567 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800568 struct audio_usecase *uc_info;
569 struct audio_device *adev = in->dev;
570
Eric Laurentc8400632013-02-14 19:04:54 -0800571 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800572
Eric Laurent994a6932013-07-17 11:51:42 -0700573 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700574 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800575 uc_info = get_usecase_from_list(adev, in->usecase);
576 if (uc_info == NULL) {
577 ALOGE("%s: Could not find the usecase (%d) in the list",
578 __func__, in->usecase);
579 return -EINVAL;
580 }
581
Eric Laurent150dbfe2013-02-27 14:31:02 -0800582 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700583 disable_audio_route(adev, uc_info, true);
584
585 /* 2. Disable the tx device */
586 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800587
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800588 list_remove(&uc_info->list);
589 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800590
Eric Laurent994a6932013-07-17 11:51:42 -0700591 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800592 return ret;
593}
594
595int start_input_stream(struct stream_in *in)
596{
597 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800598 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800599 struct audio_usecase *uc_info;
600 struct audio_device *adev = in->dev;
601
Eric Laurent994a6932013-07-17 11:51:42 -0700602 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700603 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800604 if (in->pcm_device_id < 0) {
605 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
606 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800607 ret = -EINVAL;
608 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800609 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700610
611 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800612 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
613 uc_info->id = in->usecase;
614 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800615 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700616 uc_info->devices = in->device;
617 uc_info->in_snd_device = SND_DEVICE_NONE;
618 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800619
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800620 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700621 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800622
Eric Laurentc8400632013-02-14 19:04:54 -0800623 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
624 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800625 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
626 PCM_IN, &in->config);
627 if (in->pcm && !pcm_is_ready(in->pcm)) {
628 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
629 pcm_close(in->pcm);
630 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800631 ret = -EIO;
632 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800633 }
Eric Laurent994a6932013-07-17 11:51:42 -0700634 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800635 return ret;
636
637error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800638 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800639
640error_config:
641 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700642 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800643
644 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800645}
646
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700647/* must be called with out->lock locked */
648static int send_offload_cmd_l(struct stream_out* out, int command)
649{
650 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
651
652 ALOGVV("%s %d", __func__, command);
653
654 cmd->cmd = command;
655 list_add_tail(&out->offload_cmd_list, &cmd->node);
656 pthread_cond_signal(&out->offload_cond);
657 return 0;
658}
659
660/* must be called iwth out->lock locked */
661static void stop_compressed_output_l(struct stream_out *out)
662{
663 out->offload_state = OFFLOAD_STATE_IDLE;
664 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700665 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700666 if (out->compr != NULL) {
667 compress_stop(out->compr);
668 while (out->offload_thread_blocked) {
669 pthread_cond_wait(&out->cond, &out->lock);
670 }
671 }
672}
673
674static void *offload_thread_loop(void *context)
675{
676 struct stream_out *out = (struct stream_out *) context;
677 struct listnode *item;
678
679 out->offload_state = OFFLOAD_STATE_IDLE;
680 out->playback_started = 0;
681
682 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
683 set_sched_policy(0, SP_FOREGROUND);
684 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
685
686 ALOGV("%s", __func__);
687 pthread_mutex_lock(&out->lock);
688 for (;;) {
689 struct offload_cmd *cmd = NULL;
690 stream_callback_event_t event;
691 bool send_callback = false;
692
693 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
694 __func__, list_empty(&out->offload_cmd_list),
695 out->offload_state);
696 if (list_empty(&out->offload_cmd_list)) {
697 ALOGV("%s SLEEPING", __func__);
698 pthread_cond_wait(&out->offload_cond, &out->lock);
699 ALOGV("%s RUNNING", __func__);
700 continue;
701 }
702
703 item = list_head(&out->offload_cmd_list);
704 cmd = node_to_item(item, struct offload_cmd, node);
705 list_remove(item);
706
707 ALOGVV("%s STATE %d CMD %d out->compr %p",
708 __func__, out->offload_state, cmd->cmd, out->compr);
709
710 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
711 free(cmd);
712 break;
713 }
714
715 if (out->compr == NULL) {
716 ALOGE("%s: Compress handle is NULL", __func__);
717 pthread_cond_signal(&out->cond);
718 continue;
719 }
720 out->offload_thread_blocked = true;
721 pthread_mutex_unlock(&out->lock);
722 send_callback = false;
723 switch(cmd->cmd) {
724 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
725 compress_wait(out->compr, -1);
726 send_callback = true;
727 event = STREAM_CBK_EVENT_WRITE_READY;
728 break;
729 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700730 compress_next_track(out->compr);
731 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700732 send_callback = true;
733 event = STREAM_CBK_EVENT_DRAIN_READY;
734 break;
735 case OFFLOAD_CMD_DRAIN:
736 compress_drain(out->compr);
737 send_callback = true;
738 event = STREAM_CBK_EVENT_DRAIN_READY;
739 break;
740 default:
741 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
742 break;
743 }
744 pthread_mutex_lock(&out->lock);
745 out->offload_thread_blocked = false;
746 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700747 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700748 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700749 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700750 free(cmd);
751 }
752
753 pthread_cond_signal(&out->cond);
754 while (!list_empty(&out->offload_cmd_list)) {
755 item = list_head(&out->offload_cmd_list);
756 list_remove(item);
757 free(node_to_item(item, struct offload_cmd, node));
758 }
759 pthread_mutex_unlock(&out->lock);
760
761 return NULL;
762}
763
764static int create_offload_callback_thread(struct stream_out *out)
765{
766 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
767 list_init(&out->offload_cmd_list);
768 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
769 offload_thread_loop, out);
770 return 0;
771}
772
773static int destroy_offload_callback_thread(struct stream_out *out)
774{
775 pthread_mutex_lock(&out->lock);
776 stop_compressed_output_l(out);
777 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
778
779 pthread_mutex_unlock(&out->lock);
780 pthread_join(out->offload_thread, (void **) NULL);
781 pthread_cond_destroy(&out->offload_cond);
782
783 return 0;
784}
785
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800786static int stop_output_stream(struct stream_out *out)
787{
788 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800789 struct audio_usecase *uc_info;
790 struct audio_device *adev = out->dev;
791
Eric Laurent994a6932013-07-17 11:51:42 -0700792 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700793 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800794 uc_info = get_usecase_from_list(adev, out->usecase);
795 if (uc_info == NULL) {
796 ALOGE("%s: Could not find the usecase (%d) in the list",
797 __func__, out->usecase);
798 return -EINVAL;
799 }
800
Eric Laurentc4aef752013-09-12 17:45:53 -0700801 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
802 adev->visualizer_stop_output != NULL)
803 adev->visualizer_stop_output(out->handle);
804
Eric Laurent150dbfe2013-02-27 14:31:02 -0800805 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700806 disable_audio_route(adev, uc_info, true);
807
808 /* 2. Disable the rx device */
809 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800810
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800811 list_remove(&uc_info->list);
812 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800813
Eric Laurent994a6932013-07-17 11:51:42 -0700814 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800815 return ret;
816}
817
818int start_output_stream(struct stream_out *out)
819{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800820 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800821 struct audio_usecase *uc_info;
822 struct audio_device *adev = out->dev;
823
Eric Laurent994a6932013-07-17 11:51:42 -0700824 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700825 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -0700826 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800827 if (out->pcm_device_id < 0) {
828 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
829 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800830 ret = -EINVAL;
831 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800832 }
833
834 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
835 uc_info->id = out->usecase;
836 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800837 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700838 uc_info->devices = out->devices;
839 uc_info->in_snd_device = SND_DEVICE_NONE;
840 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800841
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800842 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800843
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700844 select_devices(adev, out->usecase);
845
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800846 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
847 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700848 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
849 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -0700850 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700851 if (out->pcm && !pcm_is_ready(out->pcm)) {
852 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
853 pcm_close(out->pcm);
854 out->pcm = NULL;
855 ret = -EIO;
856 goto error_open;
857 }
858 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800859 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700860 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
861 COMPRESS_IN, &out->compr_config);
862 if (out->compr && !is_compress_ready(out->compr)) {
863 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
864 compress_close(out->compr);
865 out->compr = NULL;
866 ret = -EIO;
867 goto error_open;
868 }
869 if (out->offload_callback)
870 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -0700871
872 if (adev->visualizer_start_output != NULL)
873 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800874 }
Eric Laurent994a6932013-07-17 11:51:42 -0700875 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800876 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700877error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800878 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800879error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800880 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800881}
882
883static int stop_voice_call(struct audio_device *adev)
884{
885 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800886 struct audio_usecase *uc_info;
887
Eric Laurent994a6932013-07-17 11:51:42 -0700888 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800889 adev->in_call = false;
Eric Laurentb23d5282013-05-14 15:27:20 -0700890
891 ret = platform_stop_voice_call(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800892
893 /* 1. Close the PCM devices */
894 if (adev->voice_call_rx) {
895 pcm_close(adev->voice_call_rx);
896 adev->voice_call_rx = NULL;
897 }
898 if (adev->voice_call_tx) {
899 pcm_close(adev->voice_call_tx);
900 adev->voice_call_tx = NULL;
901 }
902
903 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
904 if (uc_info == NULL) {
905 ALOGE("%s: Could not find the usecase (%d) in the list",
906 __func__, USECASE_VOICE_CALL);
907 return -EINVAL;
908 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800909
910 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700911 disable_audio_route(adev, uc_info, true);
912
913 /* 3. Disable the rx and tx devices */
914 disable_snd_device(adev, uc_info->out_snd_device, false);
915 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800916
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800917 list_remove(&uc_info->list);
918 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800919
Eric Laurent994a6932013-07-17 11:51:42 -0700920 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800921 return ret;
922}
923
924static int start_voice_call(struct audio_device *adev)
925{
926 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800927 struct audio_usecase *uc_info;
928 int pcm_dev_rx_id, pcm_dev_tx_id;
929
Eric Laurent994a6932013-07-17 11:51:42 -0700930 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800931
932 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
933 uc_info->id = USECASE_VOICE_CALL;
934 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800935 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700936 uc_info->devices = adev->primary_output->devices;
937 uc_info->in_snd_device = SND_DEVICE_NONE;
938 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800939
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800940 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800941
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700942 select_devices(adev, USECASE_VOICE_CALL);
943
Eric Laurentb23d5282013-05-14 15:27:20 -0700944 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
945 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800946
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800947 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
948 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
949 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800950 ret = -EIO;
951 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800952 }
953
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800954 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800955 __func__, SOUND_CARD, pcm_dev_rx_id);
956 adev->voice_call_rx = pcm_open(SOUND_CARD,
957 pcm_dev_rx_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -0700958 PCM_OUT | PCM_MONOTONIC, &pcm_config_voice_call);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800959 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
960 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800961 ret = -EIO;
962 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800963 }
964
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800965 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800966 __func__, SOUND_CARD, pcm_dev_tx_id);
967 adev->voice_call_tx = pcm_open(SOUND_CARD,
968 pcm_dev_tx_id,
969 PCM_IN, &pcm_config_voice_call);
970 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
971 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800972 ret = -EIO;
973 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800974 }
Haynes Mathew George5191a852013-09-11 14:19:36 -0700975
976 /* set cached volume */
977 set_voice_volume_l(adev, adev->voice_volume);
978
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800979 pcm_start(adev->voice_call_rx);
980 pcm_start(adev->voice_call_tx);
981
Eric Laurentb23d5282013-05-14 15:27:20 -0700982 ret = platform_start_voice_call(adev->platform);
983 if (ret < 0) {
984 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
985 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800986 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800987 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800988 return 0;
989
990error_start_voice:
991 stop_voice_call(adev);
992
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800993 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800994 return ret;
995}
996
997static int check_input_parameters(uint32_t sample_rate,
998 audio_format_t format,
999 int channel_count)
1000{
1001 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1002
1003 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1004
1005 switch (sample_rate) {
1006 case 8000:
1007 case 11025:
1008 case 12000:
1009 case 16000:
1010 case 22050:
1011 case 24000:
1012 case 32000:
1013 case 44100:
1014 case 48000:
1015 break;
1016 default:
1017 return -EINVAL;
1018 }
1019
1020 return 0;
1021}
1022
1023static size_t get_input_buffer_size(uint32_t sample_rate,
1024 audio_format_t format,
1025 int channel_count)
1026{
1027 size_t size = 0;
1028
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001029 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1030 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001032 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1033 /* ToDo: should use frame_size computed based on the format and
1034 channel_count here. */
1035 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001036
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001037 /* make sure the size is multiple of 64 */
1038 size += 0x3f;
1039 size &= ~0x3f;
1040
1041 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001042}
1043
1044static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1045{
1046 struct stream_out *out = (struct stream_out *)stream;
1047
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001048 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001049}
1050
1051static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1052{
1053 return -ENOSYS;
1054}
1055
1056static size_t out_get_buffer_size(const struct audio_stream *stream)
1057{
1058 struct stream_out *out = (struct stream_out *)stream;
1059
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001060 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1061 return out->compr_config.fragment_size;
1062 }
1063
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001064 return out->config.period_size * audio_stream_frame_size(stream);
1065}
1066
1067static uint32_t out_get_channels(const struct audio_stream *stream)
1068{
1069 struct stream_out *out = (struct stream_out *)stream;
1070
1071 return out->channel_mask;
1072}
1073
1074static audio_format_t out_get_format(const struct audio_stream *stream)
1075{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001076 struct stream_out *out = (struct stream_out *)stream;
1077
1078 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001079}
1080
1081static int out_set_format(struct audio_stream *stream, audio_format_t format)
1082{
1083 return -ENOSYS;
1084}
1085
1086static int out_standby(struct audio_stream *stream)
1087{
1088 struct stream_out *out = (struct stream_out *)stream;
1089 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001090
Eric Laurent994a6932013-07-17 11:51:42 -07001091 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001092 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001093
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001094 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001095 if (!out->standby) {
1096 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001097 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1098 if (out->pcm) {
1099 pcm_close(out->pcm);
1100 out->pcm = NULL;
1101 }
1102 } else {
1103 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001104 out->gapless_mdata.encoder_delay = 0;
1105 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001106 if (out->compr != NULL) {
1107 compress_close(out->compr);
1108 out->compr = NULL;
1109 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001110 }
1111 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001112 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001113 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001114 }
1115 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001116 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001117 return 0;
1118}
1119
1120static int out_dump(const struct audio_stream *stream, int fd)
1121{
1122 return 0;
1123}
1124
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001125static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1126{
1127 int ret = 0;
1128 char value[32];
1129 struct compr_gapless_mdata tmp_mdata;
1130
1131 if (!out || !parms) {
1132 return -EINVAL;
1133 }
1134
1135 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1136 if (ret >= 0) {
1137 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1138 } else {
1139 return -EINVAL;
1140 }
1141
1142 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1143 if (ret >= 0) {
1144 tmp_mdata.encoder_padding = atoi(value);
1145 } else {
1146 return -EINVAL;
1147 }
1148
1149 out->gapless_mdata = tmp_mdata;
1150 out->send_new_metadata = 1;
1151 ALOGV("%s new encoder delay %u and padding %u", __func__,
1152 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1153
1154 return 0;
1155}
1156
1157
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001158static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1159{
1160 struct stream_out *out = (struct stream_out *)stream;
1161 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001162 struct audio_usecase *usecase;
1163 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001164 struct str_parms *parms;
1165 char value[32];
1166 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001167 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001168
sangwoobc677242013-08-08 16:53:43 +09001169 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001170 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001171 parms = str_parms_create_str(kvpairs);
1172 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1173 if (ret >= 0) {
1174 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001175 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001176 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001177
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001178 /*
1179 * When HDMI cable is unplugged the music playback is paused and
1180 * the policy manager sends routing=0. But the audioflinger
1181 * continues to write data until standby time (3sec).
1182 * As the HDMI core is turned off, the write gets blocked.
1183 * Avoid this by routing audio to speaker until standby.
1184 */
1185 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1186 val == AUDIO_DEVICE_NONE) {
1187 val = AUDIO_DEVICE_OUT_SPEAKER;
1188 }
1189
1190 /*
1191 * select_devices() call below switches all the usecases on the same
1192 * backend to the new device. Refer to check_usecases_codec_backend() in
1193 * the select_devices(). But how do we undo this?
1194 *
1195 * For example, music playback is active on headset (deep-buffer usecase)
1196 * and if we go to ringtones and select a ringtone, low-latency usecase
1197 * will be started on headset+speaker. As we can't enable headset+speaker
1198 * and headset devices at the same time, select_devices() switches the music
1199 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1200 * So when the ringtone playback is completed, how do we undo the same?
1201 *
1202 * We are relying on the out_set_parameters() call on deep-buffer output,
1203 * once the ringtone playback is ended.
1204 * NOTE: We should not check if the current devices are same as new devices.
1205 * Because select_devices() must be called to switch back the music
1206 * playback to headset.
1207 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001208 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001209 out->devices = val;
1210
1211 if (!out->standby)
1212 select_devices(adev, out->usecase);
1213
1214 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1215 (out == adev->primary_output)) {
1216 start_voice_call(adev);
1217 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1218 (out == adev->primary_output)) {
1219 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001220 }
1221 }
1222
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001223 if ((adev->mode != AUDIO_MODE_IN_CALL) && adev->in_call &&
1224 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001225 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001226 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001227
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001228 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001229 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001230 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001231
1232 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1233 parse_compress_metadata(out, parms);
1234 }
1235
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001236 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001237 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001238 return ret;
1239}
1240
1241static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1242{
1243 struct stream_out *out = (struct stream_out *)stream;
1244 struct str_parms *query = str_parms_create_str(keys);
1245 char *str;
1246 char value[256];
1247 struct str_parms *reply = str_parms_create();
1248 size_t i, j;
1249 int ret;
1250 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001251 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001252 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1253 if (ret >= 0) {
1254 value[0] = '\0';
1255 i = 0;
1256 while (out->supported_channel_masks[i] != 0) {
1257 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1258 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1259 if (!first) {
1260 strcat(value, "|");
1261 }
1262 strcat(value, out_channels_name_to_enum_table[j].name);
1263 first = false;
1264 break;
1265 }
1266 }
1267 i++;
1268 }
1269 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1270 str = str_parms_to_str(reply);
1271 } else {
1272 str = strdup(keys);
1273 }
1274 str_parms_destroy(query);
1275 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001276 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277 return str;
1278}
1279
1280static uint32_t out_get_latency(const struct audio_stream_out *stream)
1281{
1282 struct stream_out *out = (struct stream_out *)stream;
1283
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001284 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1285 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1286
1287 return (out->config.period_count * out->config.period_size * 1000) /
1288 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001289}
1290
1291static int out_set_volume(struct audio_stream_out *stream, float left,
1292 float right)
1293{
Eric Laurenta9024de2013-04-04 09:19:12 -07001294 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001295 int volume[2];
1296
Eric Laurenta9024de2013-04-04 09:19:12 -07001297 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1298 /* only take left channel into account: the API is for stereo anyway */
1299 out->muted = (left == 0.0f);
1300 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001301 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1302 const char *mixer_ctl_name = "Compress Playback Volume";
1303 struct audio_device *adev = out->dev;
1304 struct mixer_ctl *ctl;
1305
1306 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1307 if (!ctl) {
1308 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1309 __func__, mixer_ctl_name);
1310 return -EINVAL;
1311 }
1312 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1313 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1314 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1315 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001316 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001317
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001318 return -ENOSYS;
1319}
1320
1321static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1322 size_t bytes)
1323{
1324 struct stream_out *out = (struct stream_out *)stream;
1325 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001326 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001327
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001328 pthread_mutex_lock(&out->lock);
1329 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001330 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001331 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001332 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001333 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001334 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001335 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001336 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337 goto exit;
1338 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001339 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001340
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001341 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001342 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1343 if (out->send_new_metadata) {
1344 ALOGVV("send new gapless metadata");
1345 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1346 out->send_new_metadata = 0;
1347 }
1348
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001349 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001350 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001351 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001352 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1353 }
1354 if (!out->playback_started) {
1355 compress_start(out->compr);
1356 out->playback_started = 1;
1357 out->offload_state = OFFLOAD_STATE_PLAYING;
1358 }
1359 pthread_mutex_unlock(&out->lock);
1360 return ret;
1361 } else {
1362 if (out->pcm) {
1363 if (out->muted)
1364 memset((void *)buffer, 0, bytes);
1365 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1366 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001367 if (ret == 0)
1368 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001369 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001370 }
1371
1372exit:
1373 pthread_mutex_unlock(&out->lock);
1374
1375 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001376 if (out->pcm)
1377 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001378 out_standby(&out->stream.common);
1379 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1380 out_get_sample_rate(&out->stream.common));
1381 }
1382 return bytes;
1383}
1384
1385static int out_get_render_position(const struct audio_stream_out *stream,
1386 uint32_t *dsp_frames)
1387{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001388 struct stream_out *out = (struct stream_out *)stream;
1389 *dsp_frames = 0;
1390 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1391 pthread_mutex_lock(&out->lock);
1392 if (out->compr != NULL) {
1393 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1394 &out->sample_rate);
1395 ALOGVV("%s rendered frames %d sample_rate %d",
1396 __func__, *dsp_frames, out->sample_rate);
1397 }
1398 pthread_mutex_unlock(&out->lock);
1399 return 0;
1400 } else
1401 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001402}
1403
1404static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1405{
1406 return 0;
1407}
1408
1409static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1410{
1411 return 0;
1412}
1413
1414static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1415 int64_t *timestamp)
1416{
1417 return -EINVAL;
1418}
1419
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001420static int out_get_presentation_position(const struct audio_stream_out *stream,
1421 uint64_t *frames, struct timespec *timestamp)
1422{
1423 struct stream_out *out = (struct stream_out *)stream;
1424 int ret = -1;
1425
1426 pthread_mutex_lock(&out->lock);
1427
1428 if (out->pcm) {
1429 size_t avail;
1430 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1431 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
1432 // FIXME This calculation is incorrect if there is buffering after app processor
1433 int64_t signed_frames = out->written - kernel_buffer_size + avail;
1434 // It would be unusual for this value to be negative, but check just in case ...
1435 if (signed_frames >= 0) {
1436 *frames = signed_frames;
1437 ret = 0;
1438 }
1439 }
1440 }
1441
1442 pthread_mutex_unlock(&out->lock);
1443
1444 return ret;
1445}
1446
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001447static int out_set_callback(struct audio_stream_out *stream,
1448 stream_callback_t callback, void *cookie)
1449{
1450 struct stream_out *out = (struct stream_out *)stream;
1451
1452 ALOGV("%s", __func__);
1453 pthread_mutex_lock(&out->lock);
1454 out->offload_callback = callback;
1455 out->offload_cookie = cookie;
1456 pthread_mutex_unlock(&out->lock);
1457 return 0;
1458}
1459
1460static int out_pause(struct audio_stream_out* stream)
1461{
1462 struct stream_out *out = (struct stream_out *)stream;
1463 int status = -ENOSYS;
1464 ALOGV("%s", __func__);
1465 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1466 pthread_mutex_lock(&out->lock);
1467 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1468 status = compress_pause(out->compr);
1469 out->offload_state = OFFLOAD_STATE_PAUSED;
1470 }
1471 pthread_mutex_unlock(&out->lock);
1472 }
1473 return status;
1474}
1475
1476static int out_resume(struct audio_stream_out* stream)
1477{
1478 struct stream_out *out = (struct stream_out *)stream;
1479 int status = -ENOSYS;
1480 ALOGV("%s", __func__);
1481 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1482 status = 0;
1483 pthread_mutex_lock(&out->lock);
1484 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1485 status = compress_resume(out->compr);
1486 out->offload_state = OFFLOAD_STATE_PLAYING;
1487 }
1488 pthread_mutex_unlock(&out->lock);
1489 }
1490 return status;
1491}
1492
1493static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1494{
1495 struct stream_out *out = (struct stream_out *)stream;
1496 int status = -ENOSYS;
1497 ALOGV("%s", __func__);
1498 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1499 pthread_mutex_lock(&out->lock);
1500 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1501 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1502 else
1503 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1504 pthread_mutex_unlock(&out->lock);
1505 }
1506 return status;
1507}
1508
1509static int out_flush(struct audio_stream_out* stream)
1510{
1511 struct stream_out *out = (struct stream_out *)stream;
1512 ALOGV("%s", __func__);
1513 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1514 pthread_mutex_lock(&out->lock);
1515 stop_compressed_output_l(out);
1516 pthread_mutex_unlock(&out->lock);
1517 return 0;
1518 }
1519 return -ENOSYS;
1520}
1521
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001522/** audio_stream_in implementation **/
1523static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1524{
1525 struct stream_in *in = (struct stream_in *)stream;
1526
1527 return in->config.rate;
1528}
1529
1530static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1531{
1532 return -ENOSYS;
1533}
1534
1535static size_t in_get_buffer_size(const struct audio_stream *stream)
1536{
1537 struct stream_in *in = (struct stream_in *)stream;
1538
1539 return in->config.period_size * audio_stream_frame_size(stream);
1540}
1541
1542static uint32_t in_get_channels(const struct audio_stream *stream)
1543{
1544 struct stream_in *in = (struct stream_in *)stream;
1545
1546 return in->channel_mask;
1547}
1548
1549static audio_format_t in_get_format(const struct audio_stream *stream)
1550{
1551 return AUDIO_FORMAT_PCM_16_BIT;
1552}
1553
1554static int in_set_format(struct audio_stream *stream, audio_format_t format)
1555{
1556 return -ENOSYS;
1557}
1558
1559static int in_standby(struct audio_stream *stream)
1560{
1561 struct stream_in *in = (struct stream_in *)stream;
1562 struct audio_device *adev = in->dev;
1563 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001564 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001565 pthread_mutex_lock(&in->lock);
1566 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001567 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001568 if (in->pcm) {
1569 pcm_close(in->pcm);
1570 in->pcm = NULL;
1571 }
1572 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001573 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001574 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001575 }
1576 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001577 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001578 return status;
1579}
1580
1581static int in_dump(const struct audio_stream *stream, int fd)
1582{
1583 return 0;
1584}
1585
1586static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1587{
1588 struct stream_in *in = (struct stream_in *)stream;
1589 struct audio_device *adev = in->dev;
1590 struct str_parms *parms;
1591 char *str;
1592 char value[32];
1593 int ret, val = 0;
1594
Eric Laurent994a6932013-07-17 11:51:42 -07001595 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001596 parms = str_parms_create_str(kvpairs);
1597
1598 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1599
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001600 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001601 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001602 if (ret >= 0) {
1603 val = atoi(value);
1604 /* no audio source uses val == 0 */
1605 if ((in->source != val) && (val != 0)) {
1606 in->source = val;
1607 }
1608 }
1609
1610 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1611 if (ret >= 0) {
1612 val = atoi(value);
1613 if ((in->device != val) && (val != 0)) {
1614 in->device = val;
1615 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001616 if (!in->standby)
1617 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001618 }
1619 }
1620
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001621 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001622 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001623
1624 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001625 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001626 return ret;
1627}
1628
1629static char* in_get_parameters(const struct audio_stream *stream,
1630 const char *keys)
1631{
1632 return strdup("");
1633}
1634
1635static int in_set_gain(struct audio_stream_in *stream, float gain)
1636{
1637 return 0;
1638}
1639
1640static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1641 size_t bytes)
1642{
1643 struct stream_in *in = (struct stream_in *)stream;
1644 struct audio_device *adev = in->dev;
1645 int i, ret = -1;
1646
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001647 pthread_mutex_lock(&in->lock);
1648 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001649 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001650 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001651 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001652 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001653 goto exit;
1654 }
1655 in->standby = 0;
1656 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001657
1658 if (in->pcm) {
1659 ret = pcm_read(in->pcm, buffer, bytes);
1660 }
1661
1662 /*
1663 * Instead of writing zeroes here, we could trust the hardware
1664 * to always provide zeroes when muted.
1665 */
1666 if (ret == 0 && adev->mic_mute)
1667 memset(buffer, 0, bytes);
1668
1669exit:
1670 pthread_mutex_unlock(&in->lock);
1671
1672 if (ret != 0) {
1673 in_standby(&in->stream.common);
1674 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1675 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1676 in_get_sample_rate(&in->stream.common));
1677 }
1678 return bytes;
1679}
1680
1681static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1682{
1683 return 0;
1684}
1685
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001686static int add_remove_audio_effect(const struct audio_stream *stream,
1687 effect_handle_t effect,
1688 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001689{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001690 struct stream_in *in = (struct stream_in *)stream;
1691 int status = 0;
1692 effect_descriptor_t desc;
1693
1694 status = (*effect)->get_descriptor(effect, &desc);
1695 if (status != 0)
1696 return status;
1697
1698 pthread_mutex_lock(&in->lock);
1699 pthread_mutex_lock(&in->dev->lock);
1700 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1701 in->enable_aec != enable &&
1702 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1703 in->enable_aec = enable;
1704 if (!in->standby)
1705 select_devices(in->dev, in->usecase);
1706 }
1707 pthread_mutex_unlock(&in->dev->lock);
1708 pthread_mutex_unlock(&in->lock);
1709
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001710 return 0;
1711}
1712
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001713static int in_add_audio_effect(const struct audio_stream *stream,
1714 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001715{
Eric Laurent994a6932013-07-17 11:51:42 -07001716 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001717 return add_remove_audio_effect(stream, effect, true);
1718}
1719
1720static int in_remove_audio_effect(const struct audio_stream *stream,
1721 effect_handle_t effect)
1722{
Eric Laurent994a6932013-07-17 11:51:42 -07001723 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001724 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001725}
1726
1727static int adev_open_output_stream(struct audio_hw_device *dev,
1728 audio_io_handle_t handle,
1729 audio_devices_t devices,
1730 audio_output_flags_t flags,
1731 struct audio_config *config,
1732 struct audio_stream_out **stream_out)
1733{
1734 struct audio_device *adev = (struct audio_device *)dev;
1735 struct stream_out *out;
1736 int i, ret;
1737
Eric Laurent994a6932013-07-17 11:51:42 -07001738 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001739 __func__, config->sample_rate, config->channel_mask, devices, flags);
1740 *stream_out = NULL;
1741 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1742
1743 if (devices == AUDIO_DEVICE_NONE)
1744 devices = AUDIO_DEVICE_OUT_SPEAKER;
1745
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001746 out->flags = flags;
1747 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001748 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001749 out->format = config->format;
1750 out->sample_rate = config->sample_rate;
1751 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1752 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001753 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001754
1755 /* Init use case and pcm_config */
1756 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1757 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001758 pthread_mutex_lock(&adev->lock);
1759 ret = read_hdmi_channel_masks(out);
1760 pthread_mutex_unlock(&adev->lock);
1761 if (ret != 0) {
1762 /* If HDMI does not support multi channel playback, set the default */
1763 out->config.channels = popcount(out->channel_mask);
Eric Laurentb23d5282013-05-14 15:27:20 -07001764 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001765 goto error_open;
1766 }
1767
1768 if (config->sample_rate == 0)
1769 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1770 if (config->channel_mask == 0)
1771 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1772
1773 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001774 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001775 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1776 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001777 out->config.rate = config->sample_rate;
1778 out->config.channels = popcount(out->channel_mask);
1779 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Eric Laurentb23d5282013-05-14 15:27:20 -07001780 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001781 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1782 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1783 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001784 out->sample_rate = out->config.rate;
1785 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1786 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1787 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1788 ALOGE("%s: Unsupported Offload information", __func__);
1789 ret = -EINVAL;
1790 goto error_open;
1791 }
1792 if (!is_supported_format(config->offload_info.format)) {
1793 ALOGE("%s: Unsupported audio format", __func__);
1794 ret = -EINVAL;
1795 goto error_open;
1796 }
1797
1798 out->compr_config.codec = (struct snd_codec *)
1799 calloc(1, sizeof(struct snd_codec));
1800
1801 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1802 if (config->offload_info.channel_mask)
1803 out->channel_mask = config->offload_info.channel_mask;
1804 else if (config->channel_mask)
1805 out->channel_mask = config->channel_mask;
1806 out->format = config->offload_info.format;
1807 out->sample_rate = config->offload_info.sample_rate;
1808
1809 out->stream.set_callback = out_set_callback;
1810 out->stream.pause = out_pause;
1811 out->stream.resume = out_resume;
1812 out->stream.drain = out_drain;
1813 out->stream.flush = out_flush;
1814
1815 out->compr_config.codec->id =
1816 get_snd_codec_id(config->offload_info.format);
1817 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1818 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1819 out->compr_config.codec->sample_rate =
1820 compress_get_alsa_rate(config->offload_info.sample_rate);
1821 out->compr_config.codec->bit_rate =
1822 config->offload_info.bit_rate;
1823 out->compr_config.codec->ch_in =
1824 popcount(config->channel_mask);
1825 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1826
1827 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1828 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001829
1830 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001831 create_offload_callback_thread(out);
1832 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1833 __func__, config->offload_info.version,
1834 config->offload_info.bit_rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001835 } else {
1836 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1837 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001838 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001839 }
1840
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001841 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1842 if(adev->primary_output == NULL)
1843 adev->primary_output = out;
1844 else {
1845 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001846 ret = -EEXIST;
1847 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001848 }
1849 }
1850
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001851 /* Check if this usecase is already existing */
1852 pthread_mutex_lock(&adev->lock);
1853 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1854 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001855 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001856 ret = -EEXIST;
1857 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001858 }
1859 pthread_mutex_unlock(&adev->lock);
1860
1861 out->stream.common.get_sample_rate = out_get_sample_rate;
1862 out->stream.common.set_sample_rate = out_set_sample_rate;
1863 out->stream.common.get_buffer_size = out_get_buffer_size;
1864 out->stream.common.get_channels = out_get_channels;
1865 out->stream.common.get_format = out_get_format;
1866 out->stream.common.set_format = out_set_format;
1867 out->stream.common.standby = out_standby;
1868 out->stream.common.dump = out_dump;
1869 out->stream.common.set_parameters = out_set_parameters;
1870 out->stream.common.get_parameters = out_get_parameters;
1871 out->stream.common.add_audio_effect = out_add_audio_effect;
1872 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1873 out->stream.get_latency = out_get_latency;
1874 out->stream.set_volume = out_set_volume;
1875 out->stream.write = out_write;
1876 out->stream.get_render_position = out_get_render_position;
1877 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001878 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001879
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001880 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001881 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001882 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001883
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001884 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
1885 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
1886
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001887 config->format = out->stream.common.get_format(&out->stream.common);
1888 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1889 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1890
1891 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001892 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001893 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001894
1895error_open:
1896 free(out);
1897 *stream_out = NULL;
1898 ALOGD("%s: exit: ret %d", __func__, ret);
1899 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001900}
1901
1902static void adev_close_output_stream(struct audio_hw_device *dev,
1903 struct audio_stream_out *stream)
1904{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001905 struct stream_out *out = (struct stream_out *)stream;
1906 struct audio_device *adev = out->dev;
1907
Eric Laurent994a6932013-07-17 11:51:42 -07001908 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001909 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001910 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1911 destroy_offload_callback_thread(out);
1912
1913 if (out->compr_config.codec != NULL)
1914 free(out->compr_config.codec);
1915 }
1916 pthread_cond_destroy(&out->cond);
1917 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001918 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07001919 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001920}
1921
1922static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1923{
1924 struct audio_device *adev = (struct audio_device *)dev;
1925 struct str_parms *parms;
1926 char *str;
1927 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001928 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001929 int ret;
1930
Eric Laurent994a6932013-07-17 11:51:42 -07001931 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001932
1933 parms = str_parms_create_str(kvpairs);
1934 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1935 if (ret >= 0) {
1936 int tty_mode;
1937
1938 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1939 tty_mode = TTY_MODE_OFF;
1940 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1941 tty_mode = TTY_MODE_VCO;
1942 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1943 tty_mode = TTY_MODE_HCO;
1944 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1945 tty_mode = TTY_MODE_FULL;
1946 else
1947 return -EINVAL;
1948
1949 pthread_mutex_lock(&adev->lock);
1950 if (tty_mode != adev->tty_mode) {
1951 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001952 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1953 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001954 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001955 }
1956 pthread_mutex_unlock(&adev->lock);
1957 }
1958
1959 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1960 if (ret >= 0) {
1961 /* When set to false, HAL should disable EC and NS
1962 * But it is currently not supported.
1963 */
1964 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1965 adev->bluetooth_nrec = true;
1966 else
1967 adev->bluetooth_nrec = false;
1968 }
1969
1970 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1971 if (ret >= 0) {
1972 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1973 adev->screen_off = false;
1974 else
1975 adev->screen_off = true;
1976 }
1977
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001978 ret = str_parms_get_int(parms, "rotation", &val);
1979 if (ret >= 0) {
1980 bool reverse_speakers = false;
1981 switch(val) {
1982 // FIXME: note that the code below assumes that the speakers are in the correct placement
1983 // relative to the user when the device is rotated 90deg from its default rotation. This
1984 // assumption is device-specific, not platform-specific like this code.
1985 case 270:
1986 reverse_speakers = true;
1987 break;
1988 case 0:
1989 case 90:
1990 case 180:
1991 break;
1992 default:
1993 ALOGE("%s: unexpected rotation of %d", __func__, val);
1994 }
1995 pthread_mutex_lock(&adev->lock);
1996 if (adev->speaker_lr_swap != reverse_speakers) {
1997 adev->speaker_lr_swap = reverse_speakers;
1998 // only update the selected device if there is active pcm playback
1999 struct audio_usecase *usecase;
2000 struct listnode *node;
2001 list_for_each(node, &adev->usecase_list) {
2002 usecase = node_to_item(node, struct audio_usecase, list);
2003 if (usecase->type == PCM_PLAYBACK) {
2004 select_devices(adev, usecase->id);
2005 break;
2006 }
2007 }
2008 }
2009 pthread_mutex_unlock(&adev->lock);
2010 }
2011
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002012 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002013 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002014 return ret;
2015}
2016
2017static char* adev_get_parameters(const struct audio_hw_device *dev,
2018 const char *keys)
2019{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002020 return strdup("");
2021}
2022
2023static int adev_init_check(const struct audio_hw_device *dev)
2024{
2025 return 0;
2026}
2027
Haynes Mathew George5191a852013-09-11 14:19:36 -07002028/* always called with adev lock held */
2029static int set_voice_volume_l(struct audio_device *adev, float volume)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002030{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002031 int vol, err = 0;
2032
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002033 if (adev->mode == AUDIO_MODE_IN_CALL) {
2034 if (volume < 0.0) {
2035 volume = 0.0;
2036 } else if (volume > 1.0) {
2037 volume = 1.0;
2038 }
2039
2040 vol = lrint(volume * 100.0);
2041
2042 // Voice volume levels from android are mapped to driver volume levels as follows.
2043 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
2044 // So adjust the volume to get the correct volume index in driver
2045 vol = 100 - vol;
Eric Laurentb23d5282013-05-14 15:27:20 -07002046
2047 err = platform_set_voice_volume(adev->platform, vol);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002048 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002049 return err;
2050}
2051
Haynes Mathew George5191a852013-09-11 14:19:36 -07002052static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2053{
2054 int ret;
2055 struct audio_device *adev = (struct audio_device *)dev;
2056 pthread_mutex_lock(&adev->lock);
2057 /* cache volume */
2058 adev->voice_volume = volume;
2059 ret = set_voice_volume_l(adev, adev->voice_volume);
2060 pthread_mutex_unlock(&adev->lock);
2061 return ret;
2062}
2063
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002064static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2065{
2066 return -ENOSYS;
2067}
2068
2069static int adev_get_master_volume(struct audio_hw_device *dev,
2070 float *volume)
2071{
2072 return -ENOSYS;
2073}
2074
2075static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2076{
2077 return -ENOSYS;
2078}
2079
2080static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2081{
2082 return -ENOSYS;
2083}
2084
2085static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2086{
2087 struct audio_device *adev = (struct audio_device *)dev;
2088
2089 pthread_mutex_lock(&adev->lock);
2090 if (adev->mode != mode) {
2091 adev->mode = mode;
2092 }
2093 pthread_mutex_unlock(&adev->lock);
2094 return 0;
2095}
2096
2097static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2098{
2099 struct audio_device *adev = (struct audio_device *)dev;
2100 int err = 0;
2101
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002102 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002103 adev->mic_mute = state;
Eric Laurentb23d5282013-05-14 15:27:20 -07002104
2105 err = platform_set_mic_mute(adev->platform, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002106 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002107 return err;
2108}
2109
2110static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2111{
2112 struct audio_device *adev = (struct audio_device *)dev;
2113
2114 *state = adev->mic_mute;
2115
2116 return 0;
2117}
2118
2119static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2120 const struct audio_config *config)
2121{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002122 int channel_count = popcount(config->channel_mask);
2123
2124 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2125}
2126
2127static int adev_open_input_stream(struct audio_hw_device *dev,
2128 audio_io_handle_t handle,
2129 audio_devices_t devices,
2130 struct audio_config *config,
2131 struct audio_stream_in **stream_in)
2132{
2133 struct audio_device *adev = (struct audio_device *)dev;
2134 struct stream_in *in;
2135 int ret, buffer_size, frame_size;
2136 int channel_count = popcount(config->channel_mask);
2137
Eric Laurent994a6932013-07-17 11:51:42 -07002138 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002139 *stream_in = NULL;
2140 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2141 return -EINVAL;
2142
2143 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2144
2145 in->stream.common.get_sample_rate = in_get_sample_rate;
2146 in->stream.common.set_sample_rate = in_set_sample_rate;
2147 in->stream.common.get_buffer_size = in_get_buffer_size;
2148 in->stream.common.get_channels = in_get_channels;
2149 in->stream.common.get_format = in_get_format;
2150 in->stream.common.set_format = in_set_format;
2151 in->stream.common.standby = in_standby;
2152 in->stream.common.dump = in_dump;
2153 in->stream.common.set_parameters = in_set_parameters;
2154 in->stream.common.get_parameters = in_get_parameters;
2155 in->stream.common.add_audio_effect = in_add_audio_effect;
2156 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2157 in->stream.set_gain = in_set_gain;
2158 in->stream.read = in_read;
2159 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2160
2161 in->device = devices;
2162 in->source = AUDIO_SOURCE_DEFAULT;
2163 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002164 in->standby = 1;
2165 in->channel_mask = config->channel_mask;
2166
2167 /* Update config params with the requested sample rate and channels */
2168 in->usecase = USECASE_AUDIO_RECORD;
2169 in->config = pcm_config_audio_capture;
2170 in->config.channels = channel_count;
2171 in->config.rate = config->sample_rate;
2172
2173 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2174 buffer_size = get_input_buffer_size(config->sample_rate,
2175 config->format,
2176 channel_count);
2177 in->config.period_size = buffer_size / frame_size;
2178
2179 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002180 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002181 return 0;
2182
2183err_open:
2184 free(in);
2185 *stream_in = NULL;
2186 return ret;
2187}
2188
2189static void adev_close_input_stream(struct audio_hw_device *dev,
2190 struct audio_stream_in *stream)
2191{
Eric Laurent994a6932013-07-17 11:51:42 -07002192 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002193
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002194 in_standby(&stream->common);
2195 free(stream);
2196
2197 return;
2198}
2199
2200static int adev_dump(const audio_hw_device_t *device, int fd)
2201{
2202 return 0;
2203}
2204
2205static int adev_close(hw_device_t *device)
2206{
2207 struct audio_device *adev = (struct audio_device *)device;
2208 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002209 free(adev->snd_dev_ref_cnt);
2210 platform_deinit(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002211 free(device);
2212 return 0;
2213}
2214
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002215static int adev_open(const hw_module_t *module, const char *name,
2216 hw_device_t **device)
2217{
2218 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002219 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002220
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002221 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002222 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2223
2224 adev = calloc(1, sizeof(struct audio_device));
2225
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002226 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2227 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2228 adev->device.common.module = (struct hw_module_t *)module;
2229 adev->device.common.close = adev_close;
2230
2231 adev->device.init_check = adev_init_check;
2232 adev->device.set_voice_volume = adev_set_voice_volume;
2233 adev->device.set_master_volume = adev_set_master_volume;
2234 adev->device.get_master_volume = adev_get_master_volume;
2235 adev->device.set_master_mute = adev_set_master_mute;
2236 adev->device.get_master_mute = adev_get_master_mute;
2237 adev->device.set_mode = adev_set_mode;
2238 adev->device.set_mic_mute = adev_set_mic_mute;
2239 adev->device.get_mic_mute = adev_get_mic_mute;
2240 adev->device.set_parameters = adev_set_parameters;
2241 adev->device.get_parameters = adev_get_parameters;
2242 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2243 adev->device.open_output_stream = adev_open_output_stream;
2244 adev->device.close_output_stream = adev_close_output_stream;
2245 adev->device.open_input_stream = adev_open_input_stream;
2246 adev->device.close_input_stream = adev_close_input_stream;
2247 adev->device.dump = adev_dump;
2248
2249 /* Set the default route before the PCM stream is opened */
2250 pthread_mutex_lock(&adev->lock);
2251 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002252 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002253 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002254 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002255 adev->voice_call_rx = NULL;
2256 adev->voice_call_tx = NULL;
2257 adev->voice_volume = 1.0f;
2258 adev->tty_mode = TTY_MODE_OFF;
2259 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002260 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002261 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurentb23d5282013-05-14 15:27:20 -07002262 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002263 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002264 pthread_mutex_unlock(&adev->lock);
2265
2266 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002267 adev->platform = platform_init(adev);
2268 if (!adev->platform) {
2269 free(adev->snd_dev_ref_cnt);
2270 free(adev);
2271 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2272 *device = NULL;
2273 return -EINVAL;
2274 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002275
2276 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2277 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2278 if (adev->visualizer_lib == NULL) {
2279 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2280 } else {
2281 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2282 adev->visualizer_start_output =
2283 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2284 "visualizer_hal_start_output");
2285 adev->visualizer_stop_output =
2286 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2287 "visualizer_hal_stop_output");
2288 }
2289 }
2290
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002291 *device = &adev->device.common;
2292
Eric Laurent994a6932013-07-17 11:51:42 -07002293 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002294 return 0;
2295}
2296
2297static struct hw_module_methods_t hal_module_methods = {
2298 .open = adev_open,
2299};
2300
2301struct audio_module HAL_MODULE_INFO_SYM = {
2302 .common = {
2303 .tag = HARDWARE_MODULE_TAG,
2304 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2305 .hal_api_version = HARDWARE_HAL_API_VERSION,
2306 .id = AUDIO_HARDWARE_MODULE_ID,
2307 .name = "QCOM Audio HAL",
2308 .author = "Code Aurora Forum",
2309 .methods = &hal_module_methods,
2310 },
2311};