blob: 59243f51c4cab9f348c3f80f1187b1cb866aa1bc [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>
Andy Hung31aca912014-03-20 17:14:59 -070043#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070044#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <audio_effects/effect_aec.h>
46#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080047#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070048#include "platform_api.h"
49#include <platform.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080050
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070051#include "sound/compress_params.h"
52
53#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
54#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
55/* ToDo: Check and update a proper value in msec */
56#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
57#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
58
Andy Hung31aca912014-03-20 17:14:59 -070059/* This constant enables extended precision handling.
60 * TODO The flag is off until more testing is done.
61 */
62static const bool k_enable_extended_precision = false;
63
Eric Laurentb23d5282013-05-14 15:27:20 -070064struct pcm_config pcm_config_deep_buffer = {
65 .channels = 2,
66 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
67 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
68 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
69 .format = PCM_FORMAT_S16_LE,
70 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
71 .stop_threshold = INT_MAX,
72 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
73};
74
75struct pcm_config pcm_config_low_latency = {
76 .channels = 2,
77 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
78 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
79 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
80 .format = PCM_FORMAT_S16_LE,
81 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
82 .stop_threshold = INT_MAX,
83 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
84};
85
86struct pcm_config pcm_config_hdmi_multi = {
87 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
88 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
89 .period_size = HDMI_MULTI_PERIOD_SIZE,
90 .period_count = HDMI_MULTI_PERIOD_COUNT,
91 .format = PCM_FORMAT_S16_LE,
92 .start_threshold = 0,
93 .stop_threshold = INT_MAX,
94 .avail_min = 0,
95};
96
97struct pcm_config pcm_config_audio_capture = {
98 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070099 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
100 .format = PCM_FORMAT_S16_LE,
101};
102
103struct pcm_config pcm_config_voice_call = {
104 .channels = 1,
105 .rate = 8000,
106 .period_size = 160,
107 .period_count = 2,
108 .format = PCM_FORMAT_S16_LE,
109};
110
111static const char * const use_case_table[AUDIO_USECASE_MAX] = {
112 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
113 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
114 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
115 [USECASE_AUDIO_RECORD] = "audio-record",
116 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
117 [USECASE_VOICE_CALL] = "voice-call",
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700118 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700119};
120
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800121
122#define STRING_TO_ENUM(string) { #string, string }
123
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800124struct string_to_enum {
125 const char *name;
126 uint32_t value;
127};
128
129static const struct string_to_enum out_channels_name_to_enum_table[] = {
130 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
131 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
132 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
133};
134
Haynes Mathew George5191a852013-09-11 14:19:36 -0700135static int set_voice_volume_l(struct audio_device *adev, float volume);
136
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700137static bool is_supported_format(audio_format_t format)
138{
Eric Laurent86e17132013-09-12 17:49:30 -0700139 if (format == AUDIO_FORMAT_MP3 ||
140 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700141 return true;
142
143 return false;
144}
145
146static int get_snd_codec_id(audio_format_t format)
147{
148 int id = 0;
149
150 switch (format) {
151 case AUDIO_FORMAT_MP3:
152 id = SND_AUDIOCODEC_MP3;
153 break;
154 case AUDIO_FORMAT_AAC:
155 id = SND_AUDIOCODEC_AAC;
156 break;
157 default:
158 ALOGE("%s: Unsupported audio format", __func__);
159 }
160
161 return id;
162}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800163
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700164static int enable_audio_route(struct audio_device *adev,
165 struct audio_usecase *usecase,
166 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800167{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700168 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800169 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800170
171 if (usecase == NULL)
172 return -EINVAL;
173
174 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
175
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800176 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700177 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800178 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700179 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800180
181 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700182 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700183 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700184 audio_route_apply_path(adev->audio_route, mixer_path);
185 if (update_mixer)
186 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800187
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800188 ALOGV("%s: exit", __func__);
189 return 0;
190}
191
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700192static int disable_audio_route(struct audio_device *adev,
193 struct audio_usecase *usecase,
194 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800195{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700196 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800197 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800198
199 if (usecase == NULL)
200 return -EINVAL;
201
202 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700203 if (usecase->type == PCM_CAPTURE)
204 snd_device = usecase->in_snd_device;
205 else
206 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800207 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700208 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700209 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700210 audio_route_reset_path(adev->audio_route, mixer_path);
211 if (update_mixer)
212 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800213
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800214 ALOGV("%s: exit", __func__);
215 return 0;
216}
217
218static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700219 snd_device_t snd_device,
220 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800221{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800222 if (snd_device < SND_DEVICE_MIN ||
223 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800224 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800225 return -EINVAL;
226 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700227
228 adev->snd_dev_ref_cnt[snd_device]++;
229 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700230 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700231 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700232 return 0;
233 }
234
Eric Laurentb23d5282013-05-14 15:27:20 -0700235 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700236 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800237 return -EINVAL;
238 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800239
Eric Laurent994a6932013-07-17 11:51:42 -0700240 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700241 snd_device, platform_get_snd_device_name(snd_device));
242 audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700243 if (update_mixer)
244 audio_route_update_mixer(adev->audio_route);
245
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800246 return 0;
247}
248
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249static int disable_snd_device(struct audio_device *adev,
250 snd_device_t snd_device,
251 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800252{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800253 if (snd_device < SND_DEVICE_MIN ||
254 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800255 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800256 return -EINVAL;
257 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700258 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
259 ALOGE("%s: device ref cnt is already 0", __func__);
260 return -EINVAL;
261 }
262 adev->snd_dev_ref_cnt[snd_device]--;
263 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700264 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700265 snd_device, platform_get_snd_device_name(snd_device));
266 audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700267 if (update_mixer)
268 audio_route_update_mixer(adev->audio_route);
269 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800270 return 0;
271}
272
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700273static void check_usecases_codec_backend(struct audio_device *adev,
274 struct audio_usecase *uc_info,
275 snd_device_t snd_device)
276{
277 struct listnode *node;
278 struct audio_usecase *usecase;
279 bool switch_device[AUDIO_USECASE_MAX];
280 int i, num_uc_to_switch = 0;
281
282 /*
283 * This function is to make sure that all the usecases that are active on
284 * the hardware codec backend are always routed to any one device that is
285 * handled by the hardware codec.
286 * For example, if low-latency and deep-buffer usecases are currently active
287 * on speaker and out_set_parameters(headset) is received on low-latency
288 * output, then we have to make sure deep-buffer is also switched to headset,
289 * because of the limitation that both the devices cannot be enabled
290 * at the same time as they share the same backend.
291 */
292 /* Disable all the usecases on the shared backend other than the
293 specified usecase */
294 for (i = 0; i < AUDIO_USECASE_MAX; i++)
295 switch_device[i] = false;
296
297 list_for_each(node, &adev->usecase_list) {
298 usecase = node_to_item(node, struct audio_usecase, list);
299 if (usecase->type != PCM_CAPTURE &&
300 usecase != uc_info &&
301 usecase->out_snd_device != snd_device &&
302 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
303 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
304 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700305 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700306 disable_audio_route(adev, usecase, false);
307 switch_device[usecase->id] = true;
308 num_uc_to_switch++;
309 }
310 }
311
312 if (num_uc_to_switch) {
313 /* Make sure all the streams are de-routed before disabling the device */
314 audio_route_update_mixer(adev->audio_route);
315
316 list_for_each(node, &adev->usecase_list) {
317 usecase = node_to_item(node, struct audio_usecase, list);
318 if (switch_device[usecase->id]) {
319 disable_snd_device(adev, usecase->out_snd_device, false);
sangwon.jeon866d5ff2013-10-17 21:42:50 +0900320 }
321 }
322
323 list_for_each(node, &adev->usecase_list) {
324 usecase = node_to_item(node, struct audio_usecase, list);
325 if (switch_device[usecase->id]) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700326 enable_snd_device(adev, snd_device, false);
327 }
328 }
329
330 /* Make sure new snd device is enabled before re-routing the streams */
331 audio_route_update_mixer(adev->audio_route);
332
333 /* Re-route all the usecases on the shared backend other than the
334 specified usecase to new snd devices */
335 list_for_each(node, &adev->usecase_list) {
336 usecase = node_to_item(node, struct audio_usecase, list);
337 /* Update the out_snd_device only before enabling the audio route */
338 if (switch_device[usecase->id] ) {
339 usecase->out_snd_device = snd_device;
340 enable_audio_route(adev, usecase, false);
341 }
342 }
343
344 audio_route_update_mixer(adev->audio_route);
345 }
346}
347
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700348static void check_and_route_capture_usecases(struct audio_device *adev,
349 struct audio_usecase *uc_info,
350 snd_device_t snd_device)
351{
352 struct listnode *node;
353 struct audio_usecase *usecase;
354 bool switch_device[AUDIO_USECASE_MAX];
355 int i, num_uc_to_switch = 0;
356
357 /*
358 * This function is to make sure that all the active capture usecases
359 * are always routed to the same input sound device.
360 * For example, if audio-record and voice-call usecases are currently
361 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
362 * is received for voice call then we have to make sure that audio-record
363 * usecase is also switched to earpiece i.e. voice-dmic-ef,
364 * because of the limitation that two devices cannot be enabled
365 * at the same time if they share the same backend.
366 */
367 for (i = 0; i < AUDIO_USECASE_MAX; i++)
368 switch_device[i] = false;
369
370 list_for_each(node, &adev->usecase_list) {
371 usecase = node_to_item(node, struct audio_usecase, list);
372 if (usecase->type != PCM_PLAYBACK &&
373 usecase != uc_info &&
374 usecase->in_snd_device != snd_device) {
375 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
376 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700377 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700378 disable_audio_route(adev, usecase, false);
379 switch_device[usecase->id] = true;
380 num_uc_to_switch++;
381 }
382 }
383
384 if (num_uc_to_switch) {
385 /* Make sure all the streams are de-routed before disabling the device */
386 audio_route_update_mixer(adev->audio_route);
387
388 list_for_each(node, &adev->usecase_list) {
389 usecase = node_to_item(node, struct audio_usecase, list);
390 if (switch_device[usecase->id]) {
391 disable_snd_device(adev, usecase->in_snd_device, false);
392 enable_snd_device(adev, snd_device, false);
393 }
394 }
395
396 /* Make sure new snd device is enabled before re-routing the streams */
397 audio_route_update_mixer(adev->audio_route);
398
399 /* Re-route all the usecases on the shared backend other than the
400 specified usecase to new snd devices */
401 list_for_each(node, &adev->usecase_list) {
402 usecase = node_to_item(node, struct audio_usecase, list);
403 /* Update the in_snd_device only before enabling the audio route */
404 if (switch_device[usecase->id] ) {
405 usecase->in_snd_device = snd_device;
406 enable_audio_route(adev, usecase, false);
407 }
408 }
409
410 audio_route_update_mixer(adev->audio_route);
411 }
412}
413
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800414
415/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700416static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800417{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700418 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700419 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800420
421 switch (channels) {
422 /*
423 * Do not handle stereo output in Multi-channel cases
424 * Stereo case is handled in normal playback path
425 */
426 case 6:
427 ALOGV("%s: HDMI supports 5.1", __func__);
428 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
429 break;
430 case 8:
431 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
432 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
433 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
434 break;
435 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700436 ALOGE("HDMI does not support multi channel playback");
437 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800438 break;
439 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700440 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800441}
442
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700443static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
444 audio_usecase_t uc_id)
445{
446 struct audio_usecase *usecase;
447 struct listnode *node;
448
449 list_for_each(node, &adev->usecase_list) {
450 usecase = node_to_item(node, struct audio_usecase, list);
451 if (usecase->id == uc_id)
452 return usecase;
453 }
454 return NULL;
455}
456
457static int select_devices(struct audio_device *adev,
458 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800459{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800460 snd_device_t out_snd_device = SND_DEVICE_NONE;
461 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700462 struct audio_usecase *usecase = NULL;
463 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800464 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700465 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800466
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700467 usecase = get_usecase_from_list(adev, uc_id);
468 if (usecase == NULL) {
469 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
470 return -EINVAL;
471 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800472
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700473 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700474 out_snd_device = platform_get_output_snd_device(adev->platform,
475 usecase->stream.out->devices);
476 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700477 usecase->devices = usecase->stream.out->devices;
478 } else {
479 /*
480 * If the voice call is active, use the sound devices of voice call usecase
481 * so that it would not result any device switch. All the usecases will
482 * be switched to new device when select_devices() is called for voice call
483 * usecase. This is to avoid switching devices for voice call when
484 * check_usecases_codec_backend() is called below.
485 */
486 if (adev->in_call) {
487 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
488 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
489 in_snd_device = vc_usecase->in_snd_device;
490 out_snd_device = vc_usecase->out_snd_device;
491 }
492 }
493 if (usecase->type == PCM_PLAYBACK) {
494 usecase->devices = usecase->stream.out->devices;
495 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700496 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700497 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700498 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700499 if (usecase->stream.out == adev->primary_output &&
500 adev->active_input &&
501 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
502 select_devices(adev, adev->active_input->usecase);
503 }
504 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700505 } else if (usecase->type == PCM_CAPTURE) {
506 usecase->devices = usecase->stream.in->device;
507 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700508 if (in_snd_device == SND_DEVICE_NONE) {
509 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
510 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700511 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700512 adev->primary_output->devices);
513 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700514 in_snd_device = platform_get_input_snd_device(adev->platform,
515 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700516 }
517 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700518 }
519 }
520
521 if (out_snd_device == usecase->out_snd_device &&
522 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800523 return 0;
524 }
525
sangwoobc677242013-08-08 16:53:43 +0900526 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700527 out_snd_device, platform_get_snd_device_name(out_snd_device),
528 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800529
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800530 /*
531 * Limitation: While in call, to do a device switch we need to disable
532 * and enable both RX and TX devices though one of them is same as current
533 * device.
534 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700535 if (usecase->type == VOICE_CALL) {
536 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800537 }
538
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700539 /* Disable current sound devices */
540 if (usecase->out_snd_device != SND_DEVICE_NONE) {
541 disable_audio_route(adev, usecase, true);
542 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800543 }
544
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700545 if (usecase->in_snd_device != SND_DEVICE_NONE) {
546 disable_audio_route(adev, usecase, true);
547 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800548 }
549
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700550 /* Enable new sound devices */
551 if (out_snd_device != SND_DEVICE_NONE) {
552 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
553 check_usecases_codec_backend(adev, usecase, out_snd_device);
554 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800555 }
556
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700557 if (in_snd_device != SND_DEVICE_NONE) {
558 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700559 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700560 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700561
Eric Laurentb23d5282013-05-14 15:27:20 -0700562 if (usecase->type == VOICE_CALL)
563 status = platform_switch_voice_call_device_post(adev->platform,
564 out_snd_device,
565 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800566
sangwoo170731f2013-06-08 15:36:36 +0900567 audio_route_update_mixer(adev->audio_route);
568
569 usecase->in_snd_device = in_snd_device;
570 usecase->out_snd_device = out_snd_device;
571
572 enable_audio_route(adev, usecase, true);
573
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800574 return status;
575}
576
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800577static int stop_input_stream(struct stream_in *in)
578{
579 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800580 struct audio_usecase *uc_info;
581 struct audio_device *adev = in->dev;
582
Eric Laurentc8400632013-02-14 19:04:54 -0800583 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800584
Eric Laurent994a6932013-07-17 11:51:42 -0700585 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700586 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800587 uc_info = get_usecase_from_list(adev, in->usecase);
588 if (uc_info == NULL) {
589 ALOGE("%s: Could not find the usecase (%d) in the list",
590 __func__, in->usecase);
591 return -EINVAL;
592 }
593
Eric Laurent150dbfe2013-02-27 14:31:02 -0800594 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700595 disable_audio_route(adev, uc_info, true);
596
597 /* 2. Disable the tx device */
598 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800599
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800600 list_remove(&uc_info->list);
601 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800602
Eric Laurent994a6932013-07-17 11:51:42 -0700603 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800604 return ret;
605}
606
607int start_input_stream(struct stream_in *in)
608{
609 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800610 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800611 struct audio_usecase *uc_info;
612 struct audio_device *adev = in->dev;
613
Eric Laurent994a6932013-07-17 11:51:42 -0700614 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700615 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800616 if (in->pcm_device_id < 0) {
617 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
618 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800619 ret = -EINVAL;
620 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800621 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700622
623 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800624 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
625 uc_info->id = in->usecase;
626 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800627 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700628 uc_info->devices = in->device;
629 uc_info->in_snd_device = SND_DEVICE_NONE;
630 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800631
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800632 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700633 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800634
Eric Laurentc8400632013-02-14 19:04:54 -0800635 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
636 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800637 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
638 PCM_IN, &in->config);
639 if (in->pcm && !pcm_is_ready(in->pcm)) {
640 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
641 pcm_close(in->pcm);
642 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800643 ret = -EIO;
644 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800645 }
Eric Laurent994a6932013-07-17 11:51:42 -0700646 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800647 return ret;
648
649error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800650 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800651
652error_config:
653 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700654 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800655
656 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800657}
658
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700659/* must be called with out->lock locked */
660static int send_offload_cmd_l(struct stream_out* out, int command)
661{
662 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
663
664 ALOGVV("%s %d", __func__, command);
665
666 cmd->cmd = command;
667 list_add_tail(&out->offload_cmd_list, &cmd->node);
668 pthread_cond_signal(&out->offload_cond);
669 return 0;
670}
671
672/* must be called iwth out->lock locked */
673static void stop_compressed_output_l(struct stream_out *out)
674{
675 out->offload_state = OFFLOAD_STATE_IDLE;
676 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700677 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700678 if (out->compr != NULL) {
679 compress_stop(out->compr);
680 while (out->offload_thread_blocked) {
681 pthread_cond_wait(&out->cond, &out->lock);
682 }
683 }
684}
685
686static void *offload_thread_loop(void *context)
687{
688 struct stream_out *out = (struct stream_out *) context;
689 struct listnode *item;
690
691 out->offload_state = OFFLOAD_STATE_IDLE;
692 out->playback_started = 0;
693
694 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
695 set_sched_policy(0, SP_FOREGROUND);
696 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
697
698 ALOGV("%s", __func__);
699 pthread_mutex_lock(&out->lock);
700 for (;;) {
701 struct offload_cmd *cmd = NULL;
702 stream_callback_event_t event;
703 bool send_callback = false;
704
705 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
706 __func__, list_empty(&out->offload_cmd_list),
707 out->offload_state);
708 if (list_empty(&out->offload_cmd_list)) {
709 ALOGV("%s SLEEPING", __func__);
710 pthread_cond_wait(&out->offload_cond, &out->lock);
711 ALOGV("%s RUNNING", __func__);
712 continue;
713 }
714
715 item = list_head(&out->offload_cmd_list);
716 cmd = node_to_item(item, struct offload_cmd, node);
717 list_remove(item);
718
719 ALOGVV("%s STATE %d CMD %d out->compr %p",
720 __func__, out->offload_state, cmd->cmd, out->compr);
721
722 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
723 free(cmd);
724 break;
725 }
726
727 if (out->compr == NULL) {
728 ALOGE("%s: Compress handle is NULL", __func__);
729 pthread_cond_signal(&out->cond);
730 continue;
731 }
732 out->offload_thread_blocked = true;
733 pthread_mutex_unlock(&out->lock);
734 send_callback = false;
735 switch(cmd->cmd) {
736 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
737 compress_wait(out->compr, -1);
738 send_callback = true;
739 event = STREAM_CBK_EVENT_WRITE_READY;
740 break;
741 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700742 compress_next_track(out->compr);
743 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700744 send_callback = true;
745 event = STREAM_CBK_EVENT_DRAIN_READY;
746 break;
747 case OFFLOAD_CMD_DRAIN:
748 compress_drain(out->compr);
749 send_callback = true;
750 event = STREAM_CBK_EVENT_DRAIN_READY;
751 break;
752 default:
753 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
754 break;
755 }
756 pthread_mutex_lock(&out->lock);
757 out->offload_thread_blocked = false;
758 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700759 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700760 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700761 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700762 free(cmd);
763 }
764
765 pthread_cond_signal(&out->cond);
766 while (!list_empty(&out->offload_cmd_list)) {
767 item = list_head(&out->offload_cmd_list);
768 list_remove(item);
769 free(node_to_item(item, struct offload_cmd, node));
770 }
771 pthread_mutex_unlock(&out->lock);
772
773 return NULL;
774}
775
776static int create_offload_callback_thread(struct stream_out *out)
777{
778 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
779 list_init(&out->offload_cmd_list);
780 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
781 offload_thread_loop, out);
782 return 0;
783}
784
785static int destroy_offload_callback_thread(struct stream_out *out)
786{
787 pthread_mutex_lock(&out->lock);
788 stop_compressed_output_l(out);
789 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
790
791 pthread_mutex_unlock(&out->lock);
792 pthread_join(out->offload_thread, (void **) NULL);
793 pthread_cond_destroy(&out->offload_cond);
794
795 return 0;
796}
797
Eric Laurent07eeafd2013-10-06 12:52:49 -0700798static bool allow_hdmi_channel_config(struct audio_device *adev)
799{
800 struct listnode *node;
801 struct audio_usecase *usecase;
802 bool ret = true;
803
804 list_for_each(node, &adev->usecase_list) {
805 usecase = node_to_item(node, struct audio_usecase, list);
806 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
807 /*
808 * If voice call is already existing, do not proceed further to avoid
809 * disabling/enabling both RX and TX devices, CSD calls, etc.
810 * Once the voice call done, the HDMI channels can be configured to
811 * max channels of remaining use cases.
812 */
813 if (usecase->id == USECASE_VOICE_CALL) {
814 ALOGD("%s: voice call is active, no change in HDMI channels",
815 __func__);
816 ret = false;
817 break;
818 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
819 ALOGD("%s: multi channel playback is active, "
820 "no change in HDMI channels", __func__);
821 ret = false;
822 break;
823 }
824 }
825 }
826 return ret;
827}
828
829static int check_and_set_hdmi_channels(struct audio_device *adev,
830 unsigned int channels)
831{
832 struct listnode *node;
833 struct audio_usecase *usecase;
834
835 /* Check if change in HDMI channel config is allowed */
836 if (!allow_hdmi_channel_config(adev))
837 return 0;
838
839 if (channels == adev->cur_hdmi_channels) {
840 ALOGD("%s: Requested channels are same as current", __func__);
841 return 0;
842 }
843
844 platform_set_hdmi_channels(adev->platform, channels);
845 adev->cur_hdmi_channels = channels;
846
847 /*
848 * Deroute all the playback streams routed to HDMI so that
849 * the back end is deactivated. Note that backend will not
850 * be deactivated if any one stream is connected to it.
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 disable_audio_route(adev, usecase, true);
857 }
858 }
859
860 /*
861 * Enable all the streams disabled above. Now the HDMI backend
862 * will be activated with new channel configuration
863 */
864 list_for_each(node, &adev->usecase_list) {
865 usecase = node_to_item(node, struct audio_usecase, list);
866 if (usecase->type == PCM_PLAYBACK &&
867 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
868 enable_audio_route(adev, usecase, true);
869 }
870 }
871
872 return 0;
873}
874
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800875static int stop_output_stream(struct stream_out *out)
876{
877 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800878 struct audio_usecase *uc_info;
879 struct audio_device *adev = out->dev;
880
Eric Laurent994a6932013-07-17 11:51:42 -0700881 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700882 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800883 uc_info = get_usecase_from_list(adev, out->usecase);
884 if (uc_info == NULL) {
885 ALOGE("%s: Could not find the usecase (%d) in the list",
886 __func__, out->usecase);
887 return -EINVAL;
888 }
889
Eric Laurentc4aef752013-09-12 17:45:53 -0700890 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
891 adev->visualizer_stop_output != NULL)
892 adev->visualizer_stop_output(out->handle);
893
Eric Laurent150dbfe2013-02-27 14:31:02 -0800894 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700895 disable_audio_route(adev, uc_info, true);
896
897 /* 2. Disable the rx device */
898 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800899
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800900 list_remove(&uc_info->list);
901 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800902
Eric Laurent07eeafd2013-10-06 12:52:49 -0700903 /* Must be called after removing the usecase from list */
904 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
905 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
906
Eric Laurent994a6932013-07-17 11:51:42 -0700907 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800908 return ret;
909}
910
911int start_output_stream(struct stream_out *out)
912{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800913 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800914 struct audio_usecase *uc_info;
915 struct audio_device *adev = out->dev;
916
Eric Laurent994a6932013-07-17 11:51:42 -0700917 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700918 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -0700919 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800920 if (out->pcm_device_id < 0) {
921 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
922 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800923 ret = -EINVAL;
924 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800925 }
926
927 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
928 uc_info->id = out->usecase;
929 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800930 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700931 uc_info->devices = out->devices;
932 uc_info->in_snd_device = SND_DEVICE_NONE;
933 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800934
Eric Laurent07eeafd2013-10-06 12:52:49 -0700935 /* This must be called before adding this usecase to the list */
936 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
937 check_and_set_hdmi_channels(adev, out->config.channels);
938
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800939 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800940
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700941 select_devices(adev, out->usecase);
942
Andy Hung31aca912014-03-20 17:14:59 -0700943 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
944 __func__, SOUND_CARD, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700945 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
946 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -0700947 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700948 if (out->pcm && !pcm_is_ready(out->pcm)) {
949 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
950 pcm_close(out->pcm);
951 out->pcm = NULL;
952 ret = -EIO;
953 goto error_open;
954 }
955 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800956 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700957 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
958 COMPRESS_IN, &out->compr_config);
959 if (out->compr && !is_compress_ready(out->compr)) {
960 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
961 compress_close(out->compr);
962 out->compr = NULL;
963 ret = -EIO;
964 goto error_open;
965 }
966 if (out->offload_callback)
967 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -0700968
969 if (adev->visualizer_start_output != NULL)
970 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800971 }
Eric Laurent994a6932013-07-17 11:51:42 -0700972 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800973 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700974error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800975 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800976error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800977 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800978}
979
980static int stop_voice_call(struct audio_device *adev)
981{
982 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800983 struct audio_usecase *uc_info;
984
Eric Laurent994a6932013-07-17 11:51:42 -0700985 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800986 adev->in_call = false;
Eric Laurentb23d5282013-05-14 15:27:20 -0700987
988 ret = platform_stop_voice_call(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800989
990 /* 1. Close the PCM devices */
991 if (adev->voice_call_rx) {
992 pcm_close(adev->voice_call_rx);
993 adev->voice_call_rx = NULL;
994 }
995 if (adev->voice_call_tx) {
996 pcm_close(adev->voice_call_tx);
997 adev->voice_call_tx = NULL;
998 }
999
1000 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
1001 if (uc_info == NULL) {
1002 ALOGE("%s: Could not find the usecase (%d) in the list",
1003 __func__, USECASE_VOICE_CALL);
1004 return -EINVAL;
1005 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001006
1007 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001008 disable_audio_route(adev, uc_info, true);
1009
1010 /* 3. Disable the rx and tx devices */
1011 disable_snd_device(adev, uc_info->out_snd_device, false);
1012 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001013
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001014 list_remove(&uc_info->list);
1015 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001016
Eric Laurent994a6932013-07-17 11:51:42 -07001017 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001018 return ret;
1019}
1020
1021static int start_voice_call(struct audio_device *adev)
1022{
1023 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001024 struct audio_usecase *uc_info;
1025 int pcm_dev_rx_id, pcm_dev_tx_id;
1026
Eric Laurent994a6932013-07-17 11:51:42 -07001027 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001028
1029 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1030 uc_info->id = USECASE_VOICE_CALL;
1031 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001032 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001033 uc_info->devices = adev->primary_output->devices;
1034 uc_info->in_snd_device = SND_DEVICE_NONE;
1035 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001036
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001037 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001038
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001039 select_devices(adev, USECASE_VOICE_CALL);
1040
Eric Laurentb23d5282013-05-14 15:27:20 -07001041 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
1042 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001043
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001044 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1045 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1046 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001047 ret = -EIO;
1048 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001049 }
1050
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001051 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001052 __func__, SOUND_CARD, pcm_dev_rx_id);
1053 adev->voice_call_rx = pcm_open(SOUND_CARD,
1054 pcm_dev_rx_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001055 PCM_OUT | PCM_MONOTONIC, &pcm_config_voice_call);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001056 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1057 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001058 ret = -EIO;
1059 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001060 }
1061
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001062 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001063 __func__, SOUND_CARD, pcm_dev_tx_id);
1064 adev->voice_call_tx = pcm_open(SOUND_CARD,
1065 pcm_dev_tx_id,
1066 PCM_IN, &pcm_config_voice_call);
1067 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1068 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001069 ret = -EIO;
1070 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001071 }
Haynes Mathew George5191a852013-09-11 14:19:36 -07001072
1073 /* set cached volume */
1074 set_voice_volume_l(adev, adev->voice_volume);
1075
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001076 pcm_start(adev->voice_call_rx);
1077 pcm_start(adev->voice_call_tx);
1078
Eric Laurentb23d5282013-05-14 15:27:20 -07001079 ret = platform_start_voice_call(adev->platform);
1080 if (ret < 0) {
1081 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
1082 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001083 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001084 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001085 return 0;
1086
1087error_start_voice:
1088 stop_voice_call(adev);
1089
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001090 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001091 return ret;
1092}
1093
1094static int check_input_parameters(uint32_t sample_rate,
1095 audio_format_t format,
1096 int channel_count)
1097{
1098 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1099
1100 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1101
1102 switch (sample_rate) {
1103 case 8000:
1104 case 11025:
1105 case 12000:
1106 case 16000:
1107 case 22050:
1108 case 24000:
1109 case 32000:
1110 case 44100:
1111 case 48000:
1112 break;
1113 default:
1114 return -EINVAL;
1115 }
1116
1117 return 0;
1118}
1119
1120static size_t get_input_buffer_size(uint32_t sample_rate,
1121 audio_format_t format,
1122 int channel_count)
1123{
1124 size_t size = 0;
1125
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001126 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1127 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001128
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001129 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1130 /* ToDo: should use frame_size computed based on the format and
1131 channel_count here. */
1132 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001133
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001134 /* make sure the size is multiple of 64 */
1135 size += 0x3f;
1136 size &= ~0x3f;
1137
1138 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001139}
1140
1141static uint32_t out_get_sample_rate(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 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001146}
1147
1148static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1149{
1150 return -ENOSYS;
1151}
1152
1153static size_t out_get_buffer_size(const struct audio_stream *stream)
1154{
1155 struct stream_out *out = (struct stream_out *)stream;
1156
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001157 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1158 return out->compr_config.fragment_size;
1159 }
1160
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001161 return out->config.period_size * audio_stream_frame_size(stream);
1162}
1163
1164static uint32_t out_get_channels(const struct audio_stream *stream)
1165{
1166 struct stream_out *out = (struct stream_out *)stream;
1167
1168 return out->channel_mask;
1169}
1170
1171static audio_format_t out_get_format(const struct audio_stream *stream)
1172{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001173 struct stream_out *out = (struct stream_out *)stream;
1174
1175 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001176}
1177
1178static int out_set_format(struct audio_stream *stream, audio_format_t format)
1179{
1180 return -ENOSYS;
1181}
1182
1183static int out_standby(struct audio_stream *stream)
1184{
1185 struct stream_out *out = (struct stream_out *)stream;
1186 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001187
Eric Laurent994a6932013-07-17 11:51:42 -07001188 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001189 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001190
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001191 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001192 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001193 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001194 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001195 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1196 if (out->pcm) {
1197 pcm_close(out->pcm);
1198 out->pcm = NULL;
1199 }
1200 } else {
1201 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001202 out->gapless_mdata.encoder_delay = 0;
1203 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001204 if (out->compr != NULL) {
1205 compress_close(out->compr);
1206 out->compr = NULL;
1207 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001208 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001209 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001210 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001211 }
1212 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001213 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001214 return 0;
1215}
1216
1217static int out_dump(const struct audio_stream *stream, int fd)
1218{
1219 return 0;
1220}
1221
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001222static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1223{
1224 int ret = 0;
1225 char value[32];
1226 struct compr_gapless_mdata tmp_mdata;
1227
1228 if (!out || !parms) {
1229 return -EINVAL;
1230 }
1231
1232 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1233 if (ret >= 0) {
1234 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1235 } else {
1236 return -EINVAL;
1237 }
1238
1239 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1240 if (ret >= 0) {
1241 tmp_mdata.encoder_padding = atoi(value);
1242 } else {
1243 return -EINVAL;
1244 }
1245
1246 out->gapless_mdata = tmp_mdata;
1247 out->send_new_metadata = 1;
1248 ALOGV("%s new encoder delay %u and padding %u", __func__,
1249 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1250
1251 return 0;
1252}
1253
1254
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001255static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1256{
1257 struct stream_out *out = (struct stream_out *)stream;
1258 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001259 struct audio_usecase *usecase;
1260 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001261 struct str_parms *parms;
1262 char value[32];
1263 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001264 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001265 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001266
sangwoobc677242013-08-08 16:53:43 +09001267 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001268 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001269 parms = str_parms_create_str(kvpairs);
1270 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1271 if (ret >= 0) {
1272 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001274 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001275
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001276 /*
1277 * When HDMI cable is unplugged the music playback is paused and
1278 * the policy manager sends routing=0. But the audioflinger
1279 * continues to write data until standby time (3sec).
1280 * As the HDMI core is turned off, the write gets blocked.
1281 * Avoid this by routing audio to speaker until standby.
1282 */
1283 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1284 val == AUDIO_DEVICE_NONE) {
1285 val = AUDIO_DEVICE_OUT_SPEAKER;
1286 }
1287
1288 /*
1289 * select_devices() call below switches all the usecases on the same
1290 * backend to the new device. Refer to check_usecases_codec_backend() in
1291 * the select_devices(). But how do we undo this?
1292 *
1293 * For example, music playback is active on headset (deep-buffer usecase)
1294 * and if we go to ringtones and select a ringtone, low-latency usecase
1295 * will be started on headset+speaker. As we can't enable headset+speaker
1296 * and headset devices at the same time, select_devices() switches the music
1297 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1298 * So when the ringtone playback is completed, how do we undo the same?
1299 *
1300 * We are relying on the out_set_parameters() call on deep-buffer output,
1301 * once the ringtone playback is ended.
1302 * NOTE: We should not check if the current devices are same as new devices.
1303 * Because select_devices() must be called to switch back the music
1304 * playback to headset.
1305 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001306 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001307 out->devices = val;
1308
1309 if (!out->standby)
1310 select_devices(adev, out->usecase);
1311
1312 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1313 (out == adev->primary_output)) {
1314 start_voice_call(adev);
1315 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1316 (out == adev->primary_output)) {
1317 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001318 }
1319 }
1320
Ravi Kumar Alamandaed9c56a2013-10-02 09:56:18 -07001321 if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001322 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001324 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001325
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001326 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001327 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001328 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001329
1330 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1331 parse_compress_metadata(out, parms);
1332 }
1333
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001334 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001335 ALOGV("%s: exit: code(%d)", __func__, status);
1336 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337}
1338
1339static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1340{
1341 struct stream_out *out = (struct stream_out *)stream;
1342 struct str_parms *query = str_parms_create_str(keys);
1343 char *str;
1344 char value[256];
1345 struct str_parms *reply = str_parms_create();
1346 size_t i, j;
1347 int ret;
1348 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001349 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001350 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1351 if (ret >= 0) {
1352 value[0] = '\0';
1353 i = 0;
1354 while (out->supported_channel_masks[i] != 0) {
1355 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1356 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1357 if (!first) {
1358 strcat(value, "|");
1359 }
1360 strcat(value, out_channels_name_to_enum_table[j].name);
1361 first = false;
1362 break;
1363 }
1364 }
1365 i++;
1366 }
1367 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1368 str = str_parms_to_str(reply);
1369 } else {
1370 str = strdup(keys);
1371 }
1372 str_parms_destroy(query);
1373 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001374 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001375 return str;
1376}
1377
1378static uint32_t out_get_latency(const struct audio_stream_out *stream)
1379{
1380 struct stream_out *out = (struct stream_out *)stream;
1381
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001382 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1383 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1384
1385 return (out->config.period_count * out->config.period_size * 1000) /
1386 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001387}
1388
1389static int out_set_volume(struct audio_stream_out *stream, float left,
1390 float right)
1391{
Eric Laurenta9024de2013-04-04 09:19:12 -07001392 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001393 int volume[2];
1394
Eric Laurenta9024de2013-04-04 09:19:12 -07001395 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1396 /* only take left channel into account: the API is for stereo anyway */
1397 out->muted = (left == 0.0f);
1398 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001399 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1400 const char *mixer_ctl_name = "Compress Playback Volume";
1401 struct audio_device *adev = out->dev;
1402 struct mixer_ctl *ctl;
1403
1404 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1405 if (!ctl) {
1406 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1407 __func__, mixer_ctl_name);
1408 return -EINVAL;
1409 }
1410 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1411 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1412 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1413 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001414 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001415
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001416 return -ENOSYS;
1417}
1418
1419static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1420 size_t bytes)
1421{
1422 struct stream_out *out = (struct stream_out *)stream;
1423 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001424 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001425
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001426 pthread_mutex_lock(&out->lock);
1427 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001428 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001429 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001430 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001431 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001432 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001433 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001434 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001435 goto exit;
1436 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001437 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001438
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001439 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001440 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1441 if (out->send_new_metadata) {
1442 ALOGVV("send new gapless metadata");
1443 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1444 out->send_new_metadata = 0;
1445 }
1446
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001447 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001448 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001449 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001450 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1451 }
1452 if (!out->playback_started) {
1453 compress_start(out->compr);
1454 out->playback_started = 1;
1455 out->offload_state = OFFLOAD_STATE_PLAYING;
1456 }
1457 pthread_mutex_unlock(&out->lock);
1458 return ret;
1459 } else {
1460 if (out->pcm) {
1461 if (out->muted)
1462 memset((void *)buffer, 0, bytes);
1463 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1464 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001465 if (ret == 0)
1466 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001467 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001468 }
1469
1470exit:
1471 pthread_mutex_unlock(&out->lock);
1472
1473 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001474 if (out->pcm)
1475 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001476 out_standby(&out->stream.common);
1477 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1478 out_get_sample_rate(&out->stream.common));
1479 }
1480 return bytes;
1481}
1482
1483static int out_get_render_position(const struct audio_stream_out *stream,
1484 uint32_t *dsp_frames)
1485{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001486 struct stream_out *out = (struct stream_out *)stream;
1487 *dsp_frames = 0;
1488 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1489 pthread_mutex_lock(&out->lock);
1490 if (out->compr != NULL) {
1491 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1492 &out->sample_rate);
1493 ALOGVV("%s rendered frames %d sample_rate %d",
1494 __func__, *dsp_frames, out->sample_rate);
1495 }
1496 pthread_mutex_unlock(&out->lock);
1497 return 0;
1498 } else
1499 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001500}
1501
1502static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1503{
1504 return 0;
1505}
1506
1507static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1508{
1509 return 0;
1510}
1511
1512static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1513 int64_t *timestamp)
1514{
1515 return -EINVAL;
1516}
1517
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001518static int out_get_presentation_position(const struct audio_stream_out *stream,
1519 uint64_t *frames, struct timespec *timestamp)
1520{
1521 struct stream_out *out = (struct stream_out *)stream;
1522 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001523 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001524
1525 pthread_mutex_lock(&out->lock);
1526
Eric Laurent949a0892013-09-20 09:20:13 -07001527 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1528 if (out->compr != NULL) {
1529 compress_get_tstamp(out->compr, &dsp_frames,
1530 &out->sample_rate);
1531 ALOGVV("%s rendered frames %ld sample_rate %d",
1532 __func__, dsp_frames, out->sample_rate);
1533 *frames = dsp_frames;
1534 ret = 0;
1535 /* this is the best we can do */
1536 clock_gettime(CLOCK_MONOTONIC, timestamp);
1537 }
1538 } else {
1539 if (out->pcm) {
1540 size_t avail;
1541 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1542 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001543 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001544 // This adjustment accounts for buffering after app processor.
1545 // It is based on estimated DSP latency per use case, rather than exact.
1546 signed_frames -=
1547 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1548
Eric Laurent949a0892013-09-20 09:20:13 -07001549 // It would be unusual for this value to be negative, but check just in case ...
1550 if (signed_frames >= 0) {
1551 *frames = signed_frames;
1552 ret = 0;
1553 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001554 }
1555 }
1556 }
1557
1558 pthread_mutex_unlock(&out->lock);
1559
1560 return ret;
1561}
1562
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001563static int out_set_callback(struct audio_stream_out *stream,
1564 stream_callback_t callback, void *cookie)
1565{
1566 struct stream_out *out = (struct stream_out *)stream;
1567
1568 ALOGV("%s", __func__);
1569 pthread_mutex_lock(&out->lock);
1570 out->offload_callback = callback;
1571 out->offload_cookie = cookie;
1572 pthread_mutex_unlock(&out->lock);
1573 return 0;
1574}
1575
1576static int out_pause(struct audio_stream_out* stream)
1577{
1578 struct stream_out *out = (struct stream_out *)stream;
1579 int status = -ENOSYS;
1580 ALOGV("%s", __func__);
1581 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1582 pthread_mutex_lock(&out->lock);
1583 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1584 status = compress_pause(out->compr);
1585 out->offload_state = OFFLOAD_STATE_PAUSED;
1586 }
1587 pthread_mutex_unlock(&out->lock);
1588 }
1589 return status;
1590}
1591
1592static int out_resume(struct audio_stream_out* stream)
1593{
1594 struct stream_out *out = (struct stream_out *)stream;
1595 int status = -ENOSYS;
1596 ALOGV("%s", __func__);
1597 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1598 status = 0;
1599 pthread_mutex_lock(&out->lock);
1600 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1601 status = compress_resume(out->compr);
1602 out->offload_state = OFFLOAD_STATE_PLAYING;
1603 }
1604 pthread_mutex_unlock(&out->lock);
1605 }
1606 return status;
1607}
1608
1609static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1610{
1611 struct stream_out *out = (struct stream_out *)stream;
1612 int status = -ENOSYS;
1613 ALOGV("%s", __func__);
1614 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1615 pthread_mutex_lock(&out->lock);
1616 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1617 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1618 else
1619 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1620 pthread_mutex_unlock(&out->lock);
1621 }
1622 return status;
1623}
1624
1625static int out_flush(struct audio_stream_out* stream)
1626{
1627 struct stream_out *out = (struct stream_out *)stream;
1628 ALOGV("%s", __func__);
1629 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1630 pthread_mutex_lock(&out->lock);
1631 stop_compressed_output_l(out);
1632 pthread_mutex_unlock(&out->lock);
1633 return 0;
1634 }
1635 return -ENOSYS;
1636}
1637
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001638/** audio_stream_in implementation **/
1639static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1640{
1641 struct stream_in *in = (struct stream_in *)stream;
1642
1643 return in->config.rate;
1644}
1645
1646static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1647{
1648 return -ENOSYS;
1649}
1650
1651static size_t in_get_buffer_size(const struct audio_stream *stream)
1652{
1653 struct stream_in *in = (struct stream_in *)stream;
1654
1655 return in->config.period_size * audio_stream_frame_size(stream);
1656}
1657
1658static uint32_t in_get_channels(const struct audio_stream *stream)
1659{
1660 struct stream_in *in = (struct stream_in *)stream;
1661
1662 return in->channel_mask;
1663}
1664
1665static audio_format_t in_get_format(const struct audio_stream *stream)
1666{
1667 return AUDIO_FORMAT_PCM_16_BIT;
1668}
1669
1670static int in_set_format(struct audio_stream *stream, audio_format_t format)
1671{
1672 return -ENOSYS;
1673}
1674
1675static int in_standby(struct audio_stream *stream)
1676{
1677 struct stream_in *in = (struct stream_in *)stream;
1678 struct audio_device *adev = in->dev;
1679 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001680 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001681 pthread_mutex_lock(&in->lock);
1682 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001683 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001684 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001685 if (in->pcm) {
1686 pcm_close(in->pcm);
1687 in->pcm = NULL;
1688 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001689 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001690 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001691 }
1692 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001693 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001694 return status;
1695}
1696
1697static int in_dump(const struct audio_stream *stream, int fd)
1698{
1699 return 0;
1700}
1701
1702static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1703{
1704 struct stream_in *in = (struct stream_in *)stream;
1705 struct audio_device *adev = in->dev;
1706 struct str_parms *parms;
1707 char *str;
1708 char value[32];
1709 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07001710 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001711
Eric Laurent994a6932013-07-17 11:51:42 -07001712 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001713 parms = str_parms_create_str(kvpairs);
1714
1715 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1716
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001717 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001718 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001719 if (ret >= 0) {
1720 val = atoi(value);
1721 /* no audio source uses val == 0 */
1722 if ((in->source != val) && (val != 0)) {
1723 in->source = val;
1724 }
1725 }
1726
1727 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07001728
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001729 if (ret >= 0) {
1730 val = atoi(value);
1731 if ((in->device != val) && (val != 0)) {
1732 in->device = val;
1733 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001734 if (!in->standby)
Eric Laurent03f09432014-03-25 18:09:11 -07001735 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001736 }
1737 }
1738
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001739 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001740 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001741
1742 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001743 ALOGV("%s: exit: status(%d)", __func__, status);
1744 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001745}
1746
1747static char* in_get_parameters(const struct audio_stream *stream,
1748 const char *keys)
1749{
1750 return strdup("");
1751}
1752
1753static int in_set_gain(struct audio_stream_in *stream, float gain)
1754{
1755 return 0;
1756}
1757
1758static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1759 size_t bytes)
1760{
1761 struct stream_in *in = (struct stream_in *)stream;
1762 struct audio_device *adev = in->dev;
1763 int i, ret = -1;
1764
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001765 pthread_mutex_lock(&in->lock);
1766 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001767 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001768 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001769 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001770 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001771 goto exit;
1772 }
1773 in->standby = 0;
1774 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001775
1776 if (in->pcm) {
1777 ret = pcm_read(in->pcm, buffer, bytes);
1778 }
1779
1780 /*
1781 * Instead of writing zeroes here, we could trust the hardware
1782 * to always provide zeroes when muted.
1783 */
1784 if (ret == 0 && adev->mic_mute)
1785 memset(buffer, 0, bytes);
1786
1787exit:
1788 pthread_mutex_unlock(&in->lock);
1789
1790 if (ret != 0) {
1791 in_standby(&in->stream.common);
1792 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1793 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1794 in_get_sample_rate(&in->stream.common));
1795 }
1796 return bytes;
1797}
1798
1799static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1800{
1801 return 0;
1802}
1803
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001804static int add_remove_audio_effect(const struct audio_stream *stream,
1805 effect_handle_t effect,
1806 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001807{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001808 struct stream_in *in = (struct stream_in *)stream;
1809 int status = 0;
1810 effect_descriptor_t desc;
1811
1812 status = (*effect)->get_descriptor(effect, &desc);
1813 if (status != 0)
1814 return status;
1815
1816 pthread_mutex_lock(&in->lock);
1817 pthread_mutex_lock(&in->dev->lock);
1818 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1819 in->enable_aec != enable &&
1820 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1821 in->enable_aec = enable;
1822 if (!in->standby)
1823 select_devices(in->dev, in->usecase);
1824 }
1825 pthread_mutex_unlock(&in->dev->lock);
1826 pthread_mutex_unlock(&in->lock);
1827
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001828 return 0;
1829}
1830
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001831static int in_add_audio_effect(const struct audio_stream *stream,
1832 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001833{
Eric Laurent994a6932013-07-17 11:51:42 -07001834 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001835 return add_remove_audio_effect(stream, effect, true);
1836}
1837
1838static int in_remove_audio_effect(const struct audio_stream *stream,
1839 effect_handle_t effect)
1840{
Eric Laurent994a6932013-07-17 11:51:42 -07001841 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001842 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001843}
1844
1845static int adev_open_output_stream(struct audio_hw_device *dev,
1846 audio_io_handle_t handle,
1847 audio_devices_t devices,
1848 audio_output_flags_t flags,
1849 struct audio_config *config,
1850 struct audio_stream_out **stream_out)
1851{
1852 struct audio_device *adev = (struct audio_device *)dev;
1853 struct stream_out *out;
1854 int i, ret;
1855
Eric Laurent994a6932013-07-17 11:51:42 -07001856 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001857 __func__, config->sample_rate, config->channel_mask, devices, flags);
1858 *stream_out = NULL;
1859 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1860
1861 if (devices == AUDIO_DEVICE_NONE)
1862 devices = AUDIO_DEVICE_OUT_SPEAKER;
1863
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001864 out->flags = flags;
1865 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001866 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001867 out->format = config->format;
1868 out->sample_rate = config->sample_rate;
1869 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1870 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001871 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001872
1873 /* Init use case and pcm_config */
1874 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07001875 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001876 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001877 pthread_mutex_lock(&adev->lock);
1878 ret = read_hdmi_channel_masks(out);
1879 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001880 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001881 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001882
1883 if (config->sample_rate == 0)
1884 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1885 if (config->channel_mask == 0)
1886 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1887
1888 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001889 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001890 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1891 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001892 out->config.rate = config->sample_rate;
1893 out->config.channels = popcount(out->channel_mask);
1894 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001895 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1896 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1897 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1898 ALOGE("%s: Unsupported Offload information", __func__);
1899 ret = -EINVAL;
1900 goto error_open;
1901 }
1902 if (!is_supported_format(config->offload_info.format)) {
1903 ALOGE("%s: Unsupported audio format", __func__);
1904 ret = -EINVAL;
1905 goto error_open;
1906 }
1907
1908 out->compr_config.codec = (struct snd_codec *)
1909 calloc(1, sizeof(struct snd_codec));
1910
1911 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1912 if (config->offload_info.channel_mask)
1913 out->channel_mask = config->offload_info.channel_mask;
1914 else if (config->channel_mask)
1915 out->channel_mask = config->channel_mask;
1916 out->format = config->offload_info.format;
1917 out->sample_rate = config->offload_info.sample_rate;
1918
1919 out->stream.set_callback = out_set_callback;
1920 out->stream.pause = out_pause;
1921 out->stream.resume = out_resume;
1922 out->stream.drain = out_drain;
1923 out->stream.flush = out_flush;
1924
1925 out->compr_config.codec->id =
1926 get_snd_codec_id(config->offload_info.format);
1927 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1928 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1929 out->compr_config.codec->sample_rate =
1930 compress_get_alsa_rate(config->offload_info.sample_rate);
1931 out->compr_config.codec->bit_rate =
1932 config->offload_info.bit_rate;
1933 out->compr_config.codec->ch_in =
1934 popcount(config->channel_mask);
1935 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1936
1937 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1938 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001939
1940 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001941 create_offload_callback_thread(out);
1942 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1943 __func__, config->offload_info.version,
1944 config->offload_info.bit_rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001945 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07001946 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1947 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1948 out->config = pcm_config_deep_buffer;
1949 } else {
1950 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1951 out->config = pcm_config_low_latency;
1952 }
1953 if (config->format != audio_format_from_pcm_format(out->config.format)) {
1954 if (k_enable_extended_precision
1955 && pcm_params_format_test(adev->use_case_table[out->usecase],
1956 pcm_format_from_audio_format(config->format))) {
1957 out->config.format = pcm_format_from_audio_format(config->format);
1958 /* out->format already set to config->format */
1959 } else {
1960 /* deny the externally proposed config format
1961 * and use the one specified in audio_hw layer configuration.
1962 * Note: out->format is returned by out->stream.common.get_format()
1963 * and is used to set config->format in the code several lines below.
1964 */
1965 out->format = audio_format_from_pcm_format(out->config.format);
1966 }
1967 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001968 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001969 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07001970 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
1971 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001972
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001973 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1974 if(adev->primary_output == NULL)
1975 adev->primary_output = out;
1976 else {
1977 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001978 ret = -EEXIST;
1979 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001980 }
1981 }
1982
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001983 /* Check if this usecase is already existing */
1984 pthread_mutex_lock(&adev->lock);
1985 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1986 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001987 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001988 ret = -EEXIST;
1989 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001990 }
1991 pthread_mutex_unlock(&adev->lock);
1992
1993 out->stream.common.get_sample_rate = out_get_sample_rate;
1994 out->stream.common.set_sample_rate = out_set_sample_rate;
1995 out->stream.common.get_buffer_size = out_get_buffer_size;
1996 out->stream.common.get_channels = out_get_channels;
1997 out->stream.common.get_format = out_get_format;
1998 out->stream.common.set_format = out_set_format;
1999 out->stream.common.standby = out_standby;
2000 out->stream.common.dump = out_dump;
2001 out->stream.common.set_parameters = out_set_parameters;
2002 out->stream.common.get_parameters = out_get_parameters;
2003 out->stream.common.add_audio_effect = out_add_audio_effect;
2004 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2005 out->stream.get_latency = out_get_latency;
2006 out->stream.set_volume = out_set_volume;
2007 out->stream.write = out_write;
2008 out->stream.get_render_position = out_get_render_position;
2009 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002010 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002011
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002012 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002013 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002014 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002015
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002016 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2017 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2018
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002019 config->format = out->stream.common.get_format(&out->stream.common);
2020 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2021 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2022
2023 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002024 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002025 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002026
2027error_open:
2028 free(out);
2029 *stream_out = NULL;
2030 ALOGD("%s: exit: ret %d", __func__, ret);
2031 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002032}
2033
2034static void adev_close_output_stream(struct audio_hw_device *dev,
2035 struct audio_stream_out *stream)
2036{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002037 struct stream_out *out = (struct stream_out *)stream;
2038 struct audio_device *adev = out->dev;
2039
Eric Laurent994a6932013-07-17 11:51:42 -07002040 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002041 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002042 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2043 destroy_offload_callback_thread(out);
2044
2045 if (out->compr_config.codec != NULL)
2046 free(out->compr_config.codec);
2047 }
2048 pthread_cond_destroy(&out->cond);
2049 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002050 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002051 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002052}
2053
2054static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2055{
2056 struct audio_device *adev = (struct audio_device *)dev;
2057 struct str_parms *parms;
2058 char *str;
2059 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002060 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002061 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002062 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002063
Eric Laurent994a6932013-07-17 11:51:42 -07002064 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002065
2066 parms = str_parms_create_str(kvpairs);
2067 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
2068 if (ret >= 0) {
2069 int tty_mode;
2070
2071 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
2072 tty_mode = TTY_MODE_OFF;
2073 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
2074 tty_mode = TTY_MODE_VCO;
2075 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
2076 tty_mode = TTY_MODE_HCO;
2077 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
2078 tty_mode = TTY_MODE_FULL;
2079 else
2080 return -EINVAL;
2081
2082 pthread_mutex_lock(&adev->lock);
2083 if (tty_mode != adev->tty_mode) {
2084 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002085 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
2086 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002087 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002088 }
2089 pthread_mutex_unlock(&adev->lock);
2090 }
2091
2092 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2093 if (ret >= 0) {
2094 /* When set to false, HAL should disable EC and NS
2095 * But it is currently not supported.
2096 */
2097 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2098 adev->bluetooth_nrec = true;
2099 else
2100 adev->bluetooth_nrec = false;
2101 }
2102
2103 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2104 if (ret >= 0) {
2105 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2106 adev->screen_off = false;
2107 else
2108 adev->screen_off = true;
2109 }
2110
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002111 ret = str_parms_get_int(parms, "rotation", &val);
2112 if (ret >= 0) {
2113 bool reverse_speakers = false;
2114 switch(val) {
2115 // FIXME: note that the code below assumes that the speakers are in the correct placement
2116 // relative to the user when the device is rotated 90deg from its default rotation. This
2117 // assumption is device-specific, not platform-specific like this code.
2118 case 270:
2119 reverse_speakers = true;
2120 break;
2121 case 0:
2122 case 90:
2123 case 180:
2124 break;
2125 default:
2126 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002127 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002128 }
Eric Laurent03f09432014-03-25 18:09:11 -07002129 if (status == 0) {
2130 pthread_mutex_lock(&adev->lock);
2131 if (adev->speaker_lr_swap != reverse_speakers) {
2132 adev->speaker_lr_swap = reverse_speakers;
2133 // only update the selected device if there is active pcm playback
2134 struct audio_usecase *usecase;
2135 struct listnode *node;
2136 list_for_each(node, &adev->usecase_list) {
2137 usecase = node_to_item(node, struct audio_usecase, list);
2138 if (usecase->type == PCM_PLAYBACK) {
2139 select_devices(adev, usecase->id);
2140 break;
2141 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002142 }
2143 }
Eric Laurent03f09432014-03-25 18:09:11 -07002144 pthread_mutex_unlock(&adev->lock);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002145 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002146 }
2147
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002148 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07002149 ALOGV("%s: exit with code(%d)", __func__, status);
2150 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002151}
2152
2153static char* adev_get_parameters(const struct audio_hw_device *dev,
2154 const char *keys)
2155{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002156 return strdup("");
2157}
2158
2159static int adev_init_check(const struct audio_hw_device *dev)
2160{
2161 return 0;
2162}
2163
Haynes Mathew George5191a852013-09-11 14:19:36 -07002164/* always called with adev lock held */
2165static int set_voice_volume_l(struct audio_device *adev, float volume)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002166{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002167 int vol, err = 0;
2168
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002169 if (adev->mode == AUDIO_MODE_IN_CALL) {
2170 if (volume < 0.0) {
2171 volume = 0.0;
2172 } else if (volume > 1.0) {
2173 volume = 1.0;
2174 }
2175
2176 vol = lrint(volume * 100.0);
2177
2178 // Voice volume levels from android are mapped to driver volume levels as follows.
2179 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
2180 // So adjust the volume to get the correct volume index in driver
2181 vol = 100 - vol;
Eric Laurentb23d5282013-05-14 15:27:20 -07002182
2183 err = platform_set_voice_volume(adev->platform, vol);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002184 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002185 return err;
2186}
2187
Haynes Mathew George5191a852013-09-11 14:19:36 -07002188static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2189{
2190 int ret;
2191 struct audio_device *adev = (struct audio_device *)dev;
2192 pthread_mutex_lock(&adev->lock);
2193 /* cache volume */
2194 adev->voice_volume = volume;
2195 ret = set_voice_volume_l(adev, adev->voice_volume);
2196 pthread_mutex_unlock(&adev->lock);
2197 return ret;
2198}
2199
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002200static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2201{
2202 return -ENOSYS;
2203}
2204
2205static int adev_get_master_volume(struct audio_hw_device *dev,
2206 float *volume)
2207{
2208 return -ENOSYS;
2209}
2210
2211static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2212{
2213 return -ENOSYS;
2214}
2215
2216static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2217{
2218 return -ENOSYS;
2219}
2220
2221static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2222{
2223 struct audio_device *adev = (struct audio_device *)dev;
2224
2225 pthread_mutex_lock(&adev->lock);
2226 if (adev->mode != mode) {
2227 adev->mode = mode;
2228 }
2229 pthread_mutex_unlock(&adev->lock);
2230 return 0;
2231}
2232
2233static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2234{
2235 struct audio_device *adev = (struct audio_device *)dev;
2236 int err = 0;
2237
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002238 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002239 adev->mic_mute = state;
Eric Laurentb23d5282013-05-14 15:27:20 -07002240
2241 err = platform_set_mic_mute(adev->platform, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002242 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002243 return err;
2244}
2245
2246static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2247{
2248 struct audio_device *adev = (struct audio_device *)dev;
2249
2250 *state = adev->mic_mute;
2251
2252 return 0;
2253}
2254
2255static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2256 const struct audio_config *config)
2257{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002258 int channel_count = popcount(config->channel_mask);
2259
2260 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2261}
2262
2263static int adev_open_input_stream(struct audio_hw_device *dev,
2264 audio_io_handle_t handle,
2265 audio_devices_t devices,
2266 struct audio_config *config,
2267 struct audio_stream_in **stream_in)
2268{
2269 struct audio_device *adev = (struct audio_device *)dev;
2270 struct stream_in *in;
2271 int ret, buffer_size, frame_size;
2272 int channel_count = popcount(config->channel_mask);
2273
Eric Laurent994a6932013-07-17 11:51:42 -07002274 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002275 *stream_in = NULL;
2276 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2277 return -EINVAL;
2278
2279 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2280
2281 in->stream.common.get_sample_rate = in_get_sample_rate;
2282 in->stream.common.set_sample_rate = in_set_sample_rate;
2283 in->stream.common.get_buffer_size = in_get_buffer_size;
2284 in->stream.common.get_channels = in_get_channels;
2285 in->stream.common.get_format = in_get_format;
2286 in->stream.common.set_format = in_set_format;
2287 in->stream.common.standby = in_standby;
2288 in->stream.common.dump = in_dump;
2289 in->stream.common.set_parameters = in_set_parameters;
2290 in->stream.common.get_parameters = in_get_parameters;
2291 in->stream.common.add_audio_effect = in_add_audio_effect;
2292 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2293 in->stream.set_gain = in_set_gain;
2294 in->stream.read = in_read;
2295 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2296
2297 in->device = devices;
2298 in->source = AUDIO_SOURCE_DEFAULT;
2299 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002300 in->standby = 1;
2301 in->channel_mask = config->channel_mask;
2302
2303 /* Update config params with the requested sample rate and channels */
2304 in->usecase = USECASE_AUDIO_RECORD;
2305 in->config = pcm_config_audio_capture;
2306 in->config.channels = channel_count;
2307 in->config.rate = config->sample_rate;
2308
2309 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2310 buffer_size = get_input_buffer_size(config->sample_rate,
2311 config->format,
2312 channel_count);
2313 in->config.period_size = buffer_size / frame_size;
2314
2315 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002316 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002317 return 0;
2318
2319err_open:
2320 free(in);
2321 *stream_in = NULL;
2322 return ret;
2323}
2324
2325static void adev_close_input_stream(struct audio_hw_device *dev,
2326 struct audio_stream_in *stream)
2327{
Eric Laurent994a6932013-07-17 11:51:42 -07002328 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002329
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002330 in_standby(&stream->common);
2331 free(stream);
2332
2333 return;
2334}
2335
2336static int adev_dump(const audio_hw_device_t *device, int fd)
2337{
2338 return 0;
2339}
2340
Andy Hung31aca912014-03-20 17:14:59 -07002341/* verifies input and output devices and their capabilities.
2342 *
2343 * This verification is required when enabling extended bit-depth or
2344 * sampling rates, as not all qcom products support it.
2345 *
2346 * Suitable for calling only on initialization such as adev_open().
2347 * It fills the audio_device use_case_table[] array.
2348 *
2349 * Has a side-effect that it needs to configure audio routing / devices
2350 * in order to power up the devices and read the device parameters.
2351 * It does not acquire any hw device lock. Should restore the devices
2352 * back to "normal state" upon completion.
2353 */
2354static int adev_verify_devices(struct audio_device *adev)
2355{
2356 /* enumeration is a bit difficult because one really wants to pull
2357 * the use_case, device id, etc from the hidden pcm_device_table[].
2358 * In this case there are the following use cases and device ids.
2359 *
2360 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
2361 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
2362 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
2363 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
2364 * [USECASE_AUDIO_RECORD] = {0, 0},
2365 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
2366 * [USECASE_VOICE_CALL] = {2, 2},
2367 *
2368 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
2369 * USECASE_VOICE_CALL omitted, but possible for either input or output.
2370 */
2371
2372 /* should be the usecases enabled in adev_open_input_stream() */
2373 static const int test_in_usecases[] = {
2374 USECASE_AUDIO_RECORD,
2375 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
2376 };
2377 /* should be the usecases enabled in adev_open_output_stream()*/
2378 static const int test_out_usecases[] = {
2379 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
2380 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
2381 };
2382 static const usecase_type_t usecase_type_by_dir[] = {
2383 PCM_PLAYBACK,
2384 PCM_CAPTURE,
2385 };
2386 static const unsigned flags_by_dir[] = {
2387 PCM_OUT,
2388 PCM_IN,
2389 };
2390
2391 size_t i;
2392 unsigned dir;
2393 const unsigned card_id = SOUND_CARD;
2394 char info[512]; /* for possible debug info */
2395
2396 for (dir = 0; dir < 2; ++dir) {
2397 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
2398 const unsigned flags_dir = flags_by_dir[dir];
2399 const size_t testsize =
2400 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
2401 const int *testcases =
2402 dir ? test_in_usecases : test_out_usecases;
2403 const audio_devices_t audio_device =
2404 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
2405
2406 for (i = 0; i < testsize; ++i) {
2407 const audio_usecase_t audio_usecase = testcases[i];
2408 int device_id;
2409 snd_device_t snd_device;
2410 struct pcm_params **pparams;
2411 struct stream_out out;
2412 struct stream_in in;
2413 struct audio_usecase uc_info;
2414 int retval;
2415
2416 pparams = &adev->use_case_table[audio_usecase];
2417 pcm_params_free(*pparams); /* can accept null input */
2418 *pparams = NULL;
2419
2420 /* find the device ID for the use case (signed, for error) */
2421 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
2422 if (device_id < 0)
2423 continue;
2424
2425 /* prepare structures for device probing */
2426 memset(&uc_info, 0, sizeof(uc_info));
2427 uc_info.id = audio_usecase;
2428 uc_info.type = usecase_type;
2429 if (dir) {
2430 adev->active_input = &in;
2431 memset(&in, 0, sizeof(in));
2432 in.device = audio_device;
2433 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
2434 uc_info.stream.in = &in;
2435 } else {
2436 adev->active_input = NULL;
2437 }
2438 memset(&out, 0, sizeof(out));
2439 out.devices = audio_device; /* only field needed in select_devices */
2440 uc_info.stream.out = &out;
2441 uc_info.devices = audio_device;
2442 uc_info.in_snd_device = SND_DEVICE_NONE;
2443 uc_info.out_snd_device = SND_DEVICE_NONE;
2444 list_add_tail(&adev->usecase_list, &uc_info.list);
2445
2446 /* select device - similar to start_(in/out)put_stream() */
2447 retval = select_devices(adev, audio_usecase);
2448 if (retval >= 0) {
2449 *pparams = pcm_params_get(card_id, device_id, flags_dir);
2450#if LOG_NDEBUG == 0
2451 if (*pparams) {
2452 ALOGV("%s: (%s) card %d device %d", __func__,
2453 dir ? "input" : "output", card_id, device_id);
2454 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
2455 ALOGV(info); /* print parameters */
2456 } else {
2457 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
2458 }
2459#endif
2460 }
2461
2462 /* deselect device - similar to stop_(in/out)put_stream() */
2463 /* 1. Get and set stream specific mixer controls */
2464 retval = disable_audio_route(adev, &uc_info, true);
2465 /* 2. Disable the rx device */
2466 retval = disable_snd_device(adev,
2467 dir ? uc_info.in_snd_device : uc_info.out_snd_device, true);
2468 list_remove(&uc_info.list);
2469 }
2470 }
2471 adev->active_input = NULL; /* restore adev state */
2472 return 0;
2473}
2474
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002475static int adev_close(hw_device_t *device)
2476{
Andy Hung31aca912014-03-20 17:14:59 -07002477 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002478 struct audio_device *adev = (struct audio_device *)device;
2479 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002480 free(adev->snd_dev_ref_cnt);
2481 platform_deinit(adev->platform);
Andy Hung31aca912014-03-20 17:14:59 -07002482 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
2483 pcm_params_free(adev->use_case_table[i]);
2484 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002485 free(device);
2486 return 0;
2487}
2488
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002489static int adev_open(const hw_module_t *module, const char *name,
2490 hw_device_t **device)
2491{
2492 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002493 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002494
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002495 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002496 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2497
2498 adev = calloc(1, sizeof(struct audio_device));
2499
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002500 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2501 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2502 adev->device.common.module = (struct hw_module_t *)module;
2503 adev->device.common.close = adev_close;
2504
2505 adev->device.init_check = adev_init_check;
2506 adev->device.set_voice_volume = adev_set_voice_volume;
2507 adev->device.set_master_volume = adev_set_master_volume;
2508 adev->device.get_master_volume = adev_get_master_volume;
2509 adev->device.set_master_mute = adev_set_master_mute;
2510 adev->device.get_master_mute = adev_get_master_mute;
2511 adev->device.set_mode = adev_set_mode;
2512 adev->device.set_mic_mute = adev_set_mic_mute;
2513 adev->device.get_mic_mute = adev_get_mic_mute;
2514 adev->device.set_parameters = adev_set_parameters;
2515 adev->device.get_parameters = adev_get_parameters;
2516 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2517 adev->device.open_output_stream = adev_open_output_stream;
2518 adev->device.close_output_stream = adev_close_output_stream;
2519 adev->device.open_input_stream = adev_open_input_stream;
2520 adev->device.close_input_stream = adev_close_input_stream;
2521 adev->device.dump = adev_dump;
2522
2523 /* Set the default route before the PCM stream is opened */
2524 pthread_mutex_lock(&adev->lock);
2525 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002526 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002527 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002528 adev->voice_call_rx = NULL;
2529 adev->voice_call_tx = NULL;
2530 adev->voice_volume = 1.0f;
2531 adev->tty_mode = TTY_MODE_OFF;
2532 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002533 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002534 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002535 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002536 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002537 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002538 pthread_mutex_unlock(&adev->lock);
2539
2540 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002541 adev->platform = platform_init(adev);
2542 if (!adev->platform) {
2543 free(adev->snd_dev_ref_cnt);
2544 free(adev);
2545 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2546 *device = NULL;
2547 return -EINVAL;
2548 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002549
2550 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2551 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2552 if (adev->visualizer_lib == NULL) {
2553 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2554 } else {
2555 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2556 adev->visualizer_start_output =
2557 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2558 "visualizer_hal_start_output");
2559 adev->visualizer_stop_output =
2560 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2561 "visualizer_hal_stop_output");
2562 }
2563 }
2564
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002565 *device = &adev->device.common;
Andy Hung31aca912014-03-20 17:14:59 -07002566 if (k_enable_extended_precision)
2567 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002568
Eric Laurent994a6932013-07-17 11:51:42 -07002569 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002570 return 0;
2571}
2572
2573static struct hw_module_methods_t hal_module_methods = {
2574 .open = adev_open,
2575};
2576
2577struct audio_module HAL_MODULE_INFO_SYM = {
2578 .common = {
2579 .tag = HARDWARE_MODULE_TAG,
2580 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2581 .hal_api_version = HARDWARE_HAL_API_VERSION,
2582 .id = AUDIO_HARDWARE_MODULE_ID,
2583 .name = "QCOM Audio HAL",
2584 .author = "Code Aurora Forum",
2585 .methods = &hal_module_methods,
2586 },
2587};