blob: 4ad43217bf70e8126dec388cb62563490fd6a0f4 [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 Alamandab7ea4f52015-06-08 16:44:05 -0700268 int i, num_devices = 0;
269 snd_device_t new_snd_devices[2];
270
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800271 if (snd_device < SND_DEVICE_MIN ||
272 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800273 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800274 return -EINVAL;
275 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700276
277 adev->snd_dev_ref_cnt[snd_device]++;
278 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700279 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700280 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700281 return 0;
282 }
283
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700284 /* due to the possibility of calibration overwrite between listen
285 and audio, notify sound trigger hal before audio calibration is sent */
286 audio_extn_sound_trigger_update_device_status(snd_device,
287 ST_EVENT_SND_DEVICE_BUSY);
288
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700289 if (audio_extn_spkr_prot_is_enabled())
290 audio_extn_spkr_prot_calib_cancel(adev);
291
Eric Laurentb23d5282013-05-14 15:27:20 -0700292 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700293 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700294 audio_extn_sound_trigger_update_device_status(snd_device,
295 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800296 return -EINVAL;
297 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800298
zhaoyang yin4211fad2015-06-04 21:13:25 +0800299 audio_extn_dsm_feedback_enable(adev, snd_device, true);
300
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700301 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
302 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
303 audio_extn_spkr_prot_is_enabled()) {
304 if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) {
305 adev->snd_dev_ref_cnt[snd_device]--;
306 return -EINVAL;
307 }
308 if (audio_extn_spkr_prot_start_processing(snd_device)) {
309 ALOGE("%s: spkr_start_processing failed", __func__);
310 return -EINVAL;
311 }
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700312 } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
313 for (i = 0; i < num_devices; i++) {
314 enable_snd_device(adev, new_snd_devices[i]);
315 }
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700316 } else {
317 const char * dev_path = platform_get_snd_device_name(snd_device);
318 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
319 audio_route_apply_and_update_path(adev->audio_route, dev_path);
320 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700321
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800322 return 0;
323}
324
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800325int disable_snd_device(struct audio_device *adev,
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700326 snd_device_t snd_device)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800327{
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700328 int i, num_devices = 0;
329 snd_device_t new_snd_devices[2];
330
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800331 if (snd_device < SND_DEVICE_MIN ||
332 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800333 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800334 return -EINVAL;
335 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700336 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
337 ALOGE("%s: device ref cnt is already 0", __func__);
338 return -EINVAL;
339 }
340 adev->snd_dev_ref_cnt[snd_device]--;
341 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700342 const char * dev_path = platform_get_snd_device_name(snd_device);
Ravi Kumar Alamanda64026462014-09-15 00:08:58 -0700343 ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
zhaoyang yin4211fad2015-06-04 21:13:25 +0800344
345 audio_extn_dsm_feedback_enable(adev, snd_device, false);
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700346 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
347 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
348 audio_extn_spkr_prot_is_enabled()) {
349 audio_extn_spkr_prot_stop_processing(snd_device);
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700350 } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) {
351 for (i = 0; i < num_devices; i++) {
352 disable_snd_device(adev, new_snd_devices[i]);
353 }
Ravi Kumar Alamanda63863002015-04-22 11:15:25 -0700354 } else {
355 audio_route_reset_and_update_path(adev->audio_route, dev_path);
356 }
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -0700357 audio_extn_sound_trigger_update_device_status(snd_device,
358 ST_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700359 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800360 return 0;
361}
362
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700363static void check_and_route_playback_usecases(struct audio_device *adev,
364 struct audio_usecase *uc_info,
365 snd_device_t snd_device)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700366{
367 struct listnode *node;
368 struct audio_usecase *usecase;
369 bool switch_device[AUDIO_USECASE_MAX];
370 int i, num_uc_to_switch = 0;
371
372 /*
373 * This function is to make sure that all the usecases that are active on
374 * the hardware codec backend are always routed to any one device that is
375 * handled by the hardware codec.
376 * For example, if low-latency and deep-buffer usecases are currently active
377 * on speaker and out_set_parameters(headset) is received on low-latency
378 * output, then we have to make sure deep-buffer is also switched to headset,
379 * because of the limitation that both the devices cannot be enabled
380 * at the same time as they share the same backend.
381 */
382 /* Disable all the usecases on the shared backend other than the
383 specified usecase */
384 for (i = 0; i < AUDIO_USECASE_MAX; i++)
385 switch_device[i] = false;
386
387 list_for_each(node, &adev->usecase_list) {
388 usecase = node_to_item(node, struct audio_usecase, list);
389 if (usecase->type != PCM_CAPTURE &&
390 usecase != uc_info &&
391 usecase->out_snd_device != snd_device &&
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700392 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND &&
393 platform_check_backends_match(snd_device, usecase->out_snd_device)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700394 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
395 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700396 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700397 disable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700398 switch_device[usecase->id] = true;
399 num_uc_to_switch++;
400 }
401 }
402
403 if (num_uc_to_switch) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700404 list_for_each(node, &adev->usecase_list) {
405 usecase = node_to_item(node, struct audio_usecase, list);
406 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700407 disable_snd_device(adev, usecase->out_snd_device);
sangwon.jeon866d5ff2013-10-17 21:42:50 +0900408 }
409 }
410
411 list_for_each(node, &adev->usecase_list) {
412 usecase = node_to_item(node, struct audio_usecase, list);
413 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700414 enable_snd_device(adev, snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700415 }
416 }
417
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700418 /* Re-route all the usecases on the shared backend other than the
419 specified usecase to new snd devices */
420 list_for_each(node, &adev->usecase_list) {
421 usecase = node_to_item(node, struct audio_usecase, list);
422 /* Update the out_snd_device only before enabling the audio route */
423 if (switch_device[usecase->id] ) {
424 usecase->out_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700425 enable_audio_route(adev, usecase);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700426 }
427 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700428 }
429}
430
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700431static void check_and_route_capture_usecases(struct audio_device *adev,
432 struct audio_usecase *uc_info,
433 snd_device_t snd_device)
434{
435 struct listnode *node;
436 struct audio_usecase *usecase;
437 bool switch_device[AUDIO_USECASE_MAX];
438 int i, num_uc_to_switch = 0;
439
440 /*
441 * This function is to make sure that all the active capture usecases
442 * are always routed to the same input sound device.
443 * For example, if audio-record and voice-call usecases are currently
444 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
445 * is received for voice call then we have to make sure that audio-record
446 * usecase is also switched to earpiece i.e. voice-dmic-ef,
447 * because of the limitation that two devices cannot be enabled
448 * at the same time if they share the same backend.
449 */
450 for (i = 0; i < AUDIO_USECASE_MAX; i++)
451 switch_device[i] = false;
452
453 list_for_each(node, &adev->usecase_list) {
454 usecase = node_to_item(node, struct audio_usecase, list);
455 if (usecase->type != PCM_PLAYBACK &&
456 usecase != uc_info &&
Anish Kumarff864712015-06-03 13:35:11 -0700457 usecase->in_snd_device != snd_device &&
458 (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700459 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
460 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700461 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700462 disable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700463 switch_device[usecase->id] = true;
464 num_uc_to_switch++;
465 }
466 }
467
468 if (num_uc_to_switch) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700469 list_for_each(node, &adev->usecase_list) {
470 usecase = node_to_item(node, struct audio_usecase, list);
471 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700472 disable_snd_device(adev, usecase->in_snd_device);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700473 }
474 }
475
476 list_for_each(node, &adev->usecase_list) {
477 usecase = node_to_item(node, struct audio_usecase, list);
478 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700479 enable_snd_device(adev, snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700480 }
481 }
482
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700483 /* Re-route all the usecases on the shared backend other than the
484 specified usecase to new snd devices */
485 list_for_each(node, &adev->usecase_list) {
486 usecase = node_to_item(node, struct audio_usecase, list);
487 /* Update the in_snd_device only before enabling the audio route */
488 if (switch_device[usecase->id] ) {
489 usecase->in_snd_device = snd_device;
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700490 enable_audio_route(adev, usecase);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700491 }
492 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700493 }
494}
495
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800496/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700497static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800498{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700499 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700500 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800501
502 switch (channels) {
503 /*
504 * Do not handle stereo output in Multi-channel cases
505 * Stereo case is handled in normal playback path
506 */
507 case 6:
508 ALOGV("%s: HDMI supports 5.1", __func__);
509 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
510 break;
511 case 8:
512 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
513 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
514 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
515 break;
516 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700517 ALOGE("HDMI does not support multi channel playback");
518 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800519 break;
520 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700521 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800522}
523
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700524static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
525{
526 struct audio_usecase *usecase;
527 struct listnode *node;
528
529 list_for_each(node, &adev->usecase_list) {
530 usecase = node_to_item(node, struct audio_usecase, list);
531 if (usecase->type == VOICE_CALL) {
532 ALOGV("%s: usecase id %d", __func__, usecase->id);
533 return usecase->id;
534 }
535 }
536 return USECASE_INVALID;
537}
538
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800539struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
540 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700541{
542 struct audio_usecase *usecase;
543 struct listnode *node;
544
545 list_for_each(node, &adev->usecase_list) {
546 usecase = node_to_item(node, struct audio_usecase, list);
547 if (usecase->id == uc_id)
548 return usecase;
549 }
550 return NULL;
551}
552
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800553int select_devices(struct audio_device *adev,
554 audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800555{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800556 snd_device_t out_snd_device = SND_DEVICE_NONE;
557 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700558 struct audio_usecase *usecase = NULL;
559 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800560 struct audio_usecase *hfp_usecase = NULL;
561 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800562 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700563 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800564
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700565 usecase = get_usecase_from_list(adev, uc_id);
566 if (usecase == NULL) {
567 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
568 return -EINVAL;
569 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800570
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800571 if ((usecase->type == VOICE_CALL) ||
572 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700573 out_snd_device = platform_get_output_snd_device(adev->platform,
574 usecase->stream.out->devices);
575 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700576 usecase->devices = usecase->stream.out->devices;
577 } else {
578 /*
579 * If the voice call is active, use the sound devices of voice call usecase
580 * so that it would not result any device switch. All the usecases will
581 * be switched to new device when select_devices() is called for voice call
582 * usecase. This is to avoid switching devices for voice call when
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700583 * check_and_route_playback_usecases() is called below.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700584 */
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700585 if (voice_is_in_call(adev)) {
Ravi Kumar Alamandaa237ecc2014-07-24 17:27:05 -0700586 vc_usecase = get_usecase_from_list(adev,
587 get_voice_usecase_id_from_list(adev));
588 if ((vc_usecase != NULL) &&
589 ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
590 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700591 in_snd_device = vc_usecase->in_snd_device;
592 out_snd_device = vc_usecase->out_snd_device;
593 }
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -0800594 } else if (audio_extn_hfp_is_active(adev)) {
595 hfp_ucid = audio_extn_hfp_get_usecase();
596 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
597 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
598 in_snd_device = hfp_usecase->in_snd_device;
599 out_snd_device = hfp_usecase->out_snd_device;
600 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700601 }
602 if (usecase->type == PCM_PLAYBACK) {
603 usecase->devices = usecase->stream.out->devices;
604 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700605 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700606 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700607 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700608 if (usecase->stream.out == adev->primary_output &&
609 adev->active_input &&
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800610 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
611 out_snd_device != usecase->out_snd_device) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700612 select_devices(adev, adev->active_input->usecase);
613 }
614 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700615 } else if (usecase->type == PCM_CAPTURE) {
616 usecase->devices = usecase->stream.in->device;
617 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700618 if (in_snd_device == SND_DEVICE_NONE) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700619 audio_devices_t out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700620 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
621 adev->primary_output && !adev->primary_output->standby) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700622 out_device = adev->primary_output->devices;
Ravi Kumar Alamandaf2829012014-11-12 16:16:10 -0800623 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700624 } else if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
625 out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700626 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700627 in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700628 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700629 }
630 }
631
632 if (out_snd_device == usecase->out_snd_device &&
633 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800634 return 0;
635 }
636
sangwoobc677242013-08-08 16:53:43 +0900637 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700638 out_snd_device, platform_get_snd_device_name(out_snd_device),
639 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800640
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800641 /*
642 * Limitation: While in call, to do a device switch we need to disable
643 * and enable both RX and TX devices though one of them is same as current
644 * device.
645 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700646 if ((usecase->type == VOICE_CALL) &&
647 (usecase->in_snd_device != SND_DEVICE_NONE) &&
648 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700649 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800650 }
651
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700652 /* Disable current sound devices */
653 if (usecase->out_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700654 disable_audio_route(adev, usecase);
655 disable_snd_device(adev, usecase->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800656 }
657
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700658 if (usecase->in_snd_device != SND_DEVICE_NONE) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700659 disable_audio_route(adev, usecase);
660 disable_snd_device(adev, usecase->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800661 }
662
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700663 /* Applicable only on the targets that has external modem.
664 * New device information should be sent to modem before enabling
665 * the devices to reduce in-call device switch time.
666 */
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700667 if ((usecase->type == VOICE_CALL) &&
668 (usecase->in_snd_device != SND_DEVICE_NONE) &&
669 (usecase->out_snd_device != SND_DEVICE_NONE)) {
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700670 status = platform_switch_voice_call_enable_device_config(adev->platform,
671 out_snd_device,
672 in_snd_device);
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -0700673 }
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700674
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700675 /* Enable new sound devices */
676 if (out_snd_device != SND_DEVICE_NONE) {
677 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -0700678 check_and_route_playback_usecases(adev, usecase, out_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700679 enable_snd_device(adev, out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800680 }
681
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700682 if (in_snd_device != SND_DEVICE_NONE) {
683 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700684 enable_snd_device(adev, in_snd_device);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700685 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700686
Eric Laurentb23d5282013-05-14 15:27:20 -0700687 if (usecase->type == VOICE_CALL)
688 status = platform_switch_voice_call_device_post(adev->platform,
689 out_snd_device,
690 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800691
sangwoo170731f2013-06-08 15:36:36 +0900692 usecase->in_snd_device = in_snd_device;
693 usecase->out_snd_device = out_snd_device;
694
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700695 enable_audio_route(adev, usecase);
sangwoo170731f2013-06-08 15:36:36 +0900696
Ravi Kumar Alamanda83281a92014-05-19 18:14:57 -0700697 /* Applicable only on the targets that has external modem.
698 * Enable device command should be sent to modem only after
699 * enabling voice call mixer controls
700 */
701 if (usecase->type == VOICE_CALL)
702 status = platform_switch_voice_call_usecase_route_post(adev->platform,
703 out_snd_device,
704 in_snd_device);
705
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800706 return status;
707}
708
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800709static int stop_input_stream(struct stream_in *in)
710{
711 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800712 struct audio_usecase *uc_info;
713 struct audio_device *adev = in->dev;
714
Eric Laurentc8400632013-02-14 19:04:54 -0800715 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800716
Eric Laurent994a6932013-07-17 11:51:42 -0700717 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700718 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800719 uc_info = get_usecase_from_list(adev, in->usecase);
720 if (uc_info == NULL) {
721 ALOGE("%s: Could not find the usecase (%d) in the list",
722 __func__, in->usecase);
723 return -EINVAL;
724 }
725
Eric Laurent150dbfe2013-02-27 14:31:02 -0800726 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700727 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700728
729 /* 2. Disable the tx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -0700730 disable_snd_device(adev, uc_info->in_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800731
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800732 list_remove(&uc_info->list);
733 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800734
Eric Laurent994a6932013-07-17 11:51:42 -0700735 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800736 return ret;
737}
738
739int start_input_stream(struct stream_in *in)
740{
741 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800742 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800743 struct audio_usecase *uc_info;
744 struct audio_device *adev = in->dev;
745
Eric Laurent994a6932013-07-17 11:51:42 -0700746 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Eric Laurentb23d5282013-05-14 15:27:20 -0700747 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800748 if (in->pcm_device_id < 0) {
749 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
750 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800751 ret = -EINVAL;
752 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800753 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700754
755 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800756 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
757 uc_info->id = in->usecase;
758 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800759 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700760 uc_info->devices = in->device;
761 uc_info->in_snd_device = SND_DEVICE_NONE;
762 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800763
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800764 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700765 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800766
Eric Laurentc8400632013-02-14 19:04:54 -0800767 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -0700768 __func__, adev->snd_card, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700769
770 unsigned int flags = PCM_IN;
771 unsigned int pcm_open_retry_count = 0;
772
773 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
774 flags |= PCM_MMAP | PCM_NOIRQ;
775 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800776 }
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -0700777
778 while (1) {
779 in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
780 flags, &in->config);
781 if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
782 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
783 if (in->pcm != NULL) {
784 pcm_close(in->pcm);
785 in->pcm = NULL;
786 }
787 if (pcm_open_retry_count-- == 0) {
788 ret = -EIO;
789 goto error_open;
790 }
791 usleep(PROXY_OPEN_WAIT_TIME * 1000);
792 continue;
793 }
794 break;
795 }
796
Eric Laurent994a6932013-07-17 11:51:42 -0700797 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800798 return ret;
799
800error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800801 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800802
803error_config:
804 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700805 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800806
807 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800808}
809
Eric Laurentef26c642015-06-19 16:30:44 -0700810void lock_input_stream(struct stream_in *in)
811{
812 pthread_mutex_lock(&in->pre_lock);
813 pthread_mutex_lock(&in->lock);
814 pthread_mutex_unlock(&in->pre_lock);
815}
816
817void lock_output_stream(struct stream_out *out)
818{
819 pthread_mutex_lock(&out->pre_lock);
820 pthread_mutex_lock(&out->lock);
821 pthread_mutex_unlock(&out->pre_lock);
822}
823
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700824/* must be called with out->lock locked */
825static int send_offload_cmd_l(struct stream_out* out, int command)
826{
827 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
828
829 ALOGVV("%s %d", __func__, command);
830
831 cmd->cmd = command;
832 list_add_tail(&out->offload_cmd_list, &cmd->node);
833 pthread_cond_signal(&out->offload_cond);
834 return 0;
835}
836
837/* must be called iwth out->lock locked */
838static void stop_compressed_output_l(struct stream_out *out)
839{
840 out->offload_state = OFFLOAD_STATE_IDLE;
841 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700842 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700843 if (out->compr != NULL) {
844 compress_stop(out->compr);
845 while (out->offload_thread_blocked) {
846 pthread_cond_wait(&out->cond, &out->lock);
847 }
848 }
849}
850
851static void *offload_thread_loop(void *context)
852{
853 struct stream_out *out = (struct stream_out *) context;
854 struct listnode *item;
855
856 out->offload_state = OFFLOAD_STATE_IDLE;
857 out->playback_started = 0;
858
859 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
860 set_sched_policy(0, SP_FOREGROUND);
861 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
862
863 ALOGV("%s", __func__);
Eric Laurentef26c642015-06-19 16:30:44 -0700864 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700865 for (;;) {
866 struct offload_cmd *cmd = NULL;
867 stream_callback_event_t event;
868 bool send_callback = false;
869
870 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
871 __func__, list_empty(&out->offload_cmd_list),
872 out->offload_state);
873 if (list_empty(&out->offload_cmd_list)) {
874 ALOGV("%s SLEEPING", __func__);
875 pthread_cond_wait(&out->offload_cond, &out->lock);
876 ALOGV("%s RUNNING", __func__);
877 continue;
878 }
879
880 item = list_head(&out->offload_cmd_list);
881 cmd = node_to_item(item, struct offload_cmd, node);
882 list_remove(item);
883
884 ALOGVV("%s STATE %d CMD %d out->compr %p",
885 __func__, out->offload_state, cmd->cmd, out->compr);
886
887 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
888 free(cmd);
889 break;
890 }
891
892 if (out->compr == NULL) {
893 ALOGE("%s: Compress handle is NULL", __func__);
894 pthread_cond_signal(&out->cond);
895 continue;
896 }
897 out->offload_thread_blocked = true;
898 pthread_mutex_unlock(&out->lock);
899 send_callback = false;
900 switch(cmd->cmd) {
901 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
902 compress_wait(out->compr, -1);
903 send_callback = true;
904 event = STREAM_CBK_EVENT_WRITE_READY;
905 break;
906 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700907 compress_next_track(out->compr);
908 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700909 send_callback = true;
910 event = STREAM_CBK_EVENT_DRAIN_READY;
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800911 /* Resend the metadata for next iteration */
912 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700913 break;
914 case OFFLOAD_CMD_DRAIN:
915 compress_drain(out->compr);
916 send_callback = true;
917 event = STREAM_CBK_EVENT_DRAIN_READY;
918 break;
919 default:
920 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
921 break;
922 }
Eric Laurentef26c642015-06-19 16:30:44 -0700923 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700924 out->offload_thread_blocked = false;
925 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700926 if (send_callback) {
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800927 ALOGVV("%s: sending offload_callback event %d", __func__, event);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700928 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700929 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700930 free(cmd);
931 }
932
933 pthread_cond_signal(&out->cond);
934 while (!list_empty(&out->offload_cmd_list)) {
935 item = list_head(&out->offload_cmd_list);
936 list_remove(item);
937 free(node_to_item(item, struct offload_cmd, node));
938 }
939 pthread_mutex_unlock(&out->lock);
940
941 return NULL;
942}
943
944static int create_offload_callback_thread(struct stream_out *out)
945{
946 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
947 list_init(&out->offload_cmd_list);
948 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
949 offload_thread_loop, out);
950 return 0;
951}
952
953static int destroy_offload_callback_thread(struct stream_out *out)
954{
Eric Laurentef26c642015-06-19 16:30:44 -0700955 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700956 stop_compressed_output_l(out);
957 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
958
959 pthread_mutex_unlock(&out->lock);
960 pthread_join(out->offload_thread, (void **) NULL);
961 pthread_cond_destroy(&out->offload_cond);
962
963 return 0;
964}
965
Eric Laurent07eeafd2013-10-06 12:52:49 -0700966static bool allow_hdmi_channel_config(struct audio_device *adev)
967{
968 struct listnode *node;
969 struct audio_usecase *usecase;
970 bool ret = true;
971
972 list_for_each(node, &adev->usecase_list) {
973 usecase = node_to_item(node, struct audio_usecase, list);
974 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
975 /*
976 * If voice call is already existing, do not proceed further to avoid
977 * disabling/enabling both RX and TX devices, CSD calls, etc.
978 * Once the voice call done, the HDMI channels can be configured to
979 * max channels of remaining use cases.
980 */
981 if (usecase->id == USECASE_VOICE_CALL) {
982 ALOGD("%s: voice call is active, no change in HDMI channels",
983 __func__);
984 ret = false;
985 break;
986 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
987 ALOGD("%s: multi channel playback is active, "
988 "no change in HDMI channels", __func__);
989 ret = false;
990 break;
991 }
992 }
993 }
994 return ret;
995}
996
997static int check_and_set_hdmi_channels(struct audio_device *adev,
998 unsigned int channels)
999{
1000 struct listnode *node;
1001 struct audio_usecase *usecase;
1002
1003 /* Check if change in HDMI channel config is allowed */
1004 if (!allow_hdmi_channel_config(adev))
1005 return 0;
1006
1007 if (channels == adev->cur_hdmi_channels) {
1008 ALOGD("%s: Requested channels are same as current", __func__);
1009 return 0;
1010 }
1011
1012 platform_set_hdmi_channels(adev->platform, channels);
1013 adev->cur_hdmi_channels = channels;
1014
1015 /*
1016 * Deroute all the playback streams routed to HDMI so that
1017 * the back end is deactivated. Note that backend will not
1018 * be deactivated if any one stream is connected to it.
1019 */
1020 list_for_each(node, &adev->usecase_list) {
1021 usecase = node_to_item(node, struct audio_usecase, list);
1022 if (usecase->type == PCM_PLAYBACK &&
1023 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001024 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001025 }
1026 }
1027
1028 /*
1029 * Enable all the streams disabled above. Now the HDMI backend
1030 * will be activated with new channel configuration
1031 */
1032 list_for_each(node, &adev->usecase_list) {
1033 usecase = node_to_item(node, struct audio_usecase, list);
1034 if (usecase->type == PCM_PLAYBACK &&
1035 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001036 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001037 }
1038 }
1039
1040 return 0;
1041}
1042
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001043static int stop_output_stream(struct stream_out *out)
1044{
1045 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001046 struct audio_usecase *uc_info;
1047 struct audio_device *adev = out->dev;
1048
Eric Laurent994a6932013-07-17 11:51:42 -07001049 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001050 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001051 uc_info = get_usecase_from_list(adev, out->usecase);
1052 if (uc_info == NULL) {
1053 ALOGE("%s: Could not find the usecase (%d) in the list",
1054 __func__, out->usecase);
1055 return -EINVAL;
1056 }
1057
Haynes Mathew George41f86652014-06-17 14:22:15 -07001058 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1059 if (adev->visualizer_stop_output != NULL)
1060 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1061 if (adev->offload_effects_stop_output != NULL)
1062 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1063 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001064
Eric Laurent150dbfe2013-02-27 14:31:02 -08001065 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001066 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001067
1068 /* 2. Disable the rx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001069 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001070
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001071 list_remove(&uc_info->list);
1072 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001073
Eric Laurent0499d4f2014-08-25 22:39:29 -05001074 audio_extn_extspk_update(adev->extspk);
1075
Eric Laurent07eeafd2013-10-06 12:52:49 -07001076 /* Must be called after removing the usecase from list */
1077 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1078 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1079
Eric Laurent994a6932013-07-17 11:51:42 -07001080 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001081 return ret;
1082}
1083
1084int start_output_stream(struct stream_out *out)
1085{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001086 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001087 struct audio_usecase *uc_info;
1088 struct audio_device *adev = out->dev;
1089
Eric Laurent994a6932013-07-17 11:51:42 -07001090 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001091 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001092 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001093 if (out->pcm_device_id < 0) {
1094 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1095 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001096 ret = -EINVAL;
1097 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001098 }
1099
1100 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1101 uc_info->id = out->usecase;
1102 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001103 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001104 uc_info->devices = out->devices;
1105 uc_info->in_snd_device = SND_DEVICE_NONE;
1106 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001107
Eric Laurent07eeafd2013-10-06 12:52:49 -07001108 /* This must be called before adding this usecase to the list */
1109 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1110 check_and_set_hdmi_channels(adev, out->config.channels);
1111
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001112 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001113
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001114 select_devices(adev, out->usecase);
1115
Eric Laurent0499d4f2014-08-25 22:39:29 -05001116 audio_extn_extspk_update(adev->extspk);
1117
Andy Hung31aca912014-03-20 17:14:59 -07001118 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001119 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001120 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001121 unsigned int flags = PCM_OUT;
1122 unsigned int pcm_open_retry_count = 0;
1123 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1124 flags |= PCM_MMAP | PCM_NOIRQ;
1125 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1126 } else
1127 flags |= PCM_MONOTONIC;
1128
1129 while (1) {
1130 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1131 flags, &out->config);
1132 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
1133 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1134 if (out->pcm != NULL) {
1135 pcm_close(out->pcm);
1136 out->pcm = NULL;
1137 }
1138 if (pcm_open_retry_count-- == 0) {
1139 ret = -EIO;
1140 goto error_open;
1141 }
1142 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1143 continue;
1144 }
1145 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001146 }
1147 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001148 out->pcm = NULL;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001149 out->compr = compress_open(adev->snd_card, out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001150 COMPRESS_IN, &out->compr_config);
1151 if (out->compr && !is_compress_ready(out->compr)) {
1152 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1153 compress_close(out->compr);
1154 out->compr = NULL;
1155 ret = -EIO;
1156 goto error_open;
1157 }
1158 if (out->offload_callback)
1159 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001160
1161 if (adev->visualizer_start_output != NULL)
Haynes Mathew George41f86652014-06-17 14:22:15 -07001162 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1163 if (adev->offload_effects_start_output != NULL)
1164 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001165 }
Eric Laurent994a6932013-07-17 11:51:42 -07001166 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001167 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001168error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001169 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001170error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001171 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001172}
1173
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001174static int check_input_parameters(uint32_t sample_rate,
1175 audio_format_t format,
1176 int channel_count)
1177{
1178 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1179
1180 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1181
1182 switch (sample_rate) {
1183 case 8000:
1184 case 11025:
1185 case 12000:
1186 case 16000:
1187 case 22050:
1188 case 24000:
1189 case 32000:
1190 case 44100:
1191 case 48000:
1192 break;
1193 default:
1194 return -EINVAL;
1195 }
1196
1197 return 0;
1198}
1199
1200static size_t get_input_buffer_size(uint32_t sample_rate,
1201 audio_format_t format,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001202 int channel_count,
1203 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001204{
1205 size_t size = 0;
1206
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001207 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1208 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001209
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001210 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001211 if (is_low_latency)
Glenn Kasten4f993392014-05-14 07:30:48 -07001212 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001213 /* ToDo: should use frame_size computed based on the format and
1214 channel_count here. */
1215 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001216
Glenn Kasten4f993392014-05-14 07:30:48 -07001217 /* make sure the size is multiple of 32 bytes
1218 * At 48 kHz mono 16-bit PCM:
1219 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1220 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1221 */
1222 size += 0x1f;
1223 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001224
1225 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001226}
1227
1228static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1229{
1230 struct stream_out *out = (struct stream_out *)stream;
1231
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001232 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001233}
1234
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001235static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001236{
1237 return -ENOSYS;
1238}
1239
1240static size_t out_get_buffer_size(const struct audio_stream *stream)
1241{
1242 struct stream_out *out = (struct stream_out *)stream;
1243
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001244 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1245 return out->compr_config.fragment_size;
1246 }
Eric Laurentfdf296a2014-07-03 16:41:51 -07001247 return out->config.period_size *
1248 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001249}
1250
1251static uint32_t out_get_channels(const struct audio_stream *stream)
1252{
1253 struct stream_out *out = (struct stream_out *)stream;
1254
1255 return out->channel_mask;
1256}
1257
1258static audio_format_t out_get_format(const struct audio_stream *stream)
1259{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001260 struct stream_out *out = (struct stream_out *)stream;
1261
1262 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001263}
1264
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001265static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001266{
1267 return -ENOSYS;
1268}
1269
1270static int out_standby(struct audio_stream *stream)
1271{
1272 struct stream_out *out = (struct stream_out *)stream;
1273 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001274
Eric Laurent994a6932013-07-17 11:51:42 -07001275 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001276 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277
Eric Laurentef26c642015-06-19 16:30:44 -07001278 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001279 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001280 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001281 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001282 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1283 if (out->pcm) {
1284 pcm_close(out->pcm);
1285 out->pcm = NULL;
1286 }
1287 } else {
1288 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001289 out->gapless_mdata.encoder_delay = 0;
1290 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001291 if (out->compr != NULL) {
1292 compress_close(out->compr);
1293 out->compr = NULL;
1294 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001295 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001296 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001297 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001298 }
1299 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001300 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001301 return 0;
1302}
1303
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001304static int out_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001305{
1306 return 0;
1307}
1308
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001309static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1310{
1311 int ret = 0;
1312 char value[32];
1313 struct compr_gapless_mdata tmp_mdata;
1314
1315 if (!out || !parms) {
1316 return -EINVAL;
1317 }
1318
1319 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1320 if (ret >= 0) {
1321 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1322 } else {
1323 return -EINVAL;
1324 }
1325
1326 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1327 if (ret >= 0) {
1328 tmp_mdata.encoder_padding = atoi(value);
1329 } else {
1330 return -EINVAL;
1331 }
1332
1333 out->gapless_mdata = tmp_mdata;
1334 out->send_new_metadata = 1;
1335 ALOGV("%s new encoder delay %u and padding %u", __func__,
1336 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1337
1338 return 0;
1339}
1340
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001341static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1342{
1343 return out == adev->primary_output || out == adev->voice_tx_output;
1344}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001345
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001346static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1347{
1348 struct stream_out *out = (struct stream_out *)stream;
1349 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001350 struct audio_usecase *usecase;
1351 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001352 struct str_parms *parms;
1353 char value[32];
1354 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001355 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001356 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001357
sangwoobc677242013-08-08 16:53:43 +09001358 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001359 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001360 parms = str_parms_create_str(kvpairs);
1361 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1362 if (ret >= 0) {
1363 val = atoi(value);
Eric Laurentef26c642015-06-19 16:30:44 -07001364 lock_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001365 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001366
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001367 /*
1368 * When HDMI cable is unplugged the music playback is paused and
1369 * the policy manager sends routing=0. But the audioflinger
1370 * continues to write data until standby time (3sec).
1371 * As the HDMI core is turned off, the write gets blocked.
1372 * Avoid this by routing audio to speaker until standby.
1373 */
1374 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1375 val == AUDIO_DEVICE_NONE) {
1376 val = AUDIO_DEVICE_OUT_SPEAKER;
1377 }
1378
1379 /*
1380 * select_devices() call below switches all the usecases on the same
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -07001381 * backend to the new device. Refer to check_and_route_playback_usecases() in
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001382 * the select_devices(). But how do we undo this?
1383 *
1384 * For example, music playback is active on headset (deep-buffer usecase)
1385 * and if we go to ringtones and select a ringtone, low-latency usecase
1386 * will be started on headset+speaker. As we can't enable headset+speaker
1387 * and headset devices at the same time, select_devices() switches the music
1388 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1389 * So when the ringtone playback is completed, how do we undo the same?
1390 *
1391 * We are relying on the out_set_parameters() call on deep-buffer output,
1392 * once the ringtone playback is ended.
1393 * NOTE: We should not check if the current devices are same as new devices.
1394 * Because select_devices() must be called to switch back the music
1395 * playback to headset.
1396 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001397 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001398 out->devices = val;
1399
1400 if (!out->standby)
1401 select_devices(adev, out->usecase);
1402
Eric Laurenta7657192014-10-09 21:09:33 -07001403 if (output_drives_call(adev, out)) {
1404 if (!voice_is_in_call(adev)) {
1405 if (adev->mode == AUDIO_MODE_IN_CALL) {
1406 adev->current_call_output = out;
1407 ret = voice_start_call(adev);
1408 }
1409 } else {
1410 adev->current_call_output = out;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001411 voice_update_devices_for_all_voice_usecases(adev);
Eric Laurenta7657192014-10-09 21:09:33 -07001412 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001413 }
1414 }
1415
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001416 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001417 pthread_mutex_unlock(&out->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05001418
1419 /*handles device and call state changes*/
1420 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001421 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001422
1423 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1424 parse_compress_metadata(out, parms);
1425 }
1426
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001427 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001428 ALOGV("%s: exit: code(%d)", __func__, status);
1429 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001430}
1431
1432static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1433{
1434 struct stream_out *out = (struct stream_out *)stream;
1435 struct str_parms *query = str_parms_create_str(keys);
1436 char *str;
1437 char value[256];
1438 struct str_parms *reply = str_parms_create();
1439 size_t i, j;
1440 int ret;
1441 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001442 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001443 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1444 if (ret >= 0) {
1445 value[0] = '\0';
1446 i = 0;
1447 while (out->supported_channel_masks[i] != 0) {
1448 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1449 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1450 if (!first) {
1451 strcat(value, "|");
1452 }
1453 strcat(value, out_channels_name_to_enum_table[j].name);
1454 first = false;
1455 break;
1456 }
1457 }
1458 i++;
1459 }
1460 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1461 str = str_parms_to_str(reply);
1462 } else {
1463 str = strdup(keys);
1464 }
1465 str_parms_destroy(query);
1466 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001467 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001468 return str;
1469}
1470
1471static uint32_t out_get_latency(const struct audio_stream_out *stream)
1472{
1473 struct stream_out *out = (struct stream_out *)stream;
1474
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001475 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1476 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1477
1478 return (out->config.period_count * out->config.period_size * 1000) /
1479 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001480}
1481
1482static int out_set_volume(struct audio_stream_out *stream, float left,
1483 float right)
1484{
Eric Laurenta9024de2013-04-04 09:19:12 -07001485 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001486 int volume[2];
1487
Eric Laurenta9024de2013-04-04 09:19:12 -07001488 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1489 /* only take left channel into account: the API is for stereo anyway */
1490 out->muted = (left == 0.0f);
1491 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001492 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1493 const char *mixer_ctl_name = "Compress Playback Volume";
1494 struct audio_device *adev = out->dev;
1495 struct mixer_ctl *ctl;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001496 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1497 if (!ctl) {
Haynes Mathew George20bcfa82014-06-25 13:37:03 -07001498 /* try with the control based on device id */
1499 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1500 PCM_PLAYBACK);
1501 char ctl_name[128] = {0};
1502 snprintf(ctl_name, sizeof(ctl_name),
1503 "Compress Playback %d Volume", pcm_device_id);
1504 ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name);
1505 if (!ctl) {
1506 ALOGE("%s: Could not get volume ctl mixer cmd", __func__);
1507 return -EINVAL;
1508 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001509 }
1510 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1511 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1512 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1513 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001514 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001515
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001516 return -ENOSYS;
1517}
1518
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001519#ifdef NO_AUDIO_OUT
1520static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
1521 const void *buffer, size_t bytes)
1522{
1523 struct stream_out *out = (struct stream_out *)stream;
1524
1525 /* No Output device supported other than BT for playback.
1526 * Sleep for the amount of buffer duration
1527 */
Eric Laurentef26c642015-06-19 16:30:44 -07001528 lock_output_stream(out);
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001529 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1530 out_get_sample_rate(&out->stream.common));
1531 pthread_mutex_unlock(&out->lock);
1532 return bytes;
1533}
1534#endif
1535
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001536static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1537 size_t bytes)
1538{
1539 struct stream_out *out = (struct stream_out *)stream;
1540 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001541 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001542
Eric Laurentef26c642015-06-19 16:30:44 -07001543 lock_output_stream(out);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001544 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001545 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001546 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001547 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001548 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001549 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001550 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001551 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001552 goto exit;
1553 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001554 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001555
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001556 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001557 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1558 if (out->send_new_metadata) {
1559 ALOGVV("send new gapless metadata");
1560 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1561 out->send_new_metadata = 0;
1562 }
1563
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001564 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001565 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001566 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001567 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1568 }
1569 if (!out->playback_started) {
1570 compress_start(out->compr);
1571 out->playback_started = 1;
1572 out->offload_state = OFFLOAD_STATE_PLAYING;
1573 }
1574 pthread_mutex_unlock(&out->lock);
1575 return ret;
1576 } else {
1577 if (out->pcm) {
1578 if (out->muted)
1579 memset((void *)buffer, 0, bytes);
1580 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001581 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1582 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
1583 }
1584 else
1585 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001586 if (ret == 0)
1587 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001588 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001589 }
1590
1591exit:
1592 pthread_mutex_unlock(&out->lock);
1593
1594 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001595 if (out->pcm)
1596 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001597 out_standby(&out->stream.common);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001598 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001599 out_get_sample_rate(&out->stream.common));
1600 }
1601 return bytes;
1602}
1603
1604static int out_get_render_position(const struct audio_stream_out *stream,
1605 uint32_t *dsp_frames)
1606{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001607 struct stream_out *out = (struct stream_out *)stream;
1608 *dsp_frames = 0;
1609 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
Eric Laurentef26c642015-06-19 16:30:44 -07001610 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001611 if (out->compr != NULL) {
1612 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1613 &out->sample_rate);
1614 ALOGVV("%s rendered frames %d sample_rate %d",
1615 __func__, *dsp_frames, out->sample_rate);
1616 }
1617 pthread_mutex_unlock(&out->lock);
1618 return 0;
1619 } else
1620 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001621}
1622
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001623static int out_add_audio_effect(const struct audio_stream *stream __unused,
1624 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001625{
1626 return 0;
1627}
1628
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001629static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1630 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001631{
1632 return 0;
1633}
1634
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001635static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1636 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001637{
1638 return -EINVAL;
1639}
1640
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001641static int out_get_presentation_position(const struct audio_stream_out *stream,
1642 uint64_t *frames, struct timespec *timestamp)
1643{
1644 struct stream_out *out = (struct stream_out *)stream;
1645 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001646 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001647
Eric Laurentef26c642015-06-19 16:30:44 -07001648 lock_output_stream(out);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001649
Eric Laurent949a0892013-09-20 09:20:13 -07001650 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1651 if (out->compr != NULL) {
1652 compress_get_tstamp(out->compr, &dsp_frames,
1653 &out->sample_rate);
1654 ALOGVV("%s rendered frames %ld sample_rate %d",
1655 __func__, dsp_frames, out->sample_rate);
1656 *frames = dsp_frames;
1657 ret = 0;
1658 /* this is the best we can do */
1659 clock_gettime(CLOCK_MONOTONIC, timestamp);
1660 }
1661 } else {
1662 if (out->pcm) {
1663 size_t avail;
1664 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1665 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001666 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001667 // This adjustment accounts for buffering after app processor.
1668 // It is based on estimated DSP latency per use case, rather than exact.
1669 signed_frames -=
1670 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1671
Eric Laurent949a0892013-09-20 09:20:13 -07001672 // It would be unusual for this value to be negative, but check just in case ...
1673 if (signed_frames >= 0) {
1674 *frames = signed_frames;
1675 ret = 0;
1676 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001677 }
1678 }
1679 }
1680
1681 pthread_mutex_unlock(&out->lock);
1682
1683 return ret;
1684}
1685
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001686static int out_set_callback(struct audio_stream_out *stream,
1687 stream_callback_t callback, void *cookie)
1688{
1689 struct stream_out *out = (struct stream_out *)stream;
1690
1691 ALOGV("%s", __func__);
Eric Laurentef26c642015-06-19 16:30:44 -07001692 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001693 out->offload_callback = callback;
1694 out->offload_cookie = cookie;
1695 pthread_mutex_unlock(&out->lock);
1696 return 0;
1697}
1698
1699static int out_pause(struct audio_stream_out* stream)
1700{
1701 struct stream_out *out = (struct stream_out *)stream;
1702 int status = -ENOSYS;
1703 ALOGV("%s", __func__);
1704 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurentef26c642015-06-19 16:30:44 -07001705 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001706 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1707 status = compress_pause(out->compr);
1708 out->offload_state = OFFLOAD_STATE_PAUSED;
1709 }
1710 pthread_mutex_unlock(&out->lock);
1711 }
1712 return status;
1713}
1714
1715static int out_resume(struct audio_stream_out* stream)
1716{
1717 struct stream_out *out = (struct stream_out *)stream;
1718 int status = -ENOSYS;
1719 ALOGV("%s", __func__);
1720 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1721 status = 0;
Eric Laurentef26c642015-06-19 16:30:44 -07001722 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001723 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1724 status = compress_resume(out->compr);
1725 out->offload_state = OFFLOAD_STATE_PLAYING;
1726 }
1727 pthread_mutex_unlock(&out->lock);
1728 }
1729 return status;
1730}
1731
1732static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1733{
1734 struct stream_out *out = (struct stream_out *)stream;
1735 int status = -ENOSYS;
1736 ALOGV("%s", __func__);
1737 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurentef26c642015-06-19 16:30:44 -07001738 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001739 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1740 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1741 else
1742 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1743 pthread_mutex_unlock(&out->lock);
1744 }
1745 return status;
1746}
1747
1748static int out_flush(struct audio_stream_out* stream)
1749{
1750 struct stream_out *out = (struct stream_out *)stream;
1751 ALOGV("%s", __func__);
1752 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Eric Laurentef26c642015-06-19 16:30:44 -07001753 lock_output_stream(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001754 stop_compressed_output_l(out);
1755 pthread_mutex_unlock(&out->lock);
1756 return 0;
1757 }
1758 return -ENOSYS;
1759}
1760
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001761/** audio_stream_in implementation **/
1762static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1763{
1764 struct stream_in *in = (struct stream_in *)stream;
1765
1766 return in->config.rate;
1767}
1768
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001769static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001770{
1771 return -ENOSYS;
1772}
1773
1774static size_t in_get_buffer_size(const struct audio_stream *stream)
1775{
1776 struct stream_in *in = (struct stream_in *)stream;
1777
Eric Laurentfdf296a2014-07-03 16:41:51 -07001778 return in->config.period_size *
1779 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001780}
1781
1782static uint32_t in_get_channels(const struct audio_stream *stream)
1783{
1784 struct stream_in *in = (struct stream_in *)stream;
1785
1786 return in->channel_mask;
1787}
1788
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001789static audio_format_t in_get_format(const struct audio_stream *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790{
1791 return AUDIO_FORMAT_PCM_16_BIT;
1792}
1793
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001794static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001795{
1796 return -ENOSYS;
1797}
1798
1799static int in_standby(struct audio_stream *stream)
1800{
1801 struct stream_in *in = (struct stream_in *)stream;
1802 struct audio_device *adev = in->dev;
1803 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001804 ALOGV("%s: enter", __func__);
Eric Laurentef26c642015-06-19 16:30:44 -07001805
1806 lock_input_stream(in);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001807
1808 if (!in->standby && in->is_st_session) {
1809 ALOGD("%s: sound trigger pcm stop lab", __func__);
1810 audio_extn_sound_trigger_stop_lab(in);
1811 in->standby = true;
1812 }
1813
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001814 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001815 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001816 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001817 if (in->pcm) {
1818 pcm_close(in->pcm);
1819 in->pcm = NULL;
1820 }
Eric Laurentcefbbac2014-09-04 13:54:10 -05001821 adev->enable_voicerx = false;
1822 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE );
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001823 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001824 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001825 }
1826 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001827 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001828 return status;
1829}
1830
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001831static int in_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001832{
1833 return 0;
1834}
1835
1836static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1837{
1838 struct stream_in *in = (struct stream_in *)stream;
1839 struct audio_device *adev = in->dev;
1840 struct str_parms *parms;
1841 char *str;
1842 char value[32];
1843 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07001844 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001845
Eric Laurent994a6932013-07-17 11:51:42 -07001846 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001847 parms = str_parms_create_str(kvpairs);
1848
1849 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1850
Eric Laurentef26c642015-06-19 16:30:44 -07001851 lock_input_stream(in);
1852
Eric Laurent150dbfe2013-02-27 14:31:02 -08001853 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001854 if (ret >= 0) {
1855 val = atoi(value);
1856 /* no audio source uses val == 0 */
1857 if ((in->source != val) && (val != 0)) {
1858 in->source = val;
1859 }
1860 }
1861
1862 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07001863
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001864 if (ret >= 0) {
1865 val = atoi(value);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001866 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001867 in->device = val;
1868 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001869 if (!in->standby)
Eric Laurent03f09432014-03-25 18:09:11 -07001870 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001871 }
1872 }
1873
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001874 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001875 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001876
1877 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001878 ALOGV("%s: exit: status(%d)", __func__, status);
1879 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001880}
1881
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001882static char* in_get_parameters(const struct audio_stream *stream __unused,
1883 const char *keys __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001884{
1885 return strdup("");
1886}
1887
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001888static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001889{
1890 return 0;
1891}
1892
1893static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1894 size_t bytes)
1895{
1896 struct stream_in *in = (struct stream_in *)stream;
1897 struct audio_device *adev = in->dev;
1898 int i, ret = -1;
1899
Eric Laurentef26c642015-06-19 16:30:44 -07001900 lock_input_stream(in);
1901
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001902 if (in->is_st_session) {
1903 ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
1904 /* Read from sound trigger HAL */
1905 audio_extn_sound_trigger_read(in, buffer, bytes);
1906 pthread_mutex_unlock(&in->lock);
1907 return bytes;
1908 }
1909
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001910 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001911 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001912 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001913 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001914 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001915 goto exit;
1916 }
1917 in->standby = 0;
1918 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001919
1920 if (in->pcm) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001921 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1922 ret = pcm_mmap_read(in->pcm, buffer, bytes);
1923 } else
1924 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001925 }
1926
1927 /*
1928 * Instead of writing zeroes here, we could trust the hardware
1929 * to always provide zeroes when muted.
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001930 * 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 -08001931 */
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001932 if (ret == 0 && adev->mic_muted && in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001933 memset(buffer, 0, bytes);
1934
1935exit:
1936 pthread_mutex_unlock(&in->lock);
1937
1938 if (ret != 0) {
1939 in_standby(&in->stream.common);
1940 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001941 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001942 in_get_sample_rate(&in->stream.common));
1943 }
1944 return bytes;
1945}
1946
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001947static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001948{
1949 return 0;
1950}
1951
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001952static int add_remove_audio_effect(const struct audio_stream *stream,
1953 effect_handle_t effect,
1954 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001955{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001956 struct stream_in *in = (struct stream_in *)stream;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001957 struct audio_device *adev = in->dev;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001958 int status = 0;
1959 effect_descriptor_t desc;
1960
1961 status = (*effect)->get_descriptor(effect, &desc);
1962 if (status != 0)
1963 return status;
1964
Eric Laurentef26c642015-06-19 16:30:44 -07001965 lock_input_stream(in);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001966 pthread_mutex_lock(&in->dev->lock);
1967 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1968 in->enable_aec != enable &&
1969 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1970 in->enable_aec = enable;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001971 if (!enable)
1972 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
1973 adev->enable_voicerx = enable;
1974 struct audio_usecase *usecase;
1975 struct listnode *node;
1976 list_for_each(node, &adev->usecase_list) {
1977 usecase = node_to_item(node, struct audio_usecase, list);
1978 if (usecase->type == PCM_PLAYBACK) {
1979 select_devices(adev, usecase->id);
1980 break;
1981 }
1982 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001983 if (!in->standby)
1984 select_devices(in->dev, in->usecase);
1985 }
Ravi Kumar Alamanda3ad4e1b2014-06-03 00:08:15 -07001986 if (in->enable_ns != enable &&
1987 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1988 in->enable_ns = enable;
1989 if (!in->standby)
1990 select_devices(in->dev, in->usecase);
1991 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001992 pthread_mutex_unlock(&in->dev->lock);
1993 pthread_mutex_unlock(&in->lock);
1994
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001995 return 0;
1996}
1997
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001998static int in_add_audio_effect(const struct audio_stream *stream,
1999 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000{
Eric Laurent994a6932013-07-17 11:51:42 -07002001 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002002 return add_remove_audio_effect(stream, effect, true);
2003}
2004
2005static int in_remove_audio_effect(const struct audio_stream *stream,
2006 effect_handle_t effect)
2007{
Eric Laurent994a6932013-07-17 11:51:42 -07002008 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002009 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002010}
2011
2012static int adev_open_output_stream(struct audio_hw_device *dev,
2013 audio_io_handle_t handle,
2014 audio_devices_t devices,
2015 audio_output_flags_t flags,
2016 struct audio_config *config,
Eric Laurent91975a62014-07-27 17:15:50 -07002017 struct audio_stream_out **stream_out,
2018 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002019{
2020 struct audio_device *adev = (struct audio_device *)dev;
2021 struct stream_out *out;
2022 int i, ret;
2023
Eric Laurent994a6932013-07-17 11:51:42 -07002024 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002025 __func__, config->sample_rate, config->channel_mask, devices, flags);
2026 *stream_out = NULL;
2027 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2028
2029 if (devices == AUDIO_DEVICE_NONE)
2030 devices = AUDIO_DEVICE_OUT_SPEAKER;
2031
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002032 out->flags = flags;
2033 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002034 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002035 out->format = config->format;
2036 out->sample_rate = config->sample_rate;
2037 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2038 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002039 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002040
2041 /* Init use case and pcm_config */
2042 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07002043 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002044 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002045 pthread_mutex_lock(&adev->lock);
2046 ret = read_hdmi_channel_masks(out);
2047 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002048 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002049 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002050
2051 if (config->sample_rate == 0)
2052 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2053 if (config->channel_mask == 0)
2054 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2055
2056 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002057 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002058 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2059 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002060 out->config.rate = config->sample_rate;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002061 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002062 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002063 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2064 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2065 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2066 ALOGE("%s: Unsupported Offload information", __func__);
2067 ret = -EINVAL;
2068 goto error_open;
2069 }
2070 if (!is_supported_format(config->offload_info.format)) {
2071 ALOGE("%s: Unsupported audio format", __func__);
2072 ret = -EINVAL;
2073 goto error_open;
2074 }
2075
2076 out->compr_config.codec = (struct snd_codec *)
2077 calloc(1, sizeof(struct snd_codec));
2078
2079 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2080 if (config->offload_info.channel_mask)
2081 out->channel_mask = config->offload_info.channel_mask;
2082 else if (config->channel_mask)
2083 out->channel_mask = config->channel_mask;
2084 out->format = config->offload_info.format;
2085 out->sample_rate = config->offload_info.sample_rate;
2086
2087 out->stream.set_callback = out_set_callback;
2088 out->stream.pause = out_pause;
2089 out->stream.resume = out_resume;
2090 out->stream.drain = out_drain;
2091 out->stream.flush = out_flush;
2092
2093 out->compr_config.codec->id =
2094 get_snd_codec_id(config->offload_info.format);
2095 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2096 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
Eric Laurent1ccf3972014-10-23 14:42:59 -07002097 out->compr_config.codec->sample_rate = config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002098 out->compr_config.codec->bit_rate =
2099 config->offload_info.bit_rate;
2100 out->compr_config.codec->ch_in =
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002101 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002102 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2103
2104 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2105 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002106
2107 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002108 create_offload_callback_thread(out);
2109 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2110 __func__, config->offload_info.version,
2111 config->offload_info.bit_rate);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002112 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2113 if (config->sample_rate == 0)
2114 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2115 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2116 config->sample_rate != 8000) {
2117 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2118 ret = -EINVAL;
2119 goto error_open;
2120 }
2121 out->sample_rate = config->sample_rate;
2122 out->config.rate = config->sample_rate;
2123 if (config->format == AUDIO_FORMAT_DEFAULT)
2124 config->format = AUDIO_FORMAT_PCM_16_BIT;
2125 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2126 config->format = AUDIO_FORMAT_PCM_16_BIT;
2127 ret = -EINVAL;
2128 goto error_open;
2129 }
2130 out->format = config->format;
2131 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2132 out->config = pcm_config_afe_proxy_playback;
2133 adev->voice_tx_output = out;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002134 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07002135 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2136 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2137 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamandaf78a4d92015-04-24 15:18:23 -07002138 } else if (out->flags & AUDIO_OUTPUT_FLAG_TTS) {
2139 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
2140 out->config = pcm_config_deep_buffer;
Andy Hung6fcba9c2014-03-18 11:53:32 -07002141 } else {
2142 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2143 out->config = pcm_config_low_latency;
2144 }
2145 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2146 if (k_enable_extended_precision
2147 && pcm_params_format_test(adev->use_case_table[out->usecase],
2148 pcm_format_from_audio_format(config->format))) {
2149 out->config.format = pcm_format_from_audio_format(config->format);
2150 /* out->format already set to config->format */
2151 } else {
2152 /* deny the externally proposed config format
2153 * and use the one specified in audio_hw layer configuration.
2154 * Note: out->format is returned by out->stream.common.get_format()
2155 * and is used to set config->format in the code several lines below.
2156 */
2157 out->format = audio_format_from_pcm_format(out->config.format);
2158 }
2159 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002160 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002161 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07002162 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
2163 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002164
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002165 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002166 if (adev->primary_output == NULL)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002167 adev->primary_output = out;
2168 else {
2169 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002170 ret = -EEXIST;
2171 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002172 }
2173 }
2174
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002175 /* Check if this usecase is already existing */
2176 pthread_mutex_lock(&adev->lock);
2177 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2178 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002179 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002180 ret = -EEXIST;
2181 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002182 }
2183 pthread_mutex_unlock(&adev->lock);
2184
2185 out->stream.common.get_sample_rate = out_get_sample_rate;
2186 out->stream.common.set_sample_rate = out_set_sample_rate;
2187 out->stream.common.get_buffer_size = out_get_buffer_size;
2188 out->stream.common.get_channels = out_get_channels;
2189 out->stream.common.get_format = out_get_format;
2190 out->stream.common.set_format = out_set_format;
2191 out->stream.common.standby = out_standby;
2192 out->stream.common.dump = out_dump;
2193 out->stream.common.set_parameters = out_set_parameters;
2194 out->stream.common.get_parameters = out_get_parameters;
2195 out->stream.common.add_audio_effect = out_add_audio_effect;
2196 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2197 out->stream.get_latency = out_get_latency;
2198 out->stream.set_volume = out_set_volume;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002199#ifdef NO_AUDIO_OUT
2200 out->stream.write = out_write_for_no_output;
2201#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002202 out->stream.write = out_write;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002203#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002204 out->stream.get_render_position = out_get_render_position;
2205 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002206 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002207
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002208 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002209 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002210 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002211
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002212 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
Eric Laurentef26c642015-06-19 16:30:44 -07002213 pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002214 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2215
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002216 config->format = out->stream.common.get_format(&out->stream.common);
2217 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2218 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2219
2220 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002221 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002222 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002223
2224error_open:
2225 free(out);
2226 *stream_out = NULL;
2227 ALOGD("%s: exit: ret %d", __func__, ret);
2228 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002229}
2230
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002231static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002232 struct audio_stream_out *stream)
2233{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002234 struct stream_out *out = (struct stream_out *)stream;
2235 struct audio_device *adev = out->dev;
2236
Eric Laurent994a6932013-07-17 11:51:42 -07002237 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002238 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002239 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2240 destroy_offload_callback_thread(out);
2241
2242 if (out->compr_config.codec != NULL)
2243 free(out->compr_config.codec);
2244 }
Ravi Kumar Alamandaa4fc9022014-10-08 18:57:46 -07002245
2246 if (adev->voice_tx_output == out)
2247 adev->voice_tx_output = NULL;
2248
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002249 pthread_cond_destroy(&out->cond);
2250 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002251 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002252 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002253}
2254
2255static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2256{
2257 struct audio_device *adev = (struct audio_device *)dev;
2258 struct str_parms *parms;
2259 char *str;
2260 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002261 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002262 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002263 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002264
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002265 ALOGD("%s: enter: %s", __func__, kvpairs);
2266
2267 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002268
2269 parms = str_parms_create_str(kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002270 status = voice_set_parameters(adev, parms);
2271 if (status != 0) {
2272 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002273 }
2274
2275 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2276 if (ret >= 0) {
2277 /* When set to false, HAL should disable EC and NS
2278 * But it is currently not supported.
2279 */
2280 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2281 adev->bluetooth_nrec = true;
2282 else
2283 adev->bluetooth_nrec = false;
2284 }
2285
2286 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2287 if (ret >= 0) {
2288 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2289 adev->screen_off = false;
2290 else
2291 adev->screen_off = true;
2292 }
2293
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002294 ret = str_parms_get_int(parms, "rotation", &val);
2295 if (ret >= 0) {
2296 bool reverse_speakers = false;
2297 switch(val) {
2298 // FIXME: note that the code below assumes that the speakers are in the correct placement
2299 // relative to the user when the device is rotated 90deg from its default rotation. This
2300 // assumption is device-specific, not platform-specific like this code.
2301 case 270:
2302 reverse_speakers = true;
2303 break;
2304 case 0:
2305 case 90:
2306 case 180:
2307 break;
2308 default:
2309 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002310 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002311 }
Eric Laurent03f09432014-03-25 18:09:11 -07002312 if (status == 0) {
Ravi Kumar Alamanda1f60cf82015-04-23 19:45:17 -07002313 platform_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002314 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002315 }
2316
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002317 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2318 if (ret >= 0) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002319 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002320 }
2321
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08002322 audio_extn_hfp_set_parameters(adev, parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002323done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002324 str_parms_destroy(parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002325 pthread_mutex_unlock(&adev->lock);
Eric Laurent03f09432014-03-25 18:09:11 -07002326 ALOGV("%s: exit with code(%d)", __func__, status);
2327 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002328}
2329
2330static char* adev_get_parameters(const struct audio_hw_device *dev,
2331 const char *keys)
2332{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002333 struct audio_device *adev = (struct audio_device *)dev;
2334 struct str_parms *reply = str_parms_create();
2335 struct str_parms *query = str_parms_create_str(keys);
2336 char *str;
2337
2338 pthread_mutex_lock(&adev->lock);
2339
2340 voice_get_parameters(adev, query, reply);
2341 str = str_parms_to_str(reply);
2342 str_parms_destroy(query);
2343 str_parms_destroy(reply);
2344
2345 pthread_mutex_unlock(&adev->lock);
2346 ALOGV("%s: exit: returns - %s", __func__, str);
2347 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002348}
2349
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002350static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002351{
2352 return 0;
2353}
2354
Haynes Mathew George5191a852013-09-11 14:19:36 -07002355static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2356{
2357 int ret;
2358 struct audio_device *adev = (struct audio_device *)dev;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002359
Eric Laurent4cc4ce12014-09-10 13:21:01 -05002360 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
2361
Haynes Mathew George5191a852013-09-11 14:19:36 -07002362 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002363 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002364 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002365
Haynes Mathew George5191a852013-09-11 14:19:36 -07002366 return ret;
2367}
2368
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002369static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002370{
2371 return -ENOSYS;
2372}
2373
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002374static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2375 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002376{
2377 return -ENOSYS;
2378}
2379
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002380static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002381{
2382 return -ENOSYS;
2383}
2384
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002385static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002386{
2387 return -ENOSYS;
2388}
2389
2390static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2391{
2392 struct audio_device *adev = (struct audio_device *)dev;
2393
2394 pthread_mutex_lock(&adev->lock);
2395 if (adev->mode != mode) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002396 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002397 adev->mode = mode;
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -07002398 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) &&
2399 voice_is_in_call(adev)) {
2400 voice_stop_call(adev);
2401 adev->current_call_output = NULL;
2402 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002403 }
2404 pthread_mutex_unlock(&adev->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002405
2406 audio_extn_extspk_set_mode(adev->extspk, mode);
2407
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002408 return 0;
2409}
2410
2411static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2412{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002413 int ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002414 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002415
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002416 ALOGD("%s: state %d\n", __func__, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002417 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002418 ret = voice_set_mic_mute(adev, state);
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07002419 adev->mic_muted = state;
Eric Laurenta609e8e2014-06-18 02:15:17 +00002420 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002421
2422 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002423}
2424
2425static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2426{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002427 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002428 return 0;
2429}
2430
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002431static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002432 const struct audio_config *config)
2433{
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002434 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002435
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002436 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
2437 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002438}
2439
2440static int adev_open_input_stream(struct audio_hw_device *dev,
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002441 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002442 audio_devices_t devices,
2443 struct audio_config *config,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002444 struct audio_stream_in **stream_in,
Eric Laurent91975a62014-07-27 17:15:50 -07002445 audio_input_flags_t flags,
2446 const char *address __unused,
Eric Laurentcefbbac2014-09-04 13:54:10 -05002447 audio_source_t source )
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002448{
2449 struct audio_device *adev = (struct audio_device *)dev;
2450 struct stream_in *in;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002451 int ret = 0, buffer_size, frame_size;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002452 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002453 bool is_low_latency = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002454
Eric Laurent994a6932013-07-17 11:51:42 -07002455 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002456 *stream_in = NULL;
2457 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2458 return -EINVAL;
2459
2460 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2461
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002462 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
Eric Laurentef26c642015-06-19 16:30:44 -07002463 pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002464
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002465 in->stream.common.get_sample_rate = in_get_sample_rate;
2466 in->stream.common.set_sample_rate = in_set_sample_rate;
2467 in->stream.common.get_buffer_size = in_get_buffer_size;
2468 in->stream.common.get_channels = in_get_channels;
2469 in->stream.common.get_format = in_get_format;
2470 in->stream.common.set_format = in_set_format;
2471 in->stream.common.standby = in_standby;
2472 in->stream.common.dump = in_dump;
2473 in->stream.common.set_parameters = in_set_parameters;
2474 in->stream.common.get_parameters = in_get_parameters;
2475 in->stream.common.add_audio_effect = in_add_audio_effect;
2476 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2477 in->stream.set_gain = in_set_gain;
2478 in->stream.read = in_read;
2479 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2480
2481 in->device = devices;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002482 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002483 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002484 in->standby = 1;
2485 in->channel_mask = config->channel_mask;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002486 in->capture_handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002487
2488 /* Update config params with the requested sample rate and channels */
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002489 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
2490 if (config->sample_rate == 0)
2491 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2492 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2493 config->sample_rate != 8000) {
2494 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2495 ret = -EINVAL;
2496 goto err_open;
2497 }
2498 if (config->format == AUDIO_FORMAT_DEFAULT)
2499 config->format = AUDIO_FORMAT_PCM_16_BIT;
2500 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2501 config->format = AUDIO_FORMAT_PCM_16_BIT;
2502 ret = -EINVAL;
2503 goto err_open;
2504 }
2505
2506 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
2507 in->config = pcm_config_afe_proxy_record;
2508 } else {
2509 in->usecase = USECASE_AUDIO_RECORD;
2510 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
2511 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
2512 is_low_latency = true;
Glenn Kasten4f993392014-05-14 07:30:48 -07002513#if LOW_LATENCY_CAPTURE_USE_CASE
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002514 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Glenn Kasten4f993392014-05-14 07:30:48 -07002515#endif
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002516 }
2517 in->config = pcm_config_audio_capture;
2518
2519 frame_size = audio_stream_in_frame_size(&in->stream);
2520 buffer_size = get_input_buffer_size(config->sample_rate,
2521 config->format,
2522 channel_count,
2523 is_low_latency);
2524 in->config.period_size = buffer_size / frame_size;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002525 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002526 in->config.channels = channel_count;
2527 in->config.rate = config->sample_rate;
2528
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002529 /* This stream could be for sound trigger lab,
2530 get sound trigger pcm if present */
2531 audio_extn_sound_trigger_check_and_get_session(in);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002532
2533 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002534 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002535 return 0;
2536
2537err_open:
2538 free(in);
2539 *stream_in = NULL;
2540 return ret;
2541}
2542
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002543static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002544 struct audio_stream_in *stream)
2545{
Eric Laurent994a6932013-07-17 11:51:42 -07002546 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002547
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002548 in_standby(&stream->common);
2549 free(stream);
2550
2551 return;
2552}
2553
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002554static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002555{
2556 return 0;
2557}
2558
Andy Hung31aca912014-03-20 17:14:59 -07002559/* verifies input and output devices and their capabilities.
2560 *
2561 * This verification is required when enabling extended bit-depth or
2562 * sampling rates, as not all qcom products support it.
2563 *
2564 * Suitable for calling only on initialization such as adev_open().
2565 * It fills the audio_device use_case_table[] array.
2566 *
2567 * Has a side-effect that it needs to configure audio routing / devices
2568 * in order to power up the devices and read the device parameters.
2569 * It does not acquire any hw device lock. Should restore the devices
2570 * back to "normal state" upon completion.
2571 */
2572static int adev_verify_devices(struct audio_device *adev)
2573{
2574 /* enumeration is a bit difficult because one really wants to pull
2575 * the use_case, device id, etc from the hidden pcm_device_table[].
2576 * In this case there are the following use cases and device ids.
2577 *
2578 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
2579 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
2580 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
2581 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
2582 * [USECASE_AUDIO_RECORD] = {0, 0},
2583 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
2584 * [USECASE_VOICE_CALL] = {2, 2},
2585 *
2586 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
2587 * USECASE_VOICE_CALL omitted, but possible for either input or output.
2588 */
2589
2590 /* should be the usecases enabled in adev_open_input_stream() */
2591 static const int test_in_usecases[] = {
2592 USECASE_AUDIO_RECORD,
2593 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
2594 };
2595 /* should be the usecases enabled in adev_open_output_stream()*/
2596 static const int test_out_usecases[] = {
2597 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
2598 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
2599 };
2600 static const usecase_type_t usecase_type_by_dir[] = {
2601 PCM_PLAYBACK,
2602 PCM_CAPTURE,
2603 };
2604 static const unsigned flags_by_dir[] = {
2605 PCM_OUT,
2606 PCM_IN,
2607 };
2608
2609 size_t i;
2610 unsigned dir;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002611 const unsigned card_id = adev->snd_card;
Andy Hung31aca912014-03-20 17:14:59 -07002612 char info[512]; /* for possible debug info */
2613
2614 for (dir = 0; dir < 2; ++dir) {
2615 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
2616 const unsigned flags_dir = flags_by_dir[dir];
2617 const size_t testsize =
2618 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
2619 const int *testcases =
2620 dir ? test_in_usecases : test_out_usecases;
2621 const audio_devices_t audio_device =
2622 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
2623
2624 for (i = 0; i < testsize; ++i) {
2625 const audio_usecase_t audio_usecase = testcases[i];
2626 int device_id;
2627 snd_device_t snd_device;
2628 struct pcm_params **pparams;
2629 struct stream_out out;
2630 struct stream_in in;
2631 struct audio_usecase uc_info;
2632 int retval;
2633
2634 pparams = &adev->use_case_table[audio_usecase];
2635 pcm_params_free(*pparams); /* can accept null input */
2636 *pparams = NULL;
2637
2638 /* find the device ID for the use case (signed, for error) */
2639 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
2640 if (device_id < 0)
2641 continue;
2642
2643 /* prepare structures for device probing */
2644 memset(&uc_info, 0, sizeof(uc_info));
2645 uc_info.id = audio_usecase;
2646 uc_info.type = usecase_type;
2647 if (dir) {
2648 adev->active_input = &in;
2649 memset(&in, 0, sizeof(in));
2650 in.device = audio_device;
2651 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
2652 uc_info.stream.in = &in;
2653 } else {
2654 adev->active_input = NULL;
2655 }
2656 memset(&out, 0, sizeof(out));
2657 out.devices = audio_device; /* only field needed in select_devices */
2658 uc_info.stream.out = &out;
2659 uc_info.devices = audio_device;
2660 uc_info.in_snd_device = SND_DEVICE_NONE;
2661 uc_info.out_snd_device = SND_DEVICE_NONE;
2662 list_add_tail(&adev->usecase_list, &uc_info.list);
2663
2664 /* select device - similar to start_(in/out)put_stream() */
2665 retval = select_devices(adev, audio_usecase);
2666 if (retval >= 0) {
2667 *pparams = pcm_params_get(card_id, device_id, flags_dir);
2668#if LOG_NDEBUG == 0
2669 if (*pparams) {
2670 ALOGV("%s: (%s) card %d device %d", __func__,
2671 dir ? "input" : "output", card_id, device_id);
2672 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
2673 ALOGV(info); /* print parameters */
2674 } else {
2675 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
2676 }
2677#endif
2678 }
2679
2680 /* deselect device - similar to stop_(in/out)put_stream() */
2681 /* 1. Get and set stream specific mixer controls */
Glenn Kastene7137302014-04-28 15:12:18 -07002682 retval = disable_audio_route(adev, &uc_info);
Andy Hung31aca912014-03-20 17:14:59 -07002683 /* 2. Disable the rx device */
2684 retval = disable_snd_device(adev,
Glenn Kastene7137302014-04-28 15:12:18 -07002685 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
Andy Hung31aca912014-03-20 17:14:59 -07002686 list_remove(&uc_info.list);
2687 }
2688 }
2689 adev->active_input = NULL; /* restore adev state */
2690 return 0;
2691}
2692
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002693static int adev_close(hw_device_t *device)
2694{
Andy Hung31aca912014-03-20 17:14:59 -07002695 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002696 struct audio_device *adev = (struct audio_device *)device;
2697 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002698 free(adev->snd_dev_ref_cnt);
2699 platform_deinit(adev->platform);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002700 audio_extn_extspk_deinit(adev->extspk);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002701 audio_extn_sound_trigger_deinit(adev);
Andy Hung31aca912014-03-20 17:14:59 -07002702 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
2703 pcm_params_free(adev->use_case_table[i]);
2704 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002705 free(device);
2706 return 0;
2707}
2708
Glenn Kasten4f993392014-05-14 07:30:48 -07002709/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
2710 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
2711 * just that it _might_ work.
2712 */
2713static int period_size_is_plausible_for_low_latency(int period_size)
2714{
2715 switch (period_size) {
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002716 case 48:
2717 case 96:
2718 case 144:
Glenn Kasten4f993392014-05-14 07:30:48 -07002719 case 160:
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002720 case 192:
Glenn Kasten4f993392014-05-14 07:30:48 -07002721 case 240:
2722 case 320:
2723 case 480:
2724 return 1;
2725 default:
2726 return 0;
2727 }
2728}
2729
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002730static int adev_open(const hw_module_t *module, const char *name,
2731 hw_device_t **device)
2732{
2733 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002734 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002735
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002736 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002737 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2738
2739 adev = calloc(1, sizeof(struct audio_device));
2740
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002741 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2742
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002743 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2744 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2745 adev->device.common.module = (struct hw_module_t *)module;
2746 adev->device.common.close = adev_close;
2747
2748 adev->device.init_check = adev_init_check;
2749 adev->device.set_voice_volume = adev_set_voice_volume;
2750 adev->device.set_master_volume = adev_set_master_volume;
2751 adev->device.get_master_volume = adev_get_master_volume;
2752 adev->device.set_master_mute = adev_set_master_mute;
2753 adev->device.get_master_mute = adev_get_master_mute;
2754 adev->device.set_mode = adev_set_mode;
2755 adev->device.set_mic_mute = adev_set_mic_mute;
2756 adev->device.get_mic_mute = adev_get_mic_mute;
2757 adev->device.set_parameters = adev_set_parameters;
2758 adev->device.get_parameters = adev_get_parameters;
2759 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2760 adev->device.open_output_stream = adev_open_output_stream;
2761 adev->device.close_output_stream = adev_close_output_stream;
2762 adev->device.open_input_stream = adev_open_input_stream;
2763 adev->device.close_input_stream = adev_close_input_stream;
2764 adev->device.dump = adev_dump;
2765
2766 /* Set the default route before the PCM stream is opened */
2767 pthread_mutex_lock(&adev->lock);
2768 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002769 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002770 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002771 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002772 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002773 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002774 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002775 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002776 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002777 pthread_mutex_unlock(&adev->lock);
2778
2779 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002780 adev->platform = platform_init(adev);
2781 if (!adev->platform) {
2782 free(adev->snd_dev_ref_cnt);
2783 free(adev);
2784 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2785 *device = NULL;
2786 return -EINVAL;
2787 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002788
Eric Laurent0499d4f2014-08-25 22:39:29 -05002789 adev->extspk = audio_extn_extspk_init(adev);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002790 audio_extn_sound_trigger_init(adev);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002791
Eric Laurentc4aef752013-09-12 17:45:53 -07002792 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2793 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2794 if (adev->visualizer_lib == NULL) {
2795 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2796 } else {
2797 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2798 adev->visualizer_start_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002799 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002800 "visualizer_hal_start_output");
2801 adev->visualizer_stop_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002802 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002803 "visualizer_hal_stop_output");
2804 }
2805 }
2806
Haynes Mathew George41f86652014-06-17 14:22:15 -07002807 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2808 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2809 if (adev->offload_effects_lib == NULL) {
2810 ALOGE("%s: DLOPEN failed for %s", __func__,
2811 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2812 } else {
2813 ALOGV("%s: DLOPEN successful for %s", __func__,
2814 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2815 adev->offload_effects_start_output =
2816 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2817 "offload_effects_bundle_hal_start_output");
2818 adev->offload_effects_stop_output =
2819 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2820 "offload_effects_bundle_hal_stop_output");
2821 }
2822 }
2823
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002824 adev->bt_wb_speech_enabled = false;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002825 adev->enable_voicerx = false;
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002826
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002827 *device = &adev->device.common;
Andy Hung31aca912014-03-20 17:14:59 -07002828 if (k_enable_extended_precision)
2829 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002830
Glenn Kasten4f993392014-05-14 07:30:48 -07002831 char value[PROPERTY_VALUE_MAX];
2832 int trial;
2833 if (property_get("audio_hal.period_size", value, NULL) > 0) {
2834 trial = atoi(value);
2835 if (period_size_is_plausible_for_low_latency(trial)) {
2836 pcm_config_low_latency.period_size = trial;
2837 pcm_config_low_latency.start_threshold = trial / 4;
2838 pcm_config_low_latency.avail_min = trial / 4;
2839 configured_low_latency_capture_period_size = trial;
2840 }
2841 }
2842 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
2843 trial = atoi(value);
2844 if (period_size_is_plausible_for_low_latency(trial)) {
2845 configured_low_latency_capture_period_size = trial;
2846 }
2847 }
2848
Eric Laurent994a6932013-07-17 11:51:42 -07002849 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002850 return 0;
2851}
2852
2853static struct hw_module_methods_t hal_module_methods = {
2854 .open = adev_open,
2855};
2856
2857struct audio_module HAL_MODULE_INFO_SYM = {
2858 .common = {
2859 .tag = HARDWARE_MODULE_TAG,
2860 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2861 .hal_api_version = HARDWARE_HAL_API_VERSION,
2862 .id = AUDIO_HARDWARE_MODULE_ID,
2863 .name = "QCOM Audio HAL",
2864 .author = "Code Aurora Forum",
2865 .methods = &hal_module_methods,
2866 },
2867};