blob: c89d88ead30f2936f33c6eebcb67baccffc60f89 [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;
Eric Laurent949a0892013-09-20 09:20:13 -07001425 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001426
1427 pthread_mutex_lock(&out->lock);
1428
Eric Laurent949a0892013-09-20 09:20:13 -07001429 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1430 if (out->compr != NULL) {
1431 compress_get_tstamp(out->compr, &dsp_frames,
1432 &out->sample_rate);
1433 ALOGVV("%s rendered frames %ld sample_rate %d",
1434 __func__, dsp_frames, out->sample_rate);
1435 *frames = dsp_frames;
1436 ret = 0;
1437 /* this is the best we can do */
1438 clock_gettime(CLOCK_MONOTONIC, timestamp);
1439 }
1440 } else {
1441 if (out->pcm) {
1442 size_t avail;
1443 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1444 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001445 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001446 // This adjustment accounts for buffering after app processor.
1447 // It is based on estimated DSP latency per use case, rather than exact.
1448 signed_frames -=
1449 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1450
Eric Laurent949a0892013-09-20 09:20:13 -07001451 // It would be unusual for this value to be negative, but check just in case ...
1452 if (signed_frames >= 0) {
1453 *frames = signed_frames;
1454 ret = 0;
1455 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001456 }
1457 }
1458 }
1459
1460 pthread_mutex_unlock(&out->lock);
1461
1462 return ret;
1463}
1464
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001465static int out_set_callback(struct audio_stream_out *stream,
1466 stream_callback_t callback, void *cookie)
1467{
1468 struct stream_out *out = (struct stream_out *)stream;
1469
1470 ALOGV("%s", __func__);
1471 pthread_mutex_lock(&out->lock);
1472 out->offload_callback = callback;
1473 out->offload_cookie = cookie;
1474 pthread_mutex_unlock(&out->lock);
1475 return 0;
1476}
1477
1478static int out_pause(struct audio_stream_out* stream)
1479{
1480 struct stream_out *out = (struct stream_out *)stream;
1481 int status = -ENOSYS;
1482 ALOGV("%s", __func__);
1483 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1484 pthread_mutex_lock(&out->lock);
1485 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1486 status = compress_pause(out->compr);
1487 out->offload_state = OFFLOAD_STATE_PAUSED;
1488 }
1489 pthread_mutex_unlock(&out->lock);
1490 }
1491 return status;
1492}
1493
1494static int out_resume(struct audio_stream_out* stream)
1495{
1496 struct stream_out *out = (struct stream_out *)stream;
1497 int status = -ENOSYS;
1498 ALOGV("%s", __func__);
1499 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1500 status = 0;
1501 pthread_mutex_lock(&out->lock);
1502 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1503 status = compress_resume(out->compr);
1504 out->offload_state = OFFLOAD_STATE_PLAYING;
1505 }
1506 pthread_mutex_unlock(&out->lock);
1507 }
1508 return status;
1509}
1510
1511static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1512{
1513 struct stream_out *out = (struct stream_out *)stream;
1514 int status = -ENOSYS;
1515 ALOGV("%s", __func__);
1516 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1517 pthread_mutex_lock(&out->lock);
1518 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1519 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1520 else
1521 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1522 pthread_mutex_unlock(&out->lock);
1523 }
1524 return status;
1525}
1526
1527static int out_flush(struct audio_stream_out* stream)
1528{
1529 struct stream_out *out = (struct stream_out *)stream;
1530 ALOGV("%s", __func__);
1531 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1532 pthread_mutex_lock(&out->lock);
1533 stop_compressed_output_l(out);
1534 pthread_mutex_unlock(&out->lock);
1535 return 0;
1536 }
1537 return -ENOSYS;
1538}
1539
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001540/** audio_stream_in implementation **/
1541static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1542{
1543 struct stream_in *in = (struct stream_in *)stream;
1544
1545 return in->config.rate;
1546}
1547
1548static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1549{
1550 return -ENOSYS;
1551}
1552
1553static size_t in_get_buffer_size(const struct audio_stream *stream)
1554{
1555 struct stream_in *in = (struct stream_in *)stream;
1556
1557 return in->config.period_size * audio_stream_frame_size(stream);
1558}
1559
1560static uint32_t in_get_channels(const struct audio_stream *stream)
1561{
1562 struct stream_in *in = (struct stream_in *)stream;
1563
1564 return in->channel_mask;
1565}
1566
1567static audio_format_t in_get_format(const struct audio_stream *stream)
1568{
1569 return AUDIO_FORMAT_PCM_16_BIT;
1570}
1571
1572static int in_set_format(struct audio_stream *stream, audio_format_t format)
1573{
1574 return -ENOSYS;
1575}
1576
1577static int in_standby(struct audio_stream *stream)
1578{
1579 struct stream_in *in = (struct stream_in *)stream;
1580 struct audio_device *adev = in->dev;
1581 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001582 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001583 pthread_mutex_lock(&in->lock);
1584 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001585 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001586 if (in->pcm) {
1587 pcm_close(in->pcm);
1588 in->pcm = NULL;
1589 }
1590 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001591 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001592 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001593 }
1594 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001595 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001596 return status;
1597}
1598
1599static int in_dump(const struct audio_stream *stream, int fd)
1600{
1601 return 0;
1602}
1603
1604static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1605{
1606 struct stream_in *in = (struct stream_in *)stream;
1607 struct audio_device *adev = in->dev;
1608 struct str_parms *parms;
1609 char *str;
1610 char value[32];
1611 int ret, val = 0;
1612
Eric Laurent994a6932013-07-17 11:51:42 -07001613 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001614 parms = str_parms_create_str(kvpairs);
1615
1616 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1617
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001618 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001619 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001620 if (ret >= 0) {
1621 val = atoi(value);
1622 /* no audio source uses val == 0 */
1623 if ((in->source != val) && (val != 0)) {
1624 in->source = val;
1625 }
1626 }
1627
1628 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1629 if (ret >= 0) {
1630 val = atoi(value);
1631 if ((in->device != val) && (val != 0)) {
1632 in->device = val;
1633 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001634 if (!in->standby)
1635 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001636 }
1637 }
1638
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001639 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001640 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001641
1642 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001643 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001644 return ret;
1645}
1646
1647static char* in_get_parameters(const struct audio_stream *stream,
1648 const char *keys)
1649{
1650 return strdup("");
1651}
1652
1653static int in_set_gain(struct audio_stream_in *stream, float gain)
1654{
1655 return 0;
1656}
1657
1658static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1659 size_t bytes)
1660{
1661 struct stream_in *in = (struct stream_in *)stream;
1662 struct audio_device *adev = in->dev;
1663 int i, ret = -1;
1664
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001665 pthread_mutex_lock(&in->lock);
1666 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001667 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001668 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001669 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001670 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001671 goto exit;
1672 }
1673 in->standby = 0;
1674 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001675
1676 if (in->pcm) {
1677 ret = pcm_read(in->pcm, buffer, bytes);
1678 }
1679
1680 /*
1681 * Instead of writing zeroes here, we could trust the hardware
1682 * to always provide zeroes when muted.
1683 */
1684 if (ret == 0 && adev->mic_mute)
1685 memset(buffer, 0, bytes);
1686
1687exit:
1688 pthread_mutex_unlock(&in->lock);
1689
1690 if (ret != 0) {
1691 in_standby(&in->stream.common);
1692 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1693 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1694 in_get_sample_rate(&in->stream.common));
1695 }
1696 return bytes;
1697}
1698
1699static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1700{
1701 return 0;
1702}
1703
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001704static int add_remove_audio_effect(const struct audio_stream *stream,
1705 effect_handle_t effect,
1706 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001707{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001708 struct stream_in *in = (struct stream_in *)stream;
1709 int status = 0;
1710 effect_descriptor_t desc;
1711
1712 status = (*effect)->get_descriptor(effect, &desc);
1713 if (status != 0)
1714 return status;
1715
1716 pthread_mutex_lock(&in->lock);
1717 pthread_mutex_lock(&in->dev->lock);
1718 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1719 in->enable_aec != enable &&
1720 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1721 in->enable_aec = enable;
1722 if (!in->standby)
1723 select_devices(in->dev, in->usecase);
1724 }
1725 pthread_mutex_unlock(&in->dev->lock);
1726 pthread_mutex_unlock(&in->lock);
1727
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001728 return 0;
1729}
1730
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001731static int in_add_audio_effect(const struct audio_stream *stream,
1732 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001733{
Eric Laurent994a6932013-07-17 11:51:42 -07001734 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001735 return add_remove_audio_effect(stream, effect, true);
1736}
1737
1738static int in_remove_audio_effect(const struct audio_stream *stream,
1739 effect_handle_t effect)
1740{
Eric Laurent994a6932013-07-17 11:51:42 -07001741 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001742 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001743}
1744
1745static int adev_open_output_stream(struct audio_hw_device *dev,
1746 audio_io_handle_t handle,
1747 audio_devices_t devices,
1748 audio_output_flags_t flags,
1749 struct audio_config *config,
1750 struct audio_stream_out **stream_out)
1751{
1752 struct audio_device *adev = (struct audio_device *)dev;
1753 struct stream_out *out;
1754 int i, ret;
1755
Eric Laurent994a6932013-07-17 11:51:42 -07001756 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001757 __func__, config->sample_rate, config->channel_mask, devices, flags);
1758 *stream_out = NULL;
1759 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1760
1761 if (devices == AUDIO_DEVICE_NONE)
1762 devices = AUDIO_DEVICE_OUT_SPEAKER;
1763
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001764 out->flags = flags;
1765 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001766 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001767 out->format = config->format;
1768 out->sample_rate = config->sample_rate;
1769 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1770 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001771 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001772
1773 /* Init use case and pcm_config */
1774 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1775 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001776 pthread_mutex_lock(&adev->lock);
1777 ret = read_hdmi_channel_masks(out);
1778 pthread_mutex_unlock(&adev->lock);
1779 if (ret != 0) {
1780 /* If HDMI does not support multi channel playback, set the default */
1781 out->config.channels = popcount(out->channel_mask);
Eric Laurentb23d5282013-05-14 15:27:20 -07001782 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001783 goto error_open;
1784 }
1785
1786 if (config->sample_rate == 0)
1787 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1788 if (config->channel_mask == 0)
1789 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1790
1791 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001792 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001793 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1794 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001795 out->config.rate = config->sample_rate;
1796 out->config.channels = popcount(out->channel_mask);
1797 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Eric Laurentb23d5282013-05-14 15:27:20 -07001798 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001799 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1800 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1801 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001802 out->sample_rate = out->config.rate;
1803 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1804 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1805 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1806 ALOGE("%s: Unsupported Offload information", __func__);
1807 ret = -EINVAL;
1808 goto error_open;
1809 }
1810 if (!is_supported_format(config->offload_info.format)) {
1811 ALOGE("%s: Unsupported audio format", __func__);
1812 ret = -EINVAL;
1813 goto error_open;
1814 }
1815
1816 out->compr_config.codec = (struct snd_codec *)
1817 calloc(1, sizeof(struct snd_codec));
1818
1819 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1820 if (config->offload_info.channel_mask)
1821 out->channel_mask = config->offload_info.channel_mask;
1822 else if (config->channel_mask)
1823 out->channel_mask = config->channel_mask;
1824 out->format = config->offload_info.format;
1825 out->sample_rate = config->offload_info.sample_rate;
1826
1827 out->stream.set_callback = out_set_callback;
1828 out->stream.pause = out_pause;
1829 out->stream.resume = out_resume;
1830 out->stream.drain = out_drain;
1831 out->stream.flush = out_flush;
1832
1833 out->compr_config.codec->id =
1834 get_snd_codec_id(config->offload_info.format);
1835 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1836 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1837 out->compr_config.codec->sample_rate =
1838 compress_get_alsa_rate(config->offload_info.sample_rate);
1839 out->compr_config.codec->bit_rate =
1840 config->offload_info.bit_rate;
1841 out->compr_config.codec->ch_in =
1842 popcount(config->channel_mask);
1843 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1844
1845 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1846 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001847
1848 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001849 create_offload_callback_thread(out);
1850 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1851 __func__, config->offload_info.version,
1852 config->offload_info.bit_rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001853 } else {
1854 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1855 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001856 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001857 }
1858
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001859 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1860 if(adev->primary_output == NULL)
1861 adev->primary_output = out;
1862 else {
1863 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001864 ret = -EEXIST;
1865 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001866 }
1867 }
1868
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001869 /* Check if this usecase is already existing */
1870 pthread_mutex_lock(&adev->lock);
1871 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1872 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001873 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001874 ret = -EEXIST;
1875 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001876 }
1877 pthread_mutex_unlock(&adev->lock);
1878
1879 out->stream.common.get_sample_rate = out_get_sample_rate;
1880 out->stream.common.set_sample_rate = out_set_sample_rate;
1881 out->stream.common.get_buffer_size = out_get_buffer_size;
1882 out->stream.common.get_channels = out_get_channels;
1883 out->stream.common.get_format = out_get_format;
1884 out->stream.common.set_format = out_set_format;
1885 out->stream.common.standby = out_standby;
1886 out->stream.common.dump = out_dump;
1887 out->stream.common.set_parameters = out_set_parameters;
1888 out->stream.common.get_parameters = out_get_parameters;
1889 out->stream.common.add_audio_effect = out_add_audio_effect;
1890 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1891 out->stream.get_latency = out_get_latency;
1892 out->stream.set_volume = out_set_volume;
1893 out->stream.write = out_write;
1894 out->stream.get_render_position = out_get_render_position;
1895 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001896 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001897
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001898 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001899 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001900 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001901
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001902 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
1903 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
1904
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001905 config->format = out->stream.common.get_format(&out->stream.common);
1906 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1907 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1908
1909 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001910 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001911 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001912
1913error_open:
1914 free(out);
1915 *stream_out = NULL;
1916 ALOGD("%s: exit: ret %d", __func__, ret);
1917 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001918}
1919
1920static void adev_close_output_stream(struct audio_hw_device *dev,
1921 struct audio_stream_out *stream)
1922{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001923 struct stream_out *out = (struct stream_out *)stream;
1924 struct audio_device *adev = out->dev;
1925
Eric Laurent994a6932013-07-17 11:51:42 -07001926 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001927 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001928 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1929 destroy_offload_callback_thread(out);
1930
1931 if (out->compr_config.codec != NULL)
1932 free(out->compr_config.codec);
1933 }
1934 pthread_cond_destroy(&out->cond);
1935 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001936 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07001937 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001938}
1939
1940static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1941{
1942 struct audio_device *adev = (struct audio_device *)dev;
1943 struct str_parms *parms;
1944 char *str;
1945 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001946 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001947 int ret;
1948
Eric Laurent994a6932013-07-17 11:51:42 -07001949 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001950
1951 parms = str_parms_create_str(kvpairs);
1952 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
1953 if (ret >= 0) {
1954 int tty_mode;
1955
1956 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
1957 tty_mode = TTY_MODE_OFF;
1958 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
1959 tty_mode = TTY_MODE_VCO;
1960 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
1961 tty_mode = TTY_MODE_HCO;
1962 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
1963 tty_mode = TTY_MODE_FULL;
1964 else
1965 return -EINVAL;
1966
1967 pthread_mutex_lock(&adev->lock);
1968 if (tty_mode != adev->tty_mode) {
1969 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001970 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
1971 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001972 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001973 }
1974 pthread_mutex_unlock(&adev->lock);
1975 }
1976
1977 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1978 if (ret >= 0) {
1979 /* When set to false, HAL should disable EC and NS
1980 * But it is currently not supported.
1981 */
1982 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1983 adev->bluetooth_nrec = true;
1984 else
1985 adev->bluetooth_nrec = false;
1986 }
1987
1988 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1989 if (ret >= 0) {
1990 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1991 adev->screen_off = false;
1992 else
1993 adev->screen_off = true;
1994 }
1995
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001996 ret = str_parms_get_int(parms, "rotation", &val);
1997 if (ret >= 0) {
1998 bool reverse_speakers = false;
1999 switch(val) {
2000 // FIXME: note that the code below assumes that the speakers are in the correct placement
2001 // relative to the user when the device is rotated 90deg from its default rotation. This
2002 // assumption is device-specific, not platform-specific like this code.
2003 case 270:
2004 reverse_speakers = true;
2005 break;
2006 case 0:
2007 case 90:
2008 case 180:
2009 break;
2010 default:
2011 ALOGE("%s: unexpected rotation of %d", __func__, val);
2012 }
2013 pthread_mutex_lock(&adev->lock);
2014 if (adev->speaker_lr_swap != reverse_speakers) {
2015 adev->speaker_lr_swap = reverse_speakers;
2016 // only update the selected device if there is active pcm playback
2017 struct audio_usecase *usecase;
2018 struct listnode *node;
2019 list_for_each(node, &adev->usecase_list) {
2020 usecase = node_to_item(node, struct audio_usecase, list);
2021 if (usecase->type == PCM_PLAYBACK) {
2022 select_devices(adev, usecase->id);
2023 break;
2024 }
2025 }
2026 }
2027 pthread_mutex_unlock(&adev->lock);
2028 }
2029
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002030 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002031 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002032 return ret;
2033}
2034
2035static char* adev_get_parameters(const struct audio_hw_device *dev,
2036 const char *keys)
2037{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002038 return strdup("");
2039}
2040
2041static int adev_init_check(const struct audio_hw_device *dev)
2042{
2043 return 0;
2044}
2045
Haynes Mathew George5191a852013-09-11 14:19:36 -07002046/* always called with adev lock held */
2047static int set_voice_volume_l(struct audio_device *adev, float volume)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002048{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002049 int vol, err = 0;
2050
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002051 if (adev->mode == AUDIO_MODE_IN_CALL) {
2052 if (volume < 0.0) {
2053 volume = 0.0;
2054 } else if (volume > 1.0) {
2055 volume = 1.0;
2056 }
2057
2058 vol = lrint(volume * 100.0);
2059
2060 // Voice volume levels from android are mapped to driver volume levels as follows.
2061 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
2062 // So adjust the volume to get the correct volume index in driver
2063 vol = 100 - vol;
Eric Laurentb23d5282013-05-14 15:27:20 -07002064
2065 err = platform_set_voice_volume(adev->platform, vol);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002066 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002067 return err;
2068}
2069
Haynes Mathew George5191a852013-09-11 14:19:36 -07002070static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2071{
2072 int ret;
2073 struct audio_device *adev = (struct audio_device *)dev;
2074 pthread_mutex_lock(&adev->lock);
2075 /* cache volume */
2076 adev->voice_volume = volume;
2077 ret = set_voice_volume_l(adev, adev->voice_volume);
2078 pthread_mutex_unlock(&adev->lock);
2079 return ret;
2080}
2081
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002082static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2083{
2084 return -ENOSYS;
2085}
2086
2087static int adev_get_master_volume(struct audio_hw_device *dev,
2088 float *volume)
2089{
2090 return -ENOSYS;
2091}
2092
2093static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2094{
2095 return -ENOSYS;
2096}
2097
2098static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2099{
2100 return -ENOSYS;
2101}
2102
2103static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2104{
2105 struct audio_device *adev = (struct audio_device *)dev;
2106
2107 pthread_mutex_lock(&adev->lock);
2108 if (adev->mode != mode) {
2109 adev->mode = mode;
2110 }
2111 pthread_mutex_unlock(&adev->lock);
2112 return 0;
2113}
2114
2115static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2116{
2117 struct audio_device *adev = (struct audio_device *)dev;
2118 int err = 0;
2119
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002120 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002121 adev->mic_mute = state;
Eric Laurentb23d5282013-05-14 15:27:20 -07002122
2123 err = platform_set_mic_mute(adev->platform, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002124 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002125 return err;
2126}
2127
2128static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2129{
2130 struct audio_device *adev = (struct audio_device *)dev;
2131
2132 *state = adev->mic_mute;
2133
2134 return 0;
2135}
2136
2137static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2138 const struct audio_config *config)
2139{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002140 int channel_count = popcount(config->channel_mask);
2141
2142 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2143}
2144
2145static int adev_open_input_stream(struct audio_hw_device *dev,
2146 audio_io_handle_t handle,
2147 audio_devices_t devices,
2148 struct audio_config *config,
2149 struct audio_stream_in **stream_in)
2150{
2151 struct audio_device *adev = (struct audio_device *)dev;
2152 struct stream_in *in;
2153 int ret, buffer_size, frame_size;
2154 int channel_count = popcount(config->channel_mask);
2155
Eric Laurent994a6932013-07-17 11:51:42 -07002156 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002157 *stream_in = NULL;
2158 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2159 return -EINVAL;
2160
2161 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2162
2163 in->stream.common.get_sample_rate = in_get_sample_rate;
2164 in->stream.common.set_sample_rate = in_set_sample_rate;
2165 in->stream.common.get_buffer_size = in_get_buffer_size;
2166 in->stream.common.get_channels = in_get_channels;
2167 in->stream.common.get_format = in_get_format;
2168 in->stream.common.set_format = in_set_format;
2169 in->stream.common.standby = in_standby;
2170 in->stream.common.dump = in_dump;
2171 in->stream.common.set_parameters = in_set_parameters;
2172 in->stream.common.get_parameters = in_get_parameters;
2173 in->stream.common.add_audio_effect = in_add_audio_effect;
2174 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2175 in->stream.set_gain = in_set_gain;
2176 in->stream.read = in_read;
2177 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2178
2179 in->device = devices;
2180 in->source = AUDIO_SOURCE_DEFAULT;
2181 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002182 in->standby = 1;
2183 in->channel_mask = config->channel_mask;
2184
2185 /* Update config params with the requested sample rate and channels */
2186 in->usecase = USECASE_AUDIO_RECORD;
2187 in->config = pcm_config_audio_capture;
2188 in->config.channels = channel_count;
2189 in->config.rate = config->sample_rate;
2190
2191 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2192 buffer_size = get_input_buffer_size(config->sample_rate,
2193 config->format,
2194 channel_count);
2195 in->config.period_size = buffer_size / frame_size;
2196
2197 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002198 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002199 return 0;
2200
2201err_open:
2202 free(in);
2203 *stream_in = NULL;
2204 return ret;
2205}
2206
2207static void adev_close_input_stream(struct audio_hw_device *dev,
2208 struct audio_stream_in *stream)
2209{
Eric Laurent994a6932013-07-17 11:51:42 -07002210 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002211
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002212 in_standby(&stream->common);
2213 free(stream);
2214
2215 return;
2216}
2217
2218static int adev_dump(const audio_hw_device_t *device, int fd)
2219{
2220 return 0;
2221}
2222
2223static int adev_close(hw_device_t *device)
2224{
2225 struct audio_device *adev = (struct audio_device *)device;
2226 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002227 free(adev->snd_dev_ref_cnt);
2228 platform_deinit(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002229 free(device);
2230 return 0;
2231}
2232
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002233static int adev_open(const hw_module_t *module, const char *name,
2234 hw_device_t **device)
2235{
2236 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002237 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002238
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002239 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002240 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2241
2242 adev = calloc(1, sizeof(struct audio_device));
2243
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002244 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2245 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2246 adev->device.common.module = (struct hw_module_t *)module;
2247 adev->device.common.close = adev_close;
2248
2249 adev->device.init_check = adev_init_check;
2250 adev->device.set_voice_volume = adev_set_voice_volume;
2251 adev->device.set_master_volume = adev_set_master_volume;
2252 adev->device.get_master_volume = adev_get_master_volume;
2253 adev->device.set_master_mute = adev_set_master_mute;
2254 adev->device.get_master_mute = adev_get_master_mute;
2255 adev->device.set_mode = adev_set_mode;
2256 adev->device.set_mic_mute = adev_set_mic_mute;
2257 adev->device.get_mic_mute = adev_get_mic_mute;
2258 adev->device.set_parameters = adev_set_parameters;
2259 adev->device.get_parameters = adev_get_parameters;
2260 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2261 adev->device.open_output_stream = adev_open_output_stream;
2262 adev->device.close_output_stream = adev_close_output_stream;
2263 adev->device.open_input_stream = adev_open_input_stream;
2264 adev->device.close_input_stream = adev_close_input_stream;
2265 adev->device.dump = adev_dump;
2266
2267 /* Set the default route before the PCM stream is opened */
2268 pthread_mutex_lock(&adev->lock);
2269 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002270 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002271 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002272 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002273 adev->voice_call_rx = NULL;
2274 adev->voice_call_tx = NULL;
2275 adev->voice_volume = 1.0f;
2276 adev->tty_mode = TTY_MODE_OFF;
2277 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002278 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002279 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurentb23d5282013-05-14 15:27:20 -07002280 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002281 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002282 pthread_mutex_unlock(&adev->lock);
2283
2284 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002285 adev->platform = platform_init(adev);
2286 if (!adev->platform) {
2287 free(adev->snd_dev_ref_cnt);
2288 free(adev);
2289 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2290 *device = NULL;
2291 return -EINVAL;
2292 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002293
2294 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2295 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2296 if (adev->visualizer_lib == NULL) {
2297 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2298 } else {
2299 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2300 adev->visualizer_start_output =
2301 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2302 "visualizer_hal_start_output");
2303 adev->visualizer_stop_output =
2304 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2305 "visualizer_hal_stop_output");
2306 }
2307 }
2308
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002309 *device = &adev->device.common;
2310
Eric Laurent994a6932013-07-17 11:51:42 -07002311 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002312 return 0;
2313}
2314
2315static struct hw_module_methods_t hal_module_methods = {
2316 .open = adev_open,
2317};
2318
2319struct audio_module HAL_MODULE_INFO_SYM = {
2320 .common = {
2321 .tag = HARDWARE_MODULE_TAG,
2322 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2323 .hal_api_version = HARDWARE_HAL_API_VERSION,
2324 .id = AUDIO_HARDWARE_MODULE_ID,
2325 .name = "QCOM Audio HAL",
2326 .author = "Code Aurora Forum",
2327 .methods = &hal_module_methods,
2328 },
2329};