blob: 9481d4b6c2fa495c29bc575d224383961b85248a [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_primary"
18/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070019/*#define VERY_VERY_VERBOSE_LOGGING*/
20#ifdef VERY_VERY_VERBOSE_LOGGING
21#define ALOGVV ALOGV
22#else
23#define ALOGVV(a...) do { } while(0)
24#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080025
26#include <errno.h>
27#include <pthread.h>
28#include <stdint.h>
29#include <sys/time.h>
30#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080031#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070032#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070033#include <sys/resource.h>
34#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080035
36#include <cutils/log.h>
37#include <cutils/str_parms.h>
38#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070039#include <cutils/atomic.h>
40#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080041
Eric Laurentb23d5282013-05-14 15:27:20 -070042#include <hardware/audio_effect.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070043#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070044#include <audio_effects/effect_aec.h>
45#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080046#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include "platform_api.h"
48#include <platform.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070050#include "sound/compress_params.h"
51
52#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
53#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
54/* ToDo: Check and update a proper value in msec */
55#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
56#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
57
Eric Laurentb23d5282013-05-14 15:27:20 -070058struct pcm_config pcm_config_deep_buffer = {
59 .channels = 2,
60 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
61 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
62 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
63 .format = PCM_FORMAT_S16_LE,
64 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
65 .stop_threshold = INT_MAX,
66 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
67};
68
69struct pcm_config pcm_config_low_latency = {
70 .channels = 2,
71 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
72 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
73 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
74 .format = PCM_FORMAT_S16_LE,
75 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
76 .stop_threshold = INT_MAX,
77 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
78};
79
80struct pcm_config pcm_config_hdmi_multi = {
81 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
82 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
83 .period_size = HDMI_MULTI_PERIOD_SIZE,
84 .period_count = HDMI_MULTI_PERIOD_COUNT,
85 .format = PCM_FORMAT_S16_LE,
86 .start_threshold = 0,
87 .stop_threshold = INT_MAX,
88 .avail_min = 0,
89};
90
91struct pcm_config pcm_config_audio_capture = {
92 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070093 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
94 .format = PCM_FORMAT_S16_LE,
95};
96
97struct pcm_config pcm_config_voice_call = {
98 .channels = 1,
99 .rate = 8000,
100 .period_size = 160,
101 .period_count = 2,
102 .format = PCM_FORMAT_S16_LE,
103};
104
105static const char * const use_case_table[AUDIO_USECASE_MAX] = {
106 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
107 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
108 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
109 [USECASE_AUDIO_RECORD] = "audio-record",
110 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
111 [USECASE_VOICE_CALL] = "voice-call",
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700112 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700113};
114
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800115
116#define STRING_TO_ENUM(string) { #string, string }
117
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800118struct string_to_enum {
119 const char *name;
120 uint32_t value;
121};
122
123static const struct string_to_enum out_channels_name_to_enum_table[] = {
124 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
125 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
126 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
127};
128
Haynes Mathew George5191a852013-09-11 14:19:36 -0700129static int set_voice_volume_l(struct audio_device *adev, float volume);
130
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700131static bool is_supported_format(audio_format_t format)
132{
Eric Laurent86e17132013-09-12 17:49:30 -0700133 if (format == AUDIO_FORMAT_MP3 ||
134 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700135 return true;
136
137 return false;
138}
139
140static int get_snd_codec_id(audio_format_t format)
141{
142 int id = 0;
143
144 switch (format) {
145 case AUDIO_FORMAT_MP3:
146 id = SND_AUDIOCODEC_MP3;
147 break;
148 case AUDIO_FORMAT_AAC:
149 id = SND_AUDIOCODEC_AAC;
150 break;
151 default:
152 ALOGE("%s: Unsupported audio format", __func__);
153 }
154
155 return id;
156}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800157
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700158static int enable_audio_route(struct audio_device *adev,
159 struct audio_usecase *usecase,
160 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800161{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700162 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800164
165 if (usecase == NULL)
166 return -EINVAL;
167
168 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
169
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800170 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700171 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800172 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700173 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800174
175 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700176 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700177 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700178 audio_route_apply_path(adev->audio_route, mixer_path);
179 if (update_mixer)
180 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800181
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800182 ALOGV("%s: exit", __func__);
183 return 0;
184}
185
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700186static int disable_audio_route(struct audio_device *adev,
187 struct audio_usecase *usecase,
188 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800189{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700190 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800191 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800192
193 if (usecase == NULL)
194 return -EINVAL;
195
196 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700197 if (usecase->type == PCM_CAPTURE)
198 snd_device = usecase->in_snd_device;
199 else
200 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800201 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700202 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700203 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700204 audio_route_reset_path(adev->audio_route, mixer_path);
205 if (update_mixer)
206 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800207
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800208 ALOGV("%s: exit", __func__);
209 return 0;
210}
211
212static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700213 snd_device_t snd_device,
214 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800215{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800216 if (snd_device < SND_DEVICE_MIN ||
217 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800218 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800219 return -EINVAL;
220 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700221
222 adev->snd_dev_ref_cnt[snd_device]++;
223 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700224 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700225 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700226 return 0;
227 }
228
Eric Laurentb23d5282013-05-14 15:27:20 -0700229 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700230 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800231 return -EINVAL;
232 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800233
Eric Laurent994a6932013-07-17 11:51:42 -0700234 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700235 snd_device, platform_get_snd_device_name(snd_device));
236 audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700237 if (update_mixer)
238 audio_route_update_mixer(adev->audio_route);
239
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800240 return 0;
241}
242
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700243static int disable_snd_device(struct audio_device *adev,
244 snd_device_t snd_device,
245 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800246{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800247 if (snd_device < SND_DEVICE_MIN ||
248 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800249 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800250 return -EINVAL;
251 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700252 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
253 ALOGE("%s: device ref cnt is already 0", __func__);
254 return -EINVAL;
255 }
256 adev->snd_dev_ref_cnt[snd_device]--;
257 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700258 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700259 snd_device, platform_get_snd_device_name(snd_device));
260 audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700261 if (update_mixer)
262 audio_route_update_mixer(adev->audio_route);
263 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800264 return 0;
265}
266
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700267static void check_usecases_codec_backend(struct audio_device *adev,
268 struct audio_usecase *uc_info,
269 snd_device_t snd_device)
270{
271 struct listnode *node;
272 struct audio_usecase *usecase;
273 bool switch_device[AUDIO_USECASE_MAX];
274 int i, num_uc_to_switch = 0;
275
276 /*
277 * This function is to make sure that all the usecases that are active on
278 * the hardware codec backend are always routed to any one device that is
279 * handled by the hardware codec.
280 * For example, if low-latency and deep-buffer usecases are currently active
281 * on speaker and out_set_parameters(headset) is received on low-latency
282 * output, then we have to make sure deep-buffer is also switched to headset,
283 * because of the limitation that both the devices cannot be enabled
284 * at the same time as they share the same backend.
285 */
286 /* Disable all the usecases on the shared backend other than the
287 specified usecase */
288 for (i = 0; i < AUDIO_USECASE_MAX; i++)
289 switch_device[i] = false;
290
291 list_for_each(node, &adev->usecase_list) {
292 usecase = node_to_item(node, struct audio_usecase, list);
293 if (usecase->type != PCM_CAPTURE &&
294 usecase != uc_info &&
295 usecase->out_snd_device != snd_device &&
296 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
297 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
298 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700299 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700300 disable_audio_route(adev, usecase, false);
301 switch_device[usecase->id] = true;
302 num_uc_to_switch++;
303 }
304 }
305
306 if (num_uc_to_switch) {
307 /* Make sure all the streams are de-routed before disabling the device */
308 audio_route_update_mixer(adev->audio_route);
309
310 list_for_each(node, &adev->usecase_list) {
311 usecase = node_to_item(node, struct audio_usecase, list);
312 if (switch_device[usecase->id]) {
313 disable_snd_device(adev, usecase->out_snd_device, false);
sangwon.jeon866d5ff2013-10-17 21:42:50 +0900314 }
315 }
316
317 list_for_each(node, &adev->usecase_list) {
318 usecase = node_to_item(node, struct audio_usecase, list);
319 if (switch_device[usecase->id]) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700320 enable_snd_device(adev, snd_device, false);
321 }
322 }
323
324 /* Make sure new snd device is enabled before re-routing the streams */
325 audio_route_update_mixer(adev->audio_route);
326
327 /* Re-route all the usecases on the shared backend other than the
328 specified usecase to new snd devices */
329 list_for_each(node, &adev->usecase_list) {
330 usecase = node_to_item(node, struct audio_usecase, list);
331 /* Update the out_snd_device only before enabling the audio route */
332 if (switch_device[usecase->id] ) {
333 usecase->out_snd_device = snd_device;
334 enable_audio_route(adev, usecase, false);
335 }
336 }
337
338 audio_route_update_mixer(adev->audio_route);
339 }
340}
341
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700342static void check_and_route_capture_usecases(struct audio_device *adev,
343 struct audio_usecase *uc_info,
344 snd_device_t snd_device)
345{
346 struct listnode *node;
347 struct audio_usecase *usecase;
348 bool switch_device[AUDIO_USECASE_MAX];
349 int i, num_uc_to_switch = 0;
350
351 /*
352 * This function is to make sure that all the active capture usecases
353 * are always routed to the same input sound device.
354 * For example, if audio-record and voice-call usecases are currently
355 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
356 * is received for voice call then we have to make sure that audio-record
357 * usecase is also switched to earpiece i.e. voice-dmic-ef,
358 * because of the limitation that two devices cannot be enabled
359 * at the same time if they share the same backend.
360 */
361 for (i = 0; i < AUDIO_USECASE_MAX; i++)
362 switch_device[i] = false;
363
364 list_for_each(node, &adev->usecase_list) {
365 usecase = node_to_item(node, struct audio_usecase, list);
366 if (usecase->type != PCM_PLAYBACK &&
367 usecase != uc_info &&
368 usecase->in_snd_device != snd_device) {
369 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
370 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700371 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700372 disable_audio_route(adev, usecase, false);
373 switch_device[usecase->id] = true;
374 num_uc_to_switch++;
375 }
376 }
377
378 if (num_uc_to_switch) {
379 /* Make sure all the streams are de-routed before disabling the device */
380 audio_route_update_mixer(adev->audio_route);
381
382 list_for_each(node, &adev->usecase_list) {
383 usecase = node_to_item(node, struct audio_usecase, list);
384 if (switch_device[usecase->id]) {
385 disable_snd_device(adev, usecase->in_snd_device, false);
386 enable_snd_device(adev, snd_device, false);
387 }
388 }
389
390 /* Make sure new snd device is enabled before re-routing the streams */
391 audio_route_update_mixer(adev->audio_route);
392
393 /* Re-route all the usecases on the shared backend other than the
394 specified usecase to new snd devices */
395 list_for_each(node, &adev->usecase_list) {
396 usecase = node_to_item(node, struct audio_usecase, list);
397 /* Update the in_snd_device only before enabling the audio route */
398 if (switch_device[usecase->id] ) {
399 usecase->in_snd_device = snd_device;
400 enable_audio_route(adev, usecase, false);
401 }
402 }
403
404 audio_route_update_mixer(adev->audio_route);
405 }
406}
407
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800408
409/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700410static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800411{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700412 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700413 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800414
415 switch (channels) {
416 /*
417 * Do not handle stereo output in Multi-channel cases
418 * Stereo case is handled in normal playback path
419 */
420 case 6:
421 ALOGV("%s: HDMI supports 5.1", __func__);
422 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
423 break;
424 case 8:
425 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
426 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
427 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
428 break;
429 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700430 ALOGE("HDMI does not support multi channel playback");
431 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800432 break;
433 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700434 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800435}
436
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700437static struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
438 audio_usecase_t uc_id)
439{
440 struct audio_usecase *usecase;
441 struct listnode *node;
442
443 list_for_each(node, &adev->usecase_list) {
444 usecase = node_to_item(node, struct audio_usecase, list);
445 if (usecase->id == uc_id)
446 return usecase;
447 }
448 return NULL;
449}
450
451static int select_devices(struct audio_device *adev,
452 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800453{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800454 snd_device_t out_snd_device = SND_DEVICE_NONE;
455 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700456 struct audio_usecase *usecase = NULL;
457 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800458 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700459 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800460
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700461 usecase = get_usecase_from_list(adev, uc_id);
462 if (usecase == NULL) {
463 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
464 return -EINVAL;
465 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800466
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700467 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700468 out_snd_device = platform_get_output_snd_device(adev->platform,
469 usecase->stream.out->devices);
470 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700471 usecase->devices = usecase->stream.out->devices;
472 } else {
473 /*
474 * If the voice call is active, use the sound devices of voice call usecase
475 * so that it would not result any device switch. All the usecases will
476 * be switched to new device when select_devices() is called for voice call
477 * usecase. This is to avoid switching devices for voice call when
478 * check_usecases_codec_backend() is called below.
479 */
480 if (adev->in_call) {
481 vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL);
482 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
483 in_snd_device = vc_usecase->in_snd_device;
484 out_snd_device = vc_usecase->out_snd_device;
485 }
486 }
487 if (usecase->type == PCM_PLAYBACK) {
488 usecase->devices = usecase->stream.out->devices;
489 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700490 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700491 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700492 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700493 if (usecase->stream.out == adev->primary_output &&
494 adev->active_input &&
495 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
496 select_devices(adev, adev->active_input->usecase);
497 }
498 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700499 } else if (usecase->type == PCM_CAPTURE) {
500 usecase->devices = usecase->stream.in->device;
501 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700502 if (in_snd_device == SND_DEVICE_NONE) {
503 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
504 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700505 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700506 adev->primary_output->devices);
507 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700508 in_snd_device = platform_get_input_snd_device(adev->platform,
509 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700510 }
511 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700512 }
513 }
514
515 if (out_snd_device == usecase->out_snd_device &&
516 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800517 return 0;
518 }
519
sangwoobc677242013-08-08 16:53:43 +0900520 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700521 out_snd_device, platform_get_snd_device_name(out_snd_device),
522 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800523
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800524 /*
525 * Limitation: While in call, to do a device switch we need to disable
526 * and enable both RX and TX devices though one of them is same as current
527 * device.
528 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700529 if (usecase->type == VOICE_CALL) {
530 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800531 }
532
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700533 /* Disable current sound devices */
534 if (usecase->out_snd_device != SND_DEVICE_NONE) {
535 disable_audio_route(adev, usecase, true);
536 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800537 }
538
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700539 if (usecase->in_snd_device != SND_DEVICE_NONE) {
540 disable_audio_route(adev, usecase, true);
541 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800542 }
543
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700544 /* Enable new sound devices */
545 if (out_snd_device != SND_DEVICE_NONE) {
546 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
547 check_usecases_codec_backend(adev, usecase, out_snd_device);
548 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800549 }
550
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700551 if (in_snd_device != SND_DEVICE_NONE) {
552 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700553 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700554 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700555
Eric Laurentb23d5282013-05-14 15:27:20 -0700556 if (usecase->type == VOICE_CALL)
557 status = platform_switch_voice_call_device_post(adev->platform,
558 out_snd_device,
559 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800560
sangwoo170731f2013-06-08 15:36:36 +0900561 audio_route_update_mixer(adev->audio_route);
562
563 usecase->in_snd_device = in_snd_device;
564 usecase->out_snd_device = out_snd_device;
565
566 enable_audio_route(adev, usecase, true);
567
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800568 return status;
569}
570
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800571static int stop_input_stream(struct stream_in *in)
572{
573 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800574 struct audio_usecase *uc_info;
575 struct audio_device *adev = in->dev;
576
Eric Laurentc8400632013-02-14 19:04:54 -0800577 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800578
Eric Laurent994a6932013-07-17 11:51:42 -0700579 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700580 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800581 uc_info = get_usecase_from_list(adev, in->usecase);
582 if (uc_info == NULL) {
583 ALOGE("%s: Could not find the usecase (%d) in the list",
584 __func__, in->usecase);
585 return -EINVAL;
586 }
587
Eric Laurent150dbfe2013-02-27 14:31:02 -0800588 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700589 disable_audio_route(adev, uc_info, true);
590
591 /* 2. Disable the tx device */
592 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800593
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800594 list_remove(&uc_info->list);
595 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800596
Eric Laurent994a6932013-07-17 11:51:42 -0700597 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800598 return ret;
599}
600
601int start_input_stream(struct stream_in *in)
602{
603 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800604 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800605 struct audio_usecase *uc_info;
606 struct audio_device *adev = in->dev;
607
Eric Laurent994a6932013-07-17 11:51:42 -0700608 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700609 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800610 if (in->pcm_device_id < 0) {
611 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
612 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800613 ret = -EINVAL;
614 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800615 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700616
617 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800618 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
619 uc_info->id = in->usecase;
620 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800621 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700622 uc_info->devices = in->device;
623 uc_info->in_snd_device = SND_DEVICE_NONE;
624 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800625
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800626 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700627 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800628
Eric Laurentc8400632013-02-14 19:04:54 -0800629 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
630 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800631 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
632 PCM_IN, &in->config);
633 if (in->pcm && !pcm_is_ready(in->pcm)) {
634 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
635 pcm_close(in->pcm);
636 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800637 ret = -EIO;
638 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800639 }
Eric Laurent994a6932013-07-17 11:51:42 -0700640 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800641 return ret;
642
643error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800644 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800645
646error_config:
647 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700648 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800649
650 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800651}
652
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700653/* must be called with out->lock locked */
654static int send_offload_cmd_l(struct stream_out* out, int command)
655{
656 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
657
658 ALOGVV("%s %d", __func__, command);
659
660 cmd->cmd = command;
661 list_add_tail(&out->offload_cmd_list, &cmd->node);
662 pthread_cond_signal(&out->offload_cond);
663 return 0;
664}
665
666/* must be called iwth out->lock locked */
667static void stop_compressed_output_l(struct stream_out *out)
668{
669 out->offload_state = OFFLOAD_STATE_IDLE;
670 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700671 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700672 if (out->compr != NULL) {
673 compress_stop(out->compr);
674 while (out->offload_thread_blocked) {
675 pthread_cond_wait(&out->cond, &out->lock);
676 }
677 }
678}
679
680static void *offload_thread_loop(void *context)
681{
682 struct stream_out *out = (struct stream_out *) context;
683 struct listnode *item;
684
685 out->offload_state = OFFLOAD_STATE_IDLE;
686 out->playback_started = 0;
687
688 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
689 set_sched_policy(0, SP_FOREGROUND);
690 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
691
692 ALOGV("%s", __func__);
693 pthread_mutex_lock(&out->lock);
694 for (;;) {
695 struct offload_cmd *cmd = NULL;
696 stream_callback_event_t event;
697 bool send_callback = false;
698
699 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
700 __func__, list_empty(&out->offload_cmd_list),
701 out->offload_state);
702 if (list_empty(&out->offload_cmd_list)) {
703 ALOGV("%s SLEEPING", __func__);
704 pthread_cond_wait(&out->offload_cond, &out->lock);
705 ALOGV("%s RUNNING", __func__);
706 continue;
707 }
708
709 item = list_head(&out->offload_cmd_list);
710 cmd = node_to_item(item, struct offload_cmd, node);
711 list_remove(item);
712
713 ALOGVV("%s STATE %d CMD %d out->compr %p",
714 __func__, out->offload_state, cmd->cmd, out->compr);
715
716 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
717 free(cmd);
718 break;
719 }
720
721 if (out->compr == NULL) {
722 ALOGE("%s: Compress handle is NULL", __func__);
723 pthread_cond_signal(&out->cond);
724 continue;
725 }
726 out->offload_thread_blocked = true;
727 pthread_mutex_unlock(&out->lock);
728 send_callback = false;
729 switch(cmd->cmd) {
730 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
731 compress_wait(out->compr, -1);
732 send_callback = true;
733 event = STREAM_CBK_EVENT_WRITE_READY;
734 break;
735 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700736 compress_next_track(out->compr);
737 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700738 send_callback = true;
739 event = STREAM_CBK_EVENT_DRAIN_READY;
740 break;
741 case OFFLOAD_CMD_DRAIN:
742 compress_drain(out->compr);
743 send_callback = true;
744 event = STREAM_CBK_EVENT_DRAIN_READY;
745 break;
746 default:
747 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
748 break;
749 }
750 pthread_mutex_lock(&out->lock);
751 out->offload_thread_blocked = false;
752 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700753 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700754 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700755 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700756 free(cmd);
757 }
758
759 pthread_cond_signal(&out->cond);
760 while (!list_empty(&out->offload_cmd_list)) {
761 item = list_head(&out->offload_cmd_list);
762 list_remove(item);
763 free(node_to_item(item, struct offload_cmd, node));
764 }
765 pthread_mutex_unlock(&out->lock);
766
767 return NULL;
768}
769
770static int create_offload_callback_thread(struct stream_out *out)
771{
772 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
773 list_init(&out->offload_cmd_list);
774 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
775 offload_thread_loop, out);
776 return 0;
777}
778
779static int destroy_offload_callback_thread(struct stream_out *out)
780{
781 pthread_mutex_lock(&out->lock);
782 stop_compressed_output_l(out);
783 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
784
785 pthread_mutex_unlock(&out->lock);
786 pthread_join(out->offload_thread, (void **) NULL);
787 pthread_cond_destroy(&out->offload_cond);
788
789 return 0;
790}
791
Eric Laurent07eeafd2013-10-06 12:52:49 -0700792static bool allow_hdmi_channel_config(struct audio_device *adev)
793{
794 struct listnode *node;
795 struct audio_usecase *usecase;
796 bool ret = true;
797
798 list_for_each(node, &adev->usecase_list) {
799 usecase = node_to_item(node, struct audio_usecase, list);
800 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
801 /*
802 * If voice call is already existing, do not proceed further to avoid
803 * disabling/enabling both RX and TX devices, CSD calls, etc.
804 * Once the voice call done, the HDMI channels can be configured to
805 * max channels of remaining use cases.
806 */
807 if (usecase->id == USECASE_VOICE_CALL) {
808 ALOGD("%s: voice call is active, no change in HDMI channels",
809 __func__);
810 ret = false;
811 break;
812 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
813 ALOGD("%s: multi channel playback is active, "
814 "no change in HDMI channels", __func__);
815 ret = false;
816 break;
817 }
818 }
819 }
820 return ret;
821}
822
823static int check_and_set_hdmi_channels(struct audio_device *adev,
824 unsigned int channels)
825{
826 struct listnode *node;
827 struct audio_usecase *usecase;
828
829 /* Check if change in HDMI channel config is allowed */
830 if (!allow_hdmi_channel_config(adev))
831 return 0;
832
833 if (channels == adev->cur_hdmi_channels) {
834 ALOGD("%s: Requested channels are same as current", __func__);
835 return 0;
836 }
837
838 platform_set_hdmi_channels(adev->platform, channels);
839 adev->cur_hdmi_channels = channels;
840
841 /*
842 * Deroute all the playback streams routed to HDMI so that
843 * the back end is deactivated. Note that backend will not
844 * be deactivated if any one stream is connected to it.
845 */
846 list_for_each(node, &adev->usecase_list) {
847 usecase = node_to_item(node, struct audio_usecase, list);
848 if (usecase->type == PCM_PLAYBACK &&
849 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
850 disable_audio_route(adev, usecase, true);
851 }
852 }
853
854 /*
855 * Enable all the streams disabled above. Now the HDMI backend
856 * will be activated with new channel configuration
857 */
858 list_for_each(node, &adev->usecase_list) {
859 usecase = node_to_item(node, struct audio_usecase, list);
860 if (usecase->type == PCM_PLAYBACK &&
861 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
862 enable_audio_route(adev, usecase, true);
863 }
864 }
865
866 return 0;
867}
868
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800869static int stop_output_stream(struct stream_out *out)
870{
871 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800872 struct audio_usecase *uc_info;
873 struct audio_device *adev = out->dev;
874
Eric Laurent994a6932013-07-17 11:51:42 -0700875 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700876 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800877 uc_info = get_usecase_from_list(adev, out->usecase);
878 if (uc_info == NULL) {
879 ALOGE("%s: Could not find the usecase (%d) in the list",
880 __func__, out->usecase);
881 return -EINVAL;
882 }
883
Eric Laurentc4aef752013-09-12 17:45:53 -0700884 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
885 adev->visualizer_stop_output != NULL)
886 adev->visualizer_stop_output(out->handle);
887
Eric Laurent150dbfe2013-02-27 14:31:02 -0800888 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700889 disable_audio_route(adev, uc_info, true);
890
891 /* 2. Disable the rx device */
892 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800893
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800894 list_remove(&uc_info->list);
895 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800896
Eric Laurent07eeafd2013-10-06 12:52:49 -0700897 /* Must be called after removing the usecase from list */
898 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
899 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
900
Eric Laurent994a6932013-07-17 11:51:42 -0700901 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800902 return ret;
903}
904
905int start_output_stream(struct stream_out *out)
906{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800907 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800908 struct audio_usecase *uc_info;
909 struct audio_device *adev = out->dev;
910
Eric Laurent994a6932013-07-17 11:51:42 -0700911 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700912 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -0700913 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800914 if (out->pcm_device_id < 0) {
915 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
916 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800917 ret = -EINVAL;
918 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800919 }
920
921 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
922 uc_info->id = out->usecase;
923 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800924 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700925 uc_info->devices = out->devices;
926 uc_info->in_snd_device = SND_DEVICE_NONE;
927 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800928
Eric Laurent07eeafd2013-10-06 12:52:49 -0700929 /* This must be called before adding this usecase to the list */
930 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
931 check_and_set_hdmi_channels(adev, out->config.channels);
932
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800933 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800934
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700935 select_devices(adev, out->usecase);
936
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800937 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
938 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700939 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
940 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -0700941 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700942 if (out->pcm && !pcm_is_ready(out->pcm)) {
943 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
944 pcm_close(out->pcm);
945 out->pcm = NULL;
946 ret = -EIO;
947 goto error_open;
948 }
949 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800950 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700951 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
952 COMPRESS_IN, &out->compr_config);
953 if (out->compr && !is_compress_ready(out->compr)) {
954 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
955 compress_close(out->compr);
956 out->compr = NULL;
957 ret = -EIO;
958 goto error_open;
959 }
960 if (out->offload_callback)
961 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -0700962
963 if (adev->visualizer_start_output != NULL)
964 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800965 }
Eric Laurent994a6932013-07-17 11:51:42 -0700966 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800967 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700968error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800969 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800970error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800971 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800972}
973
974static int stop_voice_call(struct audio_device *adev)
975{
976 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800977 struct audio_usecase *uc_info;
978
Eric Laurent994a6932013-07-17 11:51:42 -0700979 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -0800980 adev->in_call = false;
Eric Laurentb23d5282013-05-14 15:27:20 -0700981
982 ret = platform_stop_voice_call(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800983
984 /* 1. Close the PCM devices */
985 if (adev->voice_call_rx) {
986 pcm_close(adev->voice_call_rx);
987 adev->voice_call_rx = NULL;
988 }
989 if (adev->voice_call_tx) {
990 pcm_close(adev->voice_call_tx);
991 adev->voice_call_tx = NULL;
992 }
993
994 uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL);
995 if (uc_info == NULL) {
996 ALOGE("%s: Could not find the usecase (%d) in the list",
997 __func__, USECASE_VOICE_CALL);
998 return -EINVAL;
999 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001000
1001 /* 2. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001002 disable_audio_route(adev, uc_info, true);
1003
1004 /* 3. Disable the rx and tx devices */
1005 disable_snd_device(adev, uc_info->out_snd_device, false);
1006 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001007
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001008 list_remove(&uc_info->list);
1009 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001010
Eric Laurent994a6932013-07-17 11:51:42 -07001011 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001012 return ret;
1013}
1014
1015static int start_voice_call(struct audio_device *adev)
1016{
1017 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001018 struct audio_usecase *uc_info;
1019 int pcm_dev_rx_id, pcm_dev_tx_id;
1020
Eric Laurent994a6932013-07-17 11:51:42 -07001021 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001022
1023 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1024 uc_info->id = USECASE_VOICE_CALL;
1025 uc_info->type = VOICE_CALL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001026 uc_info->stream.out = adev->primary_output;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001027 uc_info->devices = adev->primary_output->devices;
1028 uc_info->in_snd_device = SND_DEVICE_NONE;
1029 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001030
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001031 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001032
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001033 select_devices(adev, USECASE_VOICE_CALL);
1034
Eric Laurentb23d5282013-05-14 15:27:20 -07001035 pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK);
1036 pcm_dev_tx_id = platform_get_pcm_device_id(uc_info->id, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001037
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001038 if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) {
1039 ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)",
1040 __func__, pcm_dev_rx_id, pcm_dev_tx_id, uc_info->id);
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001041 ret = -EIO;
1042 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001043 }
1044
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001045 ALOGV("%s: Opening PCM playback device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001046 __func__, SOUND_CARD, pcm_dev_rx_id);
1047 adev->voice_call_rx = pcm_open(SOUND_CARD,
1048 pcm_dev_rx_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001049 PCM_OUT | PCM_MONOTONIC, &pcm_config_voice_call);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001050 if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) {
1051 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001052 ret = -EIO;
1053 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001054 }
1055
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001056 ALOGV("%s: Opening PCM capture device card_id(%d) device_id(%d)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001057 __func__, SOUND_CARD, pcm_dev_tx_id);
1058 adev->voice_call_tx = pcm_open(SOUND_CARD,
1059 pcm_dev_tx_id,
1060 PCM_IN, &pcm_config_voice_call);
1061 if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) {
1062 ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx));
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001063 ret = -EIO;
1064 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001065 }
Haynes Mathew George5191a852013-09-11 14:19:36 -07001066
1067 /* set cached volume */
1068 set_voice_volume_l(adev, adev->voice_volume);
1069
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001070 pcm_start(adev->voice_call_rx);
1071 pcm_start(adev->voice_call_tx);
1072
Eric Laurentb23d5282013-05-14 15:27:20 -07001073 ret = platform_start_voice_call(adev->platform);
1074 if (ret < 0) {
1075 ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret);
1076 goto error_start_voice;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001077 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001078 adev->in_call = true;
Ravi Kumar Alamanda8b9c5c82013-02-20 16:59:34 -08001079 return 0;
1080
1081error_start_voice:
1082 stop_voice_call(adev);
1083
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001084 ALOGD("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001085 return ret;
1086}
1087
1088static int check_input_parameters(uint32_t sample_rate,
1089 audio_format_t format,
1090 int channel_count)
1091{
1092 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1093
1094 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1095
1096 switch (sample_rate) {
1097 case 8000:
1098 case 11025:
1099 case 12000:
1100 case 16000:
1101 case 22050:
1102 case 24000:
1103 case 32000:
1104 case 44100:
1105 case 48000:
1106 break;
1107 default:
1108 return -EINVAL;
1109 }
1110
1111 return 0;
1112}
1113
1114static size_t get_input_buffer_size(uint32_t sample_rate,
1115 audio_format_t format,
1116 int channel_count)
1117{
1118 size_t size = 0;
1119
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001120 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1121 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001122
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001123 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1124 /* ToDo: should use frame_size computed based on the format and
1125 channel_count here. */
1126 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001127
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001128 /* make sure the size is multiple of 64 */
1129 size += 0x3f;
1130 size &= ~0x3f;
1131
1132 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001133}
1134
1135static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1136{
1137 struct stream_out *out = (struct stream_out *)stream;
1138
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001139 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001140}
1141
1142static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1143{
1144 return -ENOSYS;
1145}
1146
1147static size_t out_get_buffer_size(const struct audio_stream *stream)
1148{
1149 struct stream_out *out = (struct stream_out *)stream;
1150
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001151 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1152 return out->compr_config.fragment_size;
1153 }
1154
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001155 return out->config.period_size * audio_stream_frame_size(stream);
1156}
1157
1158static uint32_t out_get_channels(const struct audio_stream *stream)
1159{
1160 struct stream_out *out = (struct stream_out *)stream;
1161
1162 return out->channel_mask;
1163}
1164
1165static audio_format_t out_get_format(const struct audio_stream *stream)
1166{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001167 struct stream_out *out = (struct stream_out *)stream;
1168
1169 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001170}
1171
1172static int out_set_format(struct audio_stream *stream, audio_format_t format)
1173{
1174 return -ENOSYS;
1175}
1176
1177static int out_standby(struct audio_stream *stream)
1178{
1179 struct stream_out *out = (struct stream_out *)stream;
1180 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001181
Eric Laurent994a6932013-07-17 11:51:42 -07001182 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001183 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001184
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001185 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001186 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001187 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001188 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001189 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1190 if (out->pcm) {
1191 pcm_close(out->pcm);
1192 out->pcm = NULL;
1193 }
1194 } else {
1195 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001196 out->gapless_mdata.encoder_delay = 0;
1197 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001198 if (out->compr != NULL) {
1199 compress_close(out->compr);
1200 out->compr = NULL;
1201 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001202 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001203 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001204 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001205 }
1206 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001207 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001208 return 0;
1209}
1210
1211static int out_dump(const struct audio_stream *stream, int fd)
1212{
1213 return 0;
1214}
1215
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001216static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1217{
1218 int ret = 0;
1219 char value[32];
1220 struct compr_gapless_mdata tmp_mdata;
1221
1222 if (!out || !parms) {
1223 return -EINVAL;
1224 }
1225
1226 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1227 if (ret >= 0) {
1228 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1229 } else {
1230 return -EINVAL;
1231 }
1232
1233 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1234 if (ret >= 0) {
1235 tmp_mdata.encoder_padding = atoi(value);
1236 } else {
1237 return -EINVAL;
1238 }
1239
1240 out->gapless_mdata = tmp_mdata;
1241 out->send_new_metadata = 1;
1242 ALOGV("%s new encoder delay %u and padding %u", __func__,
1243 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1244
1245 return 0;
1246}
1247
1248
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001249static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1250{
1251 struct stream_out *out = (struct stream_out *)stream;
1252 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001253 struct audio_usecase *usecase;
1254 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001255 struct str_parms *parms;
1256 char value[32];
1257 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001258 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001259 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001260
sangwoobc677242013-08-08 16:53:43 +09001261 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001262 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001263 parms = str_parms_create_str(kvpairs);
1264 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1265 if (ret >= 0) {
1266 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001267 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001268 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001269
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001270 /*
1271 * When HDMI cable is unplugged the music playback is paused and
1272 * the policy manager sends routing=0. But the audioflinger
1273 * continues to write data until standby time (3sec).
1274 * As the HDMI core is turned off, the write gets blocked.
1275 * Avoid this by routing audio to speaker until standby.
1276 */
1277 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1278 val == AUDIO_DEVICE_NONE) {
1279 val = AUDIO_DEVICE_OUT_SPEAKER;
1280 }
1281
1282 /*
1283 * select_devices() call below switches all the usecases on the same
1284 * backend to the new device. Refer to check_usecases_codec_backend() in
1285 * the select_devices(). But how do we undo this?
1286 *
1287 * For example, music playback is active on headset (deep-buffer usecase)
1288 * and if we go to ringtones and select a ringtone, low-latency usecase
1289 * will be started on headset+speaker. As we can't enable headset+speaker
1290 * and headset devices at the same time, select_devices() switches the music
1291 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1292 * So when the ringtone playback is completed, how do we undo the same?
1293 *
1294 * We are relying on the out_set_parameters() call on deep-buffer output,
1295 * once the ringtone playback is ended.
1296 * NOTE: We should not check if the current devices are same as new devices.
1297 * Because select_devices() must be called to switch back the music
1298 * playback to headset.
1299 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001300 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001301 out->devices = val;
1302
1303 if (!out->standby)
1304 select_devices(adev, out->usecase);
1305
1306 if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call &&
1307 (out == adev->primary_output)) {
1308 start_voice_call(adev);
1309 } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call &&
1310 (out == adev->primary_output)) {
1311 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001312 }
1313 }
1314
Ravi Kumar Alamandaed9c56a2013-10-02 09:56:18 -07001315 if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001316 (out == adev->primary_output)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001317 stop_voice_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001318 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001319
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001320 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001321 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001322 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001323
1324 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1325 parse_compress_metadata(out, parms);
1326 }
1327
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001328 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001329 ALOGV("%s: exit: code(%d)", __func__, status);
1330 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001331}
1332
1333static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1334{
1335 struct stream_out *out = (struct stream_out *)stream;
1336 struct str_parms *query = str_parms_create_str(keys);
1337 char *str;
1338 char value[256];
1339 struct str_parms *reply = str_parms_create();
1340 size_t i, j;
1341 int ret;
1342 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001343 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001344 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1345 if (ret >= 0) {
1346 value[0] = '\0';
1347 i = 0;
1348 while (out->supported_channel_masks[i] != 0) {
1349 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1350 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1351 if (!first) {
1352 strcat(value, "|");
1353 }
1354 strcat(value, out_channels_name_to_enum_table[j].name);
1355 first = false;
1356 break;
1357 }
1358 }
1359 i++;
1360 }
1361 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1362 str = str_parms_to_str(reply);
1363 } else {
1364 str = strdup(keys);
1365 }
1366 str_parms_destroy(query);
1367 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001368 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001369 return str;
1370}
1371
1372static uint32_t out_get_latency(const struct audio_stream_out *stream)
1373{
1374 struct stream_out *out = (struct stream_out *)stream;
1375
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001376 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1377 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1378
1379 return (out->config.period_count * out->config.period_size * 1000) /
1380 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001381}
1382
1383static int out_set_volume(struct audio_stream_out *stream, float left,
1384 float right)
1385{
Eric Laurenta9024de2013-04-04 09:19:12 -07001386 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001387 int volume[2];
1388
Eric Laurenta9024de2013-04-04 09:19:12 -07001389 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1390 /* only take left channel into account: the API is for stereo anyway */
1391 out->muted = (left == 0.0f);
1392 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001393 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1394 const char *mixer_ctl_name = "Compress Playback Volume";
1395 struct audio_device *adev = out->dev;
1396 struct mixer_ctl *ctl;
1397
1398 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1399 if (!ctl) {
1400 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1401 __func__, mixer_ctl_name);
1402 return -EINVAL;
1403 }
1404 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1405 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1406 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1407 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001408 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001409
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001410 return -ENOSYS;
1411}
1412
1413static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1414 size_t bytes)
1415{
1416 struct stream_out *out = (struct stream_out *)stream;
1417 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001418 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001419
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001420 pthread_mutex_lock(&out->lock);
1421 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001422 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001423 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001424 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001425 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001426 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001427 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001428 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001429 goto exit;
1430 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001431 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001432
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001433 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001434 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1435 if (out->send_new_metadata) {
1436 ALOGVV("send new gapless metadata");
1437 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1438 out->send_new_metadata = 0;
1439 }
1440
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001441 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001442 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001443 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001444 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1445 }
1446 if (!out->playback_started) {
1447 compress_start(out->compr);
1448 out->playback_started = 1;
1449 out->offload_state = OFFLOAD_STATE_PLAYING;
1450 }
1451 pthread_mutex_unlock(&out->lock);
1452 return ret;
1453 } else {
1454 if (out->pcm) {
1455 if (out->muted)
1456 memset((void *)buffer, 0, bytes);
1457 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1458 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001459 if (ret == 0)
1460 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001461 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001462 }
1463
1464exit:
1465 pthread_mutex_unlock(&out->lock);
1466
1467 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001468 if (out->pcm)
1469 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001470 out_standby(&out->stream.common);
1471 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1472 out_get_sample_rate(&out->stream.common));
1473 }
1474 return bytes;
1475}
1476
1477static int out_get_render_position(const struct audio_stream_out *stream,
1478 uint32_t *dsp_frames)
1479{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001480 struct stream_out *out = (struct stream_out *)stream;
1481 *dsp_frames = 0;
1482 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1483 pthread_mutex_lock(&out->lock);
1484 if (out->compr != NULL) {
1485 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1486 &out->sample_rate);
1487 ALOGVV("%s rendered frames %d sample_rate %d",
1488 __func__, *dsp_frames, out->sample_rate);
1489 }
1490 pthread_mutex_unlock(&out->lock);
1491 return 0;
1492 } else
1493 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001494}
1495
1496static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1497{
1498 return 0;
1499}
1500
1501static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1502{
1503 return 0;
1504}
1505
1506static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1507 int64_t *timestamp)
1508{
1509 return -EINVAL;
1510}
1511
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001512static int out_get_presentation_position(const struct audio_stream_out *stream,
1513 uint64_t *frames, struct timespec *timestamp)
1514{
1515 struct stream_out *out = (struct stream_out *)stream;
1516 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001517 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001518
1519 pthread_mutex_lock(&out->lock);
1520
Eric Laurent949a0892013-09-20 09:20:13 -07001521 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1522 if (out->compr != NULL) {
1523 compress_get_tstamp(out->compr, &dsp_frames,
1524 &out->sample_rate);
1525 ALOGVV("%s rendered frames %ld sample_rate %d",
1526 __func__, dsp_frames, out->sample_rate);
1527 *frames = dsp_frames;
1528 ret = 0;
1529 /* this is the best we can do */
1530 clock_gettime(CLOCK_MONOTONIC, timestamp);
1531 }
1532 } else {
1533 if (out->pcm) {
1534 size_t avail;
1535 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1536 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001537 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001538 // This adjustment accounts for buffering after app processor.
1539 // It is based on estimated DSP latency per use case, rather than exact.
1540 signed_frames -=
1541 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1542
Eric Laurent949a0892013-09-20 09:20:13 -07001543 // It would be unusual for this value to be negative, but check just in case ...
1544 if (signed_frames >= 0) {
1545 *frames = signed_frames;
1546 ret = 0;
1547 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001548 }
1549 }
1550 }
1551
1552 pthread_mutex_unlock(&out->lock);
1553
1554 return ret;
1555}
1556
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001557static int out_set_callback(struct audio_stream_out *stream,
1558 stream_callback_t callback, void *cookie)
1559{
1560 struct stream_out *out = (struct stream_out *)stream;
1561
1562 ALOGV("%s", __func__);
1563 pthread_mutex_lock(&out->lock);
1564 out->offload_callback = callback;
1565 out->offload_cookie = cookie;
1566 pthread_mutex_unlock(&out->lock);
1567 return 0;
1568}
1569
1570static int out_pause(struct audio_stream_out* stream)
1571{
1572 struct stream_out *out = (struct stream_out *)stream;
1573 int status = -ENOSYS;
1574 ALOGV("%s", __func__);
1575 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1576 pthread_mutex_lock(&out->lock);
1577 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1578 status = compress_pause(out->compr);
1579 out->offload_state = OFFLOAD_STATE_PAUSED;
1580 }
1581 pthread_mutex_unlock(&out->lock);
1582 }
1583 return status;
1584}
1585
1586static int out_resume(struct audio_stream_out* stream)
1587{
1588 struct stream_out *out = (struct stream_out *)stream;
1589 int status = -ENOSYS;
1590 ALOGV("%s", __func__);
1591 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1592 status = 0;
1593 pthread_mutex_lock(&out->lock);
1594 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1595 status = compress_resume(out->compr);
1596 out->offload_state = OFFLOAD_STATE_PLAYING;
1597 }
1598 pthread_mutex_unlock(&out->lock);
1599 }
1600 return status;
1601}
1602
1603static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1604{
1605 struct stream_out *out = (struct stream_out *)stream;
1606 int status = -ENOSYS;
1607 ALOGV("%s", __func__);
1608 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1609 pthread_mutex_lock(&out->lock);
1610 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1611 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1612 else
1613 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1614 pthread_mutex_unlock(&out->lock);
1615 }
1616 return status;
1617}
1618
1619static int out_flush(struct audio_stream_out* stream)
1620{
1621 struct stream_out *out = (struct stream_out *)stream;
1622 ALOGV("%s", __func__);
1623 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1624 pthread_mutex_lock(&out->lock);
1625 stop_compressed_output_l(out);
1626 pthread_mutex_unlock(&out->lock);
1627 return 0;
1628 }
1629 return -ENOSYS;
1630}
1631
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001632/** audio_stream_in implementation **/
1633static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1634{
1635 struct stream_in *in = (struct stream_in *)stream;
1636
1637 return in->config.rate;
1638}
1639
1640static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1641{
1642 return -ENOSYS;
1643}
1644
1645static size_t in_get_buffer_size(const struct audio_stream *stream)
1646{
1647 struct stream_in *in = (struct stream_in *)stream;
1648
1649 return in->config.period_size * audio_stream_frame_size(stream);
1650}
1651
1652static uint32_t in_get_channels(const struct audio_stream *stream)
1653{
1654 struct stream_in *in = (struct stream_in *)stream;
1655
1656 return in->channel_mask;
1657}
1658
1659static audio_format_t in_get_format(const struct audio_stream *stream)
1660{
1661 return AUDIO_FORMAT_PCM_16_BIT;
1662}
1663
1664static int in_set_format(struct audio_stream *stream, audio_format_t format)
1665{
1666 return -ENOSYS;
1667}
1668
1669static int in_standby(struct audio_stream *stream)
1670{
1671 struct stream_in *in = (struct stream_in *)stream;
1672 struct audio_device *adev = in->dev;
1673 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001674 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001675 pthread_mutex_lock(&in->lock);
1676 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001677 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001678 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001679 if (in->pcm) {
1680 pcm_close(in->pcm);
1681 in->pcm = NULL;
1682 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001683 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001684 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001685 }
1686 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001687 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001688 return status;
1689}
1690
1691static int in_dump(const struct audio_stream *stream, int fd)
1692{
1693 return 0;
1694}
1695
1696static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1697{
1698 struct stream_in *in = (struct stream_in *)stream;
1699 struct audio_device *adev = in->dev;
1700 struct str_parms *parms;
1701 char *str;
1702 char value[32];
1703 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07001704 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001705
Eric Laurent994a6932013-07-17 11:51:42 -07001706 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001707 parms = str_parms_create_str(kvpairs);
1708
1709 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1710
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001711 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001712 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001713 if (ret >= 0) {
1714 val = atoi(value);
1715 /* no audio source uses val == 0 */
1716 if ((in->source != val) && (val != 0)) {
1717 in->source = val;
1718 }
1719 }
1720
1721 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07001722
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001723 if (ret >= 0) {
1724 val = atoi(value);
1725 if ((in->device != val) && (val != 0)) {
1726 in->device = val;
1727 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001728 if (!in->standby)
Eric Laurent03f09432014-03-25 18:09:11 -07001729 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001730 }
1731 }
1732
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001733 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001734 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001735
1736 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001737 ALOGV("%s: exit: status(%d)", __func__, status);
1738 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001739}
1740
1741static char* in_get_parameters(const struct audio_stream *stream,
1742 const char *keys)
1743{
1744 return strdup("");
1745}
1746
1747static int in_set_gain(struct audio_stream_in *stream, float gain)
1748{
1749 return 0;
1750}
1751
1752static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1753 size_t bytes)
1754{
1755 struct stream_in *in = (struct stream_in *)stream;
1756 struct audio_device *adev = in->dev;
1757 int i, ret = -1;
1758
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001759 pthread_mutex_lock(&in->lock);
1760 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001761 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001762 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001763 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001764 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001765 goto exit;
1766 }
1767 in->standby = 0;
1768 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001769
1770 if (in->pcm) {
1771 ret = pcm_read(in->pcm, buffer, bytes);
1772 }
1773
1774 /*
1775 * Instead of writing zeroes here, we could trust the hardware
1776 * to always provide zeroes when muted.
1777 */
1778 if (ret == 0 && adev->mic_mute)
1779 memset(buffer, 0, bytes);
1780
1781exit:
1782 pthread_mutex_unlock(&in->lock);
1783
1784 if (ret != 0) {
1785 in_standby(&in->stream.common);
1786 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1787 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1788 in_get_sample_rate(&in->stream.common));
1789 }
1790 return bytes;
1791}
1792
1793static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1794{
1795 return 0;
1796}
1797
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001798static int add_remove_audio_effect(const struct audio_stream *stream,
1799 effect_handle_t effect,
1800 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001801{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001802 struct stream_in *in = (struct stream_in *)stream;
1803 int status = 0;
1804 effect_descriptor_t desc;
1805
1806 status = (*effect)->get_descriptor(effect, &desc);
1807 if (status != 0)
1808 return status;
1809
1810 pthread_mutex_lock(&in->lock);
1811 pthread_mutex_lock(&in->dev->lock);
1812 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1813 in->enable_aec != enable &&
1814 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1815 in->enable_aec = enable;
1816 if (!in->standby)
1817 select_devices(in->dev, in->usecase);
1818 }
1819 pthread_mutex_unlock(&in->dev->lock);
1820 pthread_mutex_unlock(&in->lock);
1821
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001822 return 0;
1823}
1824
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001825static int in_add_audio_effect(const struct audio_stream *stream,
1826 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001827{
Eric Laurent994a6932013-07-17 11:51:42 -07001828 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001829 return add_remove_audio_effect(stream, effect, true);
1830}
1831
1832static int in_remove_audio_effect(const struct audio_stream *stream,
1833 effect_handle_t effect)
1834{
Eric Laurent994a6932013-07-17 11:51:42 -07001835 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001836 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001837}
1838
1839static int adev_open_output_stream(struct audio_hw_device *dev,
1840 audio_io_handle_t handle,
1841 audio_devices_t devices,
1842 audio_output_flags_t flags,
1843 struct audio_config *config,
1844 struct audio_stream_out **stream_out)
1845{
1846 struct audio_device *adev = (struct audio_device *)dev;
1847 struct stream_out *out;
1848 int i, ret;
1849
Eric Laurent994a6932013-07-17 11:51:42 -07001850 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001851 __func__, config->sample_rate, config->channel_mask, devices, flags);
1852 *stream_out = NULL;
1853 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1854
1855 if (devices == AUDIO_DEVICE_NONE)
1856 devices = AUDIO_DEVICE_OUT_SPEAKER;
1857
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001858 out->flags = flags;
1859 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001860 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001861 out->format = config->format;
1862 out->sample_rate = config->sample_rate;
1863 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1864 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001865 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001866
1867 /* Init use case and pcm_config */
1868 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07001869 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001870 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001871 pthread_mutex_lock(&adev->lock);
1872 ret = read_hdmi_channel_masks(out);
1873 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001874 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001875 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001876
1877 if (config->sample_rate == 0)
1878 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1879 if (config->channel_mask == 0)
1880 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1881
1882 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001883 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001884 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1885 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001886 out->config.rate = config->sample_rate;
1887 out->config.channels = popcount(out->channel_mask);
1888 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001889 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1890 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1891 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001892 out->sample_rate = out->config.rate;
1893 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1894 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1895 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1896 ALOGE("%s: Unsupported Offload information", __func__);
1897 ret = -EINVAL;
1898 goto error_open;
1899 }
1900 if (!is_supported_format(config->offload_info.format)) {
1901 ALOGE("%s: Unsupported audio format", __func__);
1902 ret = -EINVAL;
1903 goto error_open;
1904 }
1905
1906 out->compr_config.codec = (struct snd_codec *)
1907 calloc(1, sizeof(struct snd_codec));
1908
1909 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1910 if (config->offload_info.channel_mask)
1911 out->channel_mask = config->offload_info.channel_mask;
1912 else if (config->channel_mask)
1913 out->channel_mask = config->channel_mask;
1914 out->format = config->offload_info.format;
1915 out->sample_rate = config->offload_info.sample_rate;
1916
1917 out->stream.set_callback = out_set_callback;
1918 out->stream.pause = out_pause;
1919 out->stream.resume = out_resume;
1920 out->stream.drain = out_drain;
1921 out->stream.flush = out_flush;
1922
1923 out->compr_config.codec->id =
1924 get_snd_codec_id(config->offload_info.format);
1925 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1926 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1927 out->compr_config.codec->sample_rate =
1928 compress_get_alsa_rate(config->offload_info.sample_rate);
1929 out->compr_config.codec->bit_rate =
1930 config->offload_info.bit_rate;
1931 out->compr_config.codec->ch_in =
1932 popcount(config->channel_mask);
1933 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1934
1935 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1936 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001937
1938 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001939 create_offload_callback_thread(out);
1940 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1941 __func__, config->offload_info.version,
1942 config->offload_info.bit_rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001943 } else {
1944 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1945 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001946 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001947 }
1948
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001949 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1950 if(adev->primary_output == NULL)
1951 adev->primary_output = out;
1952 else {
1953 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001954 ret = -EEXIST;
1955 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001956 }
1957 }
1958
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001959 /* Check if this usecase is already existing */
1960 pthread_mutex_lock(&adev->lock);
1961 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1962 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001963 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001964 ret = -EEXIST;
1965 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001966 }
1967 pthread_mutex_unlock(&adev->lock);
1968
1969 out->stream.common.get_sample_rate = out_get_sample_rate;
1970 out->stream.common.set_sample_rate = out_set_sample_rate;
1971 out->stream.common.get_buffer_size = out_get_buffer_size;
1972 out->stream.common.get_channels = out_get_channels;
1973 out->stream.common.get_format = out_get_format;
1974 out->stream.common.set_format = out_set_format;
1975 out->stream.common.standby = out_standby;
1976 out->stream.common.dump = out_dump;
1977 out->stream.common.set_parameters = out_set_parameters;
1978 out->stream.common.get_parameters = out_get_parameters;
1979 out->stream.common.add_audio_effect = out_add_audio_effect;
1980 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1981 out->stream.get_latency = out_get_latency;
1982 out->stream.set_volume = out_set_volume;
1983 out->stream.write = out_write;
1984 out->stream.get_render_position = out_get_render_position;
1985 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001986 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001987
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001988 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001989 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001990 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001991
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001992 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
1993 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
1994
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001995 config->format = out->stream.common.get_format(&out->stream.common);
1996 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1997 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1998
1999 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002000 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002001 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002002
2003error_open:
2004 free(out);
2005 *stream_out = NULL;
2006 ALOGD("%s: exit: ret %d", __func__, ret);
2007 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002008}
2009
2010static void adev_close_output_stream(struct audio_hw_device *dev,
2011 struct audio_stream_out *stream)
2012{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002013 struct stream_out *out = (struct stream_out *)stream;
2014 struct audio_device *adev = out->dev;
2015
Eric Laurent994a6932013-07-17 11:51:42 -07002016 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002017 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002018 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2019 destroy_offload_callback_thread(out);
2020
2021 if (out->compr_config.codec != NULL)
2022 free(out->compr_config.codec);
2023 }
2024 pthread_cond_destroy(&out->cond);
2025 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002026 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002027 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002028}
2029
2030static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2031{
2032 struct audio_device *adev = (struct audio_device *)dev;
2033 struct str_parms *parms;
2034 char *str;
2035 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002036 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002037 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002038 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002039
Eric Laurent994a6932013-07-17 11:51:42 -07002040 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002041
2042 parms = str_parms_create_str(kvpairs);
2043 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value));
2044 if (ret >= 0) {
2045 int tty_mode;
2046
2047 if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0)
2048 tty_mode = TTY_MODE_OFF;
2049 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0)
2050 tty_mode = TTY_MODE_VCO;
2051 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0)
2052 tty_mode = TTY_MODE_HCO;
2053 else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0)
2054 tty_mode = TTY_MODE_FULL;
2055 else
2056 return -EINVAL;
2057
2058 pthread_mutex_lock(&adev->lock);
2059 if (tty_mode != adev->tty_mode) {
2060 adev->tty_mode = tty_mode;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002061 adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode;
2062 if (adev->in_call)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002063 select_devices(adev, USECASE_VOICE_CALL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002064 }
2065 pthread_mutex_unlock(&adev->lock);
2066 }
2067
2068 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2069 if (ret >= 0) {
2070 /* When set to false, HAL should disable EC and NS
2071 * But it is currently not supported.
2072 */
2073 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2074 adev->bluetooth_nrec = true;
2075 else
2076 adev->bluetooth_nrec = false;
2077 }
2078
2079 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2080 if (ret >= 0) {
2081 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2082 adev->screen_off = false;
2083 else
2084 adev->screen_off = true;
2085 }
2086
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002087 ret = str_parms_get_int(parms, "rotation", &val);
2088 if (ret >= 0) {
2089 bool reverse_speakers = false;
2090 switch(val) {
2091 // FIXME: note that the code below assumes that the speakers are in the correct placement
2092 // relative to the user when the device is rotated 90deg from its default rotation. This
2093 // assumption is device-specific, not platform-specific like this code.
2094 case 270:
2095 reverse_speakers = true;
2096 break;
2097 case 0:
2098 case 90:
2099 case 180:
2100 break;
2101 default:
2102 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002103 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002104 }
Eric Laurent03f09432014-03-25 18:09:11 -07002105 if (status == 0) {
2106 pthread_mutex_lock(&adev->lock);
2107 if (adev->speaker_lr_swap != reverse_speakers) {
2108 adev->speaker_lr_swap = reverse_speakers;
2109 // only update the selected device if there is active pcm playback
2110 struct audio_usecase *usecase;
2111 struct listnode *node;
2112 list_for_each(node, &adev->usecase_list) {
2113 usecase = node_to_item(node, struct audio_usecase, list);
2114 if (usecase->type == PCM_PLAYBACK) {
2115 select_devices(adev, usecase->id);
2116 break;
2117 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002118 }
2119 }
Eric Laurent03f09432014-03-25 18:09:11 -07002120 pthread_mutex_unlock(&adev->lock);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002121 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002122 }
2123
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002124 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07002125 ALOGV("%s: exit with code(%d)", __func__, status);
2126 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002127}
2128
2129static char* adev_get_parameters(const struct audio_hw_device *dev,
2130 const char *keys)
2131{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002132 return strdup("");
2133}
2134
2135static int adev_init_check(const struct audio_hw_device *dev)
2136{
2137 return 0;
2138}
2139
Haynes Mathew George5191a852013-09-11 14:19:36 -07002140/* always called with adev lock held */
2141static int set_voice_volume_l(struct audio_device *adev, float volume)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002142{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002143 int vol, err = 0;
2144
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002145 if (adev->mode == AUDIO_MODE_IN_CALL) {
2146 if (volume < 0.0) {
2147 volume = 0.0;
2148 } else if (volume > 1.0) {
2149 volume = 1.0;
2150 }
2151
2152 vol = lrint(volume * 100.0);
2153
2154 // Voice volume levels from android are mapped to driver volume levels as follows.
2155 // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0
2156 // So adjust the volume to get the correct volume index in driver
2157 vol = 100 - vol;
Eric Laurentb23d5282013-05-14 15:27:20 -07002158
2159 err = platform_set_voice_volume(adev->platform, vol);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002160 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002161 return err;
2162}
2163
Haynes Mathew George5191a852013-09-11 14:19:36 -07002164static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2165{
2166 int ret;
2167 struct audio_device *adev = (struct audio_device *)dev;
2168 pthread_mutex_lock(&adev->lock);
2169 /* cache volume */
2170 adev->voice_volume = volume;
2171 ret = set_voice_volume_l(adev, adev->voice_volume);
2172 pthread_mutex_unlock(&adev->lock);
2173 return ret;
2174}
2175
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002176static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2177{
2178 return -ENOSYS;
2179}
2180
2181static int adev_get_master_volume(struct audio_hw_device *dev,
2182 float *volume)
2183{
2184 return -ENOSYS;
2185}
2186
2187static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2188{
2189 return -ENOSYS;
2190}
2191
2192static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2193{
2194 return -ENOSYS;
2195}
2196
2197static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2198{
2199 struct audio_device *adev = (struct audio_device *)dev;
2200
2201 pthread_mutex_lock(&adev->lock);
2202 if (adev->mode != mode) {
2203 adev->mode = mode;
2204 }
2205 pthread_mutex_unlock(&adev->lock);
2206 return 0;
2207}
2208
2209static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2210{
2211 struct audio_device *adev = (struct audio_device *)dev;
2212 int err = 0;
2213
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002214 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002215 adev->mic_mute = state;
Eric Laurentb23d5282013-05-14 15:27:20 -07002216
2217 err = platform_set_mic_mute(adev->platform, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002218 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002219 return err;
2220}
2221
2222static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2223{
2224 struct audio_device *adev = (struct audio_device *)dev;
2225
2226 *state = adev->mic_mute;
2227
2228 return 0;
2229}
2230
2231static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2232 const struct audio_config *config)
2233{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002234 int channel_count = popcount(config->channel_mask);
2235
2236 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2237}
2238
2239static int adev_open_input_stream(struct audio_hw_device *dev,
2240 audio_io_handle_t handle,
2241 audio_devices_t devices,
2242 struct audio_config *config,
2243 struct audio_stream_in **stream_in)
2244{
2245 struct audio_device *adev = (struct audio_device *)dev;
2246 struct stream_in *in;
2247 int ret, buffer_size, frame_size;
2248 int channel_count = popcount(config->channel_mask);
2249
Eric Laurent994a6932013-07-17 11:51:42 -07002250 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002251 *stream_in = NULL;
2252 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2253 return -EINVAL;
2254
2255 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2256
2257 in->stream.common.get_sample_rate = in_get_sample_rate;
2258 in->stream.common.set_sample_rate = in_set_sample_rate;
2259 in->stream.common.get_buffer_size = in_get_buffer_size;
2260 in->stream.common.get_channels = in_get_channels;
2261 in->stream.common.get_format = in_get_format;
2262 in->stream.common.set_format = in_set_format;
2263 in->stream.common.standby = in_standby;
2264 in->stream.common.dump = in_dump;
2265 in->stream.common.set_parameters = in_set_parameters;
2266 in->stream.common.get_parameters = in_get_parameters;
2267 in->stream.common.add_audio_effect = in_add_audio_effect;
2268 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2269 in->stream.set_gain = in_set_gain;
2270 in->stream.read = in_read;
2271 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2272
2273 in->device = devices;
2274 in->source = AUDIO_SOURCE_DEFAULT;
2275 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002276 in->standby = 1;
2277 in->channel_mask = config->channel_mask;
2278
2279 /* Update config params with the requested sample rate and channels */
2280 in->usecase = USECASE_AUDIO_RECORD;
2281 in->config = pcm_config_audio_capture;
2282 in->config.channels = channel_count;
2283 in->config.rate = config->sample_rate;
2284
2285 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2286 buffer_size = get_input_buffer_size(config->sample_rate,
2287 config->format,
2288 channel_count);
2289 in->config.period_size = buffer_size / frame_size;
2290
2291 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002292 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002293 return 0;
2294
2295err_open:
2296 free(in);
2297 *stream_in = NULL;
2298 return ret;
2299}
2300
2301static void adev_close_input_stream(struct audio_hw_device *dev,
2302 struct audio_stream_in *stream)
2303{
Eric Laurent994a6932013-07-17 11:51:42 -07002304 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002305
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002306 in_standby(&stream->common);
2307 free(stream);
2308
2309 return;
2310}
2311
2312static int adev_dump(const audio_hw_device_t *device, int fd)
2313{
2314 return 0;
2315}
2316
2317static int adev_close(hw_device_t *device)
2318{
2319 struct audio_device *adev = (struct audio_device *)device;
2320 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002321 free(adev->snd_dev_ref_cnt);
2322 platform_deinit(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002323 free(device);
2324 return 0;
2325}
2326
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002327static int adev_open(const hw_module_t *module, const char *name,
2328 hw_device_t **device)
2329{
2330 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002331 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002332
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002333 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002334 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2335
2336 adev = calloc(1, sizeof(struct audio_device));
2337
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002338 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2339 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2340 adev->device.common.module = (struct hw_module_t *)module;
2341 adev->device.common.close = adev_close;
2342
2343 adev->device.init_check = adev_init_check;
2344 adev->device.set_voice_volume = adev_set_voice_volume;
2345 adev->device.set_master_volume = adev_set_master_volume;
2346 adev->device.get_master_volume = adev_get_master_volume;
2347 adev->device.set_master_mute = adev_set_master_mute;
2348 adev->device.get_master_mute = adev_get_master_mute;
2349 adev->device.set_mode = adev_set_mode;
2350 adev->device.set_mic_mute = adev_set_mic_mute;
2351 adev->device.get_mic_mute = adev_get_mic_mute;
2352 adev->device.set_parameters = adev_set_parameters;
2353 adev->device.get_parameters = adev_get_parameters;
2354 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2355 adev->device.open_output_stream = adev_open_output_stream;
2356 adev->device.close_output_stream = adev_close_output_stream;
2357 adev->device.open_input_stream = adev_open_input_stream;
2358 adev->device.close_input_stream = adev_close_input_stream;
2359 adev->device.dump = adev_dump;
2360
2361 /* Set the default route before the PCM stream is opened */
2362 pthread_mutex_lock(&adev->lock);
2363 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002364 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002365 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002366 adev->voice_call_rx = NULL;
2367 adev->voice_call_tx = NULL;
2368 adev->voice_volume = 1.0f;
2369 adev->tty_mode = TTY_MODE_OFF;
2370 adev->bluetooth_nrec = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002371 adev->in_call = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002372 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002373 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002374 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002375 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002376 pthread_mutex_unlock(&adev->lock);
2377
2378 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002379 adev->platform = platform_init(adev);
2380 if (!adev->platform) {
2381 free(adev->snd_dev_ref_cnt);
2382 free(adev);
2383 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2384 *device = NULL;
2385 return -EINVAL;
2386 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002387
2388 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2389 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2390 if (adev->visualizer_lib == NULL) {
2391 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2392 } else {
2393 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2394 adev->visualizer_start_output =
2395 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2396 "visualizer_hal_start_output");
2397 adev->visualizer_stop_output =
2398 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2399 "visualizer_hal_stop_output");
2400 }
2401 }
2402
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002403 *device = &adev->device.common;
2404
Eric Laurent994a6932013-07-17 11:51:42 -07002405 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002406 return 0;
2407}
2408
2409static struct hw_module_methods_t hal_module_methods = {
2410 .open = adev_open,
2411};
2412
2413struct audio_module HAL_MODULE_INFO_SYM = {
2414 .common = {
2415 .tag = HARDWARE_MODULE_TAG,
2416 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2417 .hal_api_version = HARDWARE_HAL_API_VERSION,
2418 .id = AUDIO_HARDWARE_MODULE_ID,
2419 .name = "QCOM Audio HAL",
2420 .author = "Code Aurora Forum",
2421 .methods = &hal_module_methods,
2422 },
2423};