blob: f7e742823a93f3e749e88e442701d1e3054828f6 [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 Alamanda4e02e552013-07-17 15:22:04 -070022/*#define VERY_VERY_VERBOSE_LOGGING*/
23#ifdef VERY_VERY_VERBOSE_LOGGING
24#define ALOGVV ALOGV
25#else
26#define ALOGVV(a...) do { } while(0)
27#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080028
29#include <errno.h>
30#include <pthread.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080034#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070035#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070036#include <sys/resource.h>
37#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038
39#include <cutils/log.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070042#include <cutils/atomic.h>
43#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080044
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <hardware/audio_effect.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070050#include "platform_api.h"
51#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070052#include "audio_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080053
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070054#include "sound/compress_params.h"
55
56#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
57#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
58/* ToDo: Check and update a proper value in msec */
59#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
60#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
61
Eric Laurentb23d5282013-05-14 15:27:20 -070062struct pcm_config pcm_config_deep_buffer = {
63 .channels = 2,
64 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
65 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
66 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
67 .format = PCM_FORMAT_S16_LE,
68 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
69 .stop_threshold = INT_MAX,
70 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
71};
72
73struct pcm_config pcm_config_low_latency = {
74 .channels = 2,
75 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
76 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
77 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
78 .format = PCM_FORMAT_S16_LE,
79 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
80 .stop_threshold = INT_MAX,
81 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
82};
83
84struct pcm_config pcm_config_hdmi_multi = {
85 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
86 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
87 .period_size = HDMI_MULTI_PERIOD_SIZE,
88 .period_count = HDMI_MULTI_PERIOD_COUNT,
89 .format = PCM_FORMAT_S16_LE,
90 .start_threshold = 0,
91 .stop_threshold = INT_MAX,
92 .avail_min = 0,
93};
94
95struct pcm_config pcm_config_audio_capture = {
96 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070097 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
98 .format = PCM_FORMAT_S16_LE,
99};
100
Eric Laurentb23d5282013-05-14 15:27:20 -0700101static const char * const use_case_table[AUDIO_USECASE_MAX] = {
102 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
103 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
104 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700105 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700106 [USECASE_AUDIO_RECORD] = "audio-record",
107 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700108 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700109 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700110
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700111 [USECASE_VOICE2_CALL] = "voice2-call",
112 [USECASE_VOLTE_CALL] = "volte-call",
113 [USECASE_QCHAT_CALL] = "qchat-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700114 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
115 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
116 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700117 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
118 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Eric Laurentb23d5282013-05-14 15:27:20 -0700119};
120
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800121
122#define STRING_TO_ENUM(string) { #string, string }
123
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800124struct string_to_enum {
125 const char *name;
126 uint32_t value;
127};
128
129static const struct string_to_enum out_channels_name_to_enum_table[] = {
130 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
131 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
132 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
133};
134
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700135static struct audio_device *adev = NULL;
136static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700137static unsigned int audio_device_ref_count;
138
Haynes Mathew George5191a852013-09-11 14:19:36 -0700139static int set_voice_volume_l(struct audio_device *adev, float volume);
140
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700141static bool is_supported_format(audio_format_t format)
142{
Eric Laurent86e17132013-09-12 17:49:30 -0700143 if (format == AUDIO_FORMAT_MP3 ||
144 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700145 return true;
146
147 return false;
148}
149
150static int get_snd_codec_id(audio_format_t format)
151{
152 int id = 0;
153
154 switch (format) {
155 case AUDIO_FORMAT_MP3:
156 id = SND_AUDIOCODEC_MP3;
157 break;
158 case AUDIO_FORMAT_AAC:
159 id = SND_AUDIOCODEC_AAC;
160 break;
161 default:
162 ALOGE("%s: Unsupported audio format", __func__);
163 }
164
165 return id;
166}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800167
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700168static int enable_audio_route(struct audio_device *adev,
169 struct audio_usecase *usecase,
170 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800171{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700172 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700173 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800174
175 if (usecase == NULL)
176 return -EINVAL;
177
178 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
179
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800180 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700181 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800182 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700183 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800184
185 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700186 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700187 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700188 audio_route_apply_path(adev->audio_route, mixer_path);
189 if (update_mixer)
190 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800191
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800192 ALOGV("%s: exit", __func__);
193 return 0;
194}
195
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700196int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700197 struct audio_usecase *usecase,
198 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800199{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700200 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700201 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800202
203 if (usecase == NULL)
204 return -EINVAL;
205
206 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700207 if (usecase->type == PCM_CAPTURE)
208 snd_device = usecase->in_snd_device;
209 else
210 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800211 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700212 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700213 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700214 audio_route_reset_path(adev->audio_route, mixer_path);
215 if (update_mixer)
216 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800217
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800218 ALOGV("%s: exit", __func__);
219 return 0;
220}
221
222static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700223 snd_device_t snd_device,
224 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800225{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700226 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
227
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800228 if (snd_device < SND_DEVICE_MIN ||
229 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800230 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800231 return -EINVAL;
232 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700233
234 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700235
236 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
237 ALOGE("%s: Invalid sound device returned", __func__);
238 return -EINVAL;
239 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700240 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700241 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700242 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700243 return 0;
244 }
245
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700246 /* start usb playback thread */
247 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
248 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
249 audio_extn_usb_start_playback(adev);
250
251 /* start usb capture thread */
252 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
253 audio_extn_usb_start_capture(adev);
254
Eric Laurentb23d5282013-05-14 15:27:20 -0700255 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700256 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800257 return -EINVAL;
258 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800259
Eric Laurent994a6932013-07-17 11:51:42 -0700260 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700261 snd_device, device_name);
262 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700263 if (update_mixer)
264 audio_route_update_mixer(adev->audio_route);
265
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800266 return 0;
267}
268
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700269int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700270 snd_device_t snd_device,
271 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800272{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700273 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
274
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800275 if (snd_device < SND_DEVICE_MIN ||
276 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800277 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800278 return -EINVAL;
279 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700280 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
281 ALOGE("%s: device ref cnt is already 0", __func__);
282 return -EINVAL;
283 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700284
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700285 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700286
287 /* exit usb play back thread */
288 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
289 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
290 audio_extn_usb_stop_playback();
291
292 /* exit usb capture thread */
293 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
294 audio_extn_usb_stop_capture(adev);
295
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700296 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
297 ALOGE("%s: Invalid sound device returned", __func__);
298 return -EINVAL;
299 }
300
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700301 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700302 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700303 snd_device, device_name);
304 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700305 if (update_mixer)
306 audio_route_update_mixer(adev->audio_route);
307 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700308
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800309 return 0;
310}
311
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700312static void check_usecases_codec_backend(struct audio_device *adev,
313 struct audio_usecase *uc_info,
314 snd_device_t snd_device)
315{
316 struct listnode *node;
317 struct audio_usecase *usecase;
318 bool switch_device[AUDIO_USECASE_MAX];
319 int i, num_uc_to_switch = 0;
320
321 /*
322 * This function is to make sure that all the usecases that are active on
323 * the hardware codec backend are always routed to any one device that is
324 * handled by the hardware codec.
325 * For example, if low-latency and deep-buffer usecases are currently active
326 * on speaker and out_set_parameters(headset) is received on low-latency
327 * output, then we have to make sure deep-buffer is also switched to headset,
328 * because of the limitation that both the devices cannot be enabled
329 * at the same time as they share the same backend.
330 */
331 /* Disable all the usecases on the shared backend other than the
332 specified usecase */
333 for (i = 0; i < AUDIO_USECASE_MAX; i++)
334 switch_device[i] = false;
335
336 list_for_each(node, &adev->usecase_list) {
337 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700338 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700339 usecase != uc_info &&
340 usecase->out_snd_device != snd_device &&
341 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
342 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
343 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700344 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700345 disable_audio_route(adev, usecase, false);
346 switch_device[usecase->id] = true;
347 num_uc_to_switch++;
348 }
349 }
350
351 if (num_uc_to_switch) {
352 /* Make sure all the streams are de-routed before disabling the device */
353 audio_route_update_mixer(adev->audio_route);
354
355 list_for_each(node, &adev->usecase_list) {
356 usecase = node_to_item(node, struct audio_usecase, list);
357 if (switch_device[usecase->id]) {
358 disable_snd_device(adev, usecase->out_snd_device, false);
359 enable_snd_device(adev, snd_device, false);
360 }
361 }
362
363 /* Make sure new snd device is enabled before re-routing the streams */
364 audio_route_update_mixer(adev->audio_route);
365
366 /* Re-route all the usecases on the shared backend other than the
367 specified usecase to new snd devices */
368 list_for_each(node, &adev->usecase_list) {
369 usecase = node_to_item(node, struct audio_usecase, list);
370 /* Update the out_snd_device only before enabling the audio route */
371 if (switch_device[usecase->id] ) {
372 usecase->out_snd_device = snd_device;
373 enable_audio_route(adev, usecase, false);
374 }
375 }
376
377 audio_route_update_mixer(adev->audio_route);
378 }
379}
380
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700381static void check_and_route_capture_usecases(struct audio_device *adev,
382 struct audio_usecase *uc_info,
383 snd_device_t snd_device)
384{
385 struct listnode *node;
386 struct audio_usecase *usecase;
387 bool switch_device[AUDIO_USECASE_MAX];
388 int i, num_uc_to_switch = 0;
389
390 /*
391 * This function is to make sure that all the active capture usecases
392 * are always routed to the same input sound device.
393 * For example, if audio-record and voice-call usecases are currently
394 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
395 * is received for voice call then we have to make sure that audio-record
396 * usecase is also switched to earpiece i.e. voice-dmic-ef,
397 * because of the limitation that two devices cannot be enabled
398 * at the same time if they share the same backend.
399 */
400 for (i = 0; i < AUDIO_USECASE_MAX; i++)
401 switch_device[i] = false;
402
403 list_for_each(node, &adev->usecase_list) {
404 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700405 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700406 usecase != uc_info &&
407 usecase->in_snd_device != snd_device) {
408 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
409 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700410 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700411 disable_audio_route(adev, usecase, false);
412 switch_device[usecase->id] = true;
413 num_uc_to_switch++;
414 }
415 }
416
417 if (num_uc_to_switch) {
418 /* Make sure all the streams are de-routed before disabling the device */
419 audio_route_update_mixer(adev->audio_route);
420
421 list_for_each(node, &adev->usecase_list) {
422 usecase = node_to_item(node, struct audio_usecase, list);
423 if (switch_device[usecase->id]) {
424 disable_snd_device(adev, usecase->in_snd_device, false);
425 enable_snd_device(adev, snd_device, false);
426 }
427 }
428
429 /* Make sure new snd device is enabled before re-routing the streams */
430 audio_route_update_mixer(adev->audio_route);
431
432 /* Re-route all the usecases on the shared backend other than the
433 specified usecase to new snd devices */
434 list_for_each(node, &adev->usecase_list) {
435 usecase = node_to_item(node, struct audio_usecase, list);
436 /* Update the in_snd_device only before enabling the audio route */
437 if (switch_device[usecase->id] ) {
438 usecase->in_snd_device = snd_device;
439 enable_audio_route(adev, usecase, false);
440 }
441 }
442
443 audio_route_update_mixer(adev->audio_route);
444 }
445}
446
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700447static int disable_all_usecases_of_type(struct audio_device *adev,
448 usecase_type_t usecase_type,
449 bool update_mixer)
450{
451 struct audio_usecase *usecase;
452 struct listnode *node;
453 int ret = 0;
454
455 list_for_each(node, &adev->usecase_list) {
456 usecase = node_to_item(node, struct audio_usecase, list);
457 if (usecase->type == usecase_type) {
458 ALOGV("%s: usecase id %d", __func__, usecase->id);
459 ret = disable_audio_route(adev, usecase, update_mixer);
460 if (ret) {
461 ALOGE("%s: Failed to disable usecase id %d",
462 __func__, usecase->id);
463 }
464 }
465 }
466
467 return ret;
468}
469
470static int enable_all_usecases_of_type(struct audio_device *adev,
471 usecase_type_t usecase_type,
472 bool update_mixer)
473{
474 struct audio_usecase *usecase;
475 struct listnode *node;
476 int ret = 0;
477
478 list_for_each(node, &adev->usecase_list) {
479 usecase = node_to_item(node, struct audio_usecase, list);
480 if (usecase->type == usecase_type) {
481 ALOGV("%s: usecase id %d", __func__, usecase->id);
482 ret = enable_audio_route(adev, usecase, update_mixer);
483 if (ret) {
484 ALOGE("%s: Failed to enable usecase id %d",
485 __func__, usecase->id);
486 }
487 }
488 }
489
490 return ret;
491}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800492
493/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700494static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800495{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700496 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700497 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800498
499 switch (channels) {
500 /*
501 * Do not handle stereo output in Multi-channel cases
502 * Stereo case is handled in normal playback path
503 */
504 case 6:
505 ALOGV("%s: HDMI supports 5.1", __func__);
506 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
507 break;
508 case 8:
509 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
510 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
511 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
512 break;
513 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700514 ALOGE("HDMI does not support multi channel playback");
515 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800516 break;
517 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700518 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800519}
520
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700521static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
522{
523 struct audio_usecase *usecase;
524 struct listnode *node;
525
526 list_for_each(node, &adev->usecase_list) {
527 usecase = node_to_item(node, struct audio_usecase, list);
528 if (usecase->type == VOICE_CALL) {
529 ALOGV("%s: usecase id %d", __func__, usecase->id);
530 return usecase->id;
531 }
532 }
533 return USECASE_INVALID;
534}
535
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700536struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700537 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700538{
539 struct audio_usecase *usecase;
540 struct listnode *node;
541
542 list_for_each(node, &adev->usecase_list) {
543 usecase = node_to_item(node, struct audio_usecase, list);
544 if (usecase->id == uc_id)
545 return usecase;
546 }
547 return NULL;
548}
549
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700550int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800551{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800552 snd_device_t out_snd_device = SND_DEVICE_NONE;
553 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700554 struct audio_usecase *usecase = NULL;
555 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800556 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700557 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800558
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700559 usecase = get_usecase_from_list(adev, uc_id);
560 if (usecase == NULL) {
561 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
562 return -EINVAL;
563 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800564
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700565 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700566 out_snd_device = platform_get_output_snd_device(adev->platform,
567 usecase->stream.out->devices);
568 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700569 usecase->devices = usecase->stream.out->devices;
570 } else {
571 /*
572 * If the voice call is active, use the sound devices of voice call usecase
573 * so that it would not result any device switch. All the usecases will
574 * be switched to new device when select_devices() is called for voice call
575 * usecase. This is to avoid switching devices for voice call when
576 * check_usecases_codec_backend() is called below.
577 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700578 if (voice_is_in_call(adev)) {
579 vc_usecase = get_usecase_from_list(adev,
580 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700581 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
582 in_snd_device = vc_usecase->in_snd_device;
583 out_snd_device = vc_usecase->out_snd_device;
584 }
585 }
586 if (usecase->type == PCM_PLAYBACK) {
587 usecase->devices = usecase->stream.out->devices;
588 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700589 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700590 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700591 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700592 if (usecase->stream.out == adev->primary_output &&
593 adev->active_input &&
594 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
595 select_devices(adev, adev->active_input->usecase);
596 }
597 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700598 } else if (usecase->type == PCM_CAPTURE) {
599 usecase->devices = usecase->stream.in->device;
600 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700601 if (in_snd_device == SND_DEVICE_NONE) {
602 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
603 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700604 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700605 adev->primary_output->devices);
606 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700607 in_snd_device = platform_get_input_snd_device(adev->platform,
608 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700609 }
610 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700611 }
612 }
613
614 if (out_snd_device == usecase->out_snd_device &&
615 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800616 return 0;
617 }
618
sangwoobc677242013-08-08 16:53:43 +0900619 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700620 out_snd_device, platform_get_snd_device_name(out_snd_device),
621 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800622
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800623 /*
624 * Limitation: While in call, to do a device switch we need to disable
625 * and enable both RX and TX devices though one of them is same as current
626 * device.
627 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700628 if (usecase->type == VOICE_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700629 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700630 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800631 }
632
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700633 /* Disable current sound devices */
634 if (usecase->out_snd_device != SND_DEVICE_NONE) {
635 disable_audio_route(adev, usecase, true);
636 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800637 }
638
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700639 if (usecase->in_snd_device != SND_DEVICE_NONE) {
640 disable_audio_route(adev, usecase, true);
641 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800642 }
643
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700644 /* Enable new sound devices */
645 if (out_snd_device != SND_DEVICE_NONE) {
646 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
647 check_usecases_codec_backend(adev, usecase, out_snd_device);
648 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800649 }
650
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700651 if (in_snd_device != SND_DEVICE_NONE) {
652 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700653 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700654 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700655
Eric Laurentb23d5282013-05-14 15:27:20 -0700656 if (usecase->type == VOICE_CALL)
657 status = platform_switch_voice_call_device_post(adev->platform,
658 out_snd_device,
659 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800660
sangwoo170731f2013-06-08 15:36:36 +0900661 audio_route_update_mixer(adev->audio_route);
662
663 usecase->in_snd_device = in_snd_device;
664 usecase->out_snd_device = out_snd_device;
665
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700666 if (usecase->type == VOICE_CALL)
667 enable_all_usecases_of_type(adev, VOICE_CALL, true);
668 else
669 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900670
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800671 return status;
672}
673
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800674static int stop_input_stream(struct stream_in *in)
675{
676 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800677 struct audio_usecase *uc_info;
678 struct audio_device *adev = in->dev;
679
Eric Laurentc8400632013-02-14 19:04:54 -0800680 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800681
Eric Laurent994a6932013-07-17 11:51:42 -0700682 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700683 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800684 uc_info = get_usecase_from_list(adev, in->usecase);
685 if (uc_info == NULL) {
686 ALOGE("%s: Could not find the usecase (%d) in the list",
687 __func__, in->usecase);
688 return -EINVAL;
689 }
690
Eric Laurent150dbfe2013-02-27 14:31:02 -0800691 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700692 disable_audio_route(adev, uc_info, true);
693
694 /* 2. Disable the tx device */
695 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800696
Kiran Kandi910e1862013-10-29 13:29:42 -0700697 audio_extn_listen_update_status(uc_info,
698 LISTEN_EVENT_AUDIO_CAPTURE_INACTIVE);
699
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800700 list_remove(&uc_info->list);
701 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800702
Eric Laurent994a6932013-07-17 11:51:42 -0700703 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800704 return ret;
705}
706
707int start_input_stream(struct stream_in *in)
708{
709 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800710 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800711 struct audio_usecase *uc_info;
712 struct audio_device *adev = in->dev;
713
Eric Laurent994a6932013-07-17 11:51:42 -0700714 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700715
716 /* Check if source matches incall recording usecase criteria */
717 ret = voice_check_and_set_incall_rec_usecase(adev, in);
718 if (ret)
719 goto error_config;
720 else
721 ALOGV("%s: usecase(%d)", __func__, in->usecase);
722
Eric Laurentb23d5282013-05-14 15:27:20 -0700723 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800724 if (in->pcm_device_id < 0) {
725 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
726 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800727 ret = -EINVAL;
728 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800729 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700730
731 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
733 uc_info->id = in->usecase;
734 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800735 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700736 uc_info->devices = in->device;
737 uc_info->in_snd_device = SND_DEVICE_NONE;
738 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800739
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800740 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700741 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800742
Kiran Kandi910e1862013-10-29 13:29:42 -0700743 audio_extn_listen_update_status(uc_info,
744 LISTEN_EVENT_AUDIO_CAPTURE_ACTIVE);
745
Eric Laurentc8400632013-02-14 19:04:54 -0800746 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
747 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800748 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
749 PCM_IN, &in->config);
750 if (in->pcm && !pcm_is_ready(in->pcm)) {
751 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
752 pcm_close(in->pcm);
753 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800754 ret = -EIO;
755 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800756 }
Eric Laurent994a6932013-07-17 11:51:42 -0700757 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800758 return ret;
759
760error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800761 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800762
763error_config:
764 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700765 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800766
767 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800768}
769
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700770/* must be called with out->lock locked */
771static int send_offload_cmd_l(struct stream_out* out, int command)
772{
773 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
774
775 ALOGVV("%s %d", __func__, command);
776
777 cmd->cmd = command;
778 list_add_tail(&out->offload_cmd_list, &cmd->node);
779 pthread_cond_signal(&out->offload_cond);
780 return 0;
781}
782
783/* must be called iwth out->lock locked */
784static void stop_compressed_output_l(struct stream_out *out)
785{
786 out->offload_state = OFFLOAD_STATE_IDLE;
787 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700788 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700789 if (out->compr != NULL) {
790 compress_stop(out->compr);
791 while (out->offload_thread_blocked) {
792 pthread_cond_wait(&out->cond, &out->lock);
793 }
794 }
795}
796
797static void *offload_thread_loop(void *context)
798{
799 struct stream_out *out = (struct stream_out *) context;
800 struct listnode *item;
801
802 out->offload_state = OFFLOAD_STATE_IDLE;
803 out->playback_started = 0;
804
805 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
806 set_sched_policy(0, SP_FOREGROUND);
807 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
808
809 ALOGV("%s", __func__);
810 pthread_mutex_lock(&out->lock);
811 for (;;) {
812 struct offload_cmd *cmd = NULL;
813 stream_callback_event_t event;
814 bool send_callback = false;
815
816 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
817 __func__, list_empty(&out->offload_cmd_list),
818 out->offload_state);
819 if (list_empty(&out->offload_cmd_list)) {
820 ALOGV("%s SLEEPING", __func__);
821 pthread_cond_wait(&out->offload_cond, &out->lock);
822 ALOGV("%s RUNNING", __func__);
823 continue;
824 }
825
826 item = list_head(&out->offload_cmd_list);
827 cmd = node_to_item(item, struct offload_cmd, node);
828 list_remove(item);
829
830 ALOGVV("%s STATE %d CMD %d out->compr %p",
831 __func__, out->offload_state, cmd->cmd, out->compr);
832
833 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
834 free(cmd);
835 break;
836 }
837
838 if (out->compr == NULL) {
839 ALOGE("%s: Compress handle is NULL", __func__);
840 pthread_cond_signal(&out->cond);
841 continue;
842 }
843 out->offload_thread_blocked = true;
844 pthread_mutex_unlock(&out->lock);
845 send_callback = false;
846 switch(cmd->cmd) {
847 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
848 compress_wait(out->compr, -1);
849 send_callback = true;
850 event = STREAM_CBK_EVENT_WRITE_READY;
851 break;
852 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700853 compress_next_track(out->compr);
854 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700855 send_callback = true;
856 event = STREAM_CBK_EVENT_DRAIN_READY;
857 break;
858 case OFFLOAD_CMD_DRAIN:
859 compress_drain(out->compr);
860 send_callback = true;
861 event = STREAM_CBK_EVENT_DRAIN_READY;
862 break;
863 default:
864 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
865 break;
866 }
867 pthread_mutex_lock(&out->lock);
868 out->offload_thread_blocked = false;
869 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700870 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700871 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700872 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700873 free(cmd);
874 }
875
876 pthread_cond_signal(&out->cond);
877 while (!list_empty(&out->offload_cmd_list)) {
878 item = list_head(&out->offload_cmd_list);
879 list_remove(item);
880 free(node_to_item(item, struct offload_cmd, node));
881 }
882 pthread_mutex_unlock(&out->lock);
883
884 return NULL;
885}
886
887static int create_offload_callback_thread(struct stream_out *out)
888{
889 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
890 list_init(&out->offload_cmd_list);
891 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
892 offload_thread_loop, out);
893 return 0;
894}
895
896static int destroy_offload_callback_thread(struct stream_out *out)
897{
898 pthread_mutex_lock(&out->lock);
899 stop_compressed_output_l(out);
900 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
901
902 pthread_mutex_unlock(&out->lock);
903 pthread_join(out->offload_thread, (void **) NULL);
904 pthread_cond_destroy(&out->offload_cond);
905
906 return 0;
907}
908
Eric Laurent07eeafd2013-10-06 12:52:49 -0700909static bool allow_hdmi_channel_config(struct audio_device *adev)
910{
911 struct listnode *node;
912 struct audio_usecase *usecase;
913 bool ret = true;
914
915 list_for_each(node, &adev->usecase_list) {
916 usecase = node_to_item(node, struct audio_usecase, list);
917 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
918 /*
919 * If voice call is already existing, do not proceed further to avoid
920 * disabling/enabling both RX and TX devices, CSD calls, etc.
921 * Once the voice call done, the HDMI channels can be configured to
922 * max channels of remaining use cases.
923 */
924 if (usecase->id == USECASE_VOICE_CALL) {
925 ALOGD("%s: voice call is active, no change in HDMI channels",
926 __func__);
927 ret = false;
928 break;
929 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
930 ALOGD("%s: multi channel playback is active, "
931 "no change in HDMI channels", __func__);
932 ret = false;
933 break;
934 }
935 }
936 }
937 return ret;
938}
939
940static int check_and_set_hdmi_channels(struct audio_device *adev,
941 unsigned int channels)
942{
943 struct listnode *node;
944 struct audio_usecase *usecase;
945
946 /* Check if change in HDMI channel config is allowed */
947 if (!allow_hdmi_channel_config(adev))
948 return 0;
949
950 if (channels == adev->cur_hdmi_channels) {
951 ALOGD("%s: Requested channels are same as current", __func__);
952 return 0;
953 }
954
955 platform_set_hdmi_channels(adev->platform, channels);
956 adev->cur_hdmi_channels = channels;
957
958 /*
959 * Deroute all the playback streams routed to HDMI so that
960 * the back end is deactivated. Note that backend will not
961 * be deactivated if any one stream is connected to it.
962 */
963 list_for_each(node, &adev->usecase_list) {
964 usecase = node_to_item(node, struct audio_usecase, list);
965 if (usecase->type == PCM_PLAYBACK &&
966 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
967 disable_audio_route(adev, usecase, true);
968 }
969 }
970
971 /*
972 * Enable all the streams disabled above. Now the HDMI backend
973 * will be activated with new channel configuration
974 */
975 list_for_each(node, &adev->usecase_list) {
976 usecase = node_to_item(node, struct audio_usecase, list);
977 if (usecase->type == PCM_PLAYBACK &&
978 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
979 enable_audio_route(adev, usecase, true);
980 }
981 }
982
983 return 0;
984}
985
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800986static int stop_output_stream(struct stream_out *out)
987{
988 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800989 struct audio_usecase *uc_info;
990 struct audio_device *adev = out->dev;
991
Eric Laurent994a6932013-07-17 11:51:42 -0700992 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700993 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800994 uc_info = get_usecase_from_list(adev, out->usecase);
995 if (uc_info == NULL) {
996 ALOGE("%s: Could not find the usecase (%d) in the list",
997 __func__, out->usecase);
998 return -EINVAL;
999 }
1000
Eric Laurentc4aef752013-09-12 17:45:53 -07001001 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
1002 adev->visualizer_stop_output != NULL)
1003 adev->visualizer_stop_output(out->handle);
1004
Eric Laurent150dbfe2013-02-27 14:31:02 -08001005 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001006 disable_audio_route(adev, uc_info, true);
1007
1008 /* 2. Disable the rx device */
1009 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001010
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001011 list_remove(&uc_info->list);
1012 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001013
Eric Laurent07eeafd2013-10-06 12:52:49 -07001014 /* Must be called after removing the usecase from list */
1015 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1016 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1017
Eric Laurent994a6932013-07-17 11:51:42 -07001018 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001019 return ret;
1020}
1021
1022int start_output_stream(struct stream_out *out)
1023{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001024 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001025 struct audio_usecase *uc_info;
1026 struct audio_device *adev = out->dev;
1027
Eric Laurent994a6932013-07-17 11:51:42 -07001028 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001029 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001030 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001031 if (out->pcm_device_id < 0) {
1032 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1033 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001034 ret = -EINVAL;
1035 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001036 }
1037
1038 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1039 uc_info->id = out->usecase;
1040 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001041 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001042 uc_info->devices = out->devices;
1043 uc_info->in_snd_device = SND_DEVICE_NONE;
1044 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001045
Eric Laurent07eeafd2013-10-06 12:52:49 -07001046 /* This must be called before adding this usecase to the list */
1047 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1048 check_and_set_hdmi_channels(adev, out->config.channels);
1049
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001050 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001051
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001052 select_devices(adev, out->usecase);
1053
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001054 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1055 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001056 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1057 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001058 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001059 if (out->pcm && !pcm_is_ready(out->pcm)) {
1060 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1061 pcm_close(out->pcm);
1062 out->pcm = NULL;
1063 ret = -EIO;
1064 goto error_open;
1065 }
1066 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001067 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001068 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1069 COMPRESS_IN, &out->compr_config);
1070 if (out->compr && !is_compress_ready(out->compr)) {
1071 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1072 compress_close(out->compr);
1073 out->compr = NULL;
1074 ret = -EIO;
1075 goto error_open;
1076 }
1077 if (out->offload_callback)
1078 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001079
1080 if (adev->visualizer_start_output != NULL)
1081 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001082 }
Eric Laurent994a6932013-07-17 11:51:42 -07001083 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001084 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001085error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001086 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001087error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001088 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001089}
1090
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001091static int check_input_parameters(uint32_t sample_rate,
1092 audio_format_t format,
1093 int channel_count)
1094{
1095 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1096
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001097 if ((channel_count < 1) || (channel_count > 6)) return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001098
1099 switch (sample_rate) {
1100 case 8000:
1101 case 11025:
1102 case 12000:
1103 case 16000:
1104 case 22050:
1105 case 24000:
1106 case 32000:
1107 case 44100:
1108 case 48000:
1109 break;
1110 default:
1111 return -EINVAL;
1112 }
1113
1114 return 0;
1115}
1116
1117static size_t get_input_buffer_size(uint32_t sample_rate,
1118 audio_format_t format,
1119 int channel_count)
1120{
1121 size_t size = 0;
1122
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001123 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1124 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001125
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001126 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1127 /* ToDo: should use frame_size computed based on the format and
1128 channel_count here. */
1129 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001130
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001131 /* make sure the size is multiple of 64 */
1132 size += 0x3f;
1133 size &= ~0x3f;
1134
1135 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001136}
1137
1138static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1139{
1140 struct stream_out *out = (struct stream_out *)stream;
1141
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001142 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001143}
1144
1145static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1146{
1147 return -ENOSYS;
1148}
1149
1150static size_t out_get_buffer_size(const struct audio_stream *stream)
1151{
1152 struct stream_out *out = (struct stream_out *)stream;
1153
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001154 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1155 return out->compr_config.fragment_size;
1156 }
1157
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001158 return out->config.period_size * audio_stream_frame_size(stream);
1159}
1160
1161static uint32_t out_get_channels(const struct audio_stream *stream)
1162{
1163 struct stream_out *out = (struct stream_out *)stream;
1164
1165 return out->channel_mask;
1166}
1167
1168static audio_format_t out_get_format(const struct audio_stream *stream)
1169{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001170 struct stream_out *out = (struct stream_out *)stream;
1171
1172 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001173}
1174
1175static int out_set_format(struct audio_stream *stream, audio_format_t format)
1176{
1177 return -ENOSYS;
1178}
1179
1180static int out_standby(struct audio_stream *stream)
1181{
1182 struct stream_out *out = (struct stream_out *)stream;
1183 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001184
Eric Laurent994a6932013-07-17 11:51:42 -07001185 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001186 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001187
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001188 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001189 if (!out->standby) {
1190 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001191 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1192 if (out->pcm) {
1193 pcm_close(out->pcm);
1194 out->pcm = NULL;
1195 }
1196 } else {
1197 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001198 out->gapless_mdata.encoder_delay = 0;
1199 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001200 if (out->compr != NULL) {
1201 compress_close(out->compr);
1202 out->compr = NULL;
1203 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001204 }
1205 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001206 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001207 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001208 }
1209 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001210 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001211 return 0;
1212}
1213
1214static int out_dump(const struct audio_stream *stream, int fd)
1215{
1216 return 0;
1217}
1218
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001219static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1220{
1221 int ret = 0;
1222 char value[32];
1223 struct compr_gapless_mdata tmp_mdata;
1224
1225 if (!out || !parms) {
1226 return -EINVAL;
1227 }
1228
1229 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1230 if (ret >= 0) {
1231 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1232 } else {
1233 return -EINVAL;
1234 }
1235
1236 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1237 if (ret >= 0) {
1238 tmp_mdata.encoder_padding = atoi(value);
1239 } else {
1240 return -EINVAL;
1241 }
1242
1243 out->gapless_mdata = tmp_mdata;
1244 out->send_new_metadata = 1;
1245 ALOGV("%s new encoder delay %u and padding %u", __func__,
1246 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1247
1248 return 0;
1249}
1250
1251
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001252static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1253{
1254 struct stream_out *out = (struct stream_out *)stream;
1255 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001256 struct audio_usecase *usecase;
1257 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001258 struct str_parms *parms;
1259 char value[32];
1260 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001261 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262
sangwoobc677242013-08-08 16:53:43 +09001263 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001264 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001265 parms = str_parms_create_str(kvpairs);
1266 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1267 if (ret >= 0) {
1268 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001269 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001270 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001271
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001272 /*
1273 * When HDMI cable is unplugged the music playback is paused and
1274 * the policy manager sends routing=0. But the audioflinger
1275 * continues to write data until standby time (3sec).
1276 * As the HDMI core is turned off, the write gets blocked.
1277 * Avoid this by routing audio to speaker until standby.
1278 */
1279 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1280 val == AUDIO_DEVICE_NONE) {
1281 val = AUDIO_DEVICE_OUT_SPEAKER;
1282 }
1283
1284 /*
1285 * select_devices() call below switches all the usecases on the same
1286 * backend to the new device. Refer to check_usecases_codec_backend() in
1287 * the select_devices(). But how do we undo this?
1288 *
1289 * For example, music playback is active on headset (deep-buffer usecase)
1290 * and if we go to ringtones and select a ringtone, low-latency usecase
1291 * will be started on headset+speaker. As we can't enable headset+speaker
1292 * and headset devices at the same time, select_devices() switches the music
1293 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1294 * So when the ringtone playback is completed, how do we undo the same?
1295 *
1296 * We are relying on the out_set_parameters() call on deep-buffer output,
1297 * once the ringtone playback is ended.
1298 * NOTE: We should not check if the current devices are same as new devices.
1299 * Because select_devices() must be called to switch back the music
1300 * playback to headset.
1301 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001302 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001303 out->devices = val;
1304
1305 if (!out->standby)
1306 select_devices(adev, out->usecase);
1307
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001308 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1309 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001310 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001311 voice_start_call(adev);
1312 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1313 voice_is_in_call(adev) &&
1314 (out == adev->primary_output)) {
1315 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001316 }
1317 }
1318
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001319 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1320 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001321 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001322 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001324
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001325 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001326 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001327 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001328
1329 if (out == adev->primary_output) {
1330 pthread_mutex_lock(&adev->lock);
1331 audio_extn_set_parameters(adev, parms);
1332 pthread_mutex_unlock(&adev->lock);
1333 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001334 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1335 parse_compress_metadata(out, parms);
1336 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001337
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001338 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001339 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001340 return ret;
1341}
1342
1343static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1344{
1345 struct stream_out *out = (struct stream_out *)stream;
1346 struct str_parms *query = str_parms_create_str(keys);
1347 char *str;
1348 char value[256];
1349 struct str_parms *reply = str_parms_create();
1350 size_t i, j;
1351 int ret;
1352 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001353 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001354 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1355 if (ret >= 0) {
1356 value[0] = '\0';
1357 i = 0;
1358 while (out->supported_channel_masks[i] != 0) {
1359 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1360 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1361 if (!first) {
1362 strcat(value, "|");
1363 }
1364 strcat(value, out_channels_name_to_enum_table[j].name);
1365 first = false;
1366 break;
1367 }
1368 }
1369 i++;
1370 }
1371 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1372 str = str_parms_to_str(reply);
1373 } else {
1374 str = strdup(keys);
1375 }
1376 str_parms_destroy(query);
1377 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001378 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001379 return str;
1380}
1381
1382static uint32_t out_get_latency(const struct audio_stream_out *stream)
1383{
1384 struct stream_out *out = (struct stream_out *)stream;
1385
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001386 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1387 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1388
1389 return (out->config.period_count * out->config.period_size * 1000) /
1390 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001391}
1392
1393static int out_set_volume(struct audio_stream_out *stream, float left,
1394 float right)
1395{
Eric Laurenta9024de2013-04-04 09:19:12 -07001396 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001397 int volume[2];
1398
Eric Laurenta9024de2013-04-04 09:19:12 -07001399 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1400 /* only take left channel into account: the API is for stereo anyway */
1401 out->muted = (left == 0.0f);
1402 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001403 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1404 const char *mixer_ctl_name = "Compress Playback Volume";
1405 struct audio_device *adev = out->dev;
1406 struct mixer_ctl *ctl;
1407
1408 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1409 if (!ctl) {
1410 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1411 __func__, mixer_ctl_name);
1412 return -EINVAL;
1413 }
1414 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1415 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1416 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1417 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001418 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001419
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001420 return -ENOSYS;
1421}
1422
1423static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1424 size_t bytes)
1425{
1426 struct stream_out *out = (struct stream_out *)stream;
1427 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001428 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001429
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001430 pthread_mutex_lock(&out->lock);
1431 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001432 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001433 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001434 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001435 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001436 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001437 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001438 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001439 goto exit;
1440 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001441 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001442
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001443 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001444 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1445 if (out->send_new_metadata) {
1446 ALOGVV("send new gapless metadata");
1447 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1448 out->send_new_metadata = 0;
1449 }
1450
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001451 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001452 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001453 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001454 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1455 }
1456 if (!out->playback_started) {
1457 compress_start(out->compr);
1458 out->playback_started = 1;
1459 out->offload_state = OFFLOAD_STATE_PLAYING;
1460 }
1461 pthread_mutex_unlock(&out->lock);
1462 return ret;
1463 } else {
1464 if (out->pcm) {
1465 if (out->muted)
1466 memset((void *)buffer, 0, bytes);
1467 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1468 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001469 if (ret == 0)
1470 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001471 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001472 }
1473
1474exit:
1475 pthread_mutex_unlock(&out->lock);
1476
1477 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001478 if (out->pcm)
1479 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001480 out_standby(&out->stream.common);
1481 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1482 out_get_sample_rate(&out->stream.common));
1483 }
1484 return bytes;
1485}
1486
1487static int out_get_render_position(const struct audio_stream_out *stream,
1488 uint32_t *dsp_frames)
1489{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001490 struct stream_out *out = (struct stream_out *)stream;
1491 *dsp_frames = 0;
1492 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1493 pthread_mutex_lock(&out->lock);
1494 if (out->compr != NULL) {
1495 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1496 &out->sample_rate);
1497 ALOGVV("%s rendered frames %d sample_rate %d",
1498 __func__, *dsp_frames, out->sample_rate);
1499 }
1500 pthread_mutex_unlock(&out->lock);
1501 return 0;
1502 } else
1503 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001504}
1505
1506static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1507{
1508 return 0;
1509}
1510
1511static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1512{
1513 return 0;
1514}
1515
1516static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1517 int64_t *timestamp)
1518{
1519 return -EINVAL;
1520}
1521
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001522static int out_get_presentation_position(const struct audio_stream_out *stream,
1523 uint64_t *frames, struct timespec *timestamp)
1524{
1525 struct stream_out *out = (struct stream_out *)stream;
1526 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001527 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001528
1529 pthread_mutex_lock(&out->lock);
1530
Eric Laurent949a0892013-09-20 09:20:13 -07001531 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1532 if (out->compr != NULL) {
1533 compress_get_tstamp(out->compr, &dsp_frames,
1534 &out->sample_rate);
1535 ALOGVV("%s rendered frames %ld sample_rate %d",
1536 __func__, dsp_frames, out->sample_rate);
1537 *frames = dsp_frames;
1538 ret = 0;
1539 /* this is the best we can do */
1540 clock_gettime(CLOCK_MONOTONIC, timestamp);
1541 }
1542 } else {
1543 if (out->pcm) {
1544 size_t avail;
1545 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1546 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001547 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001548 // This adjustment accounts for buffering after app processor.
1549 // It is based on estimated DSP latency per use case, rather than exact.
1550 signed_frames -=
1551 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1552
Eric Laurent949a0892013-09-20 09:20:13 -07001553 // It would be unusual for this value to be negative, but check just in case ...
1554 if (signed_frames >= 0) {
1555 *frames = signed_frames;
1556 ret = 0;
1557 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001558 }
1559 }
1560 }
1561
1562 pthread_mutex_unlock(&out->lock);
1563
1564 return ret;
1565}
1566
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001567static int out_set_callback(struct audio_stream_out *stream,
1568 stream_callback_t callback, void *cookie)
1569{
1570 struct stream_out *out = (struct stream_out *)stream;
1571
1572 ALOGV("%s", __func__);
1573 pthread_mutex_lock(&out->lock);
1574 out->offload_callback = callback;
1575 out->offload_cookie = cookie;
1576 pthread_mutex_unlock(&out->lock);
1577 return 0;
1578}
1579
1580static int out_pause(struct audio_stream_out* stream)
1581{
1582 struct stream_out *out = (struct stream_out *)stream;
1583 int status = -ENOSYS;
1584 ALOGV("%s", __func__);
1585 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1586 pthread_mutex_lock(&out->lock);
1587 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1588 status = compress_pause(out->compr);
1589 out->offload_state = OFFLOAD_STATE_PAUSED;
1590 }
1591 pthread_mutex_unlock(&out->lock);
1592 }
1593 return status;
1594}
1595
1596static int out_resume(struct audio_stream_out* stream)
1597{
1598 struct stream_out *out = (struct stream_out *)stream;
1599 int status = -ENOSYS;
1600 ALOGV("%s", __func__);
1601 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1602 status = 0;
1603 pthread_mutex_lock(&out->lock);
1604 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1605 status = compress_resume(out->compr);
1606 out->offload_state = OFFLOAD_STATE_PLAYING;
1607 }
1608 pthread_mutex_unlock(&out->lock);
1609 }
1610 return status;
1611}
1612
1613static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1614{
1615 struct stream_out *out = (struct stream_out *)stream;
1616 int status = -ENOSYS;
1617 ALOGV("%s", __func__);
1618 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1619 pthread_mutex_lock(&out->lock);
1620 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1621 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1622 else
1623 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1624 pthread_mutex_unlock(&out->lock);
1625 }
1626 return status;
1627}
1628
1629static int out_flush(struct audio_stream_out* stream)
1630{
1631 struct stream_out *out = (struct stream_out *)stream;
1632 ALOGV("%s", __func__);
1633 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1634 pthread_mutex_lock(&out->lock);
1635 stop_compressed_output_l(out);
1636 pthread_mutex_unlock(&out->lock);
1637 return 0;
1638 }
1639 return -ENOSYS;
1640}
1641
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001642/** audio_stream_in implementation **/
1643static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1644{
1645 struct stream_in *in = (struct stream_in *)stream;
1646
1647 return in->config.rate;
1648}
1649
1650static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1651{
1652 return -ENOSYS;
1653}
1654
1655static size_t in_get_buffer_size(const struct audio_stream *stream)
1656{
1657 struct stream_in *in = (struct stream_in *)stream;
1658
1659 return in->config.period_size * audio_stream_frame_size(stream);
1660}
1661
1662static uint32_t in_get_channels(const struct audio_stream *stream)
1663{
1664 struct stream_in *in = (struct stream_in *)stream;
1665
1666 return in->channel_mask;
1667}
1668
1669static audio_format_t in_get_format(const struct audio_stream *stream)
1670{
1671 return AUDIO_FORMAT_PCM_16_BIT;
1672}
1673
1674static int in_set_format(struct audio_stream *stream, audio_format_t format)
1675{
1676 return -ENOSYS;
1677}
1678
1679static int in_standby(struct audio_stream *stream)
1680{
1681 struct stream_in *in = (struct stream_in *)stream;
1682 struct audio_device *adev = in->dev;
1683 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001684 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001685 pthread_mutex_lock(&in->lock);
1686 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001687 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001688 if (in->pcm) {
1689 pcm_close(in->pcm);
1690 in->pcm = NULL;
1691 }
1692 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001693 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001694 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001695 }
1696 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001697 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001698 return status;
1699}
1700
1701static int in_dump(const struct audio_stream *stream, int fd)
1702{
1703 return 0;
1704}
1705
1706static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1707{
1708 struct stream_in *in = (struct stream_in *)stream;
1709 struct audio_device *adev = in->dev;
1710 struct str_parms *parms;
1711 char *str;
1712 char value[32];
1713 int ret, val = 0;
1714
Eric Laurent994a6932013-07-17 11:51:42 -07001715 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001716 parms = str_parms_create_str(kvpairs);
1717
1718 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1719
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001720 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001721 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001722 if (ret >= 0) {
1723 val = atoi(value);
1724 /* no audio source uses val == 0 */
1725 if ((in->source != val) && (val != 0)) {
1726 in->source = val;
1727 }
1728 }
1729
1730 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1731 if (ret >= 0) {
1732 val = atoi(value);
1733 if ((in->device != val) && (val != 0)) {
1734 in->device = val;
1735 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001736 if (!in->standby)
1737 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001738 }
1739 }
1740
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001741 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001742 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001743
1744 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001745 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001746 return ret;
1747}
1748
1749static char* in_get_parameters(const struct audio_stream *stream,
1750 const char *keys)
1751{
1752 return strdup("");
1753}
1754
1755static int in_set_gain(struct audio_stream_in *stream, float gain)
1756{
1757 return 0;
1758}
1759
1760static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1761 size_t bytes)
1762{
1763 struct stream_in *in = (struct stream_in *)stream;
1764 struct audio_device *adev = in->dev;
1765 int i, ret = -1;
1766
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001767 pthread_mutex_lock(&in->lock);
1768 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001769 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001770 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001771 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001772 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001773 goto exit;
1774 }
1775 in->standby = 0;
1776 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001777
1778 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001779 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1780 ret = audio_extn_ssr_read(stream, buffer, bytes);
1781 else
1782 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001783 }
1784
1785 /*
1786 * Instead of writing zeroes here, we could trust the hardware
1787 * to always provide zeroes when muted.
1788 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001789 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790 memset(buffer, 0, bytes);
1791
1792exit:
1793 pthread_mutex_unlock(&in->lock);
1794
1795 if (ret != 0) {
1796 in_standby(&in->stream.common);
1797 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1798 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1799 in_get_sample_rate(&in->stream.common));
1800 }
1801 return bytes;
1802}
1803
1804static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1805{
1806 return 0;
1807}
1808
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001809static int add_remove_audio_effect(const struct audio_stream *stream,
1810 effect_handle_t effect,
1811 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001812{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001813 struct stream_in *in = (struct stream_in *)stream;
1814 int status = 0;
1815 effect_descriptor_t desc;
1816
1817 status = (*effect)->get_descriptor(effect, &desc);
1818 if (status != 0)
1819 return status;
1820
1821 pthread_mutex_lock(&in->lock);
1822 pthread_mutex_lock(&in->dev->lock);
1823 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1824 in->enable_aec != enable &&
1825 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1826 in->enable_aec = enable;
1827 if (!in->standby)
1828 select_devices(in->dev, in->usecase);
1829 }
1830 pthread_mutex_unlock(&in->dev->lock);
1831 pthread_mutex_unlock(&in->lock);
1832
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001833 return 0;
1834}
1835
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001836static int in_add_audio_effect(const struct audio_stream *stream,
1837 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001838{
Eric Laurent994a6932013-07-17 11:51:42 -07001839 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001840 return add_remove_audio_effect(stream, effect, true);
1841}
1842
1843static int in_remove_audio_effect(const struct audio_stream *stream,
1844 effect_handle_t effect)
1845{
Eric Laurent994a6932013-07-17 11:51:42 -07001846 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001847 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001848}
1849
1850static int adev_open_output_stream(struct audio_hw_device *dev,
1851 audio_io_handle_t handle,
1852 audio_devices_t devices,
1853 audio_output_flags_t flags,
1854 struct audio_config *config,
1855 struct audio_stream_out **stream_out)
1856{
1857 struct audio_device *adev = (struct audio_device *)dev;
1858 struct stream_out *out;
1859 int i, ret;
1860
Eric Laurent994a6932013-07-17 11:51:42 -07001861 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001862 __func__, config->sample_rate, config->channel_mask, devices, flags);
1863 *stream_out = NULL;
1864 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1865
1866 if (devices == AUDIO_DEVICE_NONE)
1867 devices = AUDIO_DEVICE_OUT_SPEAKER;
1868
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001869 out->flags = flags;
1870 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001871 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001872 out->format = config->format;
1873 out->sample_rate = config->sample_rate;
1874 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1875 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001876 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001877
1878 /* Init use case and pcm_config */
1879 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1880 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001881 pthread_mutex_lock(&adev->lock);
1882 ret = read_hdmi_channel_masks(out);
1883 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001884 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001885 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001886
1887 if (config->sample_rate == 0)
1888 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1889 if (config->channel_mask == 0)
1890 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1891
1892 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001893 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001894 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1895 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001896 out->config.rate = config->sample_rate;
1897 out->config.channels = popcount(out->channel_mask);
1898 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001899 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1900 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1901 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001902 out->sample_rate = out->config.rate;
1903 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1904 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1905 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1906 ALOGE("%s: Unsupported Offload information", __func__);
1907 ret = -EINVAL;
1908 goto error_open;
1909 }
1910 if (!is_supported_format(config->offload_info.format)) {
1911 ALOGE("%s: Unsupported audio format", __func__);
1912 ret = -EINVAL;
1913 goto error_open;
1914 }
1915
1916 out->compr_config.codec = (struct snd_codec *)
1917 calloc(1, sizeof(struct snd_codec));
1918
1919 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1920 if (config->offload_info.channel_mask)
1921 out->channel_mask = config->offload_info.channel_mask;
1922 else if (config->channel_mask)
1923 out->channel_mask = config->channel_mask;
1924 out->format = config->offload_info.format;
1925 out->sample_rate = config->offload_info.sample_rate;
1926
1927 out->stream.set_callback = out_set_callback;
1928 out->stream.pause = out_pause;
1929 out->stream.resume = out_resume;
1930 out->stream.drain = out_drain;
1931 out->stream.flush = out_flush;
1932
1933 out->compr_config.codec->id =
1934 get_snd_codec_id(config->offload_info.format);
1935 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1936 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1937 out->compr_config.codec->sample_rate =
1938 compress_get_alsa_rate(config->offload_info.sample_rate);
1939 out->compr_config.codec->bit_rate =
1940 config->offload_info.bit_rate;
1941 out->compr_config.codec->ch_in =
1942 popcount(config->channel_mask);
1943 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1944
1945 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1946 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001947
1948 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001949 create_offload_callback_thread(out);
1950 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1951 __func__, config->offload_info.version,
1952 config->offload_info.bit_rate);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07001953 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
1954 ret = voice_check_and_set_incall_music_usecase(adev, out);
1955 if (ret != 0) {
1956 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
1957 __func__, ret);
1958 goto error_open;
1959 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001960 } else {
1961 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1962 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001963 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001964 }
1965
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001966 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1967 if(adev->primary_output == NULL)
1968 adev->primary_output = out;
1969 else {
1970 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001971 ret = -EEXIST;
1972 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001973 }
1974 }
1975
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001976 /* Check if this usecase is already existing */
1977 pthread_mutex_lock(&adev->lock);
1978 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1979 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001980 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001981 ret = -EEXIST;
1982 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001983 }
1984 pthread_mutex_unlock(&adev->lock);
1985
1986 out->stream.common.get_sample_rate = out_get_sample_rate;
1987 out->stream.common.set_sample_rate = out_set_sample_rate;
1988 out->stream.common.get_buffer_size = out_get_buffer_size;
1989 out->stream.common.get_channels = out_get_channels;
1990 out->stream.common.get_format = out_get_format;
1991 out->stream.common.set_format = out_set_format;
1992 out->stream.common.standby = out_standby;
1993 out->stream.common.dump = out_dump;
1994 out->stream.common.set_parameters = out_set_parameters;
1995 out->stream.common.get_parameters = out_get_parameters;
1996 out->stream.common.add_audio_effect = out_add_audio_effect;
1997 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1998 out->stream.get_latency = out_get_latency;
1999 out->stream.set_volume = out_set_volume;
2000 out->stream.write = out_write;
2001 out->stream.get_render_position = out_get_render_position;
2002 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002003 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002004
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002005 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002006 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002007 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002008
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002009 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2010 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002011
2012 config->format = out->stream.common.get_format(&out->stream.common);
2013 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2014 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2015
2016 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002017 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002018 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002019
2020error_open:
2021 free(out);
2022 *stream_out = NULL;
2023 ALOGD("%s: exit: ret %d", __func__, ret);
2024 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002025}
2026
2027static void adev_close_output_stream(struct audio_hw_device *dev,
2028 struct audio_stream_out *stream)
2029{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002030 struct stream_out *out = (struct stream_out *)stream;
2031 struct audio_device *adev = out->dev;
2032
Eric Laurent994a6932013-07-17 11:51:42 -07002033 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002034 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002035 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2036 destroy_offload_callback_thread(out);
2037
2038 if (out->compr_config.codec != NULL)
2039 free(out->compr_config.codec);
2040 }
2041 pthread_cond_destroy(&out->cond);
2042 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002043 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002044 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002045}
2046
2047static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2048{
2049 struct audio_device *adev = (struct audio_device *)dev;
2050 struct str_parms *parms;
2051 char *str;
2052 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002053 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002054 int ret;
2055
Eric Laurent994a6932013-07-17 11:51:42 -07002056 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002057 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002058
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002059 voice_set_parameters(adev, parms);
2060 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002061
2062 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2063 if (ret >= 0) {
2064 /* When set to false, HAL should disable EC and NS
2065 * But it is currently not supported.
2066 */
2067 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2068 adev->bluetooth_nrec = true;
2069 else
2070 adev->bluetooth_nrec = false;
2071 }
2072
2073 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2074 if (ret >= 0) {
2075 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2076 adev->screen_off = false;
2077 else
2078 adev->screen_off = true;
2079 }
2080
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002081 ret = str_parms_get_int(parms, "rotation", &val);
2082 if (ret >= 0) {
2083 bool reverse_speakers = false;
2084 switch(val) {
2085 // FIXME: note that the code below assumes that the speakers are in the correct placement
2086 // relative to the user when the device is rotated 90deg from its default rotation. This
2087 // assumption is device-specific, not platform-specific like this code.
2088 case 270:
2089 reverse_speakers = true;
2090 break;
2091 case 0:
2092 case 90:
2093 case 180:
2094 break;
2095 default:
2096 ALOGE("%s: unexpected rotation of %d", __func__, val);
2097 }
2098 pthread_mutex_lock(&adev->lock);
2099 if (adev->speaker_lr_swap != reverse_speakers) {
2100 adev->speaker_lr_swap = reverse_speakers;
2101 // only update the selected device if there is active pcm playback
2102 struct audio_usecase *usecase;
2103 struct listnode *node;
2104 list_for_each(node, &adev->usecase_list) {
2105 usecase = node_to_item(node, struct audio_usecase, list);
2106 if (usecase->type == PCM_PLAYBACK) {
2107 select_devices(adev, usecase->id);
2108 break;
2109 }
2110 }
2111 }
2112 pthread_mutex_unlock(&adev->lock);
2113 }
2114
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002115 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002116 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002117 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002118 return ret;
2119}
2120
2121static char* adev_get_parameters(const struct audio_hw_device *dev,
2122 const char *keys)
2123{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002124 struct audio_device *adev = (struct audio_device *)dev;
2125 struct str_parms *reply = str_parms_create();
2126 struct str_parms *query = str_parms_create_str(keys);
2127 char *str;
2128
2129 audio_extn_get_parameters(adev, query, reply);
2130 platform_get_parameters(adev->platform, query, reply);
2131 str = str_parms_to_str(reply);
2132 str_parms_destroy(query);
2133 str_parms_destroy(reply);
2134
2135 ALOGV("%s: exit: returns - %s", __func__, str);
2136 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002137}
2138
2139static int adev_init_check(const struct audio_hw_device *dev)
2140{
2141 return 0;
2142}
2143
2144static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2145{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002146 int ret;
2147 struct audio_device *adev = (struct audio_device *)dev;
2148 pthread_mutex_lock(&adev->lock);
2149 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002150 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002151 pthread_mutex_unlock(&adev->lock);
2152 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002153}
2154
2155static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2156{
2157 return -ENOSYS;
2158}
2159
2160static int adev_get_master_volume(struct audio_hw_device *dev,
2161 float *volume)
2162{
2163 return -ENOSYS;
2164}
2165
2166static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2167{
2168 return -ENOSYS;
2169}
2170
2171static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2172{
2173 return -ENOSYS;
2174}
2175
2176static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2177{
2178 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002179 pthread_mutex_lock(&adev->lock);
2180 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002181 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002182 adev->mode = mode;
2183 }
2184 pthread_mutex_unlock(&adev->lock);
2185 return 0;
2186}
2187
2188static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2189{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002190 return voice_set_mic_mute((struct audio_device *)dev, state);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002191}
2192
2193static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2194{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002195 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002196 return 0;
2197}
2198
2199static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2200 const struct audio_config *config)
2201{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002202 int channel_count = popcount(config->channel_mask);
2203
2204 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2205}
2206
2207static int adev_open_input_stream(struct audio_hw_device *dev,
2208 audio_io_handle_t handle,
2209 audio_devices_t devices,
2210 struct audio_config *config,
2211 struct audio_stream_in **stream_in)
2212{
2213 struct audio_device *adev = (struct audio_device *)dev;
2214 struct stream_in *in;
2215 int ret, buffer_size, frame_size;
2216 int channel_count = popcount(config->channel_mask);
2217
Eric Laurent994a6932013-07-17 11:51:42 -07002218 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002219 *stream_in = NULL;
2220 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2221 return -EINVAL;
2222
2223 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2224
2225 in->stream.common.get_sample_rate = in_get_sample_rate;
2226 in->stream.common.set_sample_rate = in_set_sample_rate;
2227 in->stream.common.get_buffer_size = in_get_buffer_size;
2228 in->stream.common.get_channels = in_get_channels;
2229 in->stream.common.get_format = in_get_format;
2230 in->stream.common.set_format = in_set_format;
2231 in->stream.common.standby = in_standby;
2232 in->stream.common.dump = in_dump;
2233 in->stream.common.set_parameters = in_set_parameters;
2234 in->stream.common.get_parameters = in_get_parameters;
2235 in->stream.common.add_audio_effect = in_add_audio_effect;
2236 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2237 in->stream.set_gain = in_set_gain;
2238 in->stream.read = in_read;
2239 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2240
2241 in->device = devices;
2242 in->source = AUDIO_SOURCE_DEFAULT;
2243 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002244 in->standby = 1;
2245 in->channel_mask = config->channel_mask;
2246
2247 /* Update config params with the requested sample rate and channels */
2248 in->usecase = USECASE_AUDIO_RECORD;
2249 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002250 in->config.rate = config->sample_rate;
2251
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002252 if (audio_extn_ssr_get_enabled()&& channel_count == 6) {
2253 if(audio_extn_ssr_init(adev, in))
2254 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2255 } else {
2256 in->config.channels = channel_count;
2257 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2258 buffer_size = get_input_buffer_size(config->sample_rate,
2259 config->format,
2260 channel_count);
2261 in->config.period_size = buffer_size / frame_size;
2262 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002263
2264 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002265 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002266 return 0;
2267
2268err_open:
2269 free(in);
2270 *stream_in = NULL;
2271 return ret;
2272}
2273
2274static void adev_close_input_stream(struct audio_hw_device *dev,
2275 struct audio_stream_in *stream)
2276{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002277 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002278 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002279
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002280 in_standby(&stream->common);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002281 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2282 audio_extn_ssr_deinit();
2283 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002284 free(stream);
2285
2286 return;
2287}
2288
2289static int adev_dump(const audio_hw_device_t *device, int fd)
2290{
2291 return 0;
2292}
2293
2294static int adev_close(hw_device_t *device)
2295{
2296 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002297
2298 if (!adev)
2299 return 0;
2300
2301 pthread_mutex_lock(&adev_init_lock);
2302
2303 if ((--audio_device_ref_count) == 0) {
2304 audio_route_free(adev->audio_route);
2305 free(adev->snd_dev_ref_cnt);
2306 platform_deinit(adev->platform);
2307 audio_extn_listen_deinit(adev);
2308 free(device);
2309 adev = NULL;
2310 }
2311 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002312 return 0;
2313}
2314
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002315static int adev_open(const hw_module_t *module, const char *name,
2316 hw_device_t **device)
2317{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002318 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002319
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002320 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002321 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2322
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002323 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002324 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002325 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002326 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002327 ALOGD("%s: returning existing instance of adev", __func__);
2328 ALOGD("%s: exit", __func__);
2329 pthread_mutex_unlock(&adev_init_lock);
2330 return 0;
2331 }
2332
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002333 adev = calloc(1, sizeof(struct audio_device));
2334
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002335 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2336 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2337 adev->device.common.module = (struct hw_module_t *)module;
2338 adev->device.common.close = adev_close;
2339
2340 adev->device.init_check = adev_init_check;
2341 adev->device.set_voice_volume = adev_set_voice_volume;
2342 adev->device.set_master_volume = adev_set_master_volume;
2343 adev->device.get_master_volume = adev_get_master_volume;
2344 adev->device.set_master_mute = adev_set_master_mute;
2345 adev->device.get_master_mute = adev_get_master_mute;
2346 adev->device.set_mode = adev_set_mode;
2347 adev->device.set_mic_mute = adev_set_mic_mute;
2348 adev->device.get_mic_mute = adev_get_mic_mute;
2349 adev->device.set_parameters = adev_set_parameters;
2350 adev->device.get_parameters = adev_get_parameters;
2351 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2352 adev->device.open_output_stream = adev_open_output_stream;
2353 adev->device.close_output_stream = adev_close_output_stream;
2354 adev->device.open_input_stream = adev_open_input_stream;
2355 adev->device.close_input_stream = adev_close_input_stream;
2356 adev->device.dump = adev_dump;
2357
2358 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002359 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002360 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002361 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002362 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002363 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002364 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002365 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002366 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002367 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002368 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002369
2370 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002371 adev->platform = platform_init(adev);
2372 if (!adev->platform) {
2373 free(adev->snd_dev_ref_cnt);
2374 free(adev);
2375 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2376 *device = NULL;
2377 return -EINVAL;
2378 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002379
2380 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2381 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2382 if (adev->visualizer_lib == NULL) {
2383 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2384 } else {
2385 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2386 adev->visualizer_start_output =
2387 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2388 "visualizer_hal_start_output");
2389 adev->visualizer_stop_output =
2390 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2391 "visualizer_hal_stop_output");
2392 }
2393 }
Kiran Kandi910e1862013-10-29 13:29:42 -07002394 audio_extn_listen_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07002395
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002396 *device = &adev->device.common;
2397
Kiran Kandi910e1862013-10-29 13:29:42 -07002398 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002399 pthread_mutex_unlock(&adev_init_lock);
2400
Eric Laurent994a6932013-07-17 11:51:42 -07002401 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002402 return 0;
2403}
2404
2405static struct hw_module_methods_t hal_module_methods = {
2406 .open = adev_open,
2407};
2408
2409struct audio_module HAL_MODULE_INFO_SYM = {
2410 .common = {
2411 .tag = HARDWARE_MODULE_TAG,
2412 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2413 .hal_api_version = HARDWARE_HAL_API_VERSION,
2414 .id = AUDIO_HARDWARE_MODULE_ID,
2415 .name = "QCOM Audio HAL",
2416 .author = "Code Aurora Forum",
2417 .methods = &hal_module_methods,
2418 },
2419};