blob: 5c46c1efce143266849eea3a876d85b015b90645 [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",
Ravi Kumar Alamandaf78a4d92015-04-24 15:18:23 -0700148 [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700149
Eric Laurentb23d5282013-05-14 15:27:20 -0700150 [USECASE_AUDIO_RECORD] = "audio-record",
151 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700152
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800153 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
154 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700155
Eric Laurentb23d5282013-05-14 15:27:20 -0700156 [USECASE_VOICE_CALL] = "voice-call",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700157 [USECASE_VOICE2_CALL] = "voice2-call",
158 [USECASE_VOLTE_CALL] = "volte-call",
159 [USECASE_QCHAT_CALL] = "qchat-call",
160 [USECASE_VOWLAN_CALL] = "vowlan-call",
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700161
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700162 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
163 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
164
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700165 [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
166 [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700167};
168
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800169
170#define STRING_TO_ENUM(string) { #string, string }
171
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800172struct string_to_enum {
173 const char *name;
174 uint32_t value;
175};
176
177static const struct string_to_enum out_channels_name_to_enum_table[] = {
178 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
179 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
180 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
181};
182
Haynes Mathew George5191a852013-09-11 14:19:36 -0700183static int set_voice_volume_l(struct audio_device *adev, float volume);
184
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700185static bool is_supported_format(audio_format_t format)
186{
Eric Laurent8251ac82014-07-23 11:00:25 -0700187 switch (format) {
188 case AUDIO_FORMAT_MP3:
189 case AUDIO_FORMAT_AAC_LC:
190 case AUDIO_FORMAT_AAC_HE_V1:
191 case AUDIO_FORMAT_AAC_HE_V2:
192 return true;
193 default:
194 break;
195 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700196 return false;
197}
198
199static int get_snd_codec_id(audio_format_t format)
200{
201 int id = 0;
202
Eric Laurent8251ac82014-07-23 11:00:25 -0700203 switch (format & AUDIO_FORMAT_MAIN_MASK) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700204 case AUDIO_FORMAT_MP3:
205 id = SND_AUDIOCODEC_MP3;
206 break;
207 case AUDIO_FORMAT_AAC:
208 id = SND_AUDIOCODEC_AAC;
209 break;
210 default:
211 ALOGE("%s: Unsupported audio format", __func__);
212 }
213
214 return id;
215}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800216
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800217int enable_audio_route(struct audio_device *adev,
218 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800219{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700220 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800221 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800222
223 if (usecase == NULL)
224 return -EINVAL;
225
226 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
227
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800228 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700229 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800230 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700231 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800232
233 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500234 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700235 ALOGD("%s: apply and update mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700236 audio_route_apply_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800237
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800238 ALOGV("%s: exit", __func__);
239 return 0;
240}
241
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800242int disable_audio_route(struct audio_device *adev,
243 struct audio_usecase *usecase)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800244{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700245 snd_device_t snd_device;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800246 char mixer_path[50];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800247
248 if (usecase == NULL)
249 return -EINVAL;
250
251 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700252 if (usecase->type == PCM_CAPTURE)
253 snd_device = usecase->in_snd_device;
254 else
255 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800256 strcpy(mixer_path, use_case_table[usecase->id]);
Ravi Kumar Alamanda299760a2013-11-01 17:29:09 -0500257 platform_add_backend_name(adev->platform, mixer_path, snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700258 ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700259 audio_route_reset_and_update_path(adev->audio_route, mixer_path);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800260
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800261 ALOGV("%s: exit", __func__);
262 return 0;
263}
264
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800265int enable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700266 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800267{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800268 if (snd_device < SND_DEVICE_MIN ||
269 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800270 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800271 return -EINVAL;
272 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700273
274 adev->snd_dev_ref_cnt[snd_device]++;
275 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700276 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700277 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700278 return 0;
279 }
280
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700281 /* due to the possibility of calibration overwrite between listen
282 and audio, notify sound trigger hal before audio calibration is sent */
283 audio_extn_sound_trigger_update_device_status(snd_device,
284 ST_EVENT_SND_DEVICE_BUSY);
285
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700286 if (audio_extn_spkr_prot_is_enabled())
287 audio_extn_spkr_prot_calib_cancel(adev);
288
Eric Laurentb23d5282013-05-14 15:27:20 -0700289 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700290 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700291 audio_extn_sound_trigger_update_device_status(snd_device,
292 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800293 return -EINVAL;
294 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800295
zhaoyang yin4211fad2015-06-04 21:13:25 +0800296 audio_extn_dsm_feedback_enable(adev, snd_device, true);
297
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700298 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
299 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
300 audio_extn_spkr_prot_is_enabled()) {
301 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
302 adev->snd_dev_ref_cnt[snd_device]--;
303 return -EINVAL;
304 }
305 if (audio_extn_spkr_prot_start_processing(snd_device)) {
306 ALOGE("%s: spkr_start_processing failed", __func__);
307 return -EINVAL;
308 }
309 } else {
310 const char * dev_path = platform_get_snd_device_name(snd_device);
311 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
312 audio_route_apply_and_update_path(adev->audio_route, dev_path);
313 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700314
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800315 return 0;
316}
317
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800318int disable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700319 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800320{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800321 if (snd_device < SND_DEVICE_MIN ||
322 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800323 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800324 return -EINVAL;
325 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700326 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
327 ALOGE("%s: device ref cnt is already 0", __func__);
328 return -EINVAL;
329 }
330 adev->snd_dev_ref_cnt[snd_device]--;
331 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700332 const char * dev_path = platform_get_snd_device_name(snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700333 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
zhaoyang yin4211fad2015-06-04 21:13:25 +0800334
335 audio_extn_dsm_feedback_enable(adev, snd_device, false);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700336 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
337 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
338 audio_extn_spkr_prot_is_enabled()) {
339 audio_extn_spkr_prot_stop_processing(snd_device);
340 } else {
341 audio_route_reset_and_update_path(adev->audio_route, dev_path);
342 }
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700343 audio_extn_sound_trigger_update_device_status(snd_device,
344 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700345 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800346 return 0;
347}
348
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700349static void check_usecases_codec_backend(struct audio_device *adev,
350 struct audio_usecase *uc_info,
351 snd_device_t snd_device)
352{
353 struct listnode *node;
354 struct audio_usecase *usecase;
355 bool switch_device[AUDIO_USECASE_MAX];
356 int i, num_uc_to_switch = 0;
357
358 /*
359 * This function is to make sure that all the usecases that are active on
360 * the hardware codec backend are always routed to any one device that is
361 * handled by the hardware codec.
362 * For example, if low-latency and deep-buffer usecases are currently active
363 * on speaker and out_set_parameters(headset) is received on low-latency
364 * output, then we have to make sure deep-buffer is also switched to headset,
365 * because of the limitation that both the devices cannot be enabled
366 * at the same time as they share the same backend.
367 */
368 /* Disable all the usecases on the shared backend other than the
369 specified usecase */
370 for (i = 0; i < AUDIO_USECASE_MAX; i++)
371 switch_device[i] = false;
372
373 list_for_each(node, &adev->usecase_list) {
374 usecase = node_to_item(node, struct audio_usecase, list);
375 if (usecase->type != PCM_CAPTURE &&
376 usecase != uc_info &&
377 usecase->out_snd_device != snd_device &&
378 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
379 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
380 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700381 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700382 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700383 switch_device[usecase->id] = true;
384 num_uc_to_switch++;
385 }
386 }
387
388 if (num_uc_to_switch) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700389 list_for_each(node, &adev->usecase_list) {
390 usecase = node_to_item(node, struct audio_usecase, list);
391 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700392 disable_snd_device(adev, usecase->out_snd_device);
sangwon.jeon866d5ff2013-10-17 21:42:50 +0900393 }
394 }
395
396 list_for_each(node, &adev->usecase_list) {
397 usecase = node_to_item(node, struct audio_usecase, list);
398 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700399 enable_snd_device(adev, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700400 }
401 }
402
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700403 /* Re-route all the usecases on the shared backend other than the
404 specified usecase to new snd devices */
405 list_for_each(node, &adev->usecase_list) {
406 usecase = node_to_item(node, struct audio_usecase, list);
407 /* Update the out_snd_device only before enabling the audio route */
408 if (switch_device[usecase->id] ) {
409 usecase->out_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700410 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700411 }
412 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700413 }
414}
415
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700416static void check_and_route_capture_usecases(struct audio_device *adev,
417 struct audio_usecase *uc_info,
418 snd_device_t snd_device)
419{
420 struct listnode *node;
421 struct audio_usecase *usecase;
422 bool switch_device[AUDIO_USECASE_MAX];
423 int i, num_uc_to_switch = 0;
424
425 /*
426 * This function is to make sure that all the active capture usecases
427 * are always routed to the same input sound device.
428 * For example, if audio-record and voice-call usecases are currently
429 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
430 * is received for voice call then we have to make sure that audio-record
431 * usecase is also switched to earpiece i.e. voice-dmic-ef,
432 * because of the limitation that two devices cannot be enabled
433 * at the same time if they share the same backend.
434 */
435 for (i = 0; i < AUDIO_USECASE_MAX; i++)
436 switch_device[i] = false;
437
438 list_for_each(node, &adev->usecase_list) {
439 usecase = node_to_item(node, struct audio_usecase, list);
440 if (usecase->type != PCM_PLAYBACK &&
441 usecase != uc_info &&
Anish Kumarff864712015-06-03 13:35:11 -0700442 usecase->in_snd_device != snd_device &&
443 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700444 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
445 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700446 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700447 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700448 switch_device[usecase->id] = true;
449 num_uc_to_switch++;
450 }
451 }
452
453 if (num_uc_to_switch) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700454 list_for_each(node, &adev->usecase_list) {
455 usecase = node_to_item(node, struct audio_usecase, list);
456 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700457 disable_snd_device(adev, usecase->in_snd_device);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700458 }
459 }
460
461 list_for_each(node, &adev->usecase_list) {
462 usecase = node_to_item(node, struct audio_usecase, list);
463 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700464 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700465 }
466 }
467
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700468 /* Re-route all the usecases on the shared backend other than the
469 specified usecase to new snd devices */
470 list_for_each(node, &adev->usecase_list) {
471 usecase = node_to_item(node, struct audio_usecase, list);
472 /* Update the in_snd_device only before enabling the audio route */
473 if (switch_device[usecase->id] ) {
474 usecase->in_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700475 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700476 }
477 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700478 }
479}
480
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800481/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700482static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800483{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700484 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700485 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800486
487 switch (channels) {
488 /*
489 * Do not handle stereo output in Multi-channel cases
490 * Stereo case is handled in normal playback path
491 */
492 case 6:
493 ALOGV("%s: HDMI supports 5.1", __func__);
494 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
495 break;
496 case 8:
497 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
498 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
499 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
500 break;
501 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700502 ALOGE("HDMI does not support multi channel playback");
503 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800504 break;
505 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700506 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800507}
508
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700509static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
510{
511 struct audio_usecase *usecase;
512 struct listnode *node;
513
514 list_for_each(node, &adev->usecase_list) {
515 usecase = node_to_item(node, struct audio_usecase, list);
516 if (usecase->type == VOICE_CALL) {
517 ALOGV("%s: usecase id %d", __func__, usecase->id);
518 return usecase->id;
519 }
520 }
521 return USECASE_INVALID;
522}
523
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800524struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
525 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700526{
527 struct audio_usecase *usecase;
528 struct listnode *node;
529
530 list_for_each(node, &adev->usecase_list) {
531 usecase = node_to_item(node, struct audio_usecase, list);
532 if (usecase->id == uc_id)
533 return usecase;
534 }
535 return NULL;
536}
537
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800538int select_devices(struct audio_device *adev,
539 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800540{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800541 snd_device_t out_snd_device = SND_DEVICE_NONE;
542 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700543 struct audio_usecase *usecase = NULL;
544 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800545 struct audio_usecase *hfp_usecase = NULL;
546 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800547 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700548 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800549
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700550 usecase = get_usecase_from_list(adev, uc_id);
551 if (usecase == NULL) {
552 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
553 return -EINVAL;
554 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800555
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800556 if ((usecase->type == VOICE_CALL) ||
557 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700558 out_snd_device = platform_get_output_snd_device(adev->platform,
559 usecase->stream.out->devices);
560 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700561 usecase->devices = usecase->stream.out->devices;
562 } else {
563 /*
564 * If the voice call is active, use the sound devices of voice call usecase
565 * so that it would not result any device switch. All the usecases will
566 * be switched to new device when select_devices() is called for voice call
567 * usecase. This is to avoid switching devices for voice call when
568 * check_usecases_codec_backend() is called below.
569 */
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700570 if (voice_is_in_call(adev)) {
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700571 vc_usecase = get_usecase_from_list(adev,
572 get_voice_usecase_id_from_list(adev));
573 if ((vc_usecase != NULL) &&
574 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
575 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700576 in_snd_device = vc_usecase->in_snd_device;
577 out_snd_device = vc_usecase->out_snd_device;
578 }
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800579 } else if (audio_extn_hfp_is_active(adev)) {
580 hfp_ucid = audio_extn_hfp_get_usecase();
581 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
582 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
583 in_snd_device = hfp_usecase->in_snd_device;
584 out_snd_device = hfp_usecase->out_snd_device;
585 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700586 }
587 if (usecase->type == PCM_PLAYBACK) {
588 usecase->devices = usecase->stream.out->devices;
589 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700590 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700591 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700592 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700593 if (usecase->stream.out == adev->primary_output &&
594 adev->active_input &&
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800595 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
596 out_snd_device != usecase->out_snd_device) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700597 select_devices(adev, adev->active_input->usecase);
598 }
599 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700600 } else if (usecase->type == PCM_CAPTURE) {
601 usecase->devices = usecase->stream.in->device;
602 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700603 if (in_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700604 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700605 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
606 adev->primary_output && !adev->primary_output->standby) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700607 out_device = adev->primary_output->devices;
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800608 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700609 } else if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
610 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700611 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700612 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700613 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700614 }
615 }
616
617 if (out_snd_device == usecase->out_snd_device &&
618 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800619 return 0;
620 }
621
sangwoobc677242013-08-08 16:53:43 +0900622 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700623 out_snd_device, platform_get_snd_device_name(out_snd_device),
624 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800625
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800626 /*
627 * Limitation: While in call, to do a device switch we need to disable
628 * and enable both RX and TX devices though one of them is same as current
629 * device.
630 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700631 if ((usecase->type == VOICE_CALL) &&
632 (usecase->in_snd_device != SND_DEVICE_NONE) &&
633 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700634 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800635 }
636
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700637 /* Disable current sound devices */
638 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700639 disable_audio_route(adev, usecase);
640 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800641 }
642
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700643 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700644 disable_audio_route(adev, usecase);
645 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800646 }
647
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700648 /* Applicable only on the targets that has external modem.
649 * New device information should be sent to modem before enabling
650 * the devices to reduce in-call device switch time.
651 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700652 if ((usecase->type == VOICE_CALL) &&
653 (usecase->in_snd_device != SND_DEVICE_NONE) &&
654 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700655 status = platform_switch_voice_call_enable_device_config(adev->platform,
656 out_snd_device,
657 in_snd_device);
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700658 }
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700659
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700660 /* Enable new sound devices */
661 if (out_snd_device != SND_DEVICE_NONE) {
662 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
663 check_usecases_codec_backend(adev, usecase, out_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700664 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800665 }
666
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700667 if (in_snd_device != SND_DEVICE_NONE) {
668 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700669 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700670 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700671
Eric Laurentb23d5282013-05-14 15:27:20 -0700672 if (usecase->type == VOICE_CALL)
673 status = platform_switch_voice_call_device_post(adev->platform,
674 out_snd_device,
675 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800676
sangwoo170731f2013-06-08 15:36:36 +0900677 usecase->in_snd_device = in_snd_device;
678 usecase->out_snd_device = out_snd_device;
679
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700680 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900681
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700682 /* Applicable only on the targets that has external modem.
683 * Enable device command should be sent to modem only after
684 * enabling voice call mixer controls
685 */
686 if (usecase->type == VOICE_CALL)
687 status = platform_switch_voice_call_usecase_route_post(adev->platform,
688 out_snd_device,
689 in_snd_device);
690
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691 return status;
692}
693
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800694static int stop_input_stream(struct stream_in *in)
695{
696 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800697 struct audio_usecase *uc_info;
698 struct audio_device *adev = in->dev;
699
Eric Laurentc8400632013-02-14 19:04:54 -0800700 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800701
Eric Laurent994a6932013-07-17 11:51:42 -0700702 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700703 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800704 uc_info = get_usecase_from_list(adev, in->usecase);
705 if (uc_info == NULL) {
706 ALOGE("%s: Could not find the usecase (%d) in the list",
707 __func__, in->usecase);
708 return -EINVAL;
709 }
710
Eric Laurent150dbfe2013-02-27 14:31:02 -0800711 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700712 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700713
714 /* 2. Disable the tx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700715 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800716
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800717 list_remove(&uc_info->list);
718 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800719
Eric Laurent994a6932013-07-17 11:51:42 -0700720 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800721 return ret;
722}
723
724int start_input_stream(struct stream_in *in)
725{
726 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800727 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800728 struct audio_usecase *uc_info;
729 struct audio_device *adev = in->dev;
730
Eric Laurent994a6932013-07-17 11:51:42 -0700731 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700732 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800733 if (in->pcm_device_id < 0) {
734 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
735 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800736 ret = -EINVAL;
737 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800738 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700739
740 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800741 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
742 uc_info->id = in->usecase;
743 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800744 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700745 uc_info->devices = in->device;
746 uc_info->in_snd_device = SND_DEVICE_NONE;
747 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800748
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800749 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700750 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800751
Eric Laurentc8400632013-02-14 19:04:54 -0800752 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700753 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700754
755 unsigned int flags = PCM_IN;
756 unsigned int pcm_open_retry_count = 0;
757
758 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
759 flags |= PCM_MMAP | PCM_NOIRQ;
760 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800761 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700762
763 while (1) {
764 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
765 flags, &in->config);
766 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
767 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
768 if (in->pcm != NULL) {
769 pcm_close(in->pcm);
770 in->pcm = NULL;
771 }
772 if (pcm_open_retry_count-- == 0) {
773 ret = -EIO;
774 goto error_open;
775 }
776 usleep(PROXY_OPEN_WAIT_TIME * 1000);
777 continue;
778 }
779 break;
780 }
781
Eric Laurent994a6932013-07-17 11:51:42 -0700782 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800783 return ret;
784
785error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800786 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800787
788error_config:
789 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700790 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800791
792 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800793}
794
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700795/* must be called with out->lock locked */
796static int send_offload_cmd_l(struct stream_out* out, int command)
797{
798 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
799
800 ALOGVV("%s %d", __func__, command);
801
802 cmd->cmd = command;
803 list_add_tail(&out->offload_cmd_list, &cmd->node);
804 pthread_cond_signal(&out->offload_cond);
805 return 0;
806}
807
808/* must be called iwth out->lock locked */
809static void stop_compressed_output_l(struct stream_out *out)
810{
811 out->offload_state = OFFLOAD_STATE_IDLE;
812 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700813 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700814 if (out->compr != NULL) {
815 compress_stop(out->compr);
816 while (out->offload_thread_blocked) {
817 pthread_cond_wait(&out->cond, &out->lock);
818 }
819 }
820}
821
822static void *offload_thread_loop(void *context)
823{
824 struct stream_out *out = (struct stream_out *) context;
825 struct listnode *item;
826
827 out->offload_state = OFFLOAD_STATE_IDLE;
828 out->playback_started = 0;
829
830 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
831 set_sched_policy(0, SP_FOREGROUND);
832 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
833
834 ALOGV("%s", __func__);
835 pthread_mutex_lock(&out->lock);
836 for (;;) {
837 struct offload_cmd *cmd = NULL;
838 stream_callback_event_t event;
839 bool send_callback = false;
840
841 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
842 __func__, list_empty(&out->offload_cmd_list),
843 out->offload_state);
844 if (list_empty(&out->offload_cmd_list)) {
845 ALOGV("%s SLEEPING", __func__);
846 pthread_cond_wait(&out->offload_cond, &out->lock);
847 ALOGV("%s RUNNING", __func__);
848 continue;
849 }
850
851 item = list_head(&out->offload_cmd_list);
852 cmd = node_to_item(item, struct offload_cmd, node);
853 list_remove(item);
854
855 ALOGVV("%s STATE %d CMD %d out->compr %p",
856 __func__, out->offload_state, cmd->cmd, out->compr);
857
858 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
859 free(cmd);
860 break;
861 }
862
863 if (out->compr == NULL) {
864 ALOGE("%s: Compress handle is NULL", __func__);
865 pthread_cond_signal(&out->cond);
866 continue;
867 }
868 out->offload_thread_blocked = true;
869 pthread_mutex_unlock(&out->lock);
870 send_callback = false;
871 switch(cmd->cmd) {
872 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
873 compress_wait(out->compr, -1);
874 send_callback = true;
875 event = STREAM_CBK_EVENT_WRITE_READY;
876 break;
877 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700878 compress_next_track(out->compr);
879 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700880 send_callback = true;
881 event = STREAM_CBK_EVENT_DRAIN_READY;
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800882 /* Resend the metadata for next iteration */
883 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700884 break;
885 case OFFLOAD_CMD_DRAIN:
886 compress_drain(out->compr);
887 send_callback = true;
888 event = STREAM_CBK_EVENT_DRAIN_READY;
889 break;
890 default:
891 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
892 break;
893 }
894 pthread_mutex_lock(&out->lock);
895 out->offload_thread_blocked = false;
896 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700897 if (send_callback) {
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800898 ALOGVV("%s: sending offload_callback event %d", __func__, event);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700899 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700900 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700901 free(cmd);
902 }
903
904 pthread_cond_signal(&out->cond);
905 while (!list_empty(&out->offload_cmd_list)) {
906 item = list_head(&out->offload_cmd_list);
907 list_remove(item);
908 free(node_to_item(item, struct offload_cmd, node));
909 }
910 pthread_mutex_unlock(&out->lock);
911
912 return NULL;
913}
914
915static int create_offload_callback_thread(struct stream_out *out)
916{
917 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
918 list_init(&out->offload_cmd_list);
919 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
920 offload_thread_loop, out);
921 return 0;
922}
923
924static int destroy_offload_callback_thread(struct stream_out *out)
925{
926 pthread_mutex_lock(&out->lock);
927 stop_compressed_output_l(out);
928 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
929
930 pthread_mutex_unlock(&out->lock);
931 pthread_join(out->offload_thread, (void **) NULL);
932 pthread_cond_destroy(&out->offload_cond);
933
934 return 0;
935}
936
Eric Laurent07eeafd2013-10-06 12:52:49 -0700937static bool allow_hdmi_channel_config(struct audio_device *adev)
938{
939 struct listnode *node;
940 struct audio_usecase *usecase;
941 bool ret = true;
942
943 list_for_each(node, &adev->usecase_list) {
944 usecase = node_to_item(node, struct audio_usecase, list);
945 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
946 /*
947 * If voice call is already existing, do not proceed further to avoid
948 * disabling/enabling both RX and TX devices, CSD calls, etc.
949 * Once the voice call done, the HDMI channels can be configured to
950 * max channels of remaining use cases.
951 */
952 if (usecase->id == USECASE_VOICE_CALL) {
953 ALOGD("%s: voice call is active, no change in HDMI channels",
954 __func__);
955 ret = false;
956 break;
957 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
958 ALOGD("%s: multi channel playback is active, "
959 "no change in HDMI channels", __func__);
960 ret = false;
961 break;
962 }
963 }
964 }
965 return ret;
966}
967
968static int check_and_set_hdmi_channels(struct audio_device *adev,
969 unsigned int channels)
970{
971 struct listnode *node;
972 struct audio_usecase *usecase;
973
974 /* Check if change in HDMI channel config is allowed */
975 if (!allow_hdmi_channel_config(adev))
976 return 0;
977
978 if (channels == adev->cur_hdmi_channels) {
979 ALOGD("%s: Requested channels are same as current", __func__);
980 return 0;
981 }
982
983 platform_set_hdmi_channels(adev->platform, channels);
984 adev->cur_hdmi_channels = channels;
985
986 /*
987 * Deroute all the playback streams routed to HDMI so that
988 * the back end is deactivated. Note that backend will not
989 * be deactivated if any one stream is connected to it.
990 */
991 list_for_each(node, &adev->usecase_list) {
992 usecase = node_to_item(node, struct audio_usecase, list);
993 if (usecase->type == PCM_PLAYBACK &&
994 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700995 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -0700996 }
997 }
998
999 /*
1000 * Enable all the streams disabled above. Now the HDMI backend
1001 * will be activated with new channel configuration
1002 */
1003 list_for_each(node, &adev->usecase_list) {
1004 usecase = node_to_item(node, struct audio_usecase, list);
1005 if (usecase->type == PCM_PLAYBACK &&
1006 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001007 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001008 }
1009 }
1010
1011 return 0;
1012}
1013
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001014static int stop_output_stream(struct stream_out *out)
1015{
1016 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001017 struct audio_usecase *uc_info;
1018 struct audio_device *adev = out->dev;
1019
Eric Laurent994a6932013-07-17 11:51:42 -07001020 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001021 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001022 uc_info = get_usecase_from_list(adev, out->usecase);
1023 if (uc_info == NULL) {
1024 ALOGE("%s: Could not find the usecase (%d) in the list",
1025 __func__, out->usecase);
1026 return -EINVAL;
1027 }
1028
Haynes Mathew George41f86652014-06-17 14:22:15 -07001029 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1030 if (adev->visualizer_stop_output != NULL)
1031 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1032 if (adev->offload_effects_stop_output != NULL)
1033 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1034 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001035
Eric Laurent150dbfe2013-02-27 14:31:02 -08001036 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001037 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001038
1039 /* 2. Disable the rx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001040 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001041
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001042 list_remove(&uc_info->list);
1043 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001044
Eric Laurent0499d4f2014-08-25 22:39:29 -05001045 audio_extn_extspk_update(adev->extspk);
1046
Eric Laurent07eeafd2013-10-06 12:52:49 -07001047 /* Must be called after removing the usecase from list */
1048 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1049 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1050
Eric Laurent994a6932013-07-17 11:51:42 -07001051 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001052 return ret;
1053}
1054
1055int start_output_stream(struct stream_out *out)
1056{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001057 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001058 struct audio_usecase *uc_info;
1059 struct audio_device *adev = out->dev;
1060
Eric Laurent994a6932013-07-17 11:51:42 -07001061 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001062 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001063 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001064 if (out->pcm_device_id < 0) {
1065 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1066 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001067 ret = -EINVAL;
1068 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001069 }
1070
1071 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1072 uc_info->id = out->usecase;
1073 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001074 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001075 uc_info->devices = out->devices;
1076 uc_info->in_snd_device = SND_DEVICE_NONE;
1077 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001078
Eric Laurent07eeafd2013-10-06 12:52:49 -07001079 /* This must be called before adding this usecase to the list */
1080 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1081 check_and_set_hdmi_channels(adev, out->config.channels);
1082
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001083 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001084
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001085 select_devices(adev, out->usecase);
1086
Eric Laurent0499d4f2014-08-25 22:39:29 -05001087 audio_extn_extspk_update(adev->extspk);
1088
Andy Hung31aca912014-03-20 17:14:59 -07001089 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001090 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001091 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001092 unsigned int flags = PCM_OUT;
1093 unsigned int pcm_open_retry_count = 0;
1094 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1095 flags |= PCM_MMAP | PCM_NOIRQ;
1096 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1097 } else
1098 flags |= PCM_MONOTONIC;
1099
1100 while (1) {
1101 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1102 flags, &out->config);
1103 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
1104 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1105 if (out->pcm != NULL) {
1106 pcm_close(out->pcm);
1107 out->pcm = NULL;
1108 }
1109 if (pcm_open_retry_count-- == 0) {
1110 ret = -EIO;
1111 goto error_open;
1112 }
1113 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1114 continue;
1115 }
1116 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001117 }
1118 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001119 out->pcm = NULL;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001120 out->compr = compress_open(adev->snd_card, out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001121 COMPRESS_IN, &out->compr_config);
1122 if (out->compr && !is_compress_ready(out->compr)) {
1123 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1124 compress_close(out->compr);
1125 out->compr = NULL;
1126 ret = -EIO;
1127 goto error_open;
1128 }
1129 if (out->offload_callback)
1130 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001131
1132 if (adev->visualizer_start_output != NULL)
Haynes Mathew George41f86652014-06-17 14:22:15 -07001133 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1134 if (adev->offload_effects_start_output != NULL)
1135 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001136 }
Eric Laurent994a6932013-07-17 11:51:42 -07001137 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001138 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001139error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001140 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001141error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001142 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001143}
1144
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001145static int check_input_parameters(uint32_t sample_rate,
1146 audio_format_t format,
1147 int channel_count)
1148{
1149 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1150
1151 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1152
1153 switch (sample_rate) {
1154 case 8000:
1155 case 11025:
1156 case 12000:
1157 case 16000:
1158 case 22050:
1159 case 24000:
1160 case 32000:
1161 case 44100:
1162 case 48000:
1163 break;
1164 default:
1165 return -EINVAL;
1166 }
1167
1168 return 0;
1169}
1170
1171static size_t get_input_buffer_size(uint32_t sample_rate,
1172 audio_format_t format,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001173 int channel_count,
1174 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001175{
1176 size_t size = 0;
1177
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001178 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1179 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001180
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001181 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001182 if (is_low_latency)
Glenn Kasten4f993392014-05-14 07:30:48 -07001183 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001184 /* ToDo: should use frame_size computed based on the format and
1185 channel_count here. */
1186 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001187
Glenn Kasten4f993392014-05-14 07:30:48 -07001188 /* make sure the size is multiple of 32 bytes
1189 * At 48 kHz mono 16-bit PCM:
1190 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1191 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1192 */
1193 size += 0x1f;
1194 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001195
1196 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001197}
1198
1199static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1200{
1201 struct stream_out *out = (struct stream_out *)stream;
1202
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001203 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001204}
1205
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001206static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001207{
1208 return -ENOSYS;
1209}
1210
1211static size_t out_get_buffer_size(const struct audio_stream *stream)
1212{
1213 struct stream_out *out = (struct stream_out *)stream;
1214
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001215 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1216 return out->compr_config.fragment_size;
1217 }
Eric Laurentfdf296a2014-07-03 16:41:51 -07001218 return out->config.period_size *
1219 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001220}
1221
1222static uint32_t out_get_channels(const struct audio_stream *stream)
1223{
1224 struct stream_out *out = (struct stream_out *)stream;
1225
1226 return out->channel_mask;
1227}
1228
1229static audio_format_t out_get_format(const struct audio_stream *stream)
1230{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001231 struct stream_out *out = (struct stream_out *)stream;
1232
1233 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001234}
1235
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001236static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001237{
1238 return -ENOSYS;
1239}
1240
1241static int out_standby(struct audio_stream *stream)
1242{
1243 struct stream_out *out = (struct stream_out *)stream;
1244 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001245
Eric Laurent994a6932013-07-17 11:51:42 -07001246 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001247 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001248
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001249 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001250 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001251 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001252 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001253 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1254 if (out->pcm) {
1255 pcm_close(out->pcm);
1256 out->pcm = NULL;
1257 }
1258 } else {
1259 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001260 out->gapless_mdata.encoder_delay = 0;
1261 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001262 if (out->compr != NULL) {
1263 compress_close(out->compr);
1264 out->compr = NULL;
1265 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001266 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001267 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001268 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001269 }
1270 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001271 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001272 return 0;
1273}
1274
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001275static int out_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001276{
1277 return 0;
1278}
1279
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001280static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1281{
1282 int ret = 0;
1283 char value[32];
1284 struct compr_gapless_mdata tmp_mdata;
1285
1286 if (!out || !parms) {
1287 return -EINVAL;
1288 }
1289
1290 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1291 if (ret >= 0) {
1292 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1293 } else {
1294 return -EINVAL;
1295 }
1296
1297 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1298 if (ret >= 0) {
1299 tmp_mdata.encoder_padding = atoi(value);
1300 } else {
1301 return -EINVAL;
1302 }
1303
1304 out->gapless_mdata = tmp_mdata;
1305 out->send_new_metadata = 1;
1306 ALOGV("%s new encoder delay %u and padding %u", __func__,
1307 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1308
1309 return 0;
1310}
1311
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001312static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1313{
1314 return out == adev->primary_output || out == adev->voice_tx_output;
1315}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001316
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001317static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1318{
1319 struct stream_out *out = (struct stream_out *)stream;
1320 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001321 struct audio_usecase *usecase;
1322 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001323 struct str_parms *parms;
1324 char value[32];
1325 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001326 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001327 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001328
sangwoobc677242013-08-08 16:53:43 +09001329 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001330 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001331 parms = str_parms_create_str(kvpairs);
1332 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1333 if (ret >= 0) {
1334 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001335 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001336 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001338 /*
1339 * When HDMI cable is unplugged the music playback is paused and
1340 * the policy manager sends routing=0. But the audioflinger
1341 * continues to write data until standby time (3sec).
1342 * As the HDMI core is turned off, the write gets blocked.
1343 * Avoid this by routing audio to speaker until standby.
1344 */
1345 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1346 val == AUDIO_DEVICE_NONE) {
1347 val = AUDIO_DEVICE_OUT_SPEAKER;
1348 }
1349
1350 /*
1351 * select_devices() call below switches all the usecases on the same
1352 * backend to the new device. Refer to check_usecases_codec_backend() in
1353 * the select_devices(). But how do we undo this?
1354 *
1355 * For example, music playback is active on headset (deep-buffer usecase)
1356 * and if we go to ringtones and select a ringtone, low-latency usecase
1357 * will be started on headset+speaker. As we can't enable headset+speaker
1358 * and headset devices at the same time, select_devices() switches the music
1359 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1360 * So when the ringtone playback is completed, how do we undo the same?
1361 *
1362 * We are relying on the out_set_parameters() call on deep-buffer output,
1363 * once the ringtone playback is ended.
1364 * NOTE: We should not check if the current devices are same as new devices.
1365 * Because select_devices() must be called to switch back the music
1366 * playback to headset.
1367 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001368 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001369 out->devices = val;
1370
1371 if (!out->standby)
1372 select_devices(adev, out->usecase);
1373
Eric Laurenta7657192014-10-09 21:09:33 -07001374 if (output_drives_call(adev, out)) {
1375 if (!voice_is_in_call(adev)) {
1376 if (adev->mode == AUDIO_MODE_IN_CALL) {
1377 adev->current_call_output = out;
1378 ret = voice_start_call(adev);
1379 }
1380 } else {
1381 adev->current_call_output = out;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001382 voice_update_devices_for_all_voice_usecases(adev);
Eric Laurenta7657192014-10-09 21:09:33 -07001383 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001384 }
1385 }
1386
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001387 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001388 pthread_mutex_unlock(&out->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05001389
1390 /*handles device and call state changes*/
1391 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001392 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001393
1394 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1395 parse_compress_metadata(out, parms);
1396 }
1397
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001398 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001399 ALOGV("%s: exit: code(%d)", __func__, status);
1400 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001401}
1402
1403static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1404{
1405 struct stream_out *out = (struct stream_out *)stream;
1406 struct str_parms *query = str_parms_create_str(keys);
1407 char *str;
1408 char value[256];
1409 struct str_parms *reply = str_parms_create();
1410 size_t i, j;
1411 int ret;
1412 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001413 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001414 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1415 if (ret >= 0) {
1416 value[0] = '\0';
1417 i = 0;
1418 while (out->supported_channel_masks[i] != 0) {
1419 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1420 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1421 if (!first) {
1422 strcat(value, "|");
1423 }
1424 strcat(value, out_channels_name_to_enum_table[j].name);
1425 first = false;
1426 break;
1427 }
1428 }
1429 i++;
1430 }
1431 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1432 str = str_parms_to_str(reply);
1433 } else {
1434 str = strdup(keys);
1435 }
1436 str_parms_destroy(query);
1437 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001438 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001439 return str;
1440}
1441
1442static uint32_t out_get_latency(const struct audio_stream_out *stream)
1443{
1444 struct stream_out *out = (struct stream_out *)stream;
1445
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001446 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1447 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1448
1449 return (out->config.period_count * out->config.period_size * 1000) /
1450 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001451}
1452
1453static int out_set_volume(struct audio_stream_out *stream, float left,
1454 float right)
1455{
Eric Laurenta9024de2013-04-04 09:19:12 -07001456 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001457 int volume[2];
1458
Eric Laurenta9024de2013-04-04 09:19:12 -07001459 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1460 /* only take left channel into account: the API is for stereo anyway */
1461 out->muted = (left == 0.0f);
1462 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001463 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1464 const char *mixer_ctl_name = "Compress Playback Volume";
1465 struct audio_device *adev = out->dev;
1466 struct mixer_ctl *ctl;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001467 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1468 if (!ctl) {
Haynes Mathew George20bcfa82014-06-25 13:37:03 -07001469 /* try with the control based on device id */
1470 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1471 PCM_PLAYBACK);
1472 char ctl_name[128] = {0};
1473 snprintf(ctl_name, sizeof(ctl_name),
1474 "Compress Playback %d Volume", pcm_device_id);
1475 ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name);
1476 if (!ctl) {
1477 ALOGE("%s: Could not get volume ctl mixer cmd", __func__);
1478 return -EINVAL;
1479 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001480 }
1481 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1482 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1483 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1484 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001485 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001486
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001487 return -ENOSYS;
1488}
1489
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001490#ifdef NO_AUDIO_OUT
1491static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
1492 const void *buffer, size_t bytes)
1493{
1494 struct stream_out *out = (struct stream_out *)stream;
1495
1496 /* No Output device supported other than BT for playback.
1497 * Sleep for the amount of buffer duration
1498 */
1499 pthread_mutex_lock(&out->lock);
1500 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1501 out_get_sample_rate(&out->stream.common));
1502 pthread_mutex_unlock(&out->lock);
1503 return bytes;
1504}
1505#endif
1506
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001507static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1508 size_t bytes)
1509{
1510 struct stream_out *out = (struct stream_out *)stream;
1511 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001512 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001513
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001514 pthread_mutex_lock(&out->lock);
1515 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001516 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001517 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001518 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001519 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001520 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001521 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001522 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001523 goto exit;
1524 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001525 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001526
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001527 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001528 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1529 if (out->send_new_metadata) {
1530 ALOGVV("send new gapless metadata");
1531 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1532 out->send_new_metadata = 0;
1533 }
1534
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001535 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001536 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001537 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001538 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1539 }
1540 if (!out->playback_started) {
1541 compress_start(out->compr);
1542 out->playback_started = 1;
1543 out->offload_state = OFFLOAD_STATE_PLAYING;
1544 }
1545 pthread_mutex_unlock(&out->lock);
1546 return ret;
1547 } else {
1548 if (out->pcm) {
1549 if (out->muted)
1550 memset((void *)buffer, 0, bytes);
1551 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001552 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1553 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
1554 }
1555 else
1556 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001557 if (ret == 0)
1558 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001559 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001560 }
1561
1562exit:
1563 pthread_mutex_unlock(&out->lock);
1564
1565 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001566 if (out->pcm)
1567 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001568 out_standby(&out->stream.common);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001569 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001570 out_get_sample_rate(&out->stream.common));
1571 }
1572 return bytes;
1573}
1574
1575static int out_get_render_position(const struct audio_stream_out *stream,
1576 uint32_t *dsp_frames)
1577{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001578 struct stream_out *out = (struct stream_out *)stream;
1579 *dsp_frames = 0;
1580 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1581 pthread_mutex_lock(&out->lock);
1582 if (out->compr != NULL) {
1583 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1584 &out->sample_rate);
1585 ALOGVV("%s rendered frames %d sample_rate %d",
1586 __func__, *dsp_frames, out->sample_rate);
1587 }
1588 pthread_mutex_unlock(&out->lock);
1589 return 0;
1590 } else
1591 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001592}
1593
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001594static int out_add_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_remove_audio_effect(const struct audio_stream *stream __unused,
1601 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001602{
1603 return 0;
1604}
1605
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001606static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1607 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001608{
1609 return -EINVAL;
1610}
1611
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001612static int out_get_presentation_position(const struct audio_stream_out *stream,
1613 uint64_t *frames, struct timespec *timestamp)
1614{
1615 struct stream_out *out = (struct stream_out *)stream;
1616 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001617 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001618
1619 pthread_mutex_lock(&out->lock);
1620
Eric Laurent949a0892013-09-20 09:20:13 -07001621 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1622 if (out->compr != NULL) {
1623 compress_get_tstamp(out->compr, &dsp_frames,
1624 &out->sample_rate);
1625 ALOGVV("%s rendered frames %ld sample_rate %d",
1626 __func__, dsp_frames, out->sample_rate);
1627 *frames = dsp_frames;
1628 ret = 0;
1629 /* this is the best we can do */
1630 clock_gettime(CLOCK_MONOTONIC, timestamp);
1631 }
1632 } else {
1633 if (out->pcm) {
1634 size_t avail;
1635 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1636 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001637 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001638 // This adjustment accounts for buffering after app processor.
1639 // It is based on estimated DSP latency per use case, rather than exact.
1640 signed_frames -=
1641 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1642
Eric Laurent949a0892013-09-20 09:20:13 -07001643 // It would be unusual for this value to be negative, but check just in case ...
1644 if (signed_frames >= 0) {
1645 *frames = signed_frames;
1646 ret = 0;
1647 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001648 }
1649 }
1650 }
1651
1652 pthread_mutex_unlock(&out->lock);
1653
1654 return ret;
1655}
1656
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001657static int out_set_callback(struct audio_stream_out *stream,
1658 stream_callback_t callback, void *cookie)
1659{
1660 struct stream_out *out = (struct stream_out *)stream;
1661
1662 ALOGV("%s", __func__);
1663 pthread_mutex_lock(&out->lock);
1664 out->offload_callback = callback;
1665 out->offload_cookie = cookie;
1666 pthread_mutex_unlock(&out->lock);
1667 return 0;
1668}
1669
1670static int out_pause(struct audio_stream_out* stream)
1671{
1672 struct stream_out *out = (struct stream_out *)stream;
1673 int status = -ENOSYS;
1674 ALOGV("%s", __func__);
1675 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1676 pthread_mutex_lock(&out->lock);
1677 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1678 status = compress_pause(out->compr);
1679 out->offload_state = OFFLOAD_STATE_PAUSED;
1680 }
1681 pthread_mutex_unlock(&out->lock);
1682 }
1683 return status;
1684}
1685
1686static int out_resume(struct audio_stream_out* stream)
1687{
1688 struct stream_out *out = (struct stream_out *)stream;
1689 int status = -ENOSYS;
1690 ALOGV("%s", __func__);
1691 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1692 status = 0;
1693 pthread_mutex_lock(&out->lock);
1694 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1695 status = compress_resume(out->compr);
1696 out->offload_state = OFFLOAD_STATE_PLAYING;
1697 }
1698 pthread_mutex_unlock(&out->lock);
1699 }
1700 return status;
1701}
1702
1703static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1704{
1705 struct stream_out *out = (struct stream_out *)stream;
1706 int status = -ENOSYS;
1707 ALOGV("%s", __func__);
1708 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1709 pthread_mutex_lock(&out->lock);
1710 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1711 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1712 else
1713 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1714 pthread_mutex_unlock(&out->lock);
1715 }
1716 return status;
1717}
1718
1719static int out_flush(struct audio_stream_out* stream)
1720{
1721 struct stream_out *out = (struct stream_out *)stream;
1722 ALOGV("%s", __func__);
1723 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1724 pthread_mutex_lock(&out->lock);
1725 stop_compressed_output_l(out);
1726 pthread_mutex_unlock(&out->lock);
1727 return 0;
1728 }
1729 return -ENOSYS;
1730}
1731
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001732/** audio_stream_in implementation **/
1733static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1734{
1735 struct stream_in *in = (struct stream_in *)stream;
1736
1737 return in->config.rate;
1738}
1739
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001740static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001741{
1742 return -ENOSYS;
1743}
1744
1745static size_t in_get_buffer_size(const struct audio_stream *stream)
1746{
1747 struct stream_in *in = (struct stream_in *)stream;
1748
Eric Laurentfdf296a2014-07-03 16:41:51 -07001749 return in->config.period_size *
1750 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001751}
1752
1753static uint32_t in_get_channels(const struct audio_stream *stream)
1754{
1755 struct stream_in *in = (struct stream_in *)stream;
1756
1757 return in->channel_mask;
1758}
1759
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001760static audio_format_t in_get_format(const struct audio_stream *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001761{
1762 return AUDIO_FORMAT_PCM_16_BIT;
1763}
1764
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001765static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001766{
1767 return -ENOSYS;
1768}
1769
1770static int in_standby(struct audio_stream *stream)
1771{
1772 struct stream_in *in = (struct stream_in *)stream;
1773 struct audio_device *adev = in->dev;
1774 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001775 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001776 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001777
1778 if (!in->standby && in->is_st_session) {
1779 ALOGD("%s: sound trigger pcm stop lab", __func__);
1780 audio_extn_sound_trigger_stop_lab(in);
1781 in->standby = true;
1782 }
1783
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001784 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001785 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001786 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001787 if (in->pcm) {
1788 pcm_close(in->pcm);
1789 in->pcm = NULL;
1790 }
Eric Laurentcefbbac2014-09-04 13:54:10 -05001791 adev->enable_voicerx = false;
1792 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE );
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001793 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001794 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001795 }
1796 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001797 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001798 return status;
1799}
1800
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001801static int in_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001802{
1803 return 0;
1804}
1805
1806static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1807{
1808 struct stream_in *in = (struct stream_in *)stream;
1809 struct audio_device *adev = in->dev;
1810 struct str_parms *parms;
1811 char *str;
1812 char value[32];
1813 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07001814 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001815
Eric Laurent994a6932013-07-17 11:51:42 -07001816 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001817 parms = str_parms_create_str(kvpairs);
1818
1819 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1820
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001821 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001822 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001823 if (ret >= 0) {
1824 val = atoi(value);
1825 /* no audio source uses val == 0 */
1826 if ((in->source != val) && (val != 0)) {
1827 in->source = val;
1828 }
1829 }
1830
1831 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07001832
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001833 if (ret >= 0) {
1834 val = atoi(value);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001835 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001836 in->device = val;
1837 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001838 if (!in->standby)
Eric Laurent03f09432014-03-25 18:09:11 -07001839 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001840 }
1841 }
1842
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001843 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001844 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001845
1846 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001847 ALOGV("%s: exit: status(%d)", __func__, status);
1848 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001849}
1850
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001851static char* in_get_parameters(const struct audio_stream *stream __unused,
1852 const char *keys __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001853{
1854 return strdup("");
1855}
1856
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001857static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001858{
1859 return 0;
1860}
1861
1862static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1863 size_t bytes)
1864{
1865 struct stream_in *in = (struct stream_in *)stream;
1866 struct audio_device *adev = in->dev;
1867 int i, ret = -1;
1868
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001869 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001870 if (in->is_st_session) {
1871 ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
1872 /* Read from sound trigger HAL */
1873 audio_extn_sound_trigger_read(in, buffer, bytes);
1874 pthread_mutex_unlock(&in->lock);
1875 return bytes;
1876 }
1877
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001878 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001879 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001880 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001881 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001882 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001883 goto exit;
1884 }
1885 in->standby = 0;
1886 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001887
1888 if (in->pcm) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001889 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1890 ret = pcm_mmap_read(in->pcm, buffer, bytes);
1891 } else
1892 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001893 }
1894
1895 /*
1896 * Instead of writing zeroes here, we could trust the hardware
1897 * to always provide zeroes when muted.
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001898 * 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 -08001899 */
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001900 if (ret == 0 && adev->mic_muted && in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001901 memset(buffer, 0, bytes);
1902
1903exit:
1904 pthread_mutex_unlock(&in->lock);
1905
1906 if (ret != 0) {
1907 in_standby(&in->stream.common);
1908 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001909 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001910 in_get_sample_rate(&in->stream.common));
1911 }
1912 return bytes;
1913}
1914
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001915static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001916{
1917 return 0;
1918}
1919
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001920static int add_remove_audio_effect(const struct audio_stream *stream,
1921 effect_handle_t effect,
1922 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001923{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001924 struct stream_in *in = (struct stream_in *)stream;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001925 struct audio_device *adev = in->dev;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001926 int status = 0;
1927 effect_descriptor_t desc;
1928
1929 status = (*effect)->get_descriptor(effect, &desc);
1930 if (status != 0)
1931 return status;
1932
1933 pthread_mutex_lock(&in->lock);
1934 pthread_mutex_lock(&in->dev->lock);
1935 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1936 in->enable_aec != enable &&
1937 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1938 in->enable_aec = enable;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001939 if (!enable)
1940 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
1941 adev->enable_voicerx = enable;
1942 struct audio_usecase *usecase;
1943 struct listnode *node;
1944 list_for_each(node, &adev->usecase_list) {
1945 usecase = node_to_item(node, struct audio_usecase, list);
1946 if (usecase->type == PCM_PLAYBACK) {
1947 select_devices(adev, usecase->id);
1948 break;
1949 }
1950 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001951 if (!in->standby)
1952 select_devices(in->dev, in->usecase);
1953 }
Ravi Kumar Alamanda3ad4e1b2014-06-03 00:08:15 -07001954 if (in->enable_ns != enable &&
1955 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1956 in->enable_ns = enable;
1957 if (!in->standby)
1958 select_devices(in->dev, in->usecase);
1959 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001960 pthread_mutex_unlock(&in->dev->lock);
1961 pthread_mutex_unlock(&in->lock);
1962
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001963 return 0;
1964}
1965
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001966static int in_add_audio_effect(const struct audio_stream *stream,
1967 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001968{
Eric Laurent994a6932013-07-17 11:51:42 -07001969 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001970 return add_remove_audio_effect(stream, effect, true);
1971}
1972
1973static int in_remove_audio_effect(const struct audio_stream *stream,
1974 effect_handle_t effect)
1975{
Eric Laurent994a6932013-07-17 11:51:42 -07001976 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001977 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001978}
1979
1980static int adev_open_output_stream(struct audio_hw_device *dev,
1981 audio_io_handle_t handle,
1982 audio_devices_t devices,
1983 audio_output_flags_t flags,
1984 struct audio_config *config,
Eric Laurent91975a62014-07-27 17:15:50 -07001985 struct audio_stream_out **stream_out,
1986 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001987{
1988 struct audio_device *adev = (struct audio_device *)dev;
1989 struct stream_out *out;
1990 int i, ret;
1991
Eric Laurent994a6932013-07-17 11:51:42 -07001992 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001993 __func__, config->sample_rate, config->channel_mask, devices, flags);
1994 *stream_out = NULL;
1995 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1996
1997 if (devices == AUDIO_DEVICE_NONE)
1998 devices = AUDIO_DEVICE_OUT_SPEAKER;
1999
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 out->flags = flags;
2001 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002002 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002003 out->format = config->format;
2004 out->sample_rate = config->sample_rate;
2005 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2006 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002007 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002008
2009 /* Init use case and pcm_config */
2010 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07002011 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002012 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002013 pthread_mutex_lock(&adev->lock);
2014 ret = read_hdmi_channel_masks(out);
2015 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002016 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002017 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002018
2019 if (config->sample_rate == 0)
2020 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2021 if (config->channel_mask == 0)
2022 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2023
2024 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002025 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002026 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2027 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002028 out->config.rate = config->sample_rate;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002029 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002030 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002031 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2032 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2033 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2034 ALOGE("%s: Unsupported Offload information", __func__);
2035 ret = -EINVAL;
2036 goto error_open;
2037 }
2038 if (!is_supported_format(config->offload_info.format)) {
2039 ALOGE("%s: Unsupported audio format", __func__);
2040 ret = -EINVAL;
2041 goto error_open;
2042 }
2043
2044 out->compr_config.codec = (struct snd_codec *)
2045 calloc(1, sizeof(struct snd_codec));
2046
2047 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2048 if (config->offload_info.channel_mask)
2049 out->channel_mask = config->offload_info.channel_mask;
2050 else if (config->channel_mask)
2051 out->channel_mask = config->channel_mask;
2052 out->format = config->offload_info.format;
2053 out->sample_rate = config->offload_info.sample_rate;
2054
2055 out->stream.set_callback = out_set_callback;
2056 out->stream.pause = out_pause;
2057 out->stream.resume = out_resume;
2058 out->stream.drain = out_drain;
2059 out->stream.flush = out_flush;
2060
2061 out->compr_config.codec->id =
2062 get_snd_codec_id(config->offload_info.format);
2063 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2064 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
Eric Laurent1ccf3972014-10-23 14:42:59 -07002065 out->compr_config.codec->sample_rate = config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002066 out->compr_config.codec->bit_rate =
2067 config->offload_info.bit_rate;
2068 out->compr_config.codec->ch_in =
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002069 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002070 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2071
2072 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2073 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002074
2075 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002076 create_offload_callback_thread(out);
2077 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2078 __func__, config->offload_info.version,
2079 config->offload_info.bit_rate);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002080 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2081 if (config->sample_rate == 0)
2082 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2083 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2084 config->sample_rate != 8000) {
2085 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2086 ret = -EINVAL;
2087 goto error_open;
2088 }
2089 out->sample_rate = config->sample_rate;
2090 out->config.rate = config->sample_rate;
2091 if (config->format == AUDIO_FORMAT_DEFAULT)
2092 config->format = AUDIO_FORMAT_PCM_16_BIT;
2093 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2094 config->format = AUDIO_FORMAT_PCM_16_BIT;
2095 ret = -EINVAL;
2096 goto error_open;
2097 }
2098 out->format = config->format;
2099 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2100 out->config = pcm_config_afe_proxy_playback;
2101 adev->voice_tx_output = out;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002102 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07002103 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2104 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2105 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamandaf78a4d92015-04-24 15:18:23 -07002106 } else if (out->flags & AUDIO_OUTPUT_FLAG_TTS) {
2107 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
2108 out->config = pcm_config_deep_buffer;
Andy Hung6fcba9c2014-03-18 11:53:32 -07002109 } else {
2110 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2111 out->config = pcm_config_low_latency;
2112 }
2113 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2114 if (k_enable_extended_precision
2115 && pcm_params_format_test(adev->use_case_table[out->usecase],
2116 pcm_format_from_audio_format(config->format))) {
2117 out->config.format = pcm_format_from_audio_format(config->format);
2118 /* out->format already set to config->format */
2119 } else {
2120 /* deny the externally proposed config format
2121 * and use the one specified in audio_hw layer configuration.
2122 * Note: out->format is returned by out->stream.common.get_format()
2123 * and is used to set config->format in the code several lines below.
2124 */
2125 out->format = audio_format_from_pcm_format(out->config.format);
2126 }
2127 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002128 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002129 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07002130 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
2131 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002132
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002133 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002134 if (adev->primary_output == NULL)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002135 adev->primary_output = out;
2136 else {
2137 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002138 ret = -EEXIST;
2139 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002140 }
2141 }
2142
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002143 /* Check if this usecase is already existing */
2144 pthread_mutex_lock(&adev->lock);
2145 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2146 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002147 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002148 ret = -EEXIST;
2149 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002150 }
2151 pthread_mutex_unlock(&adev->lock);
2152
2153 out->stream.common.get_sample_rate = out_get_sample_rate;
2154 out->stream.common.set_sample_rate = out_set_sample_rate;
2155 out->stream.common.get_buffer_size = out_get_buffer_size;
2156 out->stream.common.get_channels = out_get_channels;
2157 out->stream.common.get_format = out_get_format;
2158 out->stream.common.set_format = out_set_format;
2159 out->stream.common.standby = out_standby;
2160 out->stream.common.dump = out_dump;
2161 out->stream.common.set_parameters = out_set_parameters;
2162 out->stream.common.get_parameters = out_get_parameters;
2163 out->stream.common.add_audio_effect = out_add_audio_effect;
2164 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2165 out->stream.get_latency = out_get_latency;
2166 out->stream.set_volume = out_set_volume;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002167#ifdef NO_AUDIO_OUT
2168 out->stream.write = out_write_for_no_output;
2169#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002170 out->stream.write = out_write;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002171#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002172 out->stream.get_render_position = out_get_render_position;
2173 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002174 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002175
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002176 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002177 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002178 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002179
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002180 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2181 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2182
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002183 config->format = out->stream.common.get_format(&out->stream.common);
2184 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2185 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2186
2187 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002188 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002189 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002190
2191error_open:
2192 free(out);
2193 *stream_out = NULL;
2194 ALOGD("%s: exit: ret %d", __func__, ret);
2195 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002196}
2197
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002198static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002199 struct audio_stream_out *stream)
2200{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002201 struct stream_out *out = (struct stream_out *)stream;
2202 struct audio_device *adev = out->dev;
2203
Eric Laurent994a6932013-07-17 11:51:42 -07002204 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002205 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002206 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2207 destroy_offload_callback_thread(out);
2208
2209 if (out->compr_config.codec != NULL)
2210 free(out->compr_config.codec);
2211 }
Ravi Kumar Alamandaa4fc9022014-10-08 18:57:46 -07002212
2213 if (adev->voice_tx_output == out)
2214 adev->voice_tx_output = NULL;
2215
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002216 pthread_cond_destroy(&out->cond);
2217 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002218 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002219 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002220}
2221
2222static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2223{
2224 struct audio_device *adev = (struct audio_device *)dev;
2225 struct str_parms *parms;
2226 char *str;
2227 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002228 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002229 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002230 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002231
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002232 ALOGD("%s: enter: %s", __func__, kvpairs);
2233
2234 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002235
2236 parms = str_parms_create_str(kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002237 status = voice_set_parameters(adev, parms);
2238 if (status != 0) {
2239 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002240 }
2241
2242 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2243 if (ret >= 0) {
2244 /* When set to false, HAL should disable EC and NS
2245 * But it is currently not supported.
2246 */
2247 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2248 adev->bluetooth_nrec = true;
2249 else
2250 adev->bluetooth_nrec = false;
2251 }
2252
2253 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2254 if (ret >= 0) {
2255 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2256 adev->screen_off = false;
2257 else
2258 adev->screen_off = true;
2259 }
2260
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002261 ret = str_parms_get_int(parms, "rotation", &val);
2262 if (ret >= 0) {
2263 bool reverse_speakers = false;
2264 switch(val) {
2265 // FIXME: note that the code below assumes that the speakers are in the correct placement
2266 // relative to the user when the device is rotated 90deg from its default rotation. This
2267 // assumption is device-specific, not platform-specific like this code.
2268 case 270:
2269 reverse_speakers = true;
2270 break;
2271 case 0:
2272 case 90:
2273 case 180:
2274 break;
2275 default:
2276 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002277 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002278 }
Eric Laurent03f09432014-03-25 18:09:11 -07002279 if (status == 0) {
Ravi Kumar Alamanda1f60cf82015-04-23 19:45:17 -07002280 platform_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002281 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002282 }
2283
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002284 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2285 if (ret >= 0) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002286 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002287 }
2288
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08002289 audio_extn_hfp_set_parameters(adev, parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002290done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002291 str_parms_destroy(parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002292 pthread_mutex_unlock(&adev->lock);
Eric Laurent03f09432014-03-25 18:09:11 -07002293 ALOGV("%s: exit with code(%d)", __func__, status);
2294 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002295}
2296
2297static char* adev_get_parameters(const struct audio_hw_device *dev,
2298 const char *keys)
2299{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002300 struct audio_device *adev = (struct audio_device *)dev;
2301 struct str_parms *reply = str_parms_create();
2302 struct str_parms *query = str_parms_create_str(keys);
2303 char *str;
2304
2305 pthread_mutex_lock(&adev->lock);
2306
2307 voice_get_parameters(adev, query, reply);
2308 str = str_parms_to_str(reply);
2309 str_parms_destroy(query);
2310 str_parms_destroy(reply);
2311
2312 pthread_mutex_unlock(&adev->lock);
2313 ALOGV("%s: exit: returns - %s", __func__, str);
2314 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002315}
2316
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002317static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002318{
2319 return 0;
2320}
2321
Haynes Mathew George5191a852013-09-11 14:19:36 -07002322static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2323{
2324 int ret;
2325 struct audio_device *adev = (struct audio_device *)dev;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002326
Eric Laurent4cc4ce12014-09-10 13:21:01 -05002327 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
2328
Haynes Mathew George5191a852013-09-11 14:19:36 -07002329 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002330 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002331 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002332
Haynes Mathew George5191a852013-09-11 14:19:36 -07002333 return ret;
2334}
2335
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002336static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002337{
2338 return -ENOSYS;
2339}
2340
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002341static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2342 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002343{
2344 return -ENOSYS;
2345}
2346
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002347static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002348{
2349 return -ENOSYS;
2350}
2351
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002352static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002353{
2354 return -ENOSYS;
2355}
2356
2357static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2358{
2359 struct audio_device *adev = (struct audio_device *)dev;
2360
2361 pthread_mutex_lock(&adev->lock);
2362 if (adev->mode != mode) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002363 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002364 adev->mode = mode;
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -07002365 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) &&
2366 voice_is_in_call(adev)) {
2367 voice_stop_call(adev);
2368 adev->current_call_output = NULL;
2369 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002370 }
2371 pthread_mutex_unlock(&adev->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002372
2373 audio_extn_extspk_set_mode(adev->extspk, mode);
2374
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002375 return 0;
2376}
2377
2378static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2379{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002380 int ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002381 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002382
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002383 ALOGD("%s: state %d\n", __func__, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002384 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002385 ret = voice_set_mic_mute(adev, state);
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07002386 adev->mic_muted = state;
Eric Laurenta609e8e2014-06-18 02:15:17 +00002387 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002388
2389 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002390}
2391
2392static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2393{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002394 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002395 return 0;
2396}
2397
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002398static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002399 const struct audio_config *config)
2400{
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002401 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002402
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002403 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
2404 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002405}
2406
2407static int adev_open_input_stream(struct audio_hw_device *dev,
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002408 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002409 audio_devices_t devices,
2410 struct audio_config *config,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002411 struct audio_stream_in **stream_in,
Eric Laurent91975a62014-07-27 17:15:50 -07002412 audio_input_flags_t flags,
2413 const char *address __unused,
Eric Laurentcefbbac2014-09-04 13:54:10 -05002414 audio_source_t source )
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002415{
2416 struct audio_device *adev = (struct audio_device *)dev;
2417 struct stream_in *in;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002418 int ret = 0, buffer_size, frame_size;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002419 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002420 bool is_low_latency = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002421
Eric Laurent994a6932013-07-17 11:51:42 -07002422 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002423 *stream_in = NULL;
2424 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2425 return -EINVAL;
2426
2427 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2428
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002429 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2430
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002431 in->stream.common.get_sample_rate = in_get_sample_rate;
2432 in->stream.common.set_sample_rate = in_set_sample_rate;
2433 in->stream.common.get_buffer_size = in_get_buffer_size;
2434 in->stream.common.get_channels = in_get_channels;
2435 in->stream.common.get_format = in_get_format;
2436 in->stream.common.set_format = in_set_format;
2437 in->stream.common.standby = in_standby;
2438 in->stream.common.dump = in_dump;
2439 in->stream.common.set_parameters = in_set_parameters;
2440 in->stream.common.get_parameters = in_get_parameters;
2441 in->stream.common.add_audio_effect = in_add_audio_effect;
2442 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2443 in->stream.set_gain = in_set_gain;
2444 in->stream.read = in_read;
2445 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2446
2447 in->device = devices;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002448 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002449 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002450 in->standby = 1;
2451 in->channel_mask = config->channel_mask;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002452 in->capture_handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002453
2454 /* Update config params with the requested sample rate and channels */
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002455 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
2456 if (config->sample_rate == 0)
2457 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2458 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2459 config->sample_rate != 8000) {
2460 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2461 ret = -EINVAL;
2462 goto err_open;
2463 }
2464 if (config->format == AUDIO_FORMAT_DEFAULT)
2465 config->format = AUDIO_FORMAT_PCM_16_BIT;
2466 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2467 config->format = AUDIO_FORMAT_PCM_16_BIT;
2468 ret = -EINVAL;
2469 goto err_open;
2470 }
2471
2472 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
2473 in->config = pcm_config_afe_proxy_record;
2474 } else {
2475 in->usecase = USECASE_AUDIO_RECORD;
2476 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
2477 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
2478 is_low_latency = true;
Glenn Kasten4f993392014-05-14 07:30:48 -07002479#if LOW_LATENCY_CAPTURE_USE_CASE
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002480 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Glenn Kasten4f993392014-05-14 07:30:48 -07002481#endif
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002482 }
2483 in->config = pcm_config_audio_capture;
2484
2485 frame_size = audio_stream_in_frame_size(&in->stream);
2486 buffer_size = get_input_buffer_size(config->sample_rate,
2487 config->format,
2488 channel_count,
2489 is_low_latency);
2490 in->config.period_size = buffer_size / frame_size;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002491 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002492 in->config.channels = channel_count;
2493 in->config.rate = config->sample_rate;
2494
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002495 /* This stream could be for sound trigger lab,
2496 get sound trigger pcm if present */
2497 audio_extn_sound_trigger_check_and_get_session(in);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002498
2499 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002500 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002501 return 0;
2502
2503err_open:
2504 free(in);
2505 *stream_in = NULL;
2506 return ret;
2507}
2508
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002509static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002510 struct audio_stream_in *stream)
2511{
Eric Laurent994a6932013-07-17 11:51:42 -07002512 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002513
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002514 in_standby(&stream->common);
2515 free(stream);
2516
2517 return;
2518}
2519
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002520static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002521{
2522 return 0;
2523}
2524
Andy Hung31aca912014-03-20 17:14:59 -07002525/* verifies input and output devices and their capabilities.
2526 *
2527 * This verification is required when enabling extended bit-depth or
2528 * sampling rates, as not all qcom products support it.
2529 *
2530 * Suitable for calling only on initialization such as adev_open().
2531 * It fills the audio_device use_case_table[] array.
2532 *
2533 * Has a side-effect that it needs to configure audio routing / devices
2534 * in order to power up the devices and read the device parameters.
2535 * It does not acquire any hw device lock. Should restore the devices
2536 * back to "normal state" upon completion.
2537 */
2538static int adev_verify_devices(struct audio_device *adev)
2539{
2540 /* enumeration is a bit difficult because one really wants to pull
2541 * the use_case, device id, etc from the hidden pcm_device_table[].
2542 * In this case there are the following use cases and device ids.
2543 *
2544 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
2545 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
2546 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
2547 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
2548 * [USECASE_AUDIO_RECORD] = {0, 0},
2549 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
2550 * [USECASE_VOICE_CALL] = {2, 2},
2551 *
2552 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
2553 * USECASE_VOICE_CALL omitted, but possible for either input or output.
2554 */
2555
2556 /* should be the usecases enabled in adev_open_input_stream() */
2557 static const int test_in_usecases[] = {
2558 USECASE_AUDIO_RECORD,
2559 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
2560 };
2561 /* should be the usecases enabled in adev_open_output_stream()*/
2562 static const int test_out_usecases[] = {
2563 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
2564 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
2565 };
2566 static const usecase_type_t usecase_type_by_dir[] = {
2567 PCM_PLAYBACK,
2568 PCM_CAPTURE,
2569 };
2570 static const unsigned flags_by_dir[] = {
2571 PCM_OUT,
2572 PCM_IN,
2573 };
2574
2575 size_t i;
2576 unsigned dir;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002577 const unsigned card_id = adev->snd_card;
Andy Hung31aca912014-03-20 17:14:59 -07002578 char info[512]; /* for possible debug info */
2579
2580 for (dir = 0; dir < 2; ++dir) {
2581 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
2582 const unsigned flags_dir = flags_by_dir[dir];
2583 const size_t testsize =
2584 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
2585 const int *testcases =
2586 dir ? test_in_usecases : test_out_usecases;
2587 const audio_devices_t audio_device =
2588 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
2589
2590 for (i = 0; i < testsize; ++i) {
2591 const audio_usecase_t audio_usecase = testcases[i];
2592 int device_id;
2593 snd_device_t snd_device;
2594 struct pcm_params **pparams;
2595 struct stream_out out;
2596 struct stream_in in;
2597 struct audio_usecase uc_info;
2598 int retval;
2599
2600 pparams = &adev->use_case_table[audio_usecase];
2601 pcm_params_free(*pparams); /* can accept null input */
2602 *pparams = NULL;
2603
2604 /* find the device ID for the use case (signed, for error) */
2605 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
2606 if (device_id < 0)
2607 continue;
2608
2609 /* prepare structures for device probing */
2610 memset(&uc_info, 0, sizeof(uc_info));
2611 uc_info.id = audio_usecase;
2612 uc_info.type = usecase_type;
2613 if (dir) {
2614 adev->active_input = &in;
2615 memset(&in, 0, sizeof(in));
2616 in.device = audio_device;
2617 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
2618 uc_info.stream.in = &in;
2619 } else {
2620 adev->active_input = NULL;
2621 }
2622 memset(&out, 0, sizeof(out));
2623 out.devices = audio_device; /* only field needed in select_devices */
2624 uc_info.stream.out = &out;
2625 uc_info.devices = audio_device;
2626 uc_info.in_snd_device = SND_DEVICE_NONE;
2627 uc_info.out_snd_device = SND_DEVICE_NONE;
2628 list_add_tail(&adev->usecase_list, &uc_info.list);
2629
2630 /* select device - similar to start_(in/out)put_stream() */
2631 retval = select_devices(adev, audio_usecase);
2632 if (retval >= 0) {
2633 *pparams = pcm_params_get(card_id, device_id, flags_dir);
2634#if LOG_NDEBUG == 0
2635 if (*pparams) {
2636 ALOGV("%s: (%s) card %d device %d", __func__,
2637 dir ? "input" : "output", card_id, device_id);
2638 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
2639 ALOGV(info); /* print parameters */
2640 } else {
2641 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
2642 }
2643#endif
2644 }
2645
2646 /* deselect device - similar to stop_(in/out)put_stream() */
2647 /* 1. Get and set stream specific mixer controls */
Glenn Kastene7137302014-04-28 15:12:18 -07002648 retval = disable_audio_route(adev, &uc_info);
Andy Hung31aca912014-03-20 17:14:59 -07002649 /* 2. Disable the rx device */
2650 retval = disable_snd_device(adev,
Glenn Kastene7137302014-04-28 15:12:18 -07002651 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
Andy Hung31aca912014-03-20 17:14:59 -07002652 list_remove(&uc_info.list);
2653 }
2654 }
2655 adev->active_input = NULL; /* restore adev state */
2656 return 0;
2657}
2658
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002659static int adev_close(hw_device_t *device)
2660{
Andy Hung31aca912014-03-20 17:14:59 -07002661 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002662 struct audio_device *adev = (struct audio_device *)device;
2663 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002664 free(adev->snd_dev_ref_cnt);
2665 platform_deinit(adev->platform);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002666 audio_extn_extspk_deinit(adev->extspk);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002667 audio_extn_sound_trigger_deinit(adev);
Andy Hung31aca912014-03-20 17:14:59 -07002668 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
2669 pcm_params_free(adev->use_case_table[i]);
2670 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002671 free(device);
2672 return 0;
2673}
2674
Glenn Kasten4f993392014-05-14 07:30:48 -07002675/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
2676 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
2677 * just that it _might_ work.
2678 */
2679static int period_size_is_plausible_for_low_latency(int period_size)
2680{
2681 switch (period_size) {
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002682 case 48:
2683 case 96:
2684 case 144:
Glenn Kasten4f993392014-05-14 07:30:48 -07002685 case 160:
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002686 case 192:
Glenn Kasten4f993392014-05-14 07:30:48 -07002687 case 240:
2688 case 320:
2689 case 480:
2690 return 1;
2691 default:
2692 return 0;
2693 }
2694}
2695
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002696static int adev_open(const hw_module_t *module, const char *name,
2697 hw_device_t **device)
2698{
2699 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002700 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002701
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002702 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002703 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2704
2705 adev = calloc(1, sizeof(struct audio_device));
2706
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002707 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2708
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002709 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2710 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2711 adev->device.common.module = (struct hw_module_t *)module;
2712 adev->device.common.close = adev_close;
2713
2714 adev->device.init_check = adev_init_check;
2715 adev->device.set_voice_volume = adev_set_voice_volume;
2716 adev->device.set_master_volume = adev_set_master_volume;
2717 adev->device.get_master_volume = adev_get_master_volume;
2718 adev->device.set_master_mute = adev_set_master_mute;
2719 adev->device.get_master_mute = adev_get_master_mute;
2720 adev->device.set_mode = adev_set_mode;
2721 adev->device.set_mic_mute = adev_set_mic_mute;
2722 adev->device.get_mic_mute = adev_get_mic_mute;
2723 adev->device.set_parameters = adev_set_parameters;
2724 adev->device.get_parameters = adev_get_parameters;
2725 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2726 adev->device.open_output_stream = adev_open_output_stream;
2727 adev->device.close_output_stream = adev_close_output_stream;
2728 adev->device.open_input_stream = adev_open_input_stream;
2729 adev->device.close_input_stream = adev_close_input_stream;
2730 adev->device.dump = adev_dump;
2731
2732 /* Set the default route before the PCM stream is opened */
2733 pthread_mutex_lock(&adev->lock);
2734 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002735 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002736 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002737 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002738 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002739 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002740 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002741 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002742 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002743 pthread_mutex_unlock(&adev->lock);
2744
2745 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002746 adev->platform = platform_init(adev);
2747 if (!adev->platform) {
2748 free(adev->snd_dev_ref_cnt);
2749 free(adev);
2750 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2751 *device = NULL;
2752 return -EINVAL;
2753 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002754
Eric Laurent0499d4f2014-08-25 22:39:29 -05002755 adev->extspk = audio_extn_extspk_init(adev);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002756 audio_extn_sound_trigger_init(adev);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002757
Eric Laurentc4aef752013-09-12 17:45:53 -07002758 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2759 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2760 if (adev->visualizer_lib == NULL) {
2761 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2762 } else {
2763 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2764 adev->visualizer_start_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002765 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002766 "visualizer_hal_start_output");
2767 adev->visualizer_stop_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002768 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002769 "visualizer_hal_stop_output");
2770 }
2771 }
2772
Haynes Mathew George41f86652014-06-17 14:22:15 -07002773 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2774 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2775 if (adev->offload_effects_lib == NULL) {
2776 ALOGE("%s: DLOPEN failed for %s", __func__,
2777 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2778 } else {
2779 ALOGV("%s: DLOPEN successful for %s", __func__,
2780 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2781 adev->offload_effects_start_output =
2782 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2783 "offload_effects_bundle_hal_start_output");
2784 adev->offload_effects_stop_output =
2785 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2786 "offload_effects_bundle_hal_stop_output");
2787 }
2788 }
2789
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002790 adev->bt_wb_speech_enabled = false;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002791 adev->enable_voicerx = false;
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002792
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002793 *device = &adev->device.common;
Andy Hung31aca912014-03-20 17:14:59 -07002794 if (k_enable_extended_precision)
2795 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002796
Glenn Kasten4f993392014-05-14 07:30:48 -07002797 char value[PROPERTY_VALUE_MAX];
2798 int trial;
2799 if (property_get("audio_hal.period_size", value, NULL) > 0) {
2800 trial = atoi(value);
2801 if (period_size_is_plausible_for_low_latency(trial)) {
2802 pcm_config_low_latency.period_size = trial;
2803 pcm_config_low_latency.start_threshold = trial / 4;
2804 pcm_config_low_latency.avail_min = trial / 4;
2805 configured_low_latency_capture_period_size = trial;
2806 }
2807 }
2808 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
2809 trial = atoi(value);
2810 if (period_size_is_plausible_for_low_latency(trial)) {
2811 configured_low_latency_capture_period_size = trial;
2812 }
2813 }
2814
Eric Laurent994a6932013-07-17 11:51:42 -07002815 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002816 return 0;
2817}
2818
2819static struct hw_module_methods_t hal_module_methods = {
2820 .open = adev_open,
2821};
2822
2823struct audio_module HAL_MODULE_INFO_SYM = {
2824 .common = {
2825 .tag = HARDWARE_MODULE_TAG,
2826 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2827 .hal_api_version = HARDWARE_HAL_API_VERSION,
2828 .id = AUDIO_HARDWARE_MODULE_ID,
2829 .name = "QCOM Audio HAL",
2830 .author = "Code Aurora Forum",
2831 .methods = &hal_module_methods,
2832 },
2833};