blob: 20763acf8143e3ca94aa1459df7a6a19fb245f6f [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002 * Copyright (C) 2013-2014 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "audio_hw_primary"
18/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070019/*#define VERY_VERY_VERBOSE_LOGGING*/
20#ifdef VERY_VERY_VERBOSE_LOGGING
21#define ALOGVV ALOGV
22#else
23#define ALOGVV(a...) do { } while(0)
24#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080025
26#include <errno.h>
27#include <pthread.h>
28#include <stdint.h>
29#include <sys/time.h>
30#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080031#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070032#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070033#include <sys/resource.h>
34#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080035
36#include <cutils/log.h>
37#include <cutils/str_parms.h>
38#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070039#include <cutils/atomic.h>
40#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080041
Eric Laurentb23d5282013-05-14 15:27:20 -070042#include <hardware/audio_effect.h>
Andy Hung31aca912014-03-20 17:14:59 -070043#include <hardware/audio_alsaops.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070044#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <audio_effects/effect_aec.h>
46#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080047#include "audio_hw.h"
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -080048#include "audio_extn.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070049#include "platform_api.h"
50#include <platform.h>
Vineeta Srivastava4b89e372014-06-19 14:21:42 -070051#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080052
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070053#include "sound/compress_params.h"
54
Marco Nelissen32093f52015-04-08 15:14:02 -070055#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024)
Marco Nelissen94c33a02015-05-12 09:11:34 -070056// 2 buffers causes problems with high bitrate files
57#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 3
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070058/* ToDo: Check and update a proper value in msec */
59#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
60#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
61
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -070062#define PROXY_OPEN_RETRY_COUNT 100
63#define PROXY_OPEN_WAIT_TIME 20
64
Glenn Kasten4f993392014-05-14 07:30:48 -070065static unsigned int configured_low_latency_capture_period_size =
66 LOW_LATENCY_CAPTURE_PERIOD_SIZE;
67
Andy Hung31aca912014-03-20 17:14:59 -070068/* This constant enables extended precision handling.
69 * TODO The flag is off until more testing is done.
70 */
71static const bool k_enable_extended_precision = false;
72
Eric Laurentb23d5282013-05-14 15:27:20 -070073struct pcm_config pcm_config_deep_buffer = {
74 .channels = 2,
75 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
76 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
77 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
78 .format = PCM_FORMAT_S16_LE,
79 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
80 .stop_threshold = INT_MAX,
81 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
82};
83
84struct pcm_config pcm_config_low_latency = {
85 .channels = 2,
86 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
87 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
88 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
89 .format = PCM_FORMAT_S16_LE,
90 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
91 .stop_threshold = INT_MAX,
92 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
93};
94
95struct pcm_config pcm_config_hdmi_multi = {
96 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
97 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
98 .period_size = HDMI_MULTI_PERIOD_SIZE,
99 .period_count = HDMI_MULTI_PERIOD_COUNT,
100 .format = PCM_FORMAT_S16_LE,
101 .start_threshold = 0,
102 .stop_threshold = INT_MAX,
103 .avail_min = 0,
104};
105
106struct pcm_config pcm_config_audio_capture = {
107 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700108 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
109 .format = PCM_FORMAT_S16_LE,
110};
111
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700112#define AFE_PROXY_CHANNEL_COUNT 2
113#define AFE_PROXY_SAMPLING_RATE 48000
114
115#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
116#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
117
118struct pcm_config pcm_config_afe_proxy_playback = {
119 .channels = AFE_PROXY_CHANNEL_COUNT,
120 .rate = AFE_PROXY_SAMPLING_RATE,
121 .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
122 .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
123 .format = PCM_FORMAT_S16_LE,
124 .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
125 .stop_threshold = INT_MAX,
126 .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
127};
128
129#define AFE_PROXY_RECORD_PERIOD_SIZE 768
130#define AFE_PROXY_RECORD_PERIOD_COUNT 4
131
132struct pcm_config pcm_config_afe_proxy_record = {
133 .channels = AFE_PROXY_CHANNEL_COUNT,
134 .rate = AFE_PROXY_SAMPLING_RATE,
135 .period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
136 .period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
137 .format = PCM_FORMAT_S16_LE,
138 .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
139 .stop_threshold = INT_MAX,
140 .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
141};
142
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700143const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700144 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
145 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
146 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700147 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
148
Eric Laurentb23d5282013-05-14 15:27:20 -0700149 [USECASE_AUDIO_RECORD] = "audio-record",
150 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700151
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800152 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
153 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700154
Eric Laurentb23d5282013-05-14 15:27:20 -0700155 [USECASE_VOICE_CALL] = "voice-call",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700156 [USECASE_VOICE2_CALL] = "voice2-call",
157 [USECASE_VOLTE_CALL] = "volte-call",
158 [USECASE_QCHAT_CALL] = "qchat-call",
159 [USECASE_VOWLAN_CALL] = "vowlan-call",
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700160
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700161 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
162 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
163
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700164 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
165 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700166};
167
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800168
169#define STRING_TO_ENUM(string) { #string, string }
170
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800171struct string_to_enum {
172 const char *name;
173 uint32_t value;
174};
175
176static const struct string_to_enum out_channels_name_to_enum_table[] = {
177 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
178 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
179 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
180};
181
Haynes Mathew George5191a852013-09-11 14:19:36 -0700182static int set_voice_volume_l(struct audio_device *adev, float volume);
183
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700184static bool is_supported_format(audio_format_t format)
185{
Eric Laurent8251ac82014-07-23 11:00:25 -0700186 switch (format) {
187 case AUDIO_FORMAT_MP3:
188 case AUDIO_FORMAT_AAC_LC:
189 case AUDIO_FORMAT_AAC_HE_V1:
190 case AUDIO_FORMAT_AAC_HE_V2:
191 return true;
192 default:
193 break;
194 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700195 return false;
196}
197
198static int get_snd_codec_id(audio_format_t format)
199{
200 int id = 0;
201
Eric Laurent8251ac82014-07-23 11:00:25 -0700202 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700203 case AUDIO_FORMAT_MP3:
204 id = SND_AUDIOCODEC_MP3;
205 break;
206 case AUDIO_FORMAT_AAC:
207 id = SND_AUDIOCODEC_AAC;
208 break;
209 default:
210 ALOGE("%s: Unsupported audio format", __func__);
211 }
212
213 return id;
214}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800215
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800216int enable_audio_route(struct audio_device *adev,
217 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800218{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700219 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800220 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800221
222 if (usecase == NULL)
223 return -EINVAL;
224
225 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
226
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800227 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700228 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800229 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700230 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800231
232 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500233 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700234 ALOGD("%s: apply and update mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700235 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800236
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800237 ALOGV("%s: exit", __func__);
238 return 0;
239}
240
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800241int disable_audio_route(struct audio_device *adev,
242 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800243{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700244 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800245 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800246
247 if (usecase == NULL)
248 return -EINVAL;
249
250 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700251 if (usecase->type == PCM_CAPTURE)
252 snd_device = usecase->in_snd_device;
253 else
254 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800255 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500256 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700257 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700258 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800259
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800260 ALOGV("%s: exit", __func__);
261 return 0;
262}
263
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800264int enable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700265 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800266{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800267 if (snd_device < SND_DEVICE_MIN ||
268 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800269 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800270 return -EINVAL;
271 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700272
273 adev->snd_dev_ref_cnt[snd_device]++;
274 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700275 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700276 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700277 return 0;
278 }
279
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700280 /* due to the possibility of calibration overwrite between listen
281 and audio, notify sound trigger hal before audio calibration is sent */
282 audio_extn_sound_trigger_update_device_status(snd_device,
283 ST_EVENT_SND_DEVICE_BUSY);
284
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700285 if (audio_extn_spkr_prot_is_enabled())
286 audio_extn_spkr_prot_calib_cancel(adev);
287
Eric Laurentb23d5282013-05-14 15:27:20 -0700288 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700289 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700290 audio_extn_sound_trigger_update_device_status(snd_device,
291 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800292 return -EINVAL;
293 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800294
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700295 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
296 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
297 audio_extn_spkr_prot_is_enabled()) {
298 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
299 adev->snd_dev_ref_cnt[snd_device]--;
300 return -EINVAL;
301 }
302 if (audio_extn_spkr_prot_start_processing(snd_device)) {
303 ALOGE("%s: spkr_start_processing failed", __func__);
304 return -EINVAL;
305 }
306 } else {
307 const char * dev_path = platform_get_snd_device_name(snd_device);
308 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
309 audio_route_apply_and_update_path(adev->audio_route, dev_path);
310 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700311
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800312 return 0;
313}
314
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800315int disable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700316 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800317{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800318 if (snd_device < SND_DEVICE_MIN ||
319 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800320 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800321 return -EINVAL;
322 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700323 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
324 ALOGE("%s: device ref cnt is already 0", __func__);
325 return -EINVAL;
326 }
327 adev->snd_dev_ref_cnt[snd_device]--;
328 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700329 const char * dev_path = platform_get_snd_device_name(snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700330 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700331 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
332 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
333 audio_extn_spkr_prot_is_enabled()) {
334 audio_extn_spkr_prot_stop_processing(snd_device);
335 } else {
336 audio_route_reset_and_update_path(adev->audio_route, dev_path);
337 }
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700338 audio_extn_sound_trigger_update_device_status(snd_device,
339 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700340 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800341 return 0;
342}
343
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700344static void check_usecases_codec_backend(struct audio_device *adev,
345 struct audio_usecase *uc_info,
346 snd_device_t snd_device)
347{
348 struct listnode *node;
349 struct audio_usecase *usecase;
350 bool switch_device[AUDIO_USECASE_MAX];
351 int i, num_uc_to_switch = 0;
352
353 /*
354 * This function is to make sure that all the usecases that are active on
355 * the hardware codec backend are always routed to any one device that is
356 * handled by the hardware codec.
357 * For example, if low-latency and deep-buffer usecases are currently active
358 * on speaker and out_set_parameters(headset) is received on low-latency
359 * output, then we have to make sure deep-buffer is also switched to headset,
360 * because of the limitation that both the devices cannot be enabled
361 * at the same time as they share the same backend.
362 */
363 /* Disable all the usecases on the shared backend other than the
364 specified usecase */
365 for (i = 0; i < AUDIO_USECASE_MAX; i++)
366 switch_device[i] = false;
367
368 list_for_each(node, &adev->usecase_list) {
369 usecase = node_to_item(node, struct audio_usecase, list);
370 if (usecase->type != PCM_CAPTURE &&
371 usecase != uc_info &&
372 usecase->out_snd_device != snd_device &&
373 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
374 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
375 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700376 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700377 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700378 switch_device[usecase->id] = true;
379 num_uc_to_switch++;
380 }
381 }
382
383 if (num_uc_to_switch) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700384 list_for_each(node, &adev->usecase_list) {
385 usecase = node_to_item(node, struct audio_usecase, list);
386 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700387 disable_snd_device(adev, usecase->out_snd_device);
sangwon.jeon866d5ff2013-10-17 21:42:50 +0900388 }
389 }
390
391 list_for_each(node, &adev->usecase_list) {
392 usecase = node_to_item(node, struct audio_usecase, list);
393 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700394 enable_snd_device(adev, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700395 }
396 }
397
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700398 /* Re-route all the usecases on the shared backend other than the
399 specified usecase to new snd devices */
400 list_for_each(node, &adev->usecase_list) {
401 usecase = node_to_item(node, struct audio_usecase, list);
402 /* Update the out_snd_device only before enabling the audio route */
403 if (switch_device[usecase->id] ) {
404 usecase->out_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700405 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700406 }
407 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700408 }
409}
410
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700411static void check_and_route_capture_usecases(struct audio_device *adev,
412 struct audio_usecase *uc_info,
413 snd_device_t snd_device)
414{
415 struct listnode *node;
416 struct audio_usecase *usecase;
417 bool switch_device[AUDIO_USECASE_MAX];
418 int i, num_uc_to_switch = 0;
419
420 /*
421 * This function is to make sure that all the active capture usecases
422 * are always routed to the same input sound device.
423 * For example, if audio-record and voice-call usecases are currently
424 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
425 * is received for voice call then we have to make sure that audio-record
426 * usecase is also switched to earpiece i.e. voice-dmic-ef,
427 * because of the limitation that two devices cannot be enabled
428 * at the same time if they share the same backend.
429 */
430 for (i = 0; i < AUDIO_USECASE_MAX; i++)
431 switch_device[i] = false;
432
433 list_for_each(node, &adev->usecase_list) {
434 usecase = node_to_item(node, struct audio_usecase, list);
435 if (usecase->type != PCM_PLAYBACK &&
436 usecase != uc_info &&
437 usecase->in_snd_device != snd_device) {
438 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
439 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700440 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700441 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700442 switch_device[usecase->id] = true;
443 num_uc_to_switch++;
444 }
445 }
446
447 if (num_uc_to_switch) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700448 list_for_each(node, &adev->usecase_list) {
449 usecase = node_to_item(node, struct audio_usecase, list);
450 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700451 disable_snd_device(adev, usecase->in_snd_device);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700452 }
453 }
454
455 list_for_each(node, &adev->usecase_list) {
456 usecase = node_to_item(node, struct audio_usecase, list);
457 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700458 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700459 }
460 }
461
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700462 /* Re-route all the usecases on the shared backend other than the
463 specified usecase to new snd devices */
464 list_for_each(node, &adev->usecase_list) {
465 usecase = node_to_item(node, struct audio_usecase, list);
466 /* Update the in_snd_device only before enabling the audio route */
467 if (switch_device[usecase->id] ) {
468 usecase->in_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700469 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700470 }
471 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700472 }
473}
474
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800475/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700476static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800477{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700478 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700479 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800480
481 switch (channels) {
482 /*
483 * Do not handle stereo output in Multi-channel cases
484 * Stereo case is handled in normal playback path
485 */
486 case 6:
487 ALOGV("%s: HDMI supports 5.1", __func__);
488 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
489 break;
490 case 8:
491 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
492 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
493 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
494 break;
495 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700496 ALOGE("HDMI does not support multi channel playback");
497 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800498 break;
499 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700500 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800501}
502
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700503static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
504{
505 struct audio_usecase *usecase;
506 struct listnode *node;
507
508 list_for_each(node, &adev->usecase_list) {
509 usecase = node_to_item(node, struct audio_usecase, list);
510 if (usecase->type == VOICE_CALL) {
511 ALOGV("%s: usecase id %d", __func__, usecase->id);
512 return usecase->id;
513 }
514 }
515 return USECASE_INVALID;
516}
517
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800518struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
519 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700520{
521 struct audio_usecase *usecase;
522 struct listnode *node;
523
524 list_for_each(node, &adev->usecase_list) {
525 usecase = node_to_item(node, struct audio_usecase, list);
526 if (usecase->id == uc_id)
527 return usecase;
528 }
529 return NULL;
530}
531
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800532int select_devices(struct audio_device *adev,
533 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800534{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800535 snd_device_t out_snd_device = SND_DEVICE_NONE;
536 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700537 struct audio_usecase *usecase = NULL;
538 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800539 struct audio_usecase *hfp_usecase = NULL;
540 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800541 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700542 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800543
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700544 usecase = get_usecase_from_list(adev, uc_id);
545 if (usecase == NULL) {
546 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
547 return -EINVAL;
548 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800549
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800550 if ((usecase->type == VOICE_CALL) ||
551 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700552 out_snd_device = platform_get_output_snd_device(adev->platform,
553 usecase->stream.out->devices);
554 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700555 usecase->devices = usecase->stream.out->devices;
556 } else {
557 /*
558 * If the voice call is active, use the sound devices of voice call usecase
559 * so that it would not result any device switch. All the usecases will
560 * be switched to new device when select_devices() is called for voice call
561 * usecase. This is to avoid switching devices for voice call when
562 * check_usecases_codec_backend() is called below.
563 */
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700564 if (voice_is_in_call(adev)) {
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700565 vc_usecase = get_usecase_from_list(adev,
566 get_voice_usecase_id_from_list(adev));
567 if ((vc_usecase != NULL) &&
568 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
569 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700570 in_snd_device = vc_usecase->in_snd_device;
571 out_snd_device = vc_usecase->out_snd_device;
572 }
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800573 } else if (audio_extn_hfp_is_active(adev)) {
574 hfp_ucid = audio_extn_hfp_get_usecase();
575 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
576 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
577 in_snd_device = hfp_usecase->in_snd_device;
578 out_snd_device = hfp_usecase->out_snd_device;
579 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700580 }
581 if (usecase->type == PCM_PLAYBACK) {
582 usecase->devices = usecase->stream.out->devices;
583 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700584 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700585 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700586 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700587 if (usecase->stream.out == adev->primary_output &&
588 adev->active_input &&
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800589 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
590 out_snd_device != usecase->out_snd_device) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700591 select_devices(adev, adev->active_input->usecase);
592 }
593 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700594 } else if (usecase->type == PCM_CAPTURE) {
595 usecase->devices = usecase->stream.in->device;
596 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700597 if (in_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700598 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700599 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
600 adev->primary_output && !adev->primary_output->standby) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700601 out_device = adev->primary_output->devices;
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800602 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700603 } else if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
604 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700605 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700606 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700607 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700608 }
609 }
610
611 if (out_snd_device == usecase->out_snd_device &&
612 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800613 return 0;
614 }
615
sangwoobc677242013-08-08 16:53:43 +0900616 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700617 out_snd_device, platform_get_snd_device_name(out_snd_device),
618 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800619
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800620 /*
621 * Limitation: While in call, to do a device switch we need to disable
622 * and enable both RX and TX devices though one of them is same as current
623 * device.
624 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700625 if ((usecase->type == VOICE_CALL) &&
626 (usecase->in_snd_device != SND_DEVICE_NONE) &&
627 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700628 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800629 }
630
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700631 /* Disable current sound devices */
632 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700633 disable_audio_route(adev, usecase);
634 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800635 }
636
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700637 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700638 disable_audio_route(adev, usecase);
639 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800640 }
641
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700642 /* Applicable only on the targets that has external modem.
643 * New device information should be sent to modem before enabling
644 * the devices to reduce in-call device switch time.
645 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700646 if ((usecase->type == VOICE_CALL) &&
647 (usecase->in_snd_device != SND_DEVICE_NONE) &&
648 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700649 status = platform_switch_voice_call_enable_device_config(adev->platform,
650 out_snd_device,
651 in_snd_device);
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700652 }
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700653
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700654 /* Enable new sound devices */
655 if (out_snd_device != SND_DEVICE_NONE) {
656 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
657 check_usecases_codec_backend(adev, usecase, out_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700658 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800659 }
660
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700661 if (in_snd_device != SND_DEVICE_NONE) {
662 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700663 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700664 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700665
Eric Laurentb23d5282013-05-14 15:27:20 -0700666 if (usecase->type == VOICE_CALL)
667 status = platform_switch_voice_call_device_post(adev->platform,
668 out_snd_device,
669 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800670
sangwoo170731f2013-06-08 15:36:36 +0900671 usecase->in_snd_device = in_snd_device;
672 usecase->out_snd_device = out_snd_device;
673
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700674 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900675
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700676 /* Applicable only on the targets that has external modem.
677 * Enable device command should be sent to modem only after
678 * enabling voice call mixer controls
679 */
680 if (usecase->type == VOICE_CALL)
681 status = platform_switch_voice_call_usecase_route_post(adev->platform,
682 out_snd_device,
683 in_snd_device);
684
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800685 return status;
686}
687
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800688static int stop_input_stream(struct stream_in *in)
689{
690 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691 struct audio_usecase *uc_info;
692 struct audio_device *adev = in->dev;
693
Eric Laurentc8400632013-02-14 19:04:54 -0800694 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800695
Eric Laurent994a6932013-07-17 11:51:42 -0700696 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700697 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800698 uc_info = get_usecase_from_list(adev, in->usecase);
699 if (uc_info == NULL) {
700 ALOGE("%s: Could not find the usecase (%d) in the list",
701 __func__, in->usecase);
702 return -EINVAL;
703 }
704
Eric Laurent150dbfe2013-02-27 14:31:02 -0800705 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700706 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700707
708 /* 2. Disable the tx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700709 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800710
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800711 list_remove(&uc_info->list);
712 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800713
Eric Laurent994a6932013-07-17 11:51:42 -0700714 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800715 return ret;
716}
717
718int start_input_stream(struct stream_in *in)
719{
720 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800721 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800722 struct audio_usecase *uc_info;
723 struct audio_device *adev = in->dev;
724
Eric Laurent994a6932013-07-17 11:51:42 -0700725 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700726 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800727 if (in->pcm_device_id < 0) {
728 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
729 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800730 ret = -EINVAL;
731 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700733
734 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800735 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
736 uc_info->id = in->usecase;
737 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800738 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700739 uc_info->devices = in->device;
740 uc_info->in_snd_device = SND_DEVICE_NONE;
741 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800742
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800743 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700744 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800745
Eric Laurentc8400632013-02-14 19:04:54 -0800746 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700747 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700748
749 unsigned int flags = PCM_IN;
750 unsigned int pcm_open_retry_count = 0;
751
752 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
753 flags |= PCM_MMAP | PCM_NOIRQ;
754 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800755 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700756
757 while (1) {
758 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
759 flags, &in->config);
760 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
761 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
762 if (in->pcm != NULL) {
763 pcm_close(in->pcm);
764 in->pcm = NULL;
765 }
766 if (pcm_open_retry_count-- == 0) {
767 ret = -EIO;
768 goto error_open;
769 }
770 usleep(PROXY_OPEN_WAIT_TIME * 1000);
771 continue;
772 }
773 break;
774 }
775
Eric Laurent994a6932013-07-17 11:51:42 -0700776 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800777 return ret;
778
779error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800780 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800781
782error_config:
783 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700784 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800785
786 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800787}
788
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700789/* must be called with out->lock locked */
790static int send_offload_cmd_l(struct stream_out* out, int command)
791{
792 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
793
794 ALOGVV("%s %d", __func__, command);
795
796 cmd->cmd = command;
797 list_add_tail(&out->offload_cmd_list, &cmd->node);
798 pthread_cond_signal(&out->offload_cond);
799 return 0;
800}
801
802/* must be called iwth out->lock locked */
803static void stop_compressed_output_l(struct stream_out *out)
804{
805 out->offload_state = OFFLOAD_STATE_IDLE;
806 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700807 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700808 if (out->compr != NULL) {
809 compress_stop(out->compr);
810 while (out->offload_thread_blocked) {
811 pthread_cond_wait(&out->cond, &out->lock);
812 }
813 }
814}
815
816static void *offload_thread_loop(void *context)
817{
818 struct stream_out *out = (struct stream_out *) context;
819 struct listnode *item;
820
821 out->offload_state = OFFLOAD_STATE_IDLE;
822 out->playback_started = 0;
823
824 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
825 set_sched_policy(0, SP_FOREGROUND);
826 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
827
828 ALOGV("%s", __func__);
829 pthread_mutex_lock(&out->lock);
830 for (;;) {
831 struct offload_cmd *cmd = NULL;
832 stream_callback_event_t event;
833 bool send_callback = false;
834
835 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
836 __func__, list_empty(&out->offload_cmd_list),
837 out->offload_state);
838 if (list_empty(&out->offload_cmd_list)) {
839 ALOGV("%s SLEEPING", __func__);
840 pthread_cond_wait(&out->offload_cond, &out->lock);
841 ALOGV("%s RUNNING", __func__);
842 continue;
843 }
844
845 item = list_head(&out->offload_cmd_list);
846 cmd = node_to_item(item, struct offload_cmd, node);
847 list_remove(item);
848
849 ALOGVV("%s STATE %d CMD %d out->compr %p",
850 __func__, out->offload_state, cmd->cmd, out->compr);
851
852 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
853 free(cmd);
854 break;
855 }
856
857 if (out->compr == NULL) {
858 ALOGE("%s: Compress handle is NULL", __func__);
859 pthread_cond_signal(&out->cond);
860 continue;
861 }
862 out->offload_thread_blocked = true;
863 pthread_mutex_unlock(&out->lock);
864 send_callback = false;
865 switch(cmd->cmd) {
866 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
867 compress_wait(out->compr, -1);
868 send_callback = true;
869 event = STREAM_CBK_EVENT_WRITE_READY;
870 break;
871 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700872 compress_next_track(out->compr);
873 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700874 send_callback = true;
875 event = STREAM_CBK_EVENT_DRAIN_READY;
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800876 /* Resend the metadata for next iteration */
877 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700878 break;
879 case OFFLOAD_CMD_DRAIN:
880 compress_drain(out->compr);
881 send_callback = true;
882 event = STREAM_CBK_EVENT_DRAIN_READY;
883 break;
884 default:
885 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
886 break;
887 }
888 pthread_mutex_lock(&out->lock);
889 out->offload_thread_blocked = false;
890 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700891 if (send_callback) {
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800892 ALOGVV("%s: sending offload_callback event %d", __func__, event);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700893 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700894 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700895 free(cmd);
896 }
897
898 pthread_cond_signal(&out->cond);
899 while (!list_empty(&out->offload_cmd_list)) {
900 item = list_head(&out->offload_cmd_list);
901 list_remove(item);
902 free(node_to_item(item, struct offload_cmd, node));
903 }
904 pthread_mutex_unlock(&out->lock);
905
906 return NULL;
907}
908
909static int create_offload_callback_thread(struct stream_out *out)
910{
911 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
912 list_init(&out->offload_cmd_list);
913 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
914 offload_thread_loop, out);
915 return 0;
916}
917
918static int destroy_offload_callback_thread(struct stream_out *out)
919{
920 pthread_mutex_lock(&out->lock);
921 stop_compressed_output_l(out);
922 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
923
924 pthread_mutex_unlock(&out->lock);
925 pthread_join(out->offload_thread, (void **) NULL);
926 pthread_cond_destroy(&out->offload_cond);
927
928 return 0;
929}
930
Eric Laurent07eeafd2013-10-06 12:52:49 -0700931static bool allow_hdmi_channel_config(struct audio_device *adev)
932{
933 struct listnode *node;
934 struct audio_usecase *usecase;
935 bool ret = true;
936
937 list_for_each(node, &adev->usecase_list) {
938 usecase = node_to_item(node, struct audio_usecase, list);
939 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
940 /*
941 * If voice call is already existing, do not proceed further to avoid
942 * disabling/enabling both RX and TX devices, CSD calls, etc.
943 * Once the voice call done, the HDMI channels can be configured to
944 * max channels of remaining use cases.
945 */
946 if (usecase->id == USECASE_VOICE_CALL) {
947 ALOGD("%s: voice call is active, no change in HDMI channels",
948 __func__);
949 ret = false;
950 break;
951 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
952 ALOGD("%s: multi channel playback is active, "
953 "no change in HDMI channels", __func__);
954 ret = false;
955 break;
956 }
957 }
958 }
959 return ret;
960}
961
962static int check_and_set_hdmi_channels(struct audio_device *adev,
963 unsigned int channels)
964{
965 struct listnode *node;
966 struct audio_usecase *usecase;
967
968 /* Check if change in HDMI channel config is allowed */
969 if (!allow_hdmi_channel_config(adev))
970 return 0;
971
972 if (channels == adev->cur_hdmi_channels) {
973 ALOGD("%s: Requested channels are same as current", __func__);
974 return 0;
975 }
976
977 platform_set_hdmi_channels(adev->platform, channels);
978 adev->cur_hdmi_channels = channels;
979
980 /*
981 * Deroute all the playback streams routed to HDMI so that
982 * the back end is deactivated. Note that backend will not
983 * be deactivated if any one stream is connected to it.
984 */
985 list_for_each(node, &adev->usecase_list) {
986 usecase = node_to_item(node, struct audio_usecase, list);
987 if (usecase->type == PCM_PLAYBACK &&
988 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700989 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -0700990 }
991 }
992
993 /*
994 * Enable all the streams disabled above. Now the HDMI backend
995 * will be activated with new channel configuration
996 */
997 list_for_each(node, &adev->usecase_list) {
998 usecase = node_to_item(node, struct audio_usecase, list);
999 if (usecase->type == PCM_PLAYBACK &&
1000 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001001 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001002 }
1003 }
1004
1005 return 0;
1006}
1007
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001008static int stop_output_stream(struct stream_out *out)
1009{
1010 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001011 struct audio_usecase *uc_info;
1012 struct audio_device *adev = out->dev;
1013
Eric Laurent994a6932013-07-17 11:51:42 -07001014 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001015 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001016 uc_info = get_usecase_from_list(adev, out->usecase);
1017 if (uc_info == NULL) {
1018 ALOGE("%s: Could not find the usecase (%d) in the list",
1019 __func__, out->usecase);
1020 return -EINVAL;
1021 }
1022
Haynes Mathew George41f86652014-06-17 14:22:15 -07001023 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1024 if (adev->visualizer_stop_output != NULL)
1025 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1026 if (adev->offload_effects_stop_output != NULL)
1027 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1028 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001029
Eric Laurent150dbfe2013-02-27 14:31:02 -08001030 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001031 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001032
1033 /* 2. Disable the rx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001034 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001035
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001036 list_remove(&uc_info->list);
1037 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001038
Eric Laurent0499d4f2014-08-25 22:39:29 -05001039 audio_extn_extspk_update(adev->extspk);
1040
Eric Laurent07eeafd2013-10-06 12:52:49 -07001041 /* Must be called after removing the usecase from list */
1042 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1043 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1044
Eric Laurent994a6932013-07-17 11:51:42 -07001045 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001046 return ret;
1047}
1048
1049int start_output_stream(struct stream_out *out)
1050{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001051 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001052 struct audio_usecase *uc_info;
1053 struct audio_device *adev = out->dev;
1054
Eric Laurent994a6932013-07-17 11:51:42 -07001055 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001056 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001057 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001058 if (out->pcm_device_id < 0) {
1059 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1060 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001061 ret = -EINVAL;
1062 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001063 }
1064
1065 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1066 uc_info->id = out->usecase;
1067 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001068 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001069 uc_info->devices = out->devices;
1070 uc_info->in_snd_device = SND_DEVICE_NONE;
1071 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001072
Eric Laurent07eeafd2013-10-06 12:52:49 -07001073 /* This must be called before adding this usecase to the list */
1074 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1075 check_and_set_hdmi_channels(adev, out->config.channels);
1076
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001077 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001078
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001079 select_devices(adev, out->usecase);
1080
Eric Laurent0499d4f2014-08-25 22:39:29 -05001081 audio_extn_extspk_update(adev->extspk);
1082
Andy Hung31aca912014-03-20 17:14:59 -07001083 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001084 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001085 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001086 unsigned int flags = PCM_OUT;
1087 unsigned int pcm_open_retry_count = 0;
1088 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1089 flags |= PCM_MMAP | PCM_NOIRQ;
1090 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1091 } else
1092 flags |= PCM_MONOTONIC;
1093
1094 while (1) {
1095 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1096 flags, &out->config);
1097 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
1098 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1099 if (out->pcm != NULL) {
1100 pcm_close(out->pcm);
1101 out->pcm = NULL;
1102 }
1103 if (pcm_open_retry_count-- == 0) {
1104 ret = -EIO;
1105 goto error_open;
1106 }
1107 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1108 continue;
1109 }
1110 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001111 }
1112 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001113 out->pcm = NULL;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001114 out->compr = compress_open(adev->snd_card, out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001115 COMPRESS_IN, &out->compr_config);
1116 if (out->compr && !is_compress_ready(out->compr)) {
1117 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1118 compress_close(out->compr);
1119 out->compr = NULL;
1120 ret = -EIO;
1121 goto error_open;
1122 }
1123 if (out->offload_callback)
1124 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001125
1126 if (adev->visualizer_start_output != NULL)
Haynes Mathew George41f86652014-06-17 14:22:15 -07001127 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1128 if (adev->offload_effects_start_output != NULL)
1129 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001130 }
Eric Laurent994a6932013-07-17 11:51:42 -07001131 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001132 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001133error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001134 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001135error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001136 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001137}
1138
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001139static int check_input_parameters(uint32_t sample_rate,
1140 audio_format_t format,
1141 int channel_count)
1142{
1143 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1144
1145 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1146
1147 switch (sample_rate) {
1148 case 8000:
1149 case 11025:
1150 case 12000:
1151 case 16000:
1152 case 22050:
1153 case 24000:
1154 case 32000:
1155 case 44100:
1156 case 48000:
1157 break;
1158 default:
1159 return -EINVAL;
1160 }
1161
1162 return 0;
1163}
1164
1165static size_t get_input_buffer_size(uint32_t sample_rate,
1166 audio_format_t format,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001167 int channel_count,
1168 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001169{
1170 size_t size = 0;
1171
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001172 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1173 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001174
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001175 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001176 if (is_low_latency)
Glenn Kasten4f993392014-05-14 07:30:48 -07001177 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001178 /* ToDo: should use frame_size computed based on the format and
1179 channel_count here. */
1180 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001181
Glenn Kasten4f993392014-05-14 07:30:48 -07001182 /* make sure the size is multiple of 32 bytes
1183 * At 48 kHz mono 16-bit PCM:
1184 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1185 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1186 */
1187 size += 0x1f;
1188 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001189
1190 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001191}
1192
1193static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1194{
1195 struct stream_out *out = (struct stream_out *)stream;
1196
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001197 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001198}
1199
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001200static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201{
1202 return -ENOSYS;
1203}
1204
1205static size_t out_get_buffer_size(const struct audio_stream *stream)
1206{
1207 struct stream_out *out = (struct stream_out *)stream;
1208
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001209 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1210 return out->compr_config.fragment_size;
1211 }
Eric Laurentfdf296a2014-07-03 16:41:51 -07001212 return out->config.period_size *
1213 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001214}
1215
1216static uint32_t out_get_channels(const struct audio_stream *stream)
1217{
1218 struct stream_out *out = (struct stream_out *)stream;
1219
1220 return out->channel_mask;
1221}
1222
1223static audio_format_t out_get_format(const struct audio_stream *stream)
1224{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001225 struct stream_out *out = (struct stream_out *)stream;
1226
1227 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001228}
1229
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001230static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001231{
1232 return -ENOSYS;
1233}
1234
1235static int out_standby(struct audio_stream *stream)
1236{
1237 struct stream_out *out = (struct stream_out *)stream;
1238 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001239
Eric Laurent994a6932013-07-17 11:51:42 -07001240 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001241 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001242
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001243 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001244 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001245 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001246 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001247 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1248 if (out->pcm) {
1249 pcm_close(out->pcm);
1250 out->pcm = NULL;
1251 }
1252 } else {
1253 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001254 out->gapless_mdata.encoder_delay = 0;
1255 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001256 if (out->compr != NULL) {
1257 compress_close(out->compr);
1258 out->compr = NULL;
1259 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001260 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001261 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001262 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001263 }
1264 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001265 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001266 return 0;
1267}
1268
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001269static int out_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001270{
1271 return 0;
1272}
1273
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001274static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1275{
1276 int ret = 0;
1277 char value[32];
1278 struct compr_gapless_mdata tmp_mdata;
1279
1280 if (!out || !parms) {
1281 return -EINVAL;
1282 }
1283
1284 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1285 if (ret >= 0) {
1286 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1287 } else {
1288 return -EINVAL;
1289 }
1290
1291 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1292 if (ret >= 0) {
1293 tmp_mdata.encoder_padding = atoi(value);
1294 } else {
1295 return -EINVAL;
1296 }
1297
1298 out->gapless_mdata = tmp_mdata;
1299 out->send_new_metadata = 1;
1300 ALOGV("%s new encoder delay %u and padding %u", __func__,
1301 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1302
1303 return 0;
1304}
1305
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001306static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1307{
1308 return out == adev->primary_output || out == adev->voice_tx_output;
1309}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001310
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001311static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1312{
1313 struct stream_out *out = (struct stream_out *)stream;
1314 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001315 struct audio_usecase *usecase;
1316 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001317 struct str_parms *parms;
1318 char value[32];
1319 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001320 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001321 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001322
sangwoobc677242013-08-08 16:53:43 +09001323 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001324 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001325 parms = str_parms_create_str(kvpairs);
1326 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1327 if (ret >= 0) {
1328 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001329 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001330 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001331
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001332 /*
1333 * When HDMI cable is unplugged the music playback is paused and
1334 * the policy manager sends routing=0. But the audioflinger
1335 * continues to write data until standby time (3sec).
1336 * As the HDMI core is turned off, the write gets blocked.
1337 * Avoid this by routing audio to speaker until standby.
1338 */
1339 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1340 val == AUDIO_DEVICE_NONE) {
1341 val = AUDIO_DEVICE_OUT_SPEAKER;
1342 }
1343
1344 /*
1345 * select_devices() call below switches all the usecases on the same
1346 * backend to the new device. Refer to check_usecases_codec_backend() in
1347 * the select_devices(). But how do we undo this?
1348 *
1349 * For example, music playback is active on headset (deep-buffer usecase)
1350 * and if we go to ringtones and select a ringtone, low-latency usecase
1351 * will be started on headset+speaker. As we can't enable headset+speaker
1352 * and headset devices at the same time, select_devices() switches the music
1353 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1354 * So when the ringtone playback is completed, how do we undo the same?
1355 *
1356 * We are relying on the out_set_parameters() call on deep-buffer output,
1357 * once the ringtone playback is ended.
1358 * NOTE: We should not check if the current devices are same as new devices.
1359 * Because select_devices() must be called to switch back the music
1360 * playback to headset.
1361 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001362 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001363 out->devices = val;
1364
1365 if (!out->standby)
1366 select_devices(adev, out->usecase);
1367
Eric Laurenta7657192014-10-09 21:09:33 -07001368 if (output_drives_call(adev, out)) {
1369 if (!voice_is_in_call(adev)) {
1370 if (adev->mode == AUDIO_MODE_IN_CALL) {
1371 adev->current_call_output = out;
1372 ret = voice_start_call(adev);
1373 }
1374 } else {
1375 adev->current_call_output = out;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001376 voice_update_devices_for_all_voice_usecases(adev);
Eric Laurenta7657192014-10-09 21:09:33 -07001377 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001378 }
1379 }
1380
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001381 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001382 pthread_mutex_unlock(&out->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05001383
1384 /*handles device and call state changes*/
1385 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001386 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001387
1388 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1389 parse_compress_metadata(out, parms);
1390 }
1391
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001392 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001393 ALOGV("%s: exit: code(%d)", __func__, status);
1394 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001395}
1396
1397static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1398{
1399 struct stream_out *out = (struct stream_out *)stream;
1400 struct str_parms *query = str_parms_create_str(keys);
1401 char *str;
1402 char value[256];
1403 struct str_parms *reply = str_parms_create();
1404 size_t i, j;
1405 int ret;
1406 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001407 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001408 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1409 if (ret >= 0) {
1410 value[0] = '\0';
1411 i = 0;
1412 while (out->supported_channel_masks[i] != 0) {
1413 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1414 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1415 if (!first) {
1416 strcat(value, "|");
1417 }
1418 strcat(value, out_channels_name_to_enum_table[j].name);
1419 first = false;
1420 break;
1421 }
1422 }
1423 i++;
1424 }
1425 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1426 str = str_parms_to_str(reply);
1427 } else {
1428 str = strdup(keys);
1429 }
1430 str_parms_destroy(query);
1431 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001432 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001433 return str;
1434}
1435
1436static uint32_t out_get_latency(const struct audio_stream_out *stream)
1437{
1438 struct stream_out *out = (struct stream_out *)stream;
1439
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001440 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1441 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1442
1443 return (out->config.period_count * out->config.period_size * 1000) /
1444 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001445}
1446
1447static int out_set_volume(struct audio_stream_out *stream, float left,
1448 float right)
1449{
Eric Laurenta9024de2013-04-04 09:19:12 -07001450 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001451 int volume[2];
1452
Eric Laurenta9024de2013-04-04 09:19:12 -07001453 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1454 /* only take left channel into account: the API is for stereo anyway */
1455 out->muted = (left == 0.0f);
1456 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001457 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1458 const char *mixer_ctl_name = "Compress Playback Volume";
1459 struct audio_device *adev = out->dev;
1460 struct mixer_ctl *ctl;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001461 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1462 if (!ctl) {
Haynes Mathew George20bcfa82014-06-25 13:37:03 -07001463 /* try with the control based on device id */
1464 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1465 PCM_PLAYBACK);
1466 char ctl_name[128] = {0};
1467 snprintf(ctl_name, sizeof(ctl_name),
1468 "Compress Playback %d Volume", pcm_device_id);
1469 ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name);
1470 if (!ctl) {
1471 ALOGE("%s: Could not get volume ctl mixer cmd", __func__);
1472 return -EINVAL;
1473 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001474 }
1475 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1476 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1477 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1478 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001479 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001480
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001481 return -ENOSYS;
1482}
1483
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001484#ifdef NO_AUDIO_OUT
1485static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
1486 const void *buffer, size_t bytes)
1487{
1488 struct stream_out *out = (struct stream_out *)stream;
1489
1490 /* No Output device supported other than BT for playback.
1491 * Sleep for the amount of buffer duration
1492 */
1493 pthread_mutex_lock(&out->lock);
1494 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1495 out_get_sample_rate(&out->stream.common));
1496 pthread_mutex_unlock(&out->lock);
1497 return bytes;
1498}
1499#endif
1500
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001501static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1502 size_t bytes)
1503{
1504 struct stream_out *out = (struct stream_out *)stream;
1505 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001506 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001507
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001508 pthread_mutex_lock(&out->lock);
1509 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001510 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001511 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001512 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001513 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001514 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001515 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001516 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001517 goto exit;
1518 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001519 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001520
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001521 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001522 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1523 if (out->send_new_metadata) {
1524 ALOGVV("send new gapless metadata");
1525 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1526 out->send_new_metadata = 0;
1527 }
1528
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001529 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001530 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001531 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001532 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1533 }
1534 if (!out->playback_started) {
1535 compress_start(out->compr);
1536 out->playback_started = 1;
1537 out->offload_state = OFFLOAD_STATE_PLAYING;
1538 }
1539 pthread_mutex_unlock(&out->lock);
1540 return ret;
1541 } else {
1542 if (out->pcm) {
1543 if (out->muted)
1544 memset((void *)buffer, 0, bytes);
1545 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001546 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1547 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
1548 }
1549 else
1550 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001551 if (ret == 0)
1552 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001553 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001554 }
1555
1556exit:
1557 pthread_mutex_unlock(&out->lock);
1558
1559 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001560 if (out->pcm)
1561 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001562 out_standby(&out->stream.common);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001563 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001564 out_get_sample_rate(&out->stream.common));
1565 }
1566 return bytes;
1567}
1568
1569static int out_get_render_position(const struct audio_stream_out *stream,
1570 uint32_t *dsp_frames)
1571{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001572 struct stream_out *out = (struct stream_out *)stream;
1573 *dsp_frames = 0;
1574 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1575 pthread_mutex_lock(&out->lock);
1576 if (out->compr != NULL) {
1577 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1578 &out->sample_rate);
1579 ALOGVV("%s rendered frames %d sample_rate %d",
1580 __func__, *dsp_frames, out->sample_rate);
1581 }
1582 pthread_mutex_unlock(&out->lock);
1583 return 0;
1584 } else
1585 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001586}
1587
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001588static int out_add_audio_effect(const struct audio_stream *stream __unused,
1589 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001590{
1591 return 0;
1592}
1593
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001594static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1595 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001596{
1597 return 0;
1598}
1599
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001600static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1601 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001602{
1603 return -EINVAL;
1604}
1605
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001606static int out_get_presentation_position(const struct audio_stream_out *stream,
1607 uint64_t *frames, struct timespec *timestamp)
1608{
1609 struct stream_out *out = (struct stream_out *)stream;
1610 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001611 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001612
1613 pthread_mutex_lock(&out->lock);
1614
Eric Laurent949a0892013-09-20 09:20:13 -07001615 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1616 if (out->compr != NULL) {
1617 compress_get_tstamp(out->compr, &dsp_frames,
1618 &out->sample_rate);
1619 ALOGVV("%s rendered frames %ld sample_rate %d",
1620 __func__, dsp_frames, out->sample_rate);
1621 *frames = dsp_frames;
1622 ret = 0;
1623 /* this is the best we can do */
1624 clock_gettime(CLOCK_MONOTONIC, timestamp);
1625 }
1626 } else {
1627 if (out->pcm) {
1628 size_t avail;
1629 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1630 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001631 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001632 // This adjustment accounts for buffering after app processor.
1633 // It is based on estimated DSP latency per use case, rather than exact.
1634 signed_frames -=
1635 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1636
Eric Laurent949a0892013-09-20 09:20:13 -07001637 // It would be unusual for this value to be negative, but check just in case ...
1638 if (signed_frames >= 0) {
1639 *frames = signed_frames;
1640 ret = 0;
1641 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001642 }
1643 }
1644 }
1645
1646 pthread_mutex_unlock(&out->lock);
1647
1648 return ret;
1649}
1650
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001651static int out_set_callback(struct audio_stream_out *stream,
1652 stream_callback_t callback, void *cookie)
1653{
1654 struct stream_out *out = (struct stream_out *)stream;
1655
1656 ALOGV("%s", __func__);
1657 pthread_mutex_lock(&out->lock);
1658 out->offload_callback = callback;
1659 out->offload_cookie = cookie;
1660 pthread_mutex_unlock(&out->lock);
1661 return 0;
1662}
1663
1664static int out_pause(struct audio_stream_out* stream)
1665{
1666 struct stream_out *out = (struct stream_out *)stream;
1667 int status = -ENOSYS;
1668 ALOGV("%s", __func__);
1669 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1670 pthread_mutex_lock(&out->lock);
1671 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1672 status = compress_pause(out->compr);
1673 out->offload_state = OFFLOAD_STATE_PAUSED;
1674 }
1675 pthread_mutex_unlock(&out->lock);
1676 }
1677 return status;
1678}
1679
1680static int out_resume(struct audio_stream_out* stream)
1681{
1682 struct stream_out *out = (struct stream_out *)stream;
1683 int status = -ENOSYS;
1684 ALOGV("%s", __func__);
1685 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1686 status = 0;
1687 pthread_mutex_lock(&out->lock);
1688 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1689 status = compress_resume(out->compr);
1690 out->offload_state = OFFLOAD_STATE_PLAYING;
1691 }
1692 pthread_mutex_unlock(&out->lock);
1693 }
1694 return status;
1695}
1696
1697static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1698{
1699 struct stream_out *out = (struct stream_out *)stream;
1700 int status = -ENOSYS;
1701 ALOGV("%s", __func__);
1702 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1703 pthread_mutex_lock(&out->lock);
1704 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1705 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1706 else
1707 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1708 pthread_mutex_unlock(&out->lock);
1709 }
1710 return status;
1711}
1712
1713static int out_flush(struct audio_stream_out* stream)
1714{
1715 struct stream_out *out = (struct stream_out *)stream;
1716 ALOGV("%s", __func__);
1717 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1718 pthread_mutex_lock(&out->lock);
1719 stop_compressed_output_l(out);
1720 pthread_mutex_unlock(&out->lock);
1721 return 0;
1722 }
1723 return -ENOSYS;
1724}
1725
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001726/** audio_stream_in implementation **/
1727static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1728{
1729 struct stream_in *in = (struct stream_in *)stream;
1730
1731 return in->config.rate;
1732}
1733
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001734static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001735{
1736 return -ENOSYS;
1737}
1738
1739static size_t in_get_buffer_size(const struct audio_stream *stream)
1740{
1741 struct stream_in *in = (struct stream_in *)stream;
1742
Eric Laurentfdf296a2014-07-03 16:41:51 -07001743 return in->config.period_size *
1744 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001745}
1746
1747static uint32_t in_get_channels(const struct audio_stream *stream)
1748{
1749 struct stream_in *in = (struct stream_in *)stream;
1750
1751 return in->channel_mask;
1752}
1753
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001754static audio_format_t in_get_format(const struct audio_stream *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001755{
1756 return AUDIO_FORMAT_PCM_16_BIT;
1757}
1758
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001759static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001760{
1761 return -ENOSYS;
1762}
1763
1764static int in_standby(struct audio_stream *stream)
1765{
1766 struct stream_in *in = (struct stream_in *)stream;
1767 struct audio_device *adev = in->dev;
1768 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001769 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001770 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001771
1772 if (!in->standby && in->is_st_session) {
1773 ALOGD("%s: sound trigger pcm stop lab", __func__);
1774 audio_extn_sound_trigger_stop_lab(in);
1775 in->standby = true;
1776 }
1777
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001778 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001779 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001780 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001781 if (in->pcm) {
1782 pcm_close(in->pcm);
1783 in->pcm = NULL;
1784 }
Eric Laurentcefbbac2014-09-04 13:54:10 -05001785 adev->enable_voicerx = false;
1786 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE );
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001787 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001788 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001789 }
1790 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001791 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001792 return status;
1793}
1794
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001795static int in_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001796{
1797 return 0;
1798}
1799
1800static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1801{
1802 struct stream_in *in = (struct stream_in *)stream;
1803 struct audio_device *adev = in->dev;
1804 struct str_parms *parms;
1805 char *str;
1806 char value[32];
1807 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07001808 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001809
Eric Laurent994a6932013-07-17 11:51:42 -07001810 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001811 parms = str_parms_create_str(kvpairs);
1812
1813 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1814
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001815 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001816 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001817 if (ret >= 0) {
1818 val = atoi(value);
1819 /* no audio source uses val == 0 */
1820 if ((in->source != val) && (val != 0)) {
1821 in->source = val;
1822 }
1823 }
1824
1825 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07001826
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001827 if (ret >= 0) {
1828 val = atoi(value);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001829 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001830 in->device = val;
1831 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001832 if (!in->standby)
Eric Laurent03f09432014-03-25 18:09:11 -07001833 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001834 }
1835 }
1836
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001837 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001838 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001839
1840 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001841 ALOGV("%s: exit: status(%d)", __func__, status);
1842 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001843}
1844
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001845static char* in_get_parameters(const struct audio_stream *stream __unused,
1846 const char *keys __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001847{
1848 return strdup("");
1849}
1850
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001851static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001852{
1853 return 0;
1854}
1855
1856static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1857 size_t bytes)
1858{
1859 struct stream_in *in = (struct stream_in *)stream;
1860 struct audio_device *adev = in->dev;
1861 int i, ret = -1;
1862
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001863 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001864 if (in->is_st_session) {
1865 ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
1866 /* Read from sound trigger HAL */
1867 audio_extn_sound_trigger_read(in, buffer, bytes);
1868 pthread_mutex_unlock(&in->lock);
1869 return bytes;
1870 }
1871
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001872 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001873 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001874 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001875 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001876 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001877 goto exit;
1878 }
1879 in->standby = 0;
1880 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001881
1882 if (in->pcm) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001883 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1884 ret = pcm_mmap_read(in->pcm, buffer, bytes);
1885 } else
1886 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001887 }
1888
1889 /*
1890 * Instead of writing zeroes here, we could trust the hardware
1891 * to always provide zeroes when muted.
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001892 * No need to acquire adev->lock to read mic_muted here as we don't change its state.
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001893 */
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001894 if (ret == 0 && adev->mic_muted && in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001895 memset(buffer, 0, bytes);
1896
1897exit:
1898 pthread_mutex_unlock(&in->lock);
1899
1900 if (ret != 0) {
1901 in_standby(&in->stream.common);
1902 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001903 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001904 in_get_sample_rate(&in->stream.common));
1905 }
1906 return bytes;
1907}
1908
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001909static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001910{
1911 return 0;
1912}
1913
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001914static int add_remove_audio_effect(const struct audio_stream *stream,
1915 effect_handle_t effect,
1916 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001917{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001918 struct stream_in *in = (struct stream_in *)stream;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001919 struct audio_device *adev = in->dev;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001920 int status = 0;
1921 effect_descriptor_t desc;
1922
1923 status = (*effect)->get_descriptor(effect, &desc);
1924 if (status != 0)
1925 return status;
1926
1927 pthread_mutex_lock(&in->lock);
1928 pthread_mutex_lock(&in->dev->lock);
1929 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1930 in->enable_aec != enable &&
1931 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1932 in->enable_aec = enable;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001933 if (!enable)
1934 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
1935 adev->enable_voicerx = enable;
1936 struct audio_usecase *usecase;
1937 struct listnode *node;
1938 list_for_each(node, &adev->usecase_list) {
1939 usecase = node_to_item(node, struct audio_usecase, list);
1940 if (usecase->type == PCM_PLAYBACK) {
1941 select_devices(adev, usecase->id);
1942 break;
1943 }
1944 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001945 if (!in->standby)
1946 select_devices(in->dev, in->usecase);
1947 }
Ravi Kumar Alamanda3ad4e1b2014-06-03 00:08:15 -07001948 if (in->enable_ns != enable &&
1949 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1950 in->enable_ns = enable;
1951 if (!in->standby)
1952 select_devices(in->dev, in->usecase);
1953 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001954 pthread_mutex_unlock(&in->dev->lock);
1955 pthread_mutex_unlock(&in->lock);
1956
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001957 return 0;
1958}
1959
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001960static int in_add_audio_effect(const struct audio_stream *stream,
1961 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001962{
Eric Laurent994a6932013-07-17 11:51:42 -07001963 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001964 return add_remove_audio_effect(stream, effect, true);
1965}
1966
1967static int in_remove_audio_effect(const struct audio_stream *stream,
1968 effect_handle_t effect)
1969{
Eric Laurent994a6932013-07-17 11:51:42 -07001970 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001971 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001972}
1973
1974static int adev_open_output_stream(struct audio_hw_device *dev,
1975 audio_io_handle_t handle,
1976 audio_devices_t devices,
1977 audio_output_flags_t flags,
1978 struct audio_config *config,
Eric Laurent91975a62014-07-27 17:15:50 -07001979 struct audio_stream_out **stream_out,
1980 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001981{
1982 struct audio_device *adev = (struct audio_device *)dev;
1983 struct stream_out *out;
1984 int i, ret;
1985
Eric Laurent994a6932013-07-17 11:51:42 -07001986 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001987 __func__, config->sample_rate, config->channel_mask, devices, flags);
1988 *stream_out = NULL;
1989 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1990
1991 if (devices == AUDIO_DEVICE_NONE)
1992 devices = AUDIO_DEVICE_OUT_SPEAKER;
1993
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001994 out->flags = flags;
1995 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001996 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001997 out->format = config->format;
1998 out->sample_rate = config->sample_rate;
1999 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2000 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002001 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002002
2003 /* Init use case and pcm_config */
2004 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07002005 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002006 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002007 pthread_mutex_lock(&adev->lock);
2008 ret = read_hdmi_channel_masks(out);
2009 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002010 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002011 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002012
2013 if (config->sample_rate == 0)
2014 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2015 if (config->channel_mask == 0)
2016 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2017
2018 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002019 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002020 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2021 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002022 out->config.rate = config->sample_rate;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002023 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002024 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002025 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2026 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2027 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2028 ALOGE("%s: Unsupported Offload information", __func__);
2029 ret = -EINVAL;
2030 goto error_open;
2031 }
2032 if (!is_supported_format(config->offload_info.format)) {
2033 ALOGE("%s: Unsupported audio format", __func__);
2034 ret = -EINVAL;
2035 goto error_open;
2036 }
2037
2038 out->compr_config.codec = (struct snd_codec *)
2039 calloc(1, sizeof(struct snd_codec));
2040
2041 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2042 if (config->offload_info.channel_mask)
2043 out->channel_mask = config->offload_info.channel_mask;
2044 else if (config->channel_mask)
2045 out->channel_mask = config->channel_mask;
2046 out->format = config->offload_info.format;
2047 out->sample_rate = config->offload_info.sample_rate;
2048
2049 out->stream.set_callback = out_set_callback;
2050 out->stream.pause = out_pause;
2051 out->stream.resume = out_resume;
2052 out->stream.drain = out_drain;
2053 out->stream.flush = out_flush;
2054
2055 out->compr_config.codec->id =
2056 get_snd_codec_id(config->offload_info.format);
2057 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2058 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
Eric Laurent1ccf3972014-10-23 14:42:59 -07002059 out->compr_config.codec->sample_rate = config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002060 out->compr_config.codec->bit_rate =
2061 config->offload_info.bit_rate;
2062 out->compr_config.codec->ch_in =
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002063 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002064 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2065
2066 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2067 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002068
2069 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002070 create_offload_callback_thread(out);
2071 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2072 __func__, config->offload_info.version,
2073 config->offload_info.bit_rate);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002074 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2075 if (config->sample_rate == 0)
2076 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2077 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2078 config->sample_rate != 8000) {
2079 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2080 ret = -EINVAL;
2081 goto error_open;
2082 }
2083 out->sample_rate = config->sample_rate;
2084 out->config.rate = config->sample_rate;
2085 if (config->format == AUDIO_FORMAT_DEFAULT)
2086 config->format = AUDIO_FORMAT_PCM_16_BIT;
2087 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2088 config->format = AUDIO_FORMAT_PCM_16_BIT;
2089 ret = -EINVAL;
2090 goto error_open;
2091 }
2092 out->format = config->format;
2093 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2094 out->config = pcm_config_afe_proxy_playback;
2095 adev->voice_tx_output = out;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002096 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07002097 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2098 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2099 out->config = pcm_config_deep_buffer;
2100 } else {
2101 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2102 out->config = pcm_config_low_latency;
2103 }
2104 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2105 if (k_enable_extended_precision
2106 && pcm_params_format_test(adev->use_case_table[out->usecase],
2107 pcm_format_from_audio_format(config->format))) {
2108 out->config.format = pcm_format_from_audio_format(config->format);
2109 /* out->format already set to config->format */
2110 } else {
2111 /* deny the externally proposed config format
2112 * and use the one specified in audio_hw layer configuration.
2113 * Note: out->format is returned by out->stream.common.get_format()
2114 * and is used to set config->format in the code several lines below.
2115 */
2116 out->format = audio_format_from_pcm_format(out->config.format);
2117 }
2118 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002119 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002120 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07002121 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
2122 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002123
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002124 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002125 if (adev->primary_output == NULL)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002126 adev->primary_output = out;
2127 else {
2128 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002129 ret = -EEXIST;
2130 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002131 }
2132 }
2133
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002134 /* Check if this usecase is already existing */
2135 pthread_mutex_lock(&adev->lock);
2136 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2137 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002138 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002139 ret = -EEXIST;
2140 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002141 }
2142 pthread_mutex_unlock(&adev->lock);
2143
2144 out->stream.common.get_sample_rate = out_get_sample_rate;
2145 out->stream.common.set_sample_rate = out_set_sample_rate;
2146 out->stream.common.get_buffer_size = out_get_buffer_size;
2147 out->stream.common.get_channels = out_get_channels;
2148 out->stream.common.get_format = out_get_format;
2149 out->stream.common.set_format = out_set_format;
2150 out->stream.common.standby = out_standby;
2151 out->stream.common.dump = out_dump;
2152 out->stream.common.set_parameters = out_set_parameters;
2153 out->stream.common.get_parameters = out_get_parameters;
2154 out->stream.common.add_audio_effect = out_add_audio_effect;
2155 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2156 out->stream.get_latency = out_get_latency;
2157 out->stream.set_volume = out_set_volume;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002158#ifdef NO_AUDIO_OUT
2159 out->stream.write = out_write_for_no_output;
2160#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002161 out->stream.write = out_write;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002162#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002163 out->stream.get_render_position = out_get_render_position;
2164 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002165 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002166
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002167 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002168 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002169 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002170
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002171 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2172 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2173
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002174 config->format = out->stream.common.get_format(&out->stream.common);
2175 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2176 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2177
2178 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002179 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002180 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002181
2182error_open:
2183 free(out);
2184 *stream_out = NULL;
2185 ALOGD("%s: exit: ret %d", __func__, ret);
2186 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002187}
2188
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002189static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002190 struct audio_stream_out *stream)
2191{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002192 struct stream_out *out = (struct stream_out *)stream;
2193 struct audio_device *adev = out->dev;
2194
Eric Laurent994a6932013-07-17 11:51:42 -07002195 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002196 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002197 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2198 destroy_offload_callback_thread(out);
2199
2200 if (out->compr_config.codec != NULL)
2201 free(out->compr_config.codec);
2202 }
Ravi Kumar Alamandaa4fc9022014-10-08 18:57:46 -07002203
2204 if (adev->voice_tx_output == out)
2205 adev->voice_tx_output = NULL;
2206
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002207 pthread_cond_destroy(&out->cond);
2208 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002209 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002210 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002211}
2212
2213static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2214{
2215 struct audio_device *adev = (struct audio_device *)dev;
2216 struct str_parms *parms;
2217 char *str;
2218 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002219 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002220 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002221 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002222
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002223 ALOGD("%s: enter: %s", __func__, kvpairs);
2224
2225 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002226
2227 parms = str_parms_create_str(kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002228 status = voice_set_parameters(adev, parms);
2229 if (status != 0) {
2230 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002231 }
2232
2233 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2234 if (ret >= 0) {
2235 /* When set to false, HAL should disable EC and NS
2236 * But it is currently not supported.
2237 */
2238 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2239 adev->bluetooth_nrec = true;
2240 else
2241 adev->bluetooth_nrec = false;
2242 }
2243
2244 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2245 if (ret >= 0) {
2246 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2247 adev->screen_off = false;
2248 else
2249 adev->screen_off = true;
2250 }
2251
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002252 ret = str_parms_get_int(parms, "rotation", &val);
2253 if (ret >= 0) {
2254 bool reverse_speakers = false;
2255 switch(val) {
2256 // FIXME: note that the code below assumes that the speakers are in the correct placement
2257 // relative to the user when the device is rotated 90deg from its default rotation. This
2258 // assumption is device-specific, not platform-specific like this code.
2259 case 270:
2260 reverse_speakers = true;
2261 break;
2262 case 0:
2263 case 90:
2264 case 180:
2265 break;
2266 default:
2267 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002268 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002269 }
Eric Laurent03f09432014-03-25 18:09:11 -07002270 if (status == 0) {
Ravi Kumar Alamanda1f60cf82015-04-23 19:45:17 -07002271 platform_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002272 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002273 }
2274
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002275 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2276 if (ret >= 0) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002277 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002278 }
2279
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08002280 audio_extn_hfp_set_parameters(adev, parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002281done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002282 str_parms_destroy(parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002283 pthread_mutex_unlock(&adev->lock);
Eric Laurent03f09432014-03-25 18:09:11 -07002284 ALOGV("%s: exit with code(%d)", __func__, status);
2285 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002286}
2287
2288static char* adev_get_parameters(const struct audio_hw_device *dev,
2289 const char *keys)
2290{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002291 struct audio_device *adev = (struct audio_device *)dev;
2292 struct str_parms *reply = str_parms_create();
2293 struct str_parms *query = str_parms_create_str(keys);
2294 char *str;
2295
2296 pthread_mutex_lock(&adev->lock);
2297
2298 voice_get_parameters(adev, query, reply);
2299 str = str_parms_to_str(reply);
2300 str_parms_destroy(query);
2301 str_parms_destroy(reply);
2302
2303 pthread_mutex_unlock(&adev->lock);
2304 ALOGV("%s: exit: returns - %s", __func__, str);
2305 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002306}
2307
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002308static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002309{
2310 return 0;
2311}
2312
Haynes Mathew George5191a852013-09-11 14:19:36 -07002313static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2314{
2315 int ret;
2316 struct audio_device *adev = (struct audio_device *)dev;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002317
Eric Laurent4cc4ce12014-09-10 13:21:01 -05002318 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
2319
Haynes Mathew George5191a852013-09-11 14:19:36 -07002320 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002321 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002322 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002323
Haynes Mathew George5191a852013-09-11 14:19:36 -07002324 return ret;
2325}
2326
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002327static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002328{
2329 return -ENOSYS;
2330}
2331
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002332static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2333 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002334{
2335 return -ENOSYS;
2336}
2337
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002338static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002339{
2340 return -ENOSYS;
2341}
2342
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002343static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002344{
2345 return -ENOSYS;
2346}
2347
2348static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2349{
2350 struct audio_device *adev = (struct audio_device *)dev;
2351
2352 pthread_mutex_lock(&adev->lock);
2353 if (adev->mode != mode) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002354 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002355 adev->mode = mode;
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -07002356 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) &&
2357 voice_is_in_call(adev)) {
2358 voice_stop_call(adev);
2359 adev->current_call_output = NULL;
2360 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002361 }
2362 pthread_mutex_unlock(&adev->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002363
2364 audio_extn_extspk_set_mode(adev->extspk, mode);
2365
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002366 return 0;
2367}
2368
2369static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2370{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002371 int ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002372 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002373
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002374 ALOGD("%s: state %d\n", __func__, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002375 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002376 ret = voice_set_mic_mute(adev, state);
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07002377 adev->mic_muted = state;
Eric Laurenta609e8e2014-06-18 02:15:17 +00002378 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002379
2380 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002381}
2382
2383static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2384{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002385 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002386 return 0;
2387}
2388
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002389static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002390 const struct audio_config *config)
2391{
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002392 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002393
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002394 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
2395 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002396}
2397
2398static int adev_open_input_stream(struct audio_hw_device *dev,
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002399 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002400 audio_devices_t devices,
2401 struct audio_config *config,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002402 struct audio_stream_in **stream_in,
Eric Laurent91975a62014-07-27 17:15:50 -07002403 audio_input_flags_t flags,
2404 const char *address __unused,
Eric Laurentcefbbac2014-09-04 13:54:10 -05002405 audio_source_t source )
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002406{
2407 struct audio_device *adev = (struct audio_device *)dev;
2408 struct stream_in *in;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002409 int ret = 0, buffer_size, frame_size;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002410 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002411 bool is_low_latency = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002412
Eric Laurent994a6932013-07-17 11:51:42 -07002413 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002414 *stream_in = NULL;
2415 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2416 return -EINVAL;
2417
2418 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2419
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002420 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2421
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002422 in->stream.common.get_sample_rate = in_get_sample_rate;
2423 in->stream.common.set_sample_rate = in_set_sample_rate;
2424 in->stream.common.get_buffer_size = in_get_buffer_size;
2425 in->stream.common.get_channels = in_get_channels;
2426 in->stream.common.get_format = in_get_format;
2427 in->stream.common.set_format = in_set_format;
2428 in->stream.common.standby = in_standby;
2429 in->stream.common.dump = in_dump;
2430 in->stream.common.set_parameters = in_set_parameters;
2431 in->stream.common.get_parameters = in_get_parameters;
2432 in->stream.common.add_audio_effect = in_add_audio_effect;
2433 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2434 in->stream.set_gain = in_set_gain;
2435 in->stream.read = in_read;
2436 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2437
2438 in->device = devices;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002439 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002440 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002441 in->standby = 1;
2442 in->channel_mask = config->channel_mask;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002443 in->capture_handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002444
2445 /* Update config params with the requested sample rate and channels */
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002446 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
2447 if (config->sample_rate == 0)
2448 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2449 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2450 config->sample_rate != 8000) {
2451 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2452 ret = -EINVAL;
2453 goto err_open;
2454 }
2455 if (config->format == AUDIO_FORMAT_DEFAULT)
2456 config->format = AUDIO_FORMAT_PCM_16_BIT;
2457 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2458 config->format = AUDIO_FORMAT_PCM_16_BIT;
2459 ret = -EINVAL;
2460 goto err_open;
2461 }
2462
2463 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
2464 in->config = pcm_config_afe_proxy_record;
2465 } else {
2466 in->usecase = USECASE_AUDIO_RECORD;
2467 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
2468 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
2469 is_low_latency = true;
Glenn Kasten4f993392014-05-14 07:30:48 -07002470#if LOW_LATENCY_CAPTURE_USE_CASE
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002471 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Glenn Kasten4f993392014-05-14 07:30:48 -07002472#endif
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002473 }
2474 in->config = pcm_config_audio_capture;
2475
2476 frame_size = audio_stream_in_frame_size(&in->stream);
2477 buffer_size = get_input_buffer_size(config->sample_rate,
2478 config->format,
2479 channel_count,
2480 is_low_latency);
2481 in->config.period_size = buffer_size / frame_size;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002482 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002483 in->config.channels = channel_count;
2484 in->config.rate = config->sample_rate;
2485
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002486 /* This stream could be for sound trigger lab,
2487 get sound trigger pcm if present */
2488 audio_extn_sound_trigger_check_and_get_session(in);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002489
2490 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002491 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002492 return 0;
2493
2494err_open:
2495 free(in);
2496 *stream_in = NULL;
2497 return ret;
2498}
2499
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002500static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002501 struct audio_stream_in *stream)
2502{
Eric Laurent994a6932013-07-17 11:51:42 -07002503 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002504
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002505 in_standby(&stream->common);
2506 free(stream);
2507
2508 return;
2509}
2510
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002511static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002512{
2513 return 0;
2514}
2515
Andy Hung31aca912014-03-20 17:14:59 -07002516/* verifies input and output devices and their capabilities.
2517 *
2518 * This verification is required when enabling extended bit-depth or
2519 * sampling rates, as not all qcom products support it.
2520 *
2521 * Suitable for calling only on initialization such as adev_open().
2522 * It fills the audio_device use_case_table[] array.
2523 *
2524 * Has a side-effect that it needs to configure audio routing / devices
2525 * in order to power up the devices and read the device parameters.
2526 * It does not acquire any hw device lock. Should restore the devices
2527 * back to "normal state" upon completion.
2528 */
2529static int adev_verify_devices(struct audio_device *adev)
2530{
2531 /* enumeration is a bit difficult because one really wants to pull
2532 * the use_case, device id, etc from the hidden pcm_device_table[].
2533 * In this case there are the following use cases and device ids.
2534 *
2535 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
2536 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
2537 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
2538 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
2539 * [USECASE_AUDIO_RECORD] = {0, 0},
2540 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
2541 * [USECASE_VOICE_CALL] = {2, 2},
2542 *
2543 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
2544 * USECASE_VOICE_CALL omitted, but possible for either input or output.
2545 */
2546
2547 /* should be the usecases enabled in adev_open_input_stream() */
2548 static const int test_in_usecases[] = {
2549 USECASE_AUDIO_RECORD,
2550 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
2551 };
2552 /* should be the usecases enabled in adev_open_output_stream()*/
2553 static const int test_out_usecases[] = {
2554 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
2555 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
2556 };
2557 static const usecase_type_t usecase_type_by_dir[] = {
2558 PCM_PLAYBACK,
2559 PCM_CAPTURE,
2560 };
2561 static const unsigned flags_by_dir[] = {
2562 PCM_OUT,
2563 PCM_IN,
2564 };
2565
2566 size_t i;
2567 unsigned dir;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002568 const unsigned card_id = adev->snd_card;
Andy Hung31aca912014-03-20 17:14:59 -07002569 char info[512]; /* for possible debug info */
2570
2571 for (dir = 0; dir < 2; ++dir) {
2572 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
2573 const unsigned flags_dir = flags_by_dir[dir];
2574 const size_t testsize =
2575 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
2576 const int *testcases =
2577 dir ? test_in_usecases : test_out_usecases;
2578 const audio_devices_t audio_device =
2579 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
2580
2581 for (i = 0; i < testsize; ++i) {
2582 const audio_usecase_t audio_usecase = testcases[i];
2583 int device_id;
2584 snd_device_t snd_device;
2585 struct pcm_params **pparams;
2586 struct stream_out out;
2587 struct stream_in in;
2588 struct audio_usecase uc_info;
2589 int retval;
2590
2591 pparams = &adev->use_case_table[audio_usecase];
2592 pcm_params_free(*pparams); /* can accept null input */
2593 *pparams = NULL;
2594
2595 /* find the device ID for the use case (signed, for error) */
2596 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
2597 if (device_id < 0)
2598 continue;
2599
2600 /* prepare structures for device probing */
2601 memset(&uc_info, 0, sizeof(uc_info));
2602 uc_info.id = audio_usecase;
2603 uc_info.type = usecase_type;
2604 if (dir) {
2605 adev->active_input = &in;
2606 memset(&in, 0, sizeof(in));
2607 in.device = audio_device;
2608 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
2609 uc_info.stream.in = &in;
2610 } else {
2611 adev->active_input = NULL;
2612 }
2613 memset(&out, 0, sizeof(out));
2614 out.devices = audio_device; /* only field needed in select_devices */
2615 uc_info.stream.out = &out;
2616 uc_info.devices = audio_device;
2617 uc_info.in_snd_device = SND_DEVICE_NONE;
2618 uc_info.out_snd_device = SND_DEVICE_NONE;
2619 list_add_tail(&adev->usecase_list, &uc_info.list);
2620
2621 /* select device - similar to start_(in/out)put_stream() */
2622 retval = select_devices(adev, audio_usecase);
2623 if (retval >= 0) {
2624 *pparams = pcm_params_get(card_id, device_id, flags_dir);
2625#if LOG_NDEBUG == 0
2626 if (*pparams) {
2627 ALOGV("%s: (%s) card %d device %d", __func__,
2628 dir ? "input" : "output", card_id, device_id);
2629 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
2630 ALOGV(info); /* print parameters */
2631 } else {
2632 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
2633 }
2634#endif
2635 }
2636
2637 /* deselect device - similar to stop_(in/out)put_stream() */
2638 /* 1. Get and set stream specific mixer controls */
Glenn Kastene7137302014-04-28 15:12:18 -07002639 retval = disable_audio_route(adev, &uc_info);
Andy Hung31aca912014-03-20 17:14:59 -07002640 /* 2. Disable the rx device */
2641 retval = disable_snd_device(adev,
Glenn Kastene7137302014-04-28 15:12:18 -07002642 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
Andy Hung31aca912014-03-20 17:14:59 -07002643 list_remove(&uc_info.list);
2644 }
2645 }
2646 adev->active_input = NULL; /* restore adev state */
2647 return 0;
2648}
2649
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002650static int adev_close(hw_device_t *device)
2651{
Andy Hung31aca912014-03-20 17:14:59 -07002652 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002653 struct audio_device *adev = (struct audio_device *)device;
2654 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002655 free(adev->snd_dev_ref_cnt);
2656 platform_deinit(adev->platform);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002657 audio_extn_extspk_deinit(adev->extspk);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002658 audio_extn_sound_trigger_deinit(adev);
Andy Hung31aca912014-03-20 17:14:59 -07002659 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
2660 pcm_params_free(adev->use_case_table[i]);
2661 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002662 free(device);
2663 return 0;
2664}
2665
Glenn Kasten4f993392014-05-14 07:30:48 -07002666/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
2667 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
2668 * just that it _might_ work.
2669 */
2670static int period_size_is_plausible_for_low_latency(int period_size)
2671{
2672 switch (period_size) {
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002673 case 48:
2674 case 96:
2675 case 144:
Glenn Kasten4f993392014-05-14 07:30:48 -07002676 case 160:
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002677 case 192:
Glenn Kasten4f993392014-05-14 07:30:48 -07002678 case 240:
2679 case 320:
2680 case 480:
2681 return 1;
2682 default:
2683 return 0;
2684 }
2685}
2686
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002687static int adev_open(const hw_module_t *module, const char *name,
2688 hw_device_t **device)
2689{
2690 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002691 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002692
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002693 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002694 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2695
2696 adev = calloc(1, sizeof(struct audio_device));
2697
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002698 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2699
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002700 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2701 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2702 adev->device.common.module = (struct hw_module_t *)module;
2703 adev->device.common.close = adev_close;
2704
2705 adev->device.init_check = adev_init_check;
2706 adev->device.set_voice_volume = adev_set_voice_volume;
2707 adev->device.set_master_volume = adev_set_master_volume;
2708 adev->device.get_master_volume = adev_get_master_volume;
2709 adev->device.set_master_mute = adev_set_master_mute;
2710 adev->device.get_master_mute = adev_get_master_mute;
2711 adev->device.set_mode = adev_set_mode;
2712 adev->device.set_mic_mute = adev_set_mic_mute;
2713 adev->device.get_mic_mute = adev_get_mic_mute;
2714 adev->device.set_parameters = adev_set_parameters;
2715 adev->device.get_parameters = adev_get_parameters;
2716 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2717 adev->device.open_output_stream = adev_open_output_stream;
2718 adev->device.close_output_stream = adev_close_output_stream;
2719 adev->device.open_input_stream = adev_open_input_stream;
2720 adev->device.close_input_stream = adev_close_input_stream;
2721 adev->device.dump = adev_dump;
2722
2723 /* Set the default route before the PCM stream is opened */
2724 pthread_mutex_lock(&adev->lock);
2725 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002726 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002727 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002728 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002729 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002730 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002731 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002732 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002733 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002734 pthread_mutex_unlock(&adev->lock);
2735
2736 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002737 adev->platform = platform_init(adev);
2738 if (!adev->platform) {
2739 free(adev->snd_dev_ref_cnt);
2740 free(adev);
2741 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2742 *device = NULL;
2743 return -EINVAL;
2744 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002745
Eric Laurent0499d4f2014-08-25 22:39:29 -05002746 adev->extspk = audio_extn_extspk_init(adev);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002747 audio_extn_sound_trigger_init(adev);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002748
Eric Laurentc4aef752013-09-12 17:45:53 -07002749 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2750 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2751 if (adev->visualizer_lib == NULL) {
2752 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2753 } else {
2754 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2755 adev->visualizer_start_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002756 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002757 "visualizer_hal_start_output");
2758 adev->visualizer_stop_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002759 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002760 "visualizer_hal_stop_output");
2761 }
2762 }
2763
Haynes Mathew George41f86652014-06-17 14:22:15 -07002764 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2765 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2766 if (adev->offload_effects_lib == NULL) {
2767 ALOGE("%s: DLOPEN failed for %s", __func__,
2768 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2769 } else {
2770 ALOGV("%s: DLOPEN successful for %s", __func__,
2771 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2772 adev->offload_effects_start_output =
2773 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2774 "offload_effects_bundle_hal_start_output");
2775 adev->offload_effects_stop_output =
2776 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2777 "offload_effects_bundle_hal_stop_output");
2778 }
2779 }
2780
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002781 adev->bt_wb_speech_enabled = false;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002782 adev->enable_voicerx = false;
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002783
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002784 *device = &adev->device.common;
Andy Hung31aca912014-03-20 17:14:59 -07002785 if (k_enable_extended_precision)
2786 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002787
Glenn Kasten4f993392014-05-14 07:30:48 -07002788 char value[PROPERTY_VALUE_MAX];
2789 int trial;
2790 if (property_get("audio_hal.period_size", value, NULL) > 0) {
2791 trial = atoi(value);
2792 if (period_size_is_plausible_for_low_latency(trial)) {
2793 pcm_config_low_latency.period_size = trial;
2794 pcm_config_low_latency.start_threshold = trial / 4;
2795 pcm_config_low_latency.avail_min = trial / 4;
2796 configured_low_latency_capture_period_size = trial;
2797 }
2798 }
2799 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
2800 trial = atoi(value);
2801 if (period_size_is_plausible_for_low_latency(trial)) {
2802 configured_low_latency_capture_period_size = trial;
2803 }
2804 }
2805
Eric Laurent994a6932013-07-17 11:51:42 -07002806 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002807 return 0;
2808}
2809
2810static struct hw_module_methods_t hal_module_methods = {
2811 .open = adev_open,
2812};
2813
2814struct audio_module HAL_MODULE_INFO_SYM = {
2815 .common = {
2816 .tag = HARDWARE_MODULE_TAG,
2817 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2818 .hal_api_version = HARDWARE_HAL_API_VERSION,
2819 .id = AUDIO_HARDWARE_MODULE_ID,
2820 .name = "QCOM Audio HAL",
2821 .author = "Code Aurora Forum",
2822 .methods = &hal_module_methods,
2823 },
2824};