blob: 0411b34c5960b8a3e7dbdda1a8ab10d949a15223 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
4 *
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_primary"
21/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -080022#define LOG_NDDEBUG 0
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080023
24#include <errno.h>
25#include <pthread.h>
26#include <stdint.h>
27#include <sys/time.h>
28#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080029#include <math.h>
30
31#include <cutils/log.h>
32#include <cutils/str_parms.h>
33#include <cutils/properties.h>
34
Eric Laurentb23d5282013-05-14 15:27:20 -070035#include <hardware/audio_effect.h>
36#include <audio_effects/effect_aec.h>
37#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070039#include "platform_api.h"
40#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070041#include "audio_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080042
Eric Laurentb23d5282013-05-14 15:27:20 -070043struct pcm_config pcm_config_deep_buffer = {
44 .channels = 2,
45 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
46 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
47 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
48 .format = PCM_FORMAT_S16_LE,
49 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
50 .stop_threshold = INT_MAX,
51 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
52};
53
54struct pcm_config pcm_config_low_latency = {
55 .channels = 2,
56 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
57 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
58 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
59 .format = PCM_FORMAT_S16_LE,
60 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
61 .stop_threshold = INT_MAX,
62 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
63};
64
65struct pcm_config pcm_config_hdmi_multi = {
66 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
67 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
68 .period_size = HDMI_MULTI_PERIOD_SIZE,
69 .period_count = HDMI_MULTI_PERIOD_COUNT,
70 .format = PCM_FORMAT_S16_LE,
71 .start_threshold = 0,
72 .stop_threshold = INT_MAX,
73 .avail_min = 0,
74};
75
76struct pcm_config pcm_config_audio_capture = {
77 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070078 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
79 .format = PCM_FORMAT_S16_LE,
80};
81
Eric Laurentb23d5282013-05-14 15:27:20 -070082static const char * const use_case_table[AUDIO_USECASE_MAX] = {
83 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
84 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
85 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
86 [USECASE_AUDIO_RECORD] = "audio-record",
87 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -070088 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -070089 [USECASE_VOICE_CALL] = "voice-call",
90 [USECASE_VOICE2_CALL] = "voice2-call",
91 [USECASE_VOLTE_CALL] = "volte-call",
92 [USECASE_QCHAT_CALL] = "qchat-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -070093 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
94 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
95 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Eric Laurentb23d5282013-05-14 15:27:20 -070096};
97
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080098
99#define STRING_TO_ENUM(string) { #string, string }
100
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800101struct string_to_enum {
102 const char *name;
103 uint32_t value;
104};
105
106static const struct string_to_enum out_channels_name_to_enum_table[] = {
107 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
108 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
109 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
110};
111
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700112static struct audio_device *adev = NULL;
113static pthread_mutex_t adev_init_lock;
114static bool is_adev_initialised = false;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800115
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700116static int enable_audio_route(struct audio_device *adev,
117 struct audio_usecase *usecase,
118 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800119{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700120 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700121 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800122
123 if (usecase == NULL)
124 return -EINVAL;
125
126 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
127
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800128 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700129 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800130 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700131 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800132
133 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700134 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700135 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700136 audio_route_apply_path(adev->audio_route, mixer_path);
137 if (update_mixer)
138 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800139
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800140 ALOGV("%s: exit", __func__);
141 return 0;
142}
143
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700144int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700145 struct audio_usecase *usecase,
146 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800147{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700148 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700149 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800150
151 if (usecase == NULL)
152 return -EINVAL;
153
154 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700155 if (usecase->type == PCM_CAPTURE)
156 snd_device = usecase->in_snd_device;
157 else
158 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800159 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700160 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700161 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700162 audio_route_reset_path(adev->audio_route, mixer_path);
163 if (update_mixer)
164 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800165
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800166 ALOGV("%s: exit", __func__);
167 return 0;
168}
169
170static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700171 snd_device_t snd_device,
172 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800173{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800174 if (snd_device < SND_DEVICE_MIN ||
175 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800176 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800177 return -EINVAL;
178 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700179
180 adev->snd_dev_ref_cnt[snd_device]++;
181 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700182 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700183 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700184 return 0;
185 }
186
Eric Laurentb23d5282013-05-14 15:27:20 -0700187 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700188 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800189 return -EINVAL;
190 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800191
Eric Laurent994a6932013-07-17 11:51:42 -0700192 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700193 snd_device, platform_get_snd_device_name(snd_device));
194 audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700195 if (update_mixer)
196 audio_route_update_mixer(adev->audio_route);
197
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800198 return 0;
199}
200
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700201int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700202 snd_device_t snd_device,
203 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800204{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800205 if (snd_device < SND_DEVICE_MIN ||
206 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800207 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800208 return -EINVAL;
209 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700210 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
211 ALOGE("%s: device ref cnt is already 0", __func__);
212 return -EINVAL;
213 }
214 adev->snd_dev_ref_cnt[snd_device]--;
215 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700216 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700217 snd_device, platform_get_snd_device_name(snd_device));
218 audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700219 if (update_mixer)
220 audio_route_update_mixer(adev->audio_route);
221 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800222 return 0;
223}
224
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700225static void check_usecases_codec_backend(struct audio_device *adev,
226 struct audio_usecase *uc_info,
227 snd_device_t snd_device)
228{
229 struct listnode *node;
230 struct audio_usecase *usecase;
231 bool switch_device[AUDIO_USECASE_MAX];
232 int i, num_uc_to_switch = 0;
233
234 /*
235 * This function is to make sure that all the usecases that are active on
236 * the hardware codec backend are always routed to any one device that is
237 * handled by the hardware codec.
238 * For example, if low-latency and deep-buffer usecases are currently active
239 * on speaker and out_set_parameters(headset) is received on low-latency
240 * output, then we have to make sure deep-buffer is also switched to headset,
241 * because of the limitation that both the devices cannot be enabled
242 * at the same time as they share the same backend.
243 */
244 /* Disable all the usecases on the shared backend other than the
245 specified usecase */
246 for (i = 0; i < AUDIO_USECASE_MAX; i++)
247 switch_device[i] = false;
248
249 list_for_each(node, &adev->usecase_list) {
250 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700251 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700252 usecase != uc_info &&
253 usecase->out_snd_device != snd_device &&
254 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
255 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
256 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700257 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700258 disable_audio_route(adev, usecase, false);
259 switch_device[usecase->id] = true;
260 num_uc_to_switch++;
261 }
262 }
263
264 if (num_uc_to_switch) {
265 /* Make sure all the streams are de-routed before disabling the device */
266 audio_route_update_mixer(adev->audio_route);
267
268 list_for_each(node, &adev->usecase_list) {
269 usecase = node_to_item(node, struct audio_usecase, list);
270 if (switch_device[usecase->id]) {
271 disable_snd_device(adev, usecase->out_snd_device, false);
272 enable_snd_device(adev, snd_device, false);
273 }
274 }
275
276 /* Make sure new snd device is enabled before re-routing the streams */
277 audio_route_update_mixer(adev->audio_route);
278
279 /* Re-route all the usecases on the shared backend other than the
280 specified usecase to new snd devices */
281 list_for_each(node, &adev->usecase_list) {
282 usecase = node_to_item(node, struct audio_usecase, list);
283 /* Update the out_snd_device only before enabling the audio route */
284 if (switch_device[usecase->id] ) {
285 usecase->out_snd_device = snd_device;
286 enable_audio_route(adev, usecase, false);
287 }
288 }
289
290 audio_route_update_mixer(adev->audio_route);
291 }
292}
293
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700294static void check_and_route_capture_usecases(struct audio_device *adev,
295 struct audio_usecase *uc_info,
296 snd_device_t snd_device)
297{
298 struct listnode *node;
299 struct audio_usecase *usecase;
300 bool switch_device[AUDIO_USECASE_MAX];
301 int i, num_uc_to_switch = 0;
302
303 /*
304 * This function is to make sure that all the active capture usecases
305 * are always routed to the same input sound device.
306 * For example, if audio-record and voice-call usecases are currently
307 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
308 * is received for voice call then we have to make sure that audio-record
309 * usecase is also switched to earpiece i.e. voice-dmic-ef,
310 * because of the limitation that two devices cannot be enabled
311 * at the same time if they share the same backend.
312 */
313 for (i = 0; i < AUDIO_USECASE_MAX; i++)
314 switch_device[i] = false;
315
316 list_for_each(node, &adev->usecase_list) {
317 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700318 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700319 usecase != uc_info &&
320 usecase->in_snd_device != snd_device) {
321 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
322 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700323 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700324 disable_audio_route(adev, usecase, false);
325 switch_device[usecase->id] = true;
326 num_uc_to_switch++;
327 }
328 }
329
330 if (num_uc_to_switch) {
331 /* Make sure all the streams are de-routed before disabling the device */
332 audio_route_update_mixer(adev->audio_route);
333
334 list_for_each(node, &adev->usecase_list) {
335 usecase = node_to_item(node, struct audio_usecase, list);
336 if (switch_device[usecase->id]) {
337 disable_snd_device(adev, usecase->in_snd_device, false);
338 enable_snd_device(adev, snd_device, false);
339 }
340 }
341
342 /* Make sure new snd device is enabled before re-routing the streams */
343 audio_route_update_mixer(adev->audio_route);
344
345 /* Re-route all the usecases on the shared backend other than the
346 specified usecase to new snd devices */
347 list_for_each(node, &adev->usecase_list) {
348 usecase = node_to_item(node, struct audio_usecase, list);
349 /* Update the in_snd_device only before enabling the audio route */
350 if (switch_device[usecase->id] ) {
351 usecase->in_snd_device = snd_device;
352 enable_audio_route(adev, usecase, false);
353 }
354 }
355
356 audio_route_update_mixer(adev->audio_route);
357 }
358}
359
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700360static int disable_all_usecases_of_type(struct audio_device *adev,
361 usecase_type_t usecase_type,
362 bool update_mixer)
363{
364 struct audio_usecase *usecase;
365 struct listnode *node;
366 int ret = 0;
367
368 list_for_each(node, &adev->usecase_list) {
369 usecase = node_to_item(node, struct audio_usecase, list);
370 if (usecase->type == usecase_type) {
371 ALOGV("%s: usecase id %d", __func__, usecase->id);
372 ret = disable_audio_route(adev, usecase, update_mixer);
373 if (ret) {
374 ALOGE("%s: Failed to disable usecase id %d",
375 __func__, usecase->id);
376 }
377 }
378 }
379
380 return ret;
381}
382
383static int enable_all_usecases_of_type(struct audio_device *adev,
384 usecase_type_t usecase_type,
385 bool update_mixer)
386{
387 struct audio_usecase *usecase;
388 struct listnode *node;
389 int ret = 0;
390
391 list_for_each(node, &adev->usecase_list) {
392 usecase = node_to_item(node, struct audio_usecase, list);
393 if (usecase->type == usecase_type) {
394 ALOGV("%s: usecase id %d", __func__, usecase->id);
395 ret = enable_audio_route(adev, usecase, update_mixer);
396 if (ret) {
397 ALOGE("%s: Failed to enable usecase id %d",
398 __func__, usecase->id);
399 }
400 }
401 }
402
403 return ret;
404}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800405
406/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700407static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800408{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700409 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700410 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800411
412 switch (channels) {
413 /*
414 * Do not handle stereo output in Multi-channel cases
415 * Stereo case is handled in normal playback path
416 */
417 case 6:
418 ALOGV("%s: HDMI supports 5.1", __func__);
419 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
420 break;
421 case 8:
422 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
423 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
424 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
425 break;
426 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700427 ALOGE("HDMI does not support multi channel playback");
428 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800429 break;
430 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700431 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800432}
433
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700434static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
435{
436 struct audio_usecase *usecase;
437 struct listnode *node;
438
439 list_for_each(node, &adev->usecase_list) {
440 usecase = node_to_item(node, struct audio_usecase, list);
441 if (usecase->type == VOICE_CALL) {
442 ALOGV("%s: usecase id %d", __func__, usecase->id);
443 return usecase->id;
444 }
445 }
446 return USECASE_INVALID;
447}
448
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700449struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700450 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700451{
452 struct audio_usecase *usecase;
453 struct listnode *node;
454
455 list_for_each(node, &adev->usecase_list) {
456 usecase = node_to_item(node, struct audio_usecase, list);
457 if (usecase->id == uc_id)
458 return usecase;
459 }
460 return NULL;
461}
462
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700463int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800464{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800465 snd_device_t out_snd_device = SND_DEVICE_NONE;
466 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700467 struct audio_usecase *usecase = NULL;
468 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800469 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700470 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800471
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700472 usecase = get_usecase_from_list(adev, uc_id);
473 if (usecase == NULL) {
474 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
475 return -EINVAL;
476 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800477
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700478 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700479 out_snd_device = platform_get_output_snd_device(adev->platform,
480 usecase->stream.out->devices);
481 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700482 usecase->devices = usecase->stream.out->devices;
483 } else {
484 /*
485 * If the voice call is active, use the sound devices of voice call usecase
486 * so that it would not result any device switch. All the usecases will
487 * be switched to new device when select_devices() is called for voice call
488 * usecase. This is to avoid switching devices for voice call when
489 * check_usecases_codec_backend() is called below.
490 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700491 if (voice_is_in_call(adev)) {
492 vc_usecase = get_usecase_from_list(adev,
493 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700494 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
495 in_snd_device = vc_usecase->in_snd_device;
496 out_snd_device = vc_usecase->out_snd_device;
497 }
498 }
499 if (usecase->type == PCM_PLAYBACK) {
500 usecase->devices = usecase->stream.out->devices;
501 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700502 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700503 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700504 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700505 if (usecase->stream.out == adev->primary_output &&
506 adev->active_input &&
507 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
508 select_devices(adev, adev->active_input->usecase);
509 }
510 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700511 } else if (usecase->type == PCM_CAPTURE) {
512 usecase->devices = usecase->stream.in->device;
513 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700514 if (in_snd_device == SND_DEVICE_NONE) {
515 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
516 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700517 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700518 adev->primary_output->devices);
519 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700520 in_snd_device = platform_get_input_snd_device(adev->platform,
521 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700522 }
523 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700524 }
525 }
526
527 if (out_snd_device == usecase->out_snd_device &&
528 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800529 return 0;
530 }
531
sangwoobc677242013-08-08 16:53:43 +0900532 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700533 out_snd_device, platform_get_snd_device_name(out_snd_device),
534 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800535
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800536 /*
537 * Limitation: While in call, to do a device switch we need to disable
538 * and enable both RX and TX devices though one of them is same as current
539 * device.
540 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700541 if (usecase->type == VOICE_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700542 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700543 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800544 }
545
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700546 /* Disable current sound devices */
547 if (usecase->out_snd_device != SND_DEVICE_NONE) {
548 disable_audio_route(adev, usecase, true);
549 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800550 }
551
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700552 if (usecase->in_snd_device != SND_DEVICE_NONE) {
553 disable_audio_route(adev, usecase, true);
554 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800555 }
556
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700557 /* Enable new sound devices */
558 if (out_snd_device != SND_DEVICE_NONE) {
559 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
560 check_usecases_codec_backend(adev, usecase, out_snd_device);
561 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800562 }
563
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700564 if (in_snd_device != SND_DEVICE_NONE) {
565 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700566 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700567 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700568
Eric Laurentb23d5282013-05-14 15:27:20 -0700569 if (usecase->type == VOICE_CALL)
570 status = platform_switch_voice_call_device_post(adev->platform,
571 out_snd_device,
572 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800573
sangwoo170731f2013-06-08 15:36:36 +0900574 audio_route_update_mixer(adev->audio_route);
575
576 usecase->in_snd_device = in_snd_device;
577 usecase->out_snd_device = out_snd_device;
578
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700579 if (usecase->type == VOICE_CALL)
580 enable_all_usecases_of_type(adev, VOICE_CALL, true);
581 else
582 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900583
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800584 return status;
585}
586
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800587static int stop_input_stream(struct stream_in *in)
588{
589 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800590 struct audio_usecase *uc_info;
591 struct audio_device *adev = in->dev;
592
Eric Laurentc8400632013-02-14 19:04:54 -0800593 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800594
Eric Laurent994a6932013-07-17 11:51:42 -0700595 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700596 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800597 uc_info = get_usecase_from_list(adev, in->usecase);
598 if (uc_info == NULL) {
599 ALOGE("%s: Could not find the usecase (%d) in the list",
600 __func__, in->usecase);
601 return -EINVAL;
602 }
603
Eric Laurent150dbfe2013-02-27 14:31:02 -0800604 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700605 disable_audio_route(adev, uc_info, true);
606
607 /* 2. Disable the tx device */
608 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800609
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800610 list_remove(&uc_info->list);
611 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800612
Eric Laurent994a6932013-07-17 11:51:42 -0700613 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800614 return ret;
615}
616
617int start_input_stream(struct stream_in *in)
618{
619 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800620 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800621 struct audio_usecase *uc_info;
622 struct audio_device *adev = in->dev;
623
Eric Laurent994a6932013-07-17 11:51:42 -0700624 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700625
626 /* Check if source matches incall recording usecase criteria */
627 ret = voice_check_and_set_incall_rec_usecase(adev, in);
628 if (ret)
629 goto error_config;
630 else
631 ALOGV("%s: usecase(%d)", __func__, in->usecase);
632
Eric Laurentb23d5282013-05-14 15:27:20 -0700633 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800634 if (in->pcm_device_id < 0) {
635 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
636 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800637 ret = -EINVAL;
638 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800639 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700640
641 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800642 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
643 uc_info->id = in->usecase;
644 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800645 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700646 uc_info->devices = in->device;
647 uc_info->in_snd_device = SND_DEVICE_NONE;
648 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800649
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800650 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700651 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800652
Eric Laurentc8400632013-02-14 19:04:54 -0800653 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
654 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800655 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
656 PCM_IN, &in->config);
657 if (in->pcm && !pcm_is_ready(in->pcm)) {
658 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
659 pcm_close(in->pcm);
660 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800661 ret = -EIO;
662 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800663 }
Eric Laurent994a6932013-07-17 11:51:42 -0700664 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800665 return ret;
666
667error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800668 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800669
670error_config:
671 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700672 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800673
674 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800675}
676
677static int stop_output_stream(struct stream_out *out)
678{
679 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800680 struct audio_usecase *uc_info;
681 struct audio_device *adev = out->dev;
682
Eric Laurent994a6932013-07-17 11:51:42 -0700683 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700684 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800685 uc_info = get_usecase_from_list(adev, out->usecase);
686 if (uc_info == NULL) {
687 ALOGE("%s: Could not find the usecase (%d) in the list",
688 __func__, out->usecase);
689 return -EINVAL;
690 }
691
Eric Laurent150dbfe2013-02-27 14:31:02 -0800692 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700693 disable_audio_route(adev, uc_info, true);
694
695 /* 2. Disable the rx device */
696 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800697
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800698 list_remove(&uc_info->list);
699 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800700
Eric Laurent994a6932013-07-17 11:51:42 -0700701 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800702 return ret;
703}
704
705int start_output_stream(struct stream_out *out)
706{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800707 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800708 struct audio_usecase *uc_info;
709 struct audio_device *adev = out->dev;
710
Eric Laurent994a6932013-07-17 11:51:42 -0700711 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700712 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -0700713 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800714 if (out->pcm_device_id < 0) {
715 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
716 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800717 ret = -EINVAL;
718 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800719 }
720
721 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
722 uc_info->id = out->usecase;
723 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800724 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700725 uc_info->devices = out->devices;
726 uc_info->in_snd_device = SND_DEVICE_NONE;
727 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800728
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800729 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800730
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700731 select_devices(adev, out->usecase);
732
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800733 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
734 __func__, 0, out->pcm_device_id);
735 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
736 PCM_OUT, &out->config);
737 if (out->pcm && !pcm_is_ready(out->pcm)) {
738 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
739 pcm_close(out->pcm);
740 out->pcm = NULL;
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800741 ret = -EIO;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700742 goto error_pcm_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800743 }
Eric Laurent994a6932013-07-17 11:51:42 -0700744 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745 return 0;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700746error_pcm_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800747 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800748error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800749 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800750}
751
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800752static int check_input_parameters(uint32_t sample_rate,
753 audio_format_t format,
754 int channel_count)
755{
756 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
757
758 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
759
760 switch (sample_rate) {
761 case 8000:
762 case 11025:
763 case 12000:
764 case 16000:
765 case 22050:
766 case 24000:
767 case 32000:
768 case 44100:
769 case 48000:
770 break;
771 default:
772 return -EINVAL;
773 }
774
775 return 0;
776}
777
778static size_t get_input_buffer_size(uint32_t sample_rate,
779 audio_format_t format,
780 int channel_count)
781{
782 size_t size = 0;
783
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700784 if (check_input_parameters(sample_rate, format, channel_count) != 0)
785 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800786
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700787 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
788 /* ToDo: should use frame_size computed based on the format and
789 channel_count here. */
790 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800791
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -0700792 /* make sure the size is multiple of 64 */
793 size += 0x3f;
794 size &= ~0x3f;
795
796 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800797}
798
799static uint32_t out_get_sample_rate(const struct audio_stream *stream)
800{
801 struct stream_out *out = (struct stream_out *)stream;
802
803 return out->config.rate;
804}
805
806static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
807{
808 return -ENOSYS;
809}
810
811static size_t out_get_buffer_size(const struct audio_stream *stream)
812{
813 struct stream_out *out = (struct stream_out *)stream;
814
815 return out->config.period_size * audio_stream_frame_size(stream);
816}
817
818static uint32_t out_get_channels(const struct audio_stream *stream)
819{
820 struct stream_out *out = (struct stream_out *)stream;
821
822 return out->channel_mask;
823}
824
825static audio_format_t out_get_format(const struct audio_stream *stream)
826{
827 return AUDIO_FORMAT_PCM_16_BIT;
828}
829
830static int out_set_format(struct audio_stream *stream, audio_format_t format)
831{
832 return -ENOSYS;
833}
834
835static int out_standby(struct audio_stream *stream)
836{
837 struct stream_out *out = (struct stream_out *)stream;
838 struct audio_device *adev = out->dev;
Eric Laurent994a6932013-07-17 11:51:42 -0700839 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700840 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800841 pthread_mutex_lock(&out->lock);
842
843 if (!out->standby) {
844 out->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -0800845 if (out->pcm) {
846 pcm_close(out->pcm);
847 out->pcm = NULL;
848 }
849 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800850 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800851 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800852 }
853 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -0700854 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800855 return 0;
856}
857
858static int out_dump(const struct audio_stream *stream, int fd)
859{
860 return 0;
861}
862
863static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
864{
865 struct stream_out *out = (struct stream_out *)stream;
866 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800867 struct audio_usecase *usecase;
868 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800869 struct str_parms *parms;
870 char value[32];
871 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800872 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800873
sangwoobc677242013-08-08 16:53:43 +0900874 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700875 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800876 parms = str_parms_create_str(kvpairs);
877 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
878 if (ret >= 0) {
879 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800880 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800881 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800882
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700883 /*
884 * When HDMI cable is unplugged the music playback is paused and
885 * the policy manager sends routing=0. But the audioflinger
886 * continues to write data until standby time (3sec).
887 * As the HDMI core is turned off, the write gets blocked.
888 * Avoid this by routing audio to speaker until standby.
889 */
890 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
891 val == AUDIO_DEVICE_NONE) {
892 val = AUDIO_DEVICE_OUT_SPEAKER;
893 }
894
895 /*
896 * select_devices() call below switches all the usecases on the same
897 * backend to the new device. Refer to check_usecases_codec_backend() in
898 * the select_devices(). But how do we undo this?
899 *
900 * For example, music playback is active on headset (deep-buffer usecase)
901 * and if we go to ringtones and select a ringtone, low-latency usecase
902 * will be started on headset+speaker. As we can't enable headset+speaker
903 * and headset devices at the same time, select_devices() switches the music
904 * playback to headset+speaker while starting low-lateny usecase for ringtone.
905 * So when the ringtone playback is completed, how do we undo the same?
906 *
907 * We are relying on the out_set_parameters() call on deep-buffer output,
908 * once the ringtone playback is ended.
909 * NOTE: We should not check if the current devices are same as new devices.
910 * Because select_devices() must be called to switch back the music
911 * playback to headset.
912 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800913 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700914 out->devices = val;
915
916 if (!out->standby)
917 select_devices(adev, out->usecase);
918
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700919 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
920 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700921 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700922 voice_start_call(adev);
923 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
924 voice_is_in_call(adev) &&
925 (out == adev->primary_output)) {
926 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800927 }
928 }
929
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700930 if ((adev->mode == AUDIO_MODE_NORMAL) &&
931 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700932 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700933 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800934 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800935
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800936 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -0800937 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800938 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700939
940 if (out == adev->primary_output) {
941 pthread_mutex_lock(&adev->lock);
942 audio_extn_set_parameters(adev, parms);
943 pthread_mutex_unlock(&adev->lock);
944 }
945
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800946 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -0700947 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800948 return ret;
949}
950
951static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
952{
953 struct stream_out *out = (struct stream_out *)stream;
954 struct str_parms *query = str_parms_create_str(keys);
955 char *str;
956 char value[256];
957 struct str_parms *reply = str_parms_create();
958 size_t i, j;
959 int ret;
960 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -0700961 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800962 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
963 if (ret >= 0) {
964 value[0] = '\0';
965 i = 0;
966 while (out->supported_channel_masks[i] != 0) {
967 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
968 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
969 if (!first) {
970 strcat(value, "|");
971 }
972 strcat(value, out_channels_name_to_enum_table[j].name);
973 first = false;
974 break;
975 }
976 }
977 i++;
978 }
979 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
980 str = str_parms_to_str(reply);
981 } else {
982 str = strdup(keys);
983 }
984 str_parms_destroy(query);
985 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -0700986 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800987 return str;
988}
989
990static uint32_t out_get_latency(const struct audio_stream_out *stream)
991{
992 struct stream_out *out = (struct stream_out *)stream;
993
994 return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate);
995}
996
997static int out_set_volume(struct audio_stream_out *stream, float left,
998 float right)
999{
Eric Laurenta9024de2013-04-04 09:19:12 -07001000 struct stream_out *out = (struct stream_out *)stream;
1001 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1002 /* only take left channel into account: the API is for stereo anyway */
1003 out->muted = (left == 0.0f);
1004 return 0;
1005 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001006 return -ENOSYS;
1007}
1008
1009static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1010 size_t bytes)
1011{
1012 struct stream_out *out = (struct stream_out *)stream;
1013 struct audio_device *adev = out->dev;
1014 int i, ret = -1;
1015
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001016 pthread_mutex_lock(&out->lock);
1017 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001018 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001019 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001020 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001021 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001022 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001023 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001024 goto exit;
1025 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001027
1028 if (out->pcm) {
Eric Laurenta9024de2013-04-04 09:19:12 -07001029 if (out->muted)
1030 memset((void *)buffer, 0, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031 //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1032 ret = pcm_write(out->pcm, (void *)buffer, bytes);
1033 }
1034
1035exit:
1036 pthread_mutex_unlock(&out->lock);
1037
1038 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001039 if (out->pcm)
1040 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001041 out_standby(&out->stream.common);
1042 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1043 out_get_sample_rate(&out->stream.common));
1044 }
1045 return bytes;
1046}
1047
1048static int out_get_render_position(const struct audio_stream_out *stream,
1049 uint32_t *dsp_frames)
1050{
1051 return -EINVAL;
1052}
1053
1054static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1055{
1056 return 0;
1057}
1058
1059static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1060{
1061 return 0;
1062}
1063
1064static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1065 int64_t *timestamp)
1066{
1067 return -EINVAL;
1068}
1069
1070/** audio_stream_in implementation **/
1071static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1072{
1073 struct stream_in *in = (struct stream_in *)stream;
1074
1075 return in->config.rate;
1076}
1077
1078static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1079{
1080 return -ENOSYS;
1081}
1082
1083static size_t in_get_buffer_size(const struct audio_stream *stream)
1084{
1085 struct stream_in *in = (struct stream_in *)stream;
1086
1087 return in->config.period_size * audio_stream_frame_size(stream);
1088}
1089
1090static uint32_t in_get_channels(const struct audio_stream *stream)
1091{
1092 struct stream_in *in = (struct stream_in *)stream;
1093
1094 return in->channel_mask;
1095}
1096
1097static audio_format_t in_get_format(const struct audio_stream *stream)
1098{
1099 return AUDIO_FORMAT_PCM_16_BIT;
1100}
1101
1102static int in_set_format(struct audio_stream *stream, audio_format_t format)
1103{
1104 return -ENOSYS;
1105}
1106
1107static int in_standby(struct audio_stream *stream)
1108{
1109 struct stream_in *in = (struct stream_in *)stream;
1110 struct audio_device *adev = in->dev;
1111 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001112 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001113 pthread_mutex_lock(&in->lock);
1114 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001115 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001116 if (in->pcm) {
1117 pcm_close(in->pcm);
1118 in->pcm = NULL;
1119 }
1120 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001121 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001122 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001123 }
1124 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001125 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001126 return status;
1127}
1128
1129static int in_dump(const struct audio_stream *stream, int fd)
1130{
1131 return 0;
1132}
1133
1134static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1135{
1136 struct stream_in *in = (struct stream_in *)stream;
1137 struct audio_device *adev = in->dev;
1138 struct str_parms *parms;
1139 char *str;
1140 char value[32];
1141 int ret, val = 0;
1142
Eric Laurent994a6932013-07-17 11:51:42 -07001143 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001144 parms = str_parms_create_str(kvpairs);
1145
1146 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1147
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001148 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001149 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001150 if (ret >= 0) {
1151 val = atoi(value);
1152 /* no audio source uses val == 0 */
1153 if ((in->source != val) && (val != 0)) {
1154 in->source = val;
1155 }
1156 }
1157
1158 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1159 if (ret >= 0) {
1160 val = atoi(value);
1161 if ((in->device != val) && (val != 0)) {
1162 in->device = val;
1163 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001164 if (!in->standby)
1165 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001166 }
1167 }
1168
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001169 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001170 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001171
1172 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001173 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001174 return ret;
1175}
1176
1177static char* in_get_parameters(const struct audio_stream *stream,
1178 const char *keys)
1179{
1180 return strdup("");
1181}
1182
1183static int in_set_gain(struct audio_stream_in *stream, float gain)
1184{
1185 return 0;
1186}
1187
1188static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1189 size_t bytes)
1190{
1191 struct stream_in *in = (struct stream_in *)stream;
1192 struct audio_device *adev = in->dev;
1193 int i, ret = -1;
1194
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001195 pthread_mutex_lock(&in->lock);
1196 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001197 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001198 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001199 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001200 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201 goto exit;
1202 }
1203 in->standby = 0;
1204 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001205
1206 if (in->pcm) {
1207 ret = pcm_read(in->pcm, buffer, bytes);
1208 }
1209
1210 /*
1211 * Instead of writing zeroes here, we could trust the hardware
1212 * to always provide zeroes when muted.
1213 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001214 if (ret == 0 && voice_get_mic_mute(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001215 memset(buffer, 0, bytes);
1216
1217exit:
1218 pthread_mutex_unlock(&in->lock);
1219
1220 if (ret != 0) {
1221 in_standby(&in->stream.common);
1222 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1223 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1224 in_get_sample_rate(&in->stream.common));
1225 }
1226 return bytes;
1227}
1228
1229static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1230{
1231 return 0;
1232}
1233
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001234static int add_remove_audio_effect(const struct audio_stream *stream,
1235 effect_handle_t effect,
1236 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001237{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001238 struct stream_in *in = (struct stream_in *)stream;
1239 int status = 0;
1240 effect_descriptor_t desc;
1241
1242 status = (*effect)->get_descriptor(effect, &desc);
1243 if (status != 0)
1244 return status;
1245
1246 pthread_mutex_lock(&in->lock);
1247 pthread_mutex_lock(&in->dev->lock);
1248 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1249 in->enable_aec != enable &&
1250 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1251 in->enable_aec = enable;
1252 if (!in->standby)
1253 select_devices(in->dev, in->usecase);
1254 }
1255 pthread_mutex_unlock(&in->dev->lock);
1256 pthread_mutex_unlock(&in->lock);
1257
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001258 return 0;
1259}
1260
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001261static int in_add_audio_effect(const struct audio_stream *stream,
1262 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001263{
Eric Laurent994a6932013-07-17 11:51:42 -07001264 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001265 return add_remove_audio_effect(stream, effect, true);
1266}
1267
1268static int in_remove_audio_effect(const struct audio_stream *stream,
1269 effect_handle_t effect)
1270{
Eric Laurent994a6932013-07-17 11:51:42 -07001271 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001272 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273}
1274
1275static int adev_open_output_stream(struct audio_hw_device *dev,
1276 audio_io_handle_t handle,
1277 audio_devices_t devices,
1278 audio_output_flags_t flags,
1279 struct audio_config *config,
1280 struct audio_stream_out **stream_out)
1281{
1282 struct audio_device *adev = (struct audio_device *)dev;
1283 struct stream_out *out;
1284 int i, ret;
1285
Eric Laurent994a6932013-07-17 11:51:42 -07001286 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001287 __func__, config->sample_rate, config->channel_mask, devices, flags);
1288 *stream_out = NULL;
1289 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1290
1291 if (devices == AUDIO_DEVICE_NONE)
1292 devices = AUDIO_DEVICE_OUT_SPEAKER;
1293
1294 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
1295 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1296 out->flags = flags;
1297 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001298 out->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001299
1300 /* Init use case and pcm_config */
1301 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1302 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001303 pthread_mutex_lock(&adev->lock);
1304 ret = read_hdmi_channel_masks(out);
1305 pthread_mutex_unlock(&adev->lock);
1306 if (ret != 0) {
1307 /* If HDMI does not support multi channel playback, set the default */
1308 out->config.channels = popcount(out->channel_mask);
Eric Laurentb23d5282013-05-14 15:27:20 -07001309 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001310 goto error_open;
1311 }
1312
1313 if (config->sample_rate == 0)
1314 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1315 if (config->channel_mask == 0)
1316 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1317
1318 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001319 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1320 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001321 out->config.rate = config->sample_rate;
1322 out->config.channels = popcount(out->channel_mask);
1323 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Eric Laurentb23d5282013-05-14 15:27:20 -07001324 platform_set_hdmi_channels(adev->platform, out->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001325 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1326 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1327 out->config = pcm_config_deep_buffer;
1328 } else {
1329 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1330 out->config = pcm_config_low_latency;
1331 }
1332
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001333 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1334 if(adev->primary_output == NULL)
1335 adev->primary_output = out;
1336 else {
1337 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001338 ret = -EEXIST;
1339 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001340 }
1341 }
1342
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001343 /* Check if this usecase is already existing */
1344 pthread_mutex_lock(&adev->lock);
1345 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1346 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001347 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001348 ret = -EEXIST;
1349 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001350 }
1351 pthread_mutex_unlock(&adev->lock);
1352
1353 out->stream.common.get_sample_rate = out_get_sample_rate;
1354 out->stream.common.set_sample_rate = out_set_sample_rate;
1355 out->stream.common.get_buffer_size = out_get_buffer_size;
1356 out->stream.common.get_channels = out_get_channels;
1357 out->stream.common.get_format = out_get_format;
1358 out->stream.common.set_format = out_set_format;
1359 out->stream.common.standby = out_standby;
1360 out->stream.common.dump = out_dump;
1361 out->stream.common.set_parameters = out_set_parameters;
1362 out->stream.common.get_parameters = out_get_parameters;
1363 out->stream.common.add_audio_effect = out_add_audio_effect;
1364 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1365 out->stream.get_latency = out_get_latency;
1366 out->stream.set_volume = out_set_volume;
1367 out->stream.write = out_write;
1368 out->stream.get_render_position = out_get_render_position;
1369 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
1370
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001371 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001372 /* out->muted = false; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001373
1374 config->format = out->stream.common.get_format(&out->stream.common);
1375 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1376 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1377
1378 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001379 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001380 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001381
1382error_open:
1383 free(out);
1384 *stream_out = NULL;
1385 ALOGD("%s: exit: ret %d", __func__, ret);
1386 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001387}
1388
1389static void adev_close_output_stream(struct audio_hw_device *dev,
1390 struct audio_stream_out *stream)
1391{
Eric Laurent994a6932013-07-17 11:51:42 -07001392 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001393 out_standby(&stream->common);
1394 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07001395 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001396}
1397
1398static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
1399{
1400 struct audio_device *adev = (struct audio_device *)dev;
1401 struct str_parms *parms;
1402 char *str;
1403 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001404 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001405 int ret;
1406
Eric Laurent994a6932013-07-17 11:51:42 -07001407 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001408 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001410 voice_set_parameters(adev, parms);
1411 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001412
1413 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
1414 if (ret >= 0) {
1415 /* When set to false, HAL should disable EC and NS
1416 * But it is currently not supported.
1417 */
1418 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1419 adev->bluetooth_nrec = true;
1420 else
1421 adev->bluetooth_nrec = false;
1422 }
1423
1424 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
1425 if (ret >= 0) {
1426 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
1427 adev->screen_off = false;
1428 else
1429 adev->screen_off = true;
1430 }
1431
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07001432 ret = str_parms_get_int(parms, "rotation", &val);
1433 if (ret >= 0) {
1434 bool reverse_speakers = false;
1435 switch(val) {
1436 // FIXME: note that the code below assumes that the speakers are in the correct placement
1437 // relative to the user when the device is rotated 90deg from its default rotation. This
1438 // assumption is device-specific, not platform-specific like this code.
1439 case 270:
1440 reverse_speakers = true;
1441 break;
1442 case 0:
1443 case 90:
1444 case 180:
1445 break;
1446 default:
1447 ALOGE("%s: unexpected rotation of %d", __func__, val);
1448 }
1449 pthread_mutex_lock(&adev->lock);
1450 if (adev->speaker_lr_swap != reverse_speakers) {
1451 adev->speaker_lr_swap = reverse_speakers;
1452 // only update the selected device if there is active pcm playback
1453 struct audio_usecase *usecase;
1454 struct listnode *node;
1455 list_for_each(node, &adev->usecase_list) {
1456 usecase = node_to_item(node, struct audio_usecase, list);
1457 if (usecase->type == PCM_PLAYBACK) {
1458 select_devices(adev, usecase->id);
1459 break;
1460 }
1461 }
1462 }
1463 pthread_mutex_unlock(&adev->lock);
1464 }
1465
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07001466 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001467 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001468 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001469 return ret;
1470}
1471
1472static char* adev_get_parameters(const struct audio_hw_device *dev,
1473 const char *keys)
1474{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001475 struct audio_device *adev = (struct audio_device *)dev;
1476 struct str_parms *reply = str_parms_create();
1477 struct str_parms *query = str_parms_create_str(keys);
1478 char *str;
1479
1480 audio_extn_get_parameters(adev, query, reply);
1481 platform_get_parameters(adev->platform, query, reply);
1482 str = str_parms_to_str(reply);
1483 str_parms_destroy(query);
1484 str_parms_destroy(reply);
1485
1486 ALOGV("%s: exit: returns - %s", __func__, str);
1487 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001488}
1489
1490static int adev_init_check(const struct audio_hw_device *dev)
1491{
1492 return 0;
1493}
1494
1495static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
1496{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001497 return voice_set_volume((struct audio_device *)dev, volume);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001498}
1499
1500static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
1501{
1502 return -ENOSYS;
1503}
1504
1505static int adev_get_master_volume(struct audio_hw_device *dev,
1506 float *volume)
1507{
1508 return -ENOSYS;
1509}
1510
1511static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
1512{
1513 return -ENOSYS;
1514}
1515
1516static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
1517{
1518 return -ENOSYS;
1519}
1520
1521static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
1522{
1523 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001524 pthread_mutex_lock(&adev->lock);
1525 if (adev->mode != mode) {
1526 adev->mode = mode;
1527 }
1528 pthread_mutex_unlock(&adev->lock);
1529 return 0;
1530}
1531
1532static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
1533{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001534 return voice_set_mic_mute((struct audio_device *)dev, state);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001535}
1536
1537static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
1538{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001539 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001540 return 0;
1541}
1542
1543static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
1544 const struct audio_config *config)
1545{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001546 int channel_count = popcount(config->channel_mask);
1547
1548 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
1549}
1550
1551static int adev_open_input_stream(struct audio_hw_device *dev,
1552 audio_io_handle_t handle,
1553 audio_devices_t devices,
1554 struct audio_config *config,
1555 struct audio_stream_in **stream_in)
1556{
1557 struct audio_device *adev = (struct audio_device *)dev;
1558 struct stream_in *in;
1559 int ret, buffer_size, frame_size;
1560 int channel_count = popcount(config->channel_mask);
1561
Eric Laurent994a6932013-07-17 11:51:42 -07001562 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001563 *stream_in = NULL;
1564 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
1565 return -EINVAL;
1566
1567 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
1568
1569 in->stream.common.get_sample_rate = in_get_sample_rate;
1570 in->stream.common.set_sample_rate = in_set_sample_rate;
1571 in->stream.common.get_buffer_size = in_get_buffer_size;
1572 in->stream.common.get_channels = in_get_channels;
1573 in->stream.common.get_format = in_get_format;
1574 in->stream.common.set_format = in_set_format;
1575 in->stream.common.standby = in_standby;
1576 in->stream.common.dump = in_dump;
1577 in->stream.common.set_parameters = in_set_parameters;
1578 in->stream.common.get_parameters = in_get_parameters;
1579 in->stream.common.add_audio_effect = in_add_audio_effect;
1580 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1581 in->stream.set_gain = in_set_gain;
1582 in->stream.read = in_read;
1583 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1584
1585 in->device = devices;
1586 in->source = AUDIO_SOURCE_DEFAULT;
1587 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001588 in->standby = 1;
1589 in->channel_mask = config->channel_mask;
1590
1591 /* Update config params with the requested sample rate and channels */
1592 in->usecase = USECASE_AUDIO_RECORD;
1593 in->config = pcm_config_audio_capture;
1594 in->config.channels = channel_count;
1595 in->config.rate = config->sample_rate;
1596
1597 frame_size = audio_stream_frame_size((struct audio_stream *)in);
1598 buffer_size = get_input_buffer_size(config->sample_rate,
1599 config->format,
1600 channel_count);
1601 in->config.period_size = buffer_size / frame_size;
1602
1603 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001604 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001605 return 0;
1606
1607err_open:
1608 free(in);
1609 *stream_in = NULL;
1610 return ret;
1611}
1612
1613static void adev_close_input_stream(struct audio_hw_device *dev,
1614 struct audio_stream_in *stream)
1615{
Eric Laurent994a6932013-07-17 11:51:42 -07001616 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001617
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001618 in_standby(&stream->common);
1619 free(stream);
1620
1621 return;
1622}
1623
1624static int adev_dump(const audio_hw_device_t *device, int fd)
1625{
1626 return 0;
1627}
1628
1629static int adev_close(hw_device_t *device)
1630{
1631 struct audio_device *adev = (struct audio_device *)device;
1632 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07001633 free(adev->snd_dev_ref_cnt);
1634 platform_deinit(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001635 free(device);
1636 return 0;
1637}
1638
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001639static int adev_open(const hw_module_t *module, const char *name,
1640 hw_device_t **device)
1641{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001642 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001643
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001644 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001645 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
1646
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001647 pthread_mutex_lock(&adev_init_lock);
1648 if (is_adev_initialised == true){
1649 *device = &adev->device.common;
1650 ALOGD("%s: returning existing instance of adev", __func__);
1651 ALOGD("%s: exit", __func__);
1652 pthread_mutex_unlock(&adev_init_lock);
1653 return 0;
1654 }
1655
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001656 adev = calloc(1, sizeof(struct audio_device));
1657
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001658 adev->device.common.tag = HARDWARE_DEVICE_TAG;
1659 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1660 adev->device.common.module = (struct hw_module_t *)module;
1661 adev->device.common.close = adev_close;
1662
1663 adev->device.init_check = adev_init_check;
1664 adev->device.set_voice_volume = adev_set_voice_volume;
1665 adev->device.set_master_volume = adev_set_master_volume;
1666 adev->device.get_master_volume = adev_get_master_volume;
1667 adev->device.set_master_mute = adev_set_master_mute;
1668 adev->device.get_master_mute = adev_get_master_mute;
1669 adev->device.set_mode = adev_set_mode;
1670 adev->device.set_mic_mute = adev_set_mic_mute;
1671 adev->device.get_mic_mute = adev_get_mic_mute;
1672 adev->device.set_parameters = adev_set_parameters;
1673 adev->device.get_parameters = adev_get_parameters;
1674 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
1675 adev->device.open_output_stream = adev_open_output_stream;
1676 adev->device.close_output_stream = adev_close_output_stream;
1677 adev->device.open_input_stream = adev_open_input_stream;
1678 adev->device.close_input_stream = adev_close_input_stream;
1679 adev->device.dump = adev_dump;
1680
1681 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001682 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08001683 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001684 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001685 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001686 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08001687 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurentb23d5282013-05-14 15:27:20 -07001688 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001689 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001690 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001691
1692 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07001693 adev->platform = platform_init(adev);
1694 if (!adev->platform) {
1695 free(adev->snd_dev_ref_cnt);
1696 free(adev);
1697 ALOGE("%s: Failed to init platform data, aborting.", __func__);
1698 *device = NULL;
1699 return -EINVAL;
1700 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001701 *device = &adev->device.common;
1702
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001703 /* update init flag*/
1704 is_adev_initialised = true;
1705 pthread_mutex_unlock(&adev_init_lock);
1706
Eric Laurent994a6932013-07-17 11:51:42 -07001707 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001708 return 0;
1709}
1710
1711static struct hw_module_methods_t hal_module_methods = {
1712 .open = adev_open,
1713};
1714
1715struct audio_module HAL_MODULE_INFO_SYM = {
1716 .common = {
1717 .tag = HARDWARE_MODULE_TAG,
1718 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1719 .hal_api_version = HARDWARE_HAL_API_VERSION,
1720 .id = AUDIO_HARDWARE_MODULE_ID,
1721 .name = "QCOM Audio HAL",
1722 .author = "Code Aurora Forum",
1723 .methods = &hal_module_methods,
1724 },
1725};