blob: 08d9c32c138563f3889738b4fba4cae8c2cf2447 [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
Eric Laurent07eeafd2013-10-06 12:52:49 -0700786static bool allow_hdmi_channel_config(struct audio_device *adev)
787{
788 struct listnode *node;
789 struct audio_usecase *usecase;
790 bool ret = true;
791
792 list_for_each(node, &adev->usecase_list) {
793 usecase = node_to_item(node, struct audio_usecase, list);
794 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
795 /*
796 * If voice call is already existing, do not proceed further to avoid
797 * disabling/enabling both RX and TX devices, CSD calls, etc.
798 * Once the voice call done, the HDMI channels can be configured to
799 * max channels of remaining use cases.
800 */
801 if (usecase->id == USECASE_VOICE_CALL) {
802 ALOGD("%s: voice call is active, no change in HDMI channels",
803 __func__);
804 ret = false;
805 break;
806 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
807 ALOGD("%s: multi channel playback is active, "
808 "no change in HDMI channels", __func__);
809 ret = false;
810 break;
811 }
812 }
813 }
814 return ret;
815}
816
817static int check_and_set_hdmi_channels(struct audio_device *adev,
818 unsigned int channels)
819{
820 struct listnode *node;
821 struct audio_usecase *usecase;
822
823 /* Check if change in HDMI channel config is allowed */
824 if (!allow_hdmi_channel_config(adev))
825 return 0;
826
827 if (channels == adev->cur_hdmi_channels) {
828 ALOGD("%s: Requested channels are same as current", __func__);
829 return 0;
830 }
831
832 platform_set_hdmi_channels(adev->platform, channels);
833 adev->cur_hdmi_channels = channels;
834
835 /*
836 * Deroute all the playback streams routed to HDMI so that
837 * the back end is deactivated. Note that backend will not
838 * be deactivated if any one stream is connected to it.
839 */
840 list_for_each(node, &adev->usecase_list) {
841 usecase = node_to_item(node, struct audio_usecase, list);
842 if (usecase->type == PCM_PLAYBACK &&
843 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
844 disable_audio_route(adev, usecase, true);
845 }
846 }
847
848 /*
849 * Enable all the streams disabled above. Now the HDMI backend
850 * will be activated with new channel configuration
851 */
852 list_for_each(node, &adev->usecase_list) {
853 usecase = node_to_item(node, struct audio_usecase, list);
854 if (usecase->type == PCM_PLAYBACK &&
855 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
856 enable_audio_route(adev, usecase, true);
857 }
858 }
859
860 return 0;
861}
862
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800863static int stop_output_stream(struct stream_out *out)
864{
865 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800866 struct audio_usecase *uc_info;
867 struct audio_device *adev = out->dev;
868
Eric Laurent994a6932013-07-17 11:51:42 -0700869 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700870 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800871 uc_info = get_usecase_from_list(adev, out->usecase);
872 if (uc_info == NULL) {
873 ALOGE("%s: Could not find the usecase (%d) in the list",
874 __func__, out->usecase);
875 return -EINVAL;
876 }
877
Eric Laurentc4aef752013-09-12 17:45:53 -0700878 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
879 adev->visualizer_stop_output != NULL)
880 adev->visualizer_stop_output(out->handle);
881
Eric Laurent150dbfe2013-02-27 14:31:02 -0800882 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700883 disable_audio_route(adev, uc_info, true);
884
885 /* 2. Disable the rx device */
886 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800887
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800888 list_remove(&uc_info->list);
889 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800890
Eric Laurent07eeafd2013-10-06 12:52:49 -0700891 /* Must be called after removing the usecase from list */
892 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
893 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
894
Eric Laurent994a6932013-07-17 11:51:42 -0700895 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800896 return ret;
897}
898
899int start_output_stream(struct stream_out *out)
900{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800901 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800902 struct audio_usecase *uc_info;
903 struct audio_device *adev = out->dev;
904
Eric Laurent994a6932013-07-17 11:51:42 -0700905 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700906 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -0700907 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800908 if (out->pcm_device_id < 0) {
909 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
910 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800911 ret = -EINVAL;
912 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800913 }
914
915 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
916 uc_info->id = out->usecase;
917 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800918 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700919 uc_info->devices = out->devices;
920 uc_info->in_snd_device = SND_DEVICE_NONE;
921 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800922
Eric Laurent07eeafd2013-10-06 12:52:49 -0700923 /* This must be called before adding this usecase to the list */
924 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
925 check_and_set_hdmi_channels(adev, out->config.channels);
926
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800927 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800928
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700929 select_devices(adev, out->usecase);
930
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800931 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
932 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700933 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
934 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -0700935 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700936 if (out->pcm && !pcm_is_ready(out->pcm)) {
937 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
938 pcm_close(out->pcm);
939 out->pcm = NULL;
940 ret = -EIO;
941 goto error_open;
942 }
943 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800944 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700945 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
946 COMPRESS_IN, &out->compr_config);
947 if (out->compr && !is_compress_ready(out->compr)) {
948 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
949 compress_close(out->compr);
950 out->compr = NULL;
951 ret = -EIO;
952 goto error_open;
953 }
954 if (out->offload_callback)
955 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -0700956
957 if (adev->visualizer_start_output != NULL)
958 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800959 }
Eric Laurent994a6932013-07-17 11:51:42 -0700960 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800961 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700962error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800963 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800964error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800965 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800966}
967
968static int stop_voice_call(struct audio_device *adev)
969{
970 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800971 struct audio_usecase *uc_info;
972
Eric Laurent994a6932013-07-17 11:51:42 -0700973 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800974 adev->in_call = false;
Eric Laurentb23d5282013-05-14 15:27:20 -0700975
976 ret = platform_stop_voice_call(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800977
978 /* 1. Close the PCM devices */
979 if (adev->voice_call_rx) {
980 pcm_close(adev->voice_call_rx);
981 adev->voice_call_rx = NULL;
982 }
983 if (adev->voice_call_tx) {
984 pcm_close(adev->voice_call_tx);
985 adev->voice_call_tx = NULL;
986 }
987
988 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
989 if (uc_info == NULL) {
990 ALOGE("%s: Could not find the usecase (%d) in the list",
991 __func__, USECASE_VOICE_CALL);
992 return -EINVAL;
993 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800994
995 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700996 disable_audio_route(adev, uc_info, true);
997
998 /* 3. Disable the rx and tx devices */
999 disable_snd_device(adev, uc_info->out_snd_device, false);
1000 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001001
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001002 list_remove(&uc_info->list);
1003 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001004
Eric Laurent994a6932013-07-17 11:51:42 -07001005 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001006 return ret;
1007}
1008
1009static int start_voice_call(struct audio_device *adev)
1010{
1011 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001012 struct audio_usecase *uc_info;
1013 int pcm_dev_rx_id, pcm_dev_tx_id;
1014
Eric Laurent994a6932013-07-17 11:51:42 -07001015 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001016
1017 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1018 uc_info->id = USECASE_VOICE_CALL;
1019 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001020 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001021 uc_info->devices = adev->primary_output->devices;
1022 uc_info->in_snd_device = SND_DEVICE_NONE;
1023 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001024
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001025 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001027 select_devices(adev, USECASE_VOICE_CALL);
1028
Eric Laurentb23d5282013-05-14 15:27:20 -07001029 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
1030 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001032 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1033 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1034 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001035 ret = -EIO;
1036 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001037 }
1038
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001039 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001040 __func__, SOUND_CARD, pcm_dev_rx_id);
1041 adev->voice_call_rx = pcm_open(SOUND_CARD,
1042 pcm_dev_rx_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001043 PCM_OUT | PCM_MONOTONIC, &pcm_config_voice_call);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001044 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1045 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001046 ret = -EIO;
1047 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001048 }
1049
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001050 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001051 __func__, SOUND_CARD, pcm_dev_tx_id);
1052 adev->voice_call_tx = pcm_open(SOUND_CARD,
1053 pcm_dev_tx_id,
1054 PCM_IN, &pcm_config_voice_call);
1055 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1056 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001057 ret = -EIO;
1058 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001059 }
Haynes Mathew George5191a852013-09-11 14:19:36 -07001060
1061 /* set cached volume */
1062 set_voice_volume_l(adev, adev->voice_volume);
1063
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001064 pcm_start(adev->voice_call_rx);
1065 pcm_start(adev->voice_call_tx);
1066
Eric Laurentb23d5282013-05-14 15:27:20 -07001067 ret = platform_start_voice_call(adev->platform);
1068 if (ret < 0) {
1069 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
1070 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001071 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001072 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001073 return 0;
1074
1075error_start_voice:
1076 stop_voice_call(adev);
1077
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001078 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001079 return ret;
1080}
1081
1082static int check_input_parameters(uint32_t sample_rate,
1083 audio_format_t format,
1084 int channel_count)
1085{
1086 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1087
1088 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1089
1090 switch (sample_rate) {
1091 case 8000:
1092 case 11025:
1093 case 12000:
1094 case 16000:
1095 case 22050:
1096 case 24000:
1097 case 32000:
1098 case 44100:
1099 case 48000:
1100 break;
1101 default:
1102 return -EINVAL;
1103 }
1104
1105 return 0;
1106}
1107
1108static size_t get_input_buffer_size(uint32_t sample_rate,
1109 audio_format_t format,
1110 int channel_count)
1111{
1112 size_t size = 0;
1113
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001114 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1115 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001116
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001117 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1118 /* ToDo: should use frame_size computed based on the format and
1119 channel_count here. */
1120 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001121
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001122 /* make sure the size is multiple of 64 */
1123 size += 0x3f;
1124 size &= ~0x3f;
1125
1126 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001127}
1128
1129static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1130{
1131 struct stream_out *out = (struct stream_out *)stream;
1132
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001133 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001134}
1135
1136static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1137{
1138 return -ENOSYS;
1139}
1140
1141static size_t out_get_buffer_size(const struct audio_stream *stream)
1142{
1143 struct stream_out *out = (struct stream_out *)stream;
1144
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001145 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1146 return out->compr_config.fragment_size;
1147 }
1148
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001149 return out->config.period_size * audio_stream_frame_size(stream);
1150}
1151
1152static uint32_t out_get_channels(const struct audio_stream *stream)
1153{
1154 struct stream_out *out = (struct stream_out *)stream;
1155
1156 return out->channel_mask;
1157}
1158
1159static audio_format_t out_get_format(const struct audio_stream *stream)
1160{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001161 struct stream_out *out = (struct stream_out *)stream;
1162
1163 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001164}
1165
1166static int out_set_format(struct audio_stream *stream, audio_format_t format)
1167{
1168 return -ENOSYS;
1169}
1170
1171static int out_standby(struct audio_stream *stream)
1172{
1173 struct stream_out *out = (struct stream_out *)stream;
1174 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001175
Eric Laurent994a6932013-07-17 11:51:42 -07001176 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001177 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001178
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001179 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001180 if (!out->standby) {
1181 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001182 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1183 if (out->pcm) {
1184 pcm_close(out->pcm);
1185 out->pcm = NULL;
1186 }
1187 } else {
1188 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001189 out->gapless_mdata.encoder_delay = 0;
1190 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001191 if (out->compr != NULL) {
1192 compress_close(out->compr);
1193 out->compr = NULL;
1194 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001195 }
1196 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001197 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001198 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001199 }
1200 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001201 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001202 return 0;
1203}
1204
1205static int out_dump(const struct audio_stream *stream, int fd)
1206{
1207 return 0;
1208}
1209
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001210static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1211{
1212 int ret = 0;
1213 char value[32];
1214 struct compr_gapless_mdata tmp_mdata;
1215
1216 if (!out || !parms) {
1217 return -EINVAL;
1218 }
1219
1220 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1221 if (ret >= 0) {
1222 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1223 } else {
1224 return -EINVAL;
1225 }
1226
1227 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1228 if (ret >= 0) {
1229 tmp_mdata.encoder_padding = atoi(value);
1230 } else {
1231 return -EINVAL;
1232 }
1233
1234 out->gapless_mdata = tmp_mdata;
1235 out->send_new_metadata = 1;
1236 ALOGV("%s new encoder delay %u and padding %u", __func__,
1237 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1238
1239 return 0;
1240}
1241
1242
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001243static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1244{
1245 struct stream_out *out = (struct stream_out *)stream;
1246 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001247 struct audio_usecase *usecase;
1248 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001249 struct str_parms *parms;
1250 char value[32];
1251 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001252 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001253
sangwoobc677242013-08-08 16:53:43 +09001254 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001255 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001256 parms = str_parms_create_str(kvpairs);
1257 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1258 if (ret >= 0) {
1259 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001260 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001261 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001263 /*
1264 * When HDMI cable is unplugged the music playback is paused and
1265 * the policy manager sends routing=0. But the audioflinger
1266 * continues to write data until standby time (3sec).
1267 * As the HDMI core is turned off, the write gets blocked.
1268 * Avoid this by routing audio to speaker until standby.
1269 */
1270 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1271 val == AUDIO_DEVICE_NONE) {
1272 val = AUDIO_DEVICE_OUT_SPEAKER;
1273 }
1274
1275 /*
1276 * select_devices() call below switches all the usecases on the same
1277 * backend to the new device. Refer to check_usecases_codec_backend() in
1278 * the select_devices(). But how do we undo this?
1279 *
1280 * For example, music playback is active on headset (deep-buffer usecase)
1281 * and if we go to ringtones and select a ringtone, low-latency usecase
1282 * will be started on headset+speaker. As we can't enable headset+speaker
1283 * and headset devices at the same time, select_devices() switches the music
1284 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1285 * So when the ringtone playback is completed, how do we undo the same?
1286 *
1287 * We are relying on the out_set_parameters() call on deep-buffer output,
1288 * once the ringtone playback is ended.
1289 * NOTE: We should not check if the current devices are same as new devices.
1290 * Because select_devices() must be called to switch back the music
1291 * playback to headset.
1292 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001293 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001294 out->devices = val;
1295
1296 if (!out->standby)
1297 select_devices(adev, out->usecase);
1298
1299 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1300 (out == adev->primary_output)) {
1301 start_voice_call(adev);
1302 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1303 (out == adev->primary_output)) {
1304 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001305 }
1306 }
1307
Ravi Kumar Alamandaed9c56a2013-10-02 09:56:18 -07001308 if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001309 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001310 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001311 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001312
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001313 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001314 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001315 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001316
1317 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1318 parse_compress_metadata(out, parms);
1319 }
1320
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001321 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001322 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323 return ret;
1324}
1325
1326static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1327{
1328 struct stream_out *out = (struct stream_out *)stream;
1329 struct str_parms *query = str_parms_create_str(keys);
1330 char *str;
1331 char value[256];
1332 struct str_parms *reply = str_parms_create();
1333 size_t i, j;
1334 int ret;
1335 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001336 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1338 if (ret >= 0) {
1339 value[0] = '\0';
1340 i = 0;
1341 while (out->supported_channel_masks[i] != 0) {
1342 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1343 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1344 if (!first) {
1345 strcat(value, "|");
1346 }
1347 strcat(value, out_channels_name_to_enum_table[j].name);
1348 first = false;
1349 break;
1350 }
1351 }
1352 i++;
1353 }
1354 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1355 str = str_parms_to_str(reply);
1356 } else {
1357 str = strdup(keys);
1358 }
1359 str_parms_destroy(query);
1360 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001361 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001362 return str;
1363}
1364
1365static uint32_t out_get_latency(const struct audio_stream_out *stream)
1366{
1367 struct stream_out *out = (struct stream_out *)stream;
1368
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001369 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1370 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1371
1372 return (out->config.period_count * out->config.period_size * 1000) /
1373 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001374}
1375
1376static int out_set_volume(struct audio_stream_out *stream, float left,
1377 float right)
1378{
Eric Laurenta9024de2013-04-04 09:19:12 -07001379 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001380 int volume[2];
1381
Eric Laurenta9024de2013-04-04 09:19:12 -07001382 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1383 /* only take left channel into account: the API is for stereo anyway */
1384 out->muted = (left == 0.0f);
1385 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001386 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1387 const char *mixer_ctl_name = "Compress Playback Volume";
1388 struct audio_device *adev = out->dev;
1389 struct mixer_ctl *ctl;
1390
1391 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1392 if (!ctl) {
1393 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1394 __func__, mixer_ctl_name);
1395 return -EINVAL;
1396 }
1397 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1398 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1399 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1400 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001401 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001402
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001403 return -ENOSYS;
1404}
1405
1406static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1407 size_t bytes)
1408{
1409 struct stream_out *out = (struct stream_out *)stream;
1410 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001411 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001412
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001413 pthread_mutex_lock(&out->lock);
1414 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001415 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001416 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001417 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001418 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001419 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001420 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001421 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001422 goto exit;
1423 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001424 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001425
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001426 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001427 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1428 if (out->send_new_metadata) {
1429 ALOGVV("send new gapless metadata");
1430 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1431 out->send_new_metadata = 0;
1432 }
1433
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001434 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001435 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001436 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001437 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1438 }
1439 if (!out->playback_started) {
1440 compress_start(out->compr);
1441 out->playback_started = 1;
1442 out->offload_state = OFFLOAD_STATE_PLAYING;
1443 }
1444 pthread_mutex_unlock(&out->lock);
1445 return ret;
1446 } else {
1447 if (out->pcm) {
1448 if (out->muted)
1449 memset((void *)buffer, 0, bytes);
1450 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1451 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001452 if (ret == 0)
1453 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001454 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001455 }
1456
1457exit:
1458 pthread_mutex_unlock(&out->lock);
1459
1460 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001461 if (out->pcm)
1462 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001463 out_standby(&out->stream.common);
1464 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1465 out_get_sample_rate(&out->stream.common));
1466 }
1467 return bytes;
1468}
1469
1470static int out_get_render_position(const struct audio_stream_out *stream,
1471 uint32_t *dsp_frames)
1472{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001473 struct stream_out *out = (struct stream_out *)stream;
1474 *dsp_frames = 0;
1475 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1476 pthread_mutex_lock(&out->lock);
1477 if (out->compr != NULL) {
1478 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1479 &out->sample_rate);
1480 ALOGVV("%s rendered frames %d sample_rate %d",
1481 __func__, *dsp_frames, out->sample_rate);
1482 }
1483 pthread_mutex_unlock(&out->lock);
1484 return 0;
1485 } else
1486 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001487}
1488
1489static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1490{
1491 return 0;
1492}
1493
1494static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1495{
1496 return 0;
1497}
1498
1499static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1500 int64_t *timestamp)
1501{
1502 return -EINVAL;
1503}
1504
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001505static int out_get_presentation_position(const struct audio_stream_out *stream,
1506 uint64_t *frames, struct timespec *timestamp)
1507{
1508 struct stream_out *out = (struct stream_out *)stream;
1509 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001510 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001511
1512 pthread_mutex_lock(&out->lock);
1513
Eric Laurent949a0892013-09-20 09:20:13 -07001514 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1515 if (out->compr != NULL) {
1516 compress_get_tstamp(out->compr, &dsp_frames,
1517 &out->sample_rate);
1518 ALOGVV("%s rendered frames %ld sample_rate %d",
1519 __func__, dsp_frames, out->sample_rate);
1520 *frames = dsp_frames;
1521 ret = 0;
1522 /* this is the best we can do */
1523 clock_gettime(CLOCK_MONOTONIC, timestamp);
1524 }
1525 } else {
1526 if (out->pcm) {
1527 size_t avail;
1528 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1529 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001530 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001531 // This adjustment accounts for buffering after app processor.
1532 // It is based on estimated DSP latency per use case, rather than exact.
1533 signed_frames -=
1534 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1535
Eric Laurent949a0892013-09-20 09:20:13 -07001536 // It would be unusual for this value to be negative, but check just in case ...
1537 if (signed_frames >= 0) {
1538 *frames = signed_frames;
1539 ret = 0;
1540 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001541 }
1542 }
1543 }
1544
1545 pthread_mutex_unlock(&out->lock);
1546
1547 return ret;
1548}
1549
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001550static int out_set_callback(struct audio_stream_out *stream,
1551 stream_callback_t callback, void *cookie)
1552{
1553 struct stream_out *out = (struct stream_out *)stream;
1554
1555 ALOGV("%s", __func__);
1556 pthread_mutex_lock(&out->lock);
1557 out->offload_callback = callback;
1558 out->offload_cookie = cookie;
1559 pthread_mutex_unlock(&out->lock);
1560 return 0;
1561}
1562
1563static int out_pause(struct audio_stream_out* stream)
1564{
1565 struct stream_out *out = (struct stream_out *)stream;
1566 int status = -ENOSYS;
1567 ALOGV("%s", __func__);
1568 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1569 pthread_mutex_lock(&out->lock);
1570 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1571 status = compress_pause(out->compr);
1572 out->offload_state = OFFLOAD_STATE_PAUSED;
1573 }
1574 pthread_mutex_unlock(&out->lock);
1575 }
1576 return status;
1577}
1578
1579static int out_resume(struct audio_stream_out* stream)
1580{
1581 struct stream_out *out = (struct stream_out *)stream;
1582 int status = -ENOSYS;
1583 ALOGV("%s", __func__);
1584 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1585 status = 0;
1586 pthread_mutex_lock(&out->lock);
1587 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1588 status = compress_resume(out->compr);
1589 out->offload_state = OFFLOAD_STATE_PLAYING;
1590 }
1591 pthread_mutex_unlock(&out->lock);
1592 }
1593 return status;
1594}
1595
1596static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1597{
1598 struct stream_out *out = (struct stream_out *)stream;
1599 int status = -ENOSYS;
1600 ALOGV("%s", __func__);
1601 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1602 pthread_mutex_lock(&out->lock);
1603 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1604 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1605 else
1606 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1607 pthread_mutex_unlock(&out->lock);
1608 }
1609 return status;
1610}
1611
1612static int out_flush(struct audio_stream_out* stream)
1613{
1614 struct stream_out *out = (struct stream_out *)stream;
1615 ALOGV("%s", __func__);
1616 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1617 pthread_mutex_lock(&out->lock);
1618 stop_compressed_output_l(out);
1619 pthread_mutex_unlock(&out->lock);
1620 return 0;
1621 }
1622 return -ENOSYS;
1623}
1624
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001625/** audio_stream_in implementation **/
1626static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1627{
1628 struct stream_in *in = (struct stream_in *)stream;
1629
1630 return in->config.rate;
1631}
1632
1633static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1634{
1635 return -ENOSYS;
1636}
1637
1638static size_t in_get_buffer_size(const struct audio_stream *stream)
1639{
1640 struct stream_in *in = (struct stream_in *)stream;
1641
1642 return in->config.period_size * audio_stream_frame_size(stream);
1643}
1644
1645static uint32_t in_get_channels(const struct audio_stream *stream)
1646{
1647 struct stream_in *in = (struct stream_in *)stream;
1648
1649 return in->channel_mask;
1650}
1651
1652static audio_format_t in_get_format(const struct audio_stream *stream)
1653{
1654 return AUDIO_FORMAT_PCM_16_BIT;
1655}
1656
1657static int in_set_format(struct audio_stream *stream, audio_format_t format)
1658{
1659 return -ENOSYS;
1660}
1661
1662static int in_standby(struct audio_stream *stream)
1663{
1664 struct stream_in *in = (struct stream_in *)stream;
1665 struct audio_device *adev = in->dev;
1666 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001667 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001668 pthread_mutex_lock(&in->lock);
1669 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001670 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001671 if (in->pcm) {
1672 pcm_close(in->pcm);
1673 in->pcm = NULL;
1674 }
1675 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001676 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001677 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001678 }
1679 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001680 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001681 return status;
1682}
1683
1684static int in_dump(const struct audio_stream *stream, int fd)
1685{
1686 return 0;
1687}
1688
1689static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1690{
1691 struct stream_in *in = (struct stream_in *)stream;
1692 struct audio_device *adev = in->dev;
1693 struct str_parms *parms;
1694 char *str;
1695 char value[32];
1696 int ret, val = 0;
1697
Eric Laurent994a6932013-07-17 11:51:42 -07001698 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001699 parms = str_parms_create_str(kvpairs);
1700
1701 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1702
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001703 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001704 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001705 if (ret >= 0) {
1706 val = atoi(value);
1707 /* no audio source uses val == 0 */
1708 if ((in->source != val) && (val != 0)) {
1709 in->source = val;
1710 }
1711 }
1712
1713 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1714 if (ret >= 0) {
1715 val = atoi(value);
1716 if ((in->device != val) && (val != 0)) {
1717 in->device = val;
1718 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001719 if (!in->standby)
1720 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001721 }
1722 }
1723
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001724 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001725 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001726
1727 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001728 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001729 return ret;
1730}
1731
1732static char* in_get_parameters(const struct audio_stream *stream,
1733 const char *keys)
1734{
1735 return strdup("");
1736}
1737
1738static int in_set_gain(struct audio_stream_in *stream, float gain)
1739{
1740 return 0;
1741}
1742
1743static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1744 size_t bytes)
1745{
1746 struct stream_in *in = (struct stream_in *)stream;
1747 struct audio_device *adev = in->dev;
1748 int i, ret = -1;
1749
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001750 pthread_mutex_lock(&in->lock);
1751 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001752 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001753 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001754 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001755 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001756 goto exit;
1757 }
1758 in->standby = 0;
1759 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001760
1761 if (in->pcm) {
1762 ret = pcm_read(in->pcm, buffer, bytes);
1763 }
1764
1765 /*
1766 * Instead of writing zeroes here, we could trust the hardware
1767 * to always provide zeroes when muted.
1768 */
1769 if (ret == 0 && adev->mic_mute)
1770 memset(buffer, 0, bytes);
1771
1772exit:
1773 pthread_mutex_unlock(&in->lock);
1774
1775 if (ret != 0) {
1776 in_standby(&in->stream.common);
1777 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1778 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1779 in_get_sample_rate(&in->stream.common));
1780 }
1781 return bytes;
1782}
1783
1784static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1785{
1786 return 0;
1787}
1788
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001789static int add_remove_audio_effect(const struct audio_stream *stream,
1790 effect_handle_t effect,
1791 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001792{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001793 struct stream_in *in = (struct stream_in *)stream;
1794 int status = 0;
1795 effect_descriptor_t desc;
1796
1797 status = (*effect)->get_descriptor(effect, &desc);
1798 if (status != 0)
1799 return status;
1800
1801 pthread_mutex_lock(&in->lock);
1802 pthread_mutex_lock(&in->dev->lock);
1803 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1804 in->enable_aec != enable &&
1805 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1806 in->enable_aec = enable;
1807 if (!in->standby)
1808 select_devices(in->dev, in->usecase);
1809 }
1810 pthread_mutex_unlock(&in->dev->lock);
1811 pthread_mutex_unlock(&in->lock);
1812
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001813 return 0;
1814}
1815
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001816static int in_add_audio_effect(const struct audio_stream *stream,
1817 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001818{
Eric Laurent994a6932013-07-17 11:51:42 -07001819 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001820 return add_remove_audio_effect(stream, effect, true);
1821}
1822
1823static int in_remove_audio_effect(const struct audio_stream *stream,
1824 effect_handle_t effect)
1825{
Eric Laurent994a6932013-07-17 11:51:42 -07001826 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001827 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001828}
1829
1830static int adev_open_output_stream(struct audio_hw_device *dev,
1831 audio_io_handle_t handle,
1832 audio_devices_t devices,
1833 audio_output_flags_t flags,
1834 struct audio_config *config,
1835 struct audio_stream_out **stream_out)
1836{
1837 struct audio_device *adev = (struct audio_device *)dev;
1838 struct stream_out *out;
1839 int i, ret;
1840
Eric Laurent994a6932013-07-17 11:51:42 -07001841 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001842 __func__, config->sample_rate, config->channel_mask, devices, flags);
1843 *stream_out = NULL;
1844 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1845
1846 if (devices == AUDIO_DEVICE_NONE)
1847 devices = AUDIO_DEVICE_OUT_SPEAKER;
1848
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001849 out->flags = flags;
1850 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001851 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001852 out->format = config->format;
1853 out->sample_rate = config->sample_rate;
1854 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1855 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001856 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001857
1858 /* Init use case and pcm_config */
1859 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1860 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001861 pthread_mutex_lock(&adev->lock);
1862 ret = read_hdmi_channel_masks(out);
1863 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001864 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001865 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001866
1867 if (config->sample_rate == 0)
1868 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1869 if (config->channel_mask == 0)
1870 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1871
1872 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001873 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001874 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1875 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001876 out->config.rate = config->sample_rate;
1877 out->config.channels = popcount(out->channel_mask);
1878 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001879 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1880 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1881 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001882 out->sample_rate = out->config.rate;
1883 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1884 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1885 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1886 ALOGE("%s: Unsupported Offload information", __func__);
1887 ret = -EINVAL;
1888 goto error_open;
1889 }
1890 if (!is_supported_format(config->offload_info.format)) {
1891 ALOGE("%s: Unsupported audio format", __func__);
1892 ret = -EINVAL;
1893 goto error_open;
1894 }
1895
1896 out->compr_config.codec = (struct snd_codec *)
1897 calloc(1, sizeof(struct snd_codec));
1898
1899 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1900 if (config->offload_info.channel_mask)
1901 out->channel_mask = config->offload_info.channel_mask;
1902 else if (config->channel_mask)
1903 out->channel_mask = config->channel_mask;
1904 out->format = config->offload_info.format;
1905 out->sample_rate = config->offload_info.sample_rate;
1906
1907 out->stream.set_callback = out_set_callback;
1908 out->stream.pause = out_pause;
1909 out->stream.resume = out_resume;
1910 out->stream.drain = out_drain;
1911 out->stream.flush = out_flush;
1912
1913 out->compr_config.codec->id =
1914 get_snd_codec_id(config->offload_info.format);
1915 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1916 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1917 out->compr_config.codec->sample_rate =
1918 compress_get_alsa_rate(config->offload_info.sample_rate);
1919 out->compr_config.codec->bit_rate =
1920 config->offload_info.bit_rate;
1921 out->compr_config.codec->ch_in =
1922 popcount(config->channel_mask);
1923 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1924
1925 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1926 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001927
1928 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001929 create_offload_callback_thread(out);
1930 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1931 __func__, config->offload_info.version,
1932 config->offload_info.bit_rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001933 } else {
1934 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1935 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001936 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001937 }
1938
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001939 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1940 if(adev->primary_output == NULL)
1941 adev->primary_output = out;
1942 else {
1943 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001944 ret = -EEXIST;
1945 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001946 }
1947 }
1948
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001949 /* Check if this usecase is already existing */
1950 pthread_mutex_lock(&adev->lock);
1951 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1952 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001953 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001954 ret = -EEXIST;
1955 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001956 }
1957 pthread_mutex_unlock(&adev->lock);
1958
1959 out->stream.common.get_sample_rate = out_get_sample_rate;
1960 out->stream.common.set_sample_rate = out_set_sample_rate;
1961 out->stream.common.get_buffer_size = out_get_buffer_size;
1962 out->stream.common.get_channels = out_get_channels;
1963 out->stream.common.get_format = out_get_format;
1964 out->stream.common.set_format = out_set_format;
1965 out->stream.common.standby = out_standby;
1966 out->stream.common.dump = out_dump;
1967 out->stream.common.set_parameters = out_set_parameters;
1968 out->stream.common.get_parameters = out_get_parameters;
1969 out->stream.common.add_audio_effect = out_add_audio_effect;
1970 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1971 out->stream.get_latency = out_get_latency;
1972 out->stream.set_volume = out_set_volume;
1973 out->stream.write = out_write;
1974 out->stream.get_render_position = out_get_render_position;
1975 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001976 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001977
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001978 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001979 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001980 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001981
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001982 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
1983 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
1984
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001985 config->format = out->stream.common.get_format(&out->stream.common);
1986 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1987 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1988
1989 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001990 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001991 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001992
1993error_open:
1994 free(out);
1995 *stream_out = NULL;
1996 ALOGD("%s: exit: ret %d", __func__, ret);
1997 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001998}
1999
2000static void adev_close_output_stream(struct audio_hw_device *dev,
2001 struct audio_stream_out *stream)
2002{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002003 struct stream_out *out = (struct stream_out *)stream;
2004 struct audio_device *adev = out->dev;
2005
Eric Laurent994a6932013-07-17 11:51:42 -07002006 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002007 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002008 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2009 destroy_offload_callback_thread(out);
2010
2011 if (out->compr_config.codec != NULL)
2012 free(out->compr_config.codec);
2013 }
2014 pthread_cond_destroy(&out->cond);
2015 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002016 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002017 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002018}
2019
2020static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2021{
2022 struct audio_device *adev = (struct audio_device *)dev;
2023 struct str_parms *parms;
2024 char *str;
2025 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002026 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002027 int ret;
2028
Eric Laurent994a6932013-07-17 11:51:42 -07002029 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002030
2031 parms = str_parms_create_str(kvpairs);
2032 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
2033 if (ret >= 0) {
2034 int tty_mode;
2035
2036 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
2037 tty_mode = TTY_MODE_OFF;
2038 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
2039 tty_mode = TTY_MODE_VCO;
2040 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
2041 tty_mode = TTY_MODE_HCO;
2042 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
2043 tty_mode = TTY_MODE_FULL;
2044 else
2045 return -EINVAL;
2046
2047 pthread_mutex_lock(&adev->lock);
2048 if (tty_mode != adev->tty_mode) {
2049 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002050 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
2051 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002052 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002053 }
2054 pthread_mutex_unlock(&adev->lock);
2055 }
2056
2057 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2058 if (ret >= 0) {
2059 /* When set to false, HAL should disable EC and NS
2060 * But it is currently not supported.
2061 */
2062 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2063 adev->bluetooth_nrec = true;
2064 else
2065 adev->bluetooth_nrec = false;
2066 }
2067
2068 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2069 if (ret >= 0) {
2070 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2071 adev->screen_off = false;
2072 else
2073 adev->screen_off = true;
2074 }
2075
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002076 ret = str_parms_get_int(parms, "rotation", &val);
2077 if (ret >= 0) {
2078 bool reverse_speakers = false;
2079 switch(val) {
2080 // FIXME: note that the code below assumes that the speakers are in the correct placement
2081 // relative to the user when the device is rotated 90deg from its default rotation. This
2082 // assumption is device-specific, not platform-specific like this code.
2083 case 270:
2084 reverse_speakers = true;
2085 break;
2086 case 0:
2087 case 90:
2088 case 180:
2089 break;
2090 default:
2091 ALOGE("%s: unexpected rotation of %d", __func__, val);
2092 }
2093 pthread_mutex_lock(&adev->lock);
2094 if (adev->speaker_lr_swap != reverse_speakers) {
2095 adev->speaker_lr_swap = reverse_speakers;
2096 // only update the selected device if there is active pcm playback
2097 struct audio_usecase *usecase;
2098 struct listnode *node;
2099 list_for_each(node, &adev->usecase_list) {
2100 usecase = node_to_item(node, struct audio_usecase, list);
2101 if (usecase->type == PCM_PLAYBACK) {
2102 select_devices(adev, usecase->id);
2103 break;
2104 }
2105 }
2106 }
2107 pthread_mutex_unlock(&adev->lock);
2108 }
2109
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002110 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002111 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002112 return ret;
2113}
2114
2115static char* adev_get_parameters(const struct audio_hw_device *dev,
2116 const char *keys)
2117{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002118 return strdup("");
2119}
2120
2121static int adev_init_check(const struct audio_hw_device *dev)
2122{
2123 return 0;
2124}
2125
Haynes Mathew George5191a852013-09-11 14:19:36 -07002126/* always called with adev lock held */
2127static int set_voice_volume_l(struct audio_device *adev, float volume)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002128{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002129 int vol, err = 0;
2130
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002131 if (adev->mode == AUDIO_MODE_IN_CALL) {
2132 if (volume < 0.0) {
2133 volume = 0.0;
2134 } else if (volume > 1.0) {
2135 volume = 1.0;
2136 }
2137
2138 vol = lrint(volume * 100.0);
2139
2140 // Voice volume levels from android are mapped to driver volume levels as follows.
2141 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
2142 // So adjust the volume to get the correct volume index in driver
2143 vol = 100 - vol;
Eric Laurentb23d5282013-05-14 15:27:20 -07002144
2145 err = platform_set_voice_volume(adev->platform, vol);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002146 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002147 return err;
2148}
2149
Haynes Mathew George5191a852013-09-11 14:19:36 -07002150static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2151{
2152 int ret;
2153 struct audio_device *adev = (struct audio_device *)dev;
2154 pthread_mutex_lock(&adev->lock);
2155 /* cache volume */
2156 adev->voice_volume = volume;
2157 ret = set_voice_volume_l(adev, adev->voice_volume);
2158 pthread_mutex_unlock(&adev->lock);
2159 return ret;
2160}
2161
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002162static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2163{
2164 return -ENOSYS;
2165}
2166
2167static int adev_get_master_volume(struct audio_hw_device *dev,
2168 float *volume)
2169{
2170 return -ENOSYS;
2171}
2172
2173static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2174{
2175 return -ENOSYS;
2176}
2177
2178static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2179{
2180 return -ENOSYS;
2181}
2182
2183static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2184{
2185 struct audio_device *adev = (struct audio_device *)dev;
2186
2187 pthread_mutex_lock(&adev->lock);
2188 if (adev->mode != mode) {
2189 adev->mode = mode;
2190 }
2191 pthread_mutex_unlock(&adev->lock);
2192 return 0;
2193}
2194
2195static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2196{
2197 struct audio_device *adev = (struct audio_device *)dev;
2198 int err = 0;
2199
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002200 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002201 adev->mic_mute = state;
Eric Laurentb23d5282013-05-14 15:27:20 -07002202
2203 err = platform_set_mic_mute(adev->platform, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002204 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002205 return err;
2206}
2207
2208static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2209{
2210 struct audio_device *adev = (struct audio_device *)dev;
2211
2212 *state = adev->mic_mute;
2213
2214 return 0;
2215}
2216
2217static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2218 const struct audio_config *config)
2219{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002220 int channel_count = popcount(config->channel_mask);
2221
2222 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2223}
2224
2225static int adev_open_input_stream(struct audio_hw_device *dev,
2226 audio_io_handle_t handle,
2227 audio_devices_t devices,
2228 struct audio_config *config,
2229 struct audio_stream_in **stream_in)
2230{
2231 struct audio_device *adev = (struct audio_device *)dev;
2232 struct stream_in *in;
2233 int ret, buffer_size, frame_size;
2234 int channel_count = popcount(config->channel_mask);
2235
Eric Laurent994a6932013-07-17 11:51:42 -07002236 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002237 *stream_in = NULL;
2238 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2239 return -EINVAL;
2240
2241 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2242
2243 in->stream.common.get_sample_rate = in_get_sample_rate;
2244 in->stream.common.set_sample_rate = in_set_sample_rate;
2245 in->stream.common.get_buffer_size = in_get_buffer_size;
2246 in->stream.common.get_channels = in_get_channels;
2247 in->stream.common.get_format = in_get_format;
2248 in->stream.common.set_format = in_set_format;
2249 in->stream.common.standby = in_standby;
2250 in->stream.common.dump = in_dump;
2251 in->stream.common.set_parameters = in_set_parameters;
2252 in->stream.common.get_parameters = in_get_parameters;
2253 in->stream.common.add_audio_effect = in_add_audio_effect;
2254 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2255 in->stream.set_gain = in_set_gain;
2256 in->stream.read = in_read;
2257 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2258
2259 in->device = devices;
2260 in->source = AUDIO_SOURCE_DEFAULT;
2261 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002262 in->standby = 1;
2263 in->channel_mask = config->channel_mask;
2264
2265 /* Update config params with the requested sample rate and channels */
2266 in->usecase = USECASE_AUDIO_RECORD;
2267 in->config = pcm_config_audio_capture;
2268 in->config.channels = channel_count;
2269 in->config.rate = config->sample_rate;
2270
2271 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2272 buffer_size = get_input_buffer_size(config->sample_rate,
2273 config->format,
2274 channel_count);
2275 in->config.period_size = buffer_size / frame_size;
2276
2277 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002278 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002279 return 0;
2280
2281err_open:
2282 free(in);
2283 *stream_in = NULL;
2284 return ret;
2285}
2286
2287static void adev_close_input_stream(struct audio_hw_device *dev,
2288 struct audio_stream_in *stream)
2289{
Eric Laurent994a6932013-07-17 11:51:42 -07002290 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002291
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002292 in_standby(&stream->common);
2293 free(stream);
2294
2295 return;
2296}
2297
2298static int adev_dump(const audio_hw_device_t *device, int fd)
2299{
2300 return 0;
2301}
2302
2303static int adev_close(hw_device_t *device)
2304{
2305 struct audio_device *adev = (struct audio_device *)device;
2306 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002307 free(adev->snd_dev_ref_cnt);
2308 platform_deinit(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002309 free(device);
2310 return 0;
2311}
2312
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002313static int adev_open(const hw_module_t *module, const char *name,
2314 hw_device_t **device)
2315{
2316 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002317 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002318
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002319 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002320 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2321
2322 adev = calloc(1, sizeof(struct audio_device));
2323
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002324 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2325 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2326 adev->device.common.module = (struct hw_module_t *)module;
2327 adev->device.common.close = adev_close;
2328
2329 adev->device.init_check = adev_init_check;
2330 adev->device.set_voice_volume = adev_set_voice_volume;
2331 adev->device.set_master_volume = adev_set_master_volume;
2332 adev->device.get_master_volume = adev_get_master_volume;
2333 adev->device.set_master_mute = adev_set_master_mute;
2334 adev->device.get_master_mute = adev_get_master_mute;
2335 adev->device.set_mode = adev_set_mode;
2336 adev->device.set_mic_mute = adev_set_mic_mute;
2337 adev->device.get_mic_mute = adev_get_mic_mute;
2338 adev->device.set_parameters = adev_set_parameters;
2339 adev->device.get_parameters = adev_get_parameters;
2340 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2341 adev->device.open_output_stream = adev_open_output_stream;
2342 adev->device.close_output_stream = adev_close_output_stream;
2343 adev->device.open_input_stream = adev_open_input_stream;
2344 adev->device.close_input_stream = adev_close_input_stream;
2345 adev->device.dump = adev_dump;
2346
2347 /* Set the default route before the PCM stream is opened */
2348 pthread_mutex_lock(&adev->lock);
2349 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002350 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002351 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002352 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002353 adev->voice_call_rx = NULL;
2354 adev->voice_call_tx = NULL;
2355 adev->voice_volume = 1.0f;
2356 adev->tty_mode = TTY_MODE_OFF;
2357 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002358 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002359 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002360 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002361 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002362 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002363 pthread_mutex_unlock(&adev->lock);
2364
2365 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002366 adev->platform = platform_init(adev);
2367 if (!adev->platform) {
2368 free(adev->snd_dev_ref_cnt);
2369 free(adev);
2370 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2371 *device = NULL;
2372 return -EINVAL;
2373 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002374
2375 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2376 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2377 if (adev->visualizer_lib == NULL) {
2378 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2379 } else {
2380 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2381 adev->visualizer_start_output =
2382 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2383 "visualizer_hal_start_output");
2384 adev->visualizer_stop_output =
2385 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2386 "visualizer_hal_stop_output");
2387 }
2388 }
2389
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002390 *device = &adev->device.common;
2391
Eric Laurent994a6932013-07-17 11:51:42 -07002392 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002393 return 0;
2394}
2395
2396static struct hw_module_methods_t hal_module_methods = {
2397 .open = adev_open,
2398};
2399
2400struct audio_module HAL_MODULE_INFO_SYM = {
2401 .common = {
2402 .tag = HARDWARE_MODULE_TAG,
2403 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2404 .hal_api_version = HARDWARE_HAL_API_VERSION,
2405 .id = AUDIO_HARDWARE_MODULE_ID,
2406 .name = "QCOM Audio HAL",
2407 .author = "Code Aurora Forum",
2408 .methods = &hal_module_methods,
2409 },
2410};