blob: e9baf5e96bbabc04d1f032331d9b4fb15f2f6708 [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
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700810/* must be called with out->lock locked */
811static int send_offload_cmd_l(struct stream_out* out, int command)
812{
813 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
814
815 ALOGVV("%s %d", __func__, command);
816
817 cmd->cmd = command;
818 list_add_tail(&out->offload_cmd_list, &cmd->node);
819 pthread_cond_signal(&out->offload_cond);
820 return 0;
821}
822
823/* must be called iwth out->lock locked */
824static void stop_compressed_output_l(struct stream_out *out)
825{
826 out->offload_state = OFFLOAD_STATE_IDLE;
827 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700828 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700829 if (out->compr != NULL) {
830 compress_stop(out->compr);
831 while (out->offload_thread_blocked) {
832 pthread_cond_wait(&out->cond, &out->lock);
833 }
834 }
835}
836
837static void *offload_thread_loop(void *context)
838{
839 struct stream_out *out = (struct stream_out *) context;
840 struct listnode *item;
841
842 out->offload_state = OFFLOAD_STATE_IDLE;
843 out->playback_started = 0;
844
845 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
846 set_sched_policy(0, SP_FOREGROUND);
847 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
848
849 ALOGV("%s", __func__);
850 pthread_mutex_lock(&out->lock);
851 for (;;) {
852 struct offload_cmd *cmd = NULL;
853 stream_callback_event_t event;
854 bool send_callback = false;
855
856 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
857 __func__, list_empty(&out->offload_cmd_list),
858 out->offload_state);
859 if (list_empty(&out->offload_cmd_list)) {
860 ALOGV("%s SLEEPING", __func__);
861 pthread_cond_wait(&out->offload_cond, &out->lock);
862 ALOGV("%s RUNNING", __func__);
863 continue;
864 }
865
866 item = list_head(&out->offload_cmd_list);
867 cmd = node_to_item(item, struct offload_cmd, node);
868 list_remove(item);
869
870 ALOGVV("%s STATE %d CMD %d out->compr %p",
871 __func__, out->offload_state, cmd->cmd, out->compr);
872
873 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
874 free(cmd);
875 break;
876 }
877
878 if (out->compr == NULL) {
879 ALOGE("%s: Compress handle is NULL", __func__);
880 pthread_cond_signal(&out->cond);
881 continue;
882 }
883 out->offload_thread_blocked = true;
884 pthread_mutex_unlock(&out->lock);
885 send_callback = false;
886 switch(cmd->cmd) {
887 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
888 compress_wait(out->compr, -1);
889 send_callback = true;
890 event = STREAM_CBK_EVENT_WRITE_READY;
891 break;
892 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700893 compress_next_track(out->compr);
894 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700895 send_callback = true;
896 event = STREAM_CBK_EVENT_DRAIN_READY;
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800897 /* Resend the metadata for next iteration */
898 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700899 break;
900 case OFFLOAD_CMD_DRAIN:
901 compress_drain(out->compr);
902 send_callback = true;
903 event = STREAM_CBK_EVENT_DRAIN_READY;
904 break;
905 default:
906 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
907 break;
908 }
909 pthread_mutex_lock(&out->lock);
910 out->offload_thread_blocked = false;
911 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700912 if (send_callback) {
Ravi Kumar Alamandacc4f6bf2014-12-02 19:21:51 -0800913 ALOGVV("%s: sending offload_callback event %d", __func__, event);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700914 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700915 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700916 free(cmd);
917 }
918
919 pthread_cond_signal(&out->cond);
920 while (!list_empty(&out->offload_cmd_list)) {
921 item = list_head(&out->offload_cmd_list);
922 list_remove(item);
923 free(node_to_item(item, struct offload_cmd, node));
924 }
925 pthread_mutex_unlock(&out->lock);
926
927 return NULL;
928}
929
930static int create_offload_callback_thread(struct stream_out *out)
931{
932 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
933 list_init(&out->offload_cmd_list);
934 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
935 offload_thread_loop, out);
936 return 0;
937}
938
939static int destroy_offload_callback_thread(struct stream_out *out)
940{
941 pthread_mutex_lock(&out->lock);
942 stop_compressed_output_l(out);
943 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
944
945 pthread_mutex_unlock(&out->lock);
946 pthread_join(out->offload_thread, (void **) NULL);
947 pthread_cond_destroy(&out->offload_cond);
948
949 return 0;
950}
951
Eric Laurent07eeafd2013-10-06 12:52:49 -0700952static bool allow_hdmi_channel_config(struct audio_device *adev)
953{
954 struct listnode *node;
955 struct audio_usecase *usecase;
956 bool ret = true;
957
958 list_for_each(node, &adev->usecase_list) {
959 usecase = node_to_item(node, struct audio_usecase, list);
960 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
961 /*
962 * If voice call is already existing, do not proceed further to avoid
963 * disabling/enabling both RX and TX devices, CSD calls, etc.
964 * Once the voice call done, the HDMI channels can be configured to
965 * max channels of remaining use cases.
966 */
967 if (usecase->id == USECASE_VOICE_CALL) {
968 ALOGD("%s: voice call is active, no change in HDMI channels",
969 __func__);
970 ret = false;
971 break;
972 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
973 ALOGD("%s: multi channel playback is active, "
974 "no change in HDMI channels", __func__);
975 ret = false;
976 break;
977 }
978 }
979 }
980 return ret;
981}
982
983static int check_and_set_hdmi_channels(struct audio_device *adev,
984 unsigned int channels)
985{
986 struct listnode *node;
987 struct audio_usecase *usecase;
988
989 /* Check if change in HDMI channel config is allowed */
990 if (!allow_hdmi_channel_config(adev))
991 return 0;
992
993 if (channels == adev->cur_hdmi_channels) {
994 ALOGD("%s: Requested channels are same as current", __func__);
995 return 0;
996 }
997
998 platform_set_hdmi_channels(adev->platform, channels);
999 adev->cur_hdmi_channels = channels;
1000
1001 /*
1002 * Deroute all the playback streams routed to HDMI so that
1003 * the back end is deactivated. Note that backend will not
1004 * be deactivated if any one stream is connected to it.
1005 */
1006 list_for_each(node, &adev->usecase_list) {
1007 usecase = node_to_item(node, struct audio_usecase, list);
1008 if (usecase->type == PCM_PLAYBACK &&
1009 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001010 disable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001011 }
1012 }
1013
1014 /*
1015 * Enable all the streams disabled above. Now the HDMI backend
1016 * will be activated with new channel configuration
1017 */
1018 list_for_each(node, &adev->usecase_list) {
1019 usecase = node_to_item(node, struct audio_usecase, list);
1020 if (usecase->type == PCM_PLAYBACK &&
1021 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001022 enable_audio_route(adev, usecase);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001023 }
1024 }
1025
1026 return 0;
1027}
1028
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001029static int stop_output_stream(struct stream_out *out)
1030{
1031 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001032 struct audio_usecase *uc_info;
1033 struct audio_device *adev = out->dev;
1034
Eric Laurent994a6932013-07-17 11:51:42 -07001035 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001036 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001037 uc_info = get_usecase_from_list(adev, out->usecase);
1038 if (uc_info == NULL) {
1039 ALOGE("%s: Could not find the usecase (%d) in the list",
1040 __func__, out->usecase);
1041 return -EINVAL;
1042 }
1043
Haynes Mathew George41f86652014-06-17 14:22:15 -07001044 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1045 if (adev->visualizer_stop_output != NULL)
1046 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1047 if (adev->offload_effects_stop_output != NULL)
1048 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1049 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001050
Eric Laurent150dbfe2013-02-27 14:31:02 -08001051 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001052 disable_audio_route(adev, uc_info);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001053
1054 /* 2. Disable the rx device */
Ravi Kumar Alamandac38e4522014-04-14 11:46:35 -07001055 disable_snd_device(adev, uc_info->out_snd_device);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001056
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001057 list_remove(&uc_info->list);
1058 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001059
Eric Laurent0499d4f2014-08-25 22:39:29 -05001060 audio_extn_extspk_update(adev->extspk);
1061
Eric Laurent07eeafd2013-10-06 12:52:49 -07001062 /* Must be called after removing the usecase from list */
1063 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1064 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1065
Eric Laurent994a6932013-07-17 11:51:42 -07001066 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001067 return ret;
1068}
1069
1070int start_output_stream(struct stream_out *out)
1071{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001072 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001073 struct audio_usecase *uc_info;
1074 struct audio_device *adev = out->dev;
1075
Eric Laurent994a6932013-07-17 11:51:42 -07001076 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001077 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001078 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001079 if (out->pcm_device_id < 0) {
1080 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1081 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001082 ret = -EINVAL;
1083 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001084 }
1085
1086 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1087 uc_info->id = out->usecase;
1088 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001089 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001090 uc_info->devices = out->devices;
1091 uc_info->in_snd_device = SND_DEVICE_NONE;
1092 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001093
Eric Laurent07eeafd2013-10-06 12:52:49 -07001094 /* This must be called before adding this usecase to the list */
1095 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1096 check_and_set_hdmi_channels(adev, out->config.channels);
1097
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001098 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001099
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001100 select_devices(adev, out->usecase);
1101
Eric Laurent0499d4f2014-08-25 22:39:29 -05001102 audio_extn_extspk_update(adev->extspk);
1103
Andy Hung31aca912014-03-20 17:14:59 -07001104 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001105 __func__, adev->snd_card, out->pcm_device_id, out->config.format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001106 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001107 unsigned int flags = PCM_OUT;
1108 unsigned int pcm_open_retry_count = 0;
1109 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1110 flags |= PCM_MMAP | PCM_NOIRQ;
1111 pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
1112 } else
1113 flags |= PCM_MONOTONIC;
1114
1115 while (1) {
1116 out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
1117 flags, &out->config);
1118 if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
1119 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1120 if (out->pcm != NULL) {
1121 pcm_close(out->pcm);
1122 out->pcm = NULL;
1123 }
1124 if (pcm_open_retry_count-- == 0) {
1125 ret = -EIO;
1126 goto error_open;
1127 }
1128 usleep(PROXY_OPEN_WAIT_TIME * 1000);
1129 continue;
1130 }
1131 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001132 }
1133 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001134 out->pcm = NULL;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07001135 out->compr = compress_open(adev->snd_card, out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001136 COMPRESS_IN, &out->compr_config);
1137 if (out->compr && !is_compress_ready(out->compr)) {
1138 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1139 compress_close(out->compr);
1140 out->compr = NULL;
1141 ret = -EIO;
1142 goto error_open;
1143 }
1144 if (out->offload_callback)
1145 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001146
1147 if (adev->visualizer_start_output != NULL)
Haynes Mathew George41f86652014-06-17 14:22:15 -07001148 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1149 if (adev->offload_effects_start_output != NULL)
1150 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001151 }
Eric Laurent994a6932013-07-17 11:51:42 -07001152 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001153 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001154error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001155 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001156error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001157 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001158}
1159
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001160static int check_input_parameters(uint32_t sample_rate,
1161 audio_format_t format,
1162 int channel_count)
1163{
1164 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1165
1166 if ((channel_count < 1) || (channel_count > 2)) return -EINVAL;
1167
1168 switch (sample_rate) {
1169 case 8000:
1170 case 11025:
1171 case 12000:
1172 case 16000:
1173 case 22050:
1174 case 24000:
1175 case 32000:
1176 case 44100:
1177 case 48000:
1178 break;
1179 default:
1180 return -EINVAL;
1181 }
1182
1183 return 0;
1184}
1185
1186static size_t get_input_buffer_size(uint32_t sample_rate,
1187 audio_format_t format,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001188 int channel_count,
1189 bool is_low_latency)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001190{
1191 size_t size = 0;
1192
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001193 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1194 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001195
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001196 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07001197 if (is_low_latency)
Glenn Kasten4f993392014-05-14 07:30:48 -07001198 size = configured_low_latency_capture_period_size;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001199 /* ToDo: should use frame_size computed based on the format and
1200 channel_count here. */
1201 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001202
Glenn Kasten4f993392014-05-14 07:30:48 -07001203 /* make sure the size is multiple of 32 bytes
1204 * At 48 kHz mono 16-bit PCM:
1205 * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
1206 * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
1207 */
1208 size += 0x1f;
1209 size &= ~0x1f;
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001210
1211 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001212}
1213
1214static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1215{
1216 struct stream_out *out = (struct stream_out *)stream;
1217
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001218 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001219}
1220
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001221static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001222{
1223 return -ENOSYS;
1224}
1225
1226static size_t out_get_buffer_size(const struct audio_stream *stream)
1227{
1228 struct stream_out *out = (struct stream_out *)stream;
1229
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001230 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1231 return out->compr_config.fragment_size;
1232 }
Eric Laurentfdf296a2014-07-03 16:41:51 -07001233 return out->config.period_size *
1234 audio_stream_out_frame_size((const struct audio_stream_out *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001235}
1236
1237static uint32_t out_get_channels(const struct audio_stream *stream)
1238{
1239 struct stream_out *out = (struct stream_out *)stream;
1240
1241 return out->channel_mask;
1242}
1243
1244static audio_format_t out_get_format(const struct audio_stream *stream)
1245{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001246 struct stream_out *out = (struct stream_out *)stream;
1247
1248 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001249}
1250
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001251static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001252{
1253 return -ENOSYS;
1254}
1255
1256static int out_standby(struct audio_stream *stream)
1257{
1258 struct stream_out *out = (struct stream_out *)stream;
1259 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001260
Eric Laurent994a6932013-07-17 11:51:42 -07001261 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001262 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001263
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001264 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001265 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001266 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001267 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001268 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1269 if (out->pcm) {
1270 pcm_close(out->pcm);
1271 out->pcm = NULL;
1272 }
1273 } else {
1274 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001275 out->gapless_mdata.encoder_delay = 0;
1276 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001277 if (out->compr != NULL) {
1278 compress_close(out->compr);
1279 out->compr = NULL;
1280 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001281 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001282 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001283 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001284 }
1285 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001286 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001287 return 0;
1288}
1289
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001290static int out_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001291{
1292 return 0;
1293}
1294
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001295static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1296{
1297 int ret = 0;
1298 char value[32];
1299 struct compr_gapless_mdata tmp_mdata;
1300
1301 if (!out || !parms) {
1302 return -EINVAL;
1303 }
1304
1305 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1306 if (ret >= 0) {
1307 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1308 } else {
1309 return -EINVAL;
1310 }
1311
1312 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1313 if (ret >= 0) {
1314 tmp_mdata.encoder_padding = atoi(value);
1315 } else {
1316 return -EINVAL;
1317 }
1318
1319 out->gapless_mdata = tmp_mdata;
1320 out->send_new_metadata = 1;
1321 ALOGV("%s new encoder delay %u and padding %u", __func__,
1322 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1323
1324 return 0;
1325}
1326
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001327static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
1328{
1329 return out == adev->primary_output || out == adev->voice_tx_output;
1330}
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001331
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001332static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1333{
1334 struct stream_out *out = (struct stream_out *)stream;
1335 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001336 struct audio_usecase *usecase;
1337 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001338 struct str_parms *parms;
1339 char value[32];
1340 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001341 bool select_new_device = false;
Eric Laurent03f09432014-03-25 18:09:11 -07001342 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001343
sangwoobc677242013-08-08 16:53:43 +09001344 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001345 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001346 parms = str_parms_create_str(kvpairs);
1347 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1348 if (ret >= 0) {
1349 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001350 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001351 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001352
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001353 /*
1354 * When HDMI cable is unplugged the music playback is paused and
1355 * the policy manager sends routing=0. But the audioflinger
1356 * continues to write data until standby time (3sec).
1357 * As the HDMI core is turned off, the write gets blocked.
1358 * Avoid this by routing audio to speaker until standby.
1359 */
1360 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1361 val == AUDIO_DEVICE_NONE) {
1362 val = AUDIO_DEVICE_OUT_SPEAKER;
1363 }
1364
1365 /*
1366 * select_devices() call below switches all the usecases on the same
Ravi Kumar Alamandab7ea4f52015-06-08 16:44:05 -07001367 * backend to the new device. Refer to check_and_route_playback_usecases() in
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001368 * the select_devices(). But how do we undo this?
1369 *
1370 * For example, music playback is active on headset (deep-buffer usecase)
1371 * and if we go to ringtones and select a ringtone, low-latency usecase
1372 * will be started on headset+speaker. As we can't enable headset+speaker
1373 * and headset devices at the same time, select_devices() switches the music
1374 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1375 * So when the ringtone playback is completed, how do we undo the same?
1376 *
1377 * We are relying on the out_set_parameters() call on deep-buffer output,
1378 * once the ringtone playback is ended.
1379 * NOTE: We should not check if the current devices are same as new devices.
1380 * Because select_devices() must be called to switch back the music
1381 * playback to headset.
1382 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001383 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001384 out->devices = val;
1385
1386 if (!out->standby)
1387 select_devices(adev, out->usecase);
1388
Eric Laurenta7657192014-10-09 21:09:33 -07001389 if (output_drives_call(adev, out)) {
1390 if (!voice_is_in_call(adev)) {
1391 if (adev->mode == AUDIO_MODE_IN_CALL) {
1392 adev->current_call_output = out;
1393 ret = voice_start_call(adev);
1394 }
1395 } else {
1396 adev->current_call_output = out;
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001397 voice_update_devices_for_all_voice_usecases(adev);
Eric Laurenta7657192014-10-09 21:09:33 -07001398 }
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001399 }
1400 }
1401
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001402 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001403 pthread_mutex_unlock(&out->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05001404
1405 /*handles device and call state changes*/
1406 audio_extn_extspk_update(adev->extspk);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001407 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001408
1409 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1410 parse_compress_metadata(out, parms);
1411 }
1412
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001413 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001414 ALOGV("%s: exit: code(%d)", __func__, status);
1415 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001416}
1417
1418static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1419{
1420 struct stream_out *out = (struct stream_out *)stream;
1421 struct str_parms *query = str_parms_create_str(keys);
1422 char *str;
1423 char value[256];
1424 struct str_parms *reply = str_parms_create();
1425 size_t i, j;
1426 int ret;
1427 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001428 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001429 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1430 if (ret >= 0) {
1431 value[0] = '\0';
1432 i = 0;
1433 while (out->supported_channel_masks[i] != 0) {
1434 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1435 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1436 if (!first) {
1437 strcat(value, "|");
1438 }
1439 strcat(value, out_channels_name_to_enum_table[j].name);
1440 first = false;
1441 break;
1442 }
1443 }
1444 i++;
1445 }
1446 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1447 str = str_parms_to_str(reply);
1448 } else {
1449 str = strdup(keys);
1450 }
1451 str_parms_destroy(query);
1452 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001453 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001454 return str;
1455}
1456
1457static uint32_t out_get_latency(const struct audio_stream_out *stream)
1458{
1459 struct stream_out *out = (struct stream_out *)stream;
1460
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001461 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1462 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1463
1464 return (out->config.period_count * out->config.period_size * 1000) /
1465 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001466}
1467
1468static int out_set_volume(struct audio_stream_out *stream, float left,
1469 float right)
1470{
Eric Laurenta9024de2013-04-04 09:19:12 -07001471 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001472 int volume[2];
1473
Eric Laurenta9024de2013-04-04 09:19:12 -07001474 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1475 /* only take left channel into account: the API is for stereo anyway */
1476 out->muted = (left == 0.0f);
1477 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001478 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1479 const char *mixer_ctl_name = "Compress Playback Volume";
1480 struct audio_device *adev = out->dev;
1481 struct mixer_ctl *ctl;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001482 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1483 if (!ctl) {
Haynes Mathew George20bcfa82014-06-25 13:37:03 -07001484 /* try with the control based on device id */
1485 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1486 PCM_PLAYBACK);
1487 char ctl_name[128] = {0};
1488 snprintf(ctl_name, sizeof(ctl_name),
1489 "Compress Playback %d Volume", pcm_device_id);
1490 ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name);
1491 if (!ctl) {
1492 ALOGE("%s: Could not get volume ctl mixer cmd", __func__);
1493 return -EINVAL;
1494 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001495 }
1496 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1497 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1498 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1499 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001500 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001501
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001502 return -ENOSYS;
1503}
1504
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07001505#ifdef NO_AUDIO_OUT
1506static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
1507 const void *buffer, size_t bytes)
1508{
1509 struct stream_out *out = (struct stream_out *)stream;
1510
1511 /* No Output device supported other than BT for playback.
1512 * Sleep for the amount of buffer duration
1513 */
1514 pthread_mutex_lock(&out->lock);
1515 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1516 out_get_sample_rate(&out->stream.common));
1517 pthread_mutex_unlock(&out->lock);
1518 return bytes;
1519}
1520#endif
1521
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001522static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1523 size_t bytes)
1524{
1525 struct stream_out *out = (struct stream_out *)stream;
1526 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001527 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001528
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001529 pthread_mutex_lock(&out->lock);
1530 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001531 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001532 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001533 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001534 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001535 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001536 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001537 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001538 goto exit;
1539 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001540 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001541
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001542 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001543 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1544 if (out->send_new_metadata) {
1545 ALOGVV("send new gapless metadata");
1546 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1547 out->send_new_metadata = 0;
1548 }
1549
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001550 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001551 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001552 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001553 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1554 }
1555 if (!out->playback_started) {
1556 compress_start(out->compr);
1557 out->playback_started = 1;
1558 out->offload_state = OFFLOAD_STATE_PLAYING;
1559 }
1560 pthread_mutex_unlock(&out->lock);
1561 return ret;
1562 } else {
1563 if (out->pcm) {
1564 if (out->muted)
1565 memset((void *)buffer, 0, bytes);
1566 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001567 if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
1568 ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes);
1569 }
1570 else
1571 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001572 if (ret == 0)
1573 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001574 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001575 }
1576
1577exit:
1578 pthread_mutex_unlock(&out->lock);
1579
1580 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001581 if (out->pcm)
1582 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001583 out_standby(&out->stream.common);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001584 usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001585 out_get_sample_rate(&out->stream.common));
1586 }
1587 return bytes;
1588}
1589
1590static int out_get_render_position(const struct audio_stream_out *stream,
1591 uint32_t *dsp_frames)
1592{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001593 struct stream_out *out = (struct stream_out *)stream;
1594 *dsp_frames = 0;
1595 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1596 pthread_mutex_lock(&out->lock);
1597 if (out->compr != NULL) {
1598 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1599 &out->sample_rate);
1600 ALOGVV("%s rendered frames %d sample_rate %d",
1601 __func__, *dsp_frames, out->sample_rate);
1602 }
1603 pthread_mutex_unlock(&out->lock);
1604 return 0;
1605 } else
1606 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001607}
1608
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001609static int out_add_audio_effect(const struct audio_stream *stream __unused,
1610 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001611{
1612 return 0;
1613}
1614
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001615static int out_remove_audio_effect(const struct audio_stream *stream __unused,
1616 effect_handle_t effect __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001617{
1618 return 0;
1619}
1620
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001621static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused,
1622 int64_t *timestamp __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001623{
1624 return -EINVAL;
1625}
1626
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001627static int out_get_presentation_position(const struct audio_stream_out *stream,
1628 uint64_t *frames, struct timespec *timestamp)
1629{
1630 struct stream_out *out = (struct stream_out *)stream;
1631 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001632 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001633
1634 pthread_mutex_lock(&out->lock);
1635
Eric Laurent949a0892013-09-20 09:20:13 -07001636 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1637 if (out->compr != NULL) {
1638 compress_get_tstamp(out->compr, &dsp_frames,
1639 &out->sample_rate);
1640 ALOGVV("%s rendered frames %ld sample_rate %d",
1641 __func__, dsp_frames, out->sample_rate);
1642 *frames = dsp_frames;
1643 ret = 0;
1644 /* this is the best we can do */
1645 clock_gettime(CLOCK_MONOTONIC, timestamp);
1646 }
1647 } else {
1648 if (out->pcm) {
1649 size_t avail;
1650 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1651 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001652 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001653 // This adjustment accounts for buffering after app processor.
1654 // It is based on estimated DSP latency per use case, rather than exact.
1655 signed_frames -=
1656 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1657
Eric Laurent949a0892013-09-20 09:20:13 -07001658 // It would be unusual for this value to be negative, but check just in case ...
1659 if (signed_frames >= 0) {
1660 *frames = signed_frames;
1661 ret = 0;
1662 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001663 }
1664 }
1665 }
1666
1667 pthread_mutex_unlock(&out->lock);
1668
1669 return ret;
1670}
1671
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001672static int out_set_callback(struct audio_stream_out *stream,
1673 stream_callback_t callback, void *cookie)
1674{
1675 struct stream_out *out = (struct stream_out *)stream;
1676
1677 ALOGV("%s", __func__);
1678 pthread_mutex_lock(&out->lock);
1679 out->offload_callback = callback;
1680 out->offload_cookie = cookie;
1681 pthread_mutex_unlock(&out->lock);
1682 return 0;
1683}
1684
1685static int out_pause(struct audio_stream_out* stream)
1686{
1687 struct stream_out *out = (struct stream_out *)stream;
1688 int status = -ENOSYS;
1689 ALOGV("%s", __func__);
1690 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1691 pthread_mutex_lock(&out->lock);
1692 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1693 status = compress_pause(out->compr);
1694 out->offload_state = OFFLOAD_STATE_PAUSED;
1695 }
1696 pthread_mutex_unlock(&out->lock);
1697 }
1698 return status;
1699}
1700
1701static int out_resume(struct audio_stream_out* stream)
1702{
1703 struct stream_out *out = (struct stream_out *)stream;
1704 int status = -ENOSYS;
1705 ALOGV("%s", __func__);
1706 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1707 status = 0;
1708 pthread_mutex_lock(&out->lock);
1709 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1710 status = compress_resume(out->compr);
1711 out->offload_state = OFFLOAD_STATE_PLAYING;
1712 }
1713 pthread_mutex_unlock(&out->lock);
1714 }
1715 return status;
1716}
1717
1718static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1719{
1720 struct stream_out *out = (struct stream_out *)stream;
1721 int status = -ENOSYS;
1722 ALOGV("%s", __func__);
1723 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1724 pthread_mutex_lock(&out->lock);
1725 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1726 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1727 else
1728 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1729 pthread_mutex_unlock(&out->lock);
1730 }
1731 return status;
1732}
1733
1734static int out_flush(struct audio_stream_out* stream)
1735{
1736 struct stream_out *out = (struct stream_out *)stream;
1737 ALOGV("%s", __func__);
1738 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1739 pthread_mutex_lock(&out->lock);
1740 stop_compressed_output_l(out);
1741 pthread_mutex_unlock(&out->lock);
1742 return 0;
1743 }
1744 return -ENOSYS;
1745}
1746
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001747/** audio_stream_in implementation **/
1748static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1749{
1750 struct stream_in *in = (struct stream_in *)stream;
1751
1752 return in->config.rate;
1753}
1754
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001755static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001756{
1757 return -ENOSYS;
1758}
1759
1760static size_t in_get_buffer_size(const struct audio_stream *stream)
1761{
1762 struct stream_in *in = (struct stream_in *)stream;
1763
Eric Laurentfdf296a2014-07-03 16:41:51 -07001764 return in->config.period_size *
1765 audio_stream_in_frame_size((const struct audio_stream_in *)stream);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001766}
1767
1768static uint32_t in_get_channels(const struct audio_stream *stream)
1769{
1770 struct stream_in *in = (struct stream_in *)stream;
1771
1772 return in->channel_mask;
1773}
1774
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001775static audio_format_t in_get_format(const struct audio_stream *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001776{
1777 return AUDIO_FORMAT_PCM_16_BIT;
1778}
1779
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001780static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001781{
1782 return -ENOSYS;
1783}
1784
1785static int in_standby(struct audio_stream *stream)
1786{
1787 struct stream_in *in = (struct stream_in *)stream;
1788 struct audio_device *adev = in->dev;
1789 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001790 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001791 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001792
1793 if (!in->standby && in->is_st_session) {
1794 ALOGD("%s: sound trigger pcm stop lab", __func__);
1795 audio_extn_sound_trigger_stop_lab(in);
1796 in->standby = true;
1797 }
1798
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001799 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001800 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001801 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001802 if (in->pcm) {
1803 pcm_close(in->pcm);
1804 in->pcm = NULL;
1805 }
Eric Laurentcefbbac2014-09-04 13:54:10 -05001806 adev->enable_voicerx = false;
1807 platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE );
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001808 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001809 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001810 }
1811 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001812 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001813 return status;
1814}
1815
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001816static int in_dump(const struct audio_stream *stream __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001817{
1818 return 0;
1819}
1820
1821static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1822{
1823 struct stream_in *in = (struct stream_in *)stream;
1824 struct audio_device *adev = in->dev;
1825 struct str_parms *parms;
1826 char *str;
1827 char value[32];
1828 int ret, val = 0;
Eric Laurent03f09432014-03-25 18:09:11 -07001829 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001830
Eric Laurent994a6932013-07-17 11:51:42 -07001831 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001832 parms = str_parms_create_str(kvpairs);
1833
1834 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1835
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001836 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001837 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001838 if (ret >= 0) {
1839 val = atoi(value);
1840 /* no audio source uses val == 0 */
1841 if ((in->source != val) && (val != 0)) {
1842 in->source = val;
1843 }
1844 }
1845
1846 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
Eric Laurent03f09432014-03-25 18:09:11 -07001847
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001848 if (ret >= 0) {
1849 val = atoi(value);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001850 if (((int)in->device != val) && (val != 0)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001851 in->device = val;
1852 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001853 if (!in->standby)
Eric Laurent03f09432014-03-25 18:09:11 -07001854 status = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001855 }
1856 }
1857
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001858 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001859 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001860
1861 str_parms_destroy(parms);
Eric Laurent03f09432014-03-25 18:09:11 -07001862 ALOGV("%s: exit: status(%d)", __func__, status);
1863 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001864}
1865
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001866static char* in_get_parameters(const struct audio_stream *stream __unused,
1867 const char *keys __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001868{
1869 return strdup("");
1870}
1871
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001872static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001873{
1874 return 0;
1875}
1876
1877static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1878 size_t bytes)
1879{
1880 struct stream_in *in = (struct stream_in *)stream;
1881 struct audio_device *adev = in->dev;
1882 int i, ret = -1;
1883
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001884 pthread_mutex_lock(&in->lock);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07001885 if (in->is_st_session) {
1886 ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
1887 /* Read from sound trigger HAL */
1888 audio_extn_sound_trigger_read(in, buffer, bytes);
1889 pthread_mutex_unlock(&in->lock);
1890 return bytes;
1891 }
1892
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001893 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001894 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001895 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001896 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001897 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001898 goto exit;
1899 }
1900 in->standby = 0;
1901 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001902
1903 if (in->pcm) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07001904 if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
1905 ret = pcm_mmap_read(in->pcm, buffer, bytes);
1906 } else
1907 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001908 }
1909
1910 /*
1911 * Instead of writing zeroes here, we could trust the hardware
1912 * to always provide zeroes when muted.
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001913 * 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 -08001914 */
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07001915 if (ret == 0 && adev->mic_muted && in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001916 memset(buffer, 0, bytes);
1917
1918exit:
1919 pthread_mutex_unlock(&in->lock);
1920
1921 if (ret != 0) {
1922 in_standby(&in->stream.common);
1923 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
Eric Laurentfdf296a2014-07-03 16:41:51 -07001924 usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001925 in_get_sample_rate(&in->stream.common));
1926 }
1927 return bytes;
1928}
1929
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07001930static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001931{
1932 return 0;
1933}
1934
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001935static int add_remove_audio_effect(const struct audio_stream *stream,
1936 effect_handle_t effect,
1937 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001938{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001939 struct stream_in *in = (struct stream_in *)stream;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001940 struct audio_device *adev = in->dev;
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001941 int status = 0;
1942 effect_descriptor_t desc;
1943
1944 status = (*effect)->get_descriptor(effect, &desc);
1945 if (status != 0)
1946 return status;
1947
1948 pthread_mutex_lock(&in->lock);
1949 pthread_mutex_lock(&in->dev->lock);
1950 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1951 in->enable_aec != enable &&
1952 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1953 in->enable_aec = enable;
Eric Laurentcefbbac2014-09-04 13:54:10 -05001954 if (!enable)
1955 platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE);
1956 adev->enable_voicerx = enable;
1957 struct audio_usecase *usecase;
1958 struct listnode *node;
1959 list_for_each(node, &adev->usecase_list) {
1960 usecase = node_to_item(node, struct audio_usecase, list);
1961 if (usecase->type == PCM_PLAYBACK) {
1962 select_devices(adev, usecase->id);
1963 break;
1964 }
1965 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001966 if (!in->standby)
1967 select_devices(in->dev, in->usecase);
1968 }
Ravi Kumar Alamanda3ad4e1b2014-06-03 00:08:15 -07001969 if (in->enable_ns != enable &&
1970 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1971 in->enable_ns = enable;
1972 if (!in->standby)
1973 select_devices(in->dev, in->usecase);
1974 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001975 pthread_mutex_unlock(&in->dev->lock);
1976 pthread_mutex_unlock(&in->lock);
1977
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001978 return 0;
1979}
1980
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001981static int in_add_audio_effect(const struct audio_stream *stream,
1982 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001983{
Eric Laurent994a6932013-07-17 11:51:42 -07001984 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001985 return add_remove_audio_effect(stream, effect, true);
1986}
1987
1988static int in_remove_audio_effect(const struct audio_stream *stream,
1989 effect_handle_t effect)
1990{
Eric Laurent994a6932013-07-17 11:51:42 -07001991 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001992 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001993}
1994
1995static int adev_open_output_stream(struct audio_hw_device *dev,
1996 audio_io_handle_t handle,
1997 audio_devices_t devices,
1998 audio_output_flags_t flags,
1999 struct audio_config *config,
Eric Laurent91975a62014-07-27 17:15:50 -07002000 struct audio_stream_out **stream_out,
2001 const char *address __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002002{
2003 struct audio_device *adev = (struct audio_device *)dev;
2004 struct stream_out *out;
2005 int i, ret;
2006
Eric Laurent994a6932013-07-17 11:51:42 -07002007 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002008 __func__, config->sample_rate, config->channel_mask, devices, flags);
2009 *stream_out = NULL;
2010 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2011
2012 if (devices == AUDIO_DEVICE_NONE)
2013 devices = AUDIO_DEVICE_OUT_SPEAKER;
2014
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002015 out->flags = flags;
2016 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002017 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002018 out->format = config->format;
2019 out->sample_rate = config->sample_rate;
2020 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2021 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002022 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002023
2024 /* Init use case and pcm_config */
2025 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
Eric Laurent7f245042013-09-30 19:22:50 -07002026 !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002027 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002028 pthread_mutex_lock(&adev->lock);
2029 ret = read_hdmi_channel_masks(out);
2030 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002031 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002032 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002033
2034 if (config->sample_rate == 0)
2035 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2036 if (config->channel_mask == 0)
2037 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2038
2039 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002040 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002041 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2042 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002043 out->config.rate = config->sample_rate;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002044 out->config.channels = audio_channel_count_from_out_mask(out->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002045 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002046 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2047 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2048 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2049 ALOGE("%s: Unsupported Offload information", __func__);
2050 ret = -EINVAL;
2051 goto error_open;
2052 }
2053 if (!is_supported_format(config->offload_info.format)) {
2054 ALOGE("%s: Unsupported audio format", __func__);
2055 ret = -EINVAL;
2056 goto error_open;
2057 }
2058
2059 out->compr_config.codec = (struct snd_codec *)
2060 calloc(1, sizeof(struct snd_codec));
2061
2062 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2063 if (config->offload_info.channel_mask)
2064 out->channel_mask = config->offload_info.channel_mask;
2065 else if (config->channel_mask)
2066 out->channel_mask = config->channel_mask;
2067 out->format = config->offload_info.format;
2068 out->sample_rate = config->offload_info.sample_rate;
2069
2070 out->stream.set_callback = out_set_callback;
2071 out->stream.pause = out_pause;
2072 out->stream.resume = out_resume;
2073 out->stream.drain = out_drain;
2074 out->stream.flush = out_flush;
2075
2076 out->compr_config.codec->id =
2077 get_snd_codec_id(config->offload_info.format);
2078 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2079 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
Eric Laurent1ccf3972014-10-23 14:42:59 -07002080 out->compr_config.codec->sample_rate = config->offload_info.sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002081 out->compr_config.codec->bit_rate =
2082 config->offload_info.bit_rate;
2083 out->compr_config.codec->ch_in =
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002084 audio_channel_count_from_out_mask(config->channel_mask);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002085 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2086
2087 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2088 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002089
2090 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002091 create_offload_callback_thread(out);
2092 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2093 __func__, config->offload_info.version,
2094 config->offload_info.bit_rate);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002095 } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) {
2096 if (config->sample_rate == 0)
2097 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2098 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2099 config->sample_rate != 8000) {
2100 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2101 ret = -EINVAL;
2102 goto error_open;
2103 }
2104 out->sample_rate = config->sample_rate;
2105 out->config.rate = config->sample_rate;
2106 if (config->format == AUDIO_FORMAT_DEFAULT)
2107 config->format = AUDIO_FORMAT_PCM_16_BIT;
2108 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2109 config->format = AUDIO_FORMAT_PCM_16_BIT;
2110 ret = -EINVAL;
2111 goto error_open;
2112 }
2113 out->format = config->format;
2114 out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY;
2115 out->config = pcm_config_afe_proxy_playback;
2116 adev->voice_tx_output = out;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002117 } else {
Andy Hung6fcba9c2014-03-18 11:53:32 -07002118 if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
2119 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2120 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamandaf78a4d92015-04-24 15:18:23 -07002121 } else if (out->flags & AUDIO_OUTPUT_FLAG_TTS) {
2122 out->usecase = USECASE_AUDIO_PLAYBACK_TTS;
2123 out->config = pcm_config_deep_buffer;
Andy Hung6fcba9c2014-03-18 11:53:32 -07002124 } else {
2125 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2126 out->config = pcm_config_low_latency;
2127 }
2128 if (config->format != audio_format_from_pcm_format(out->config.format)) {
2129 if (k_enable_extended_precision
2130 && pcm_params_format_test(adev->use_case_table[out->usecase],
2131 pcm_format_from_audio_format(config->format))) {
2132 out->config.format = pcm_format_from_audio_format(config->format);
2133 /* out->format already set to config->format */
2134 } else {
2135 /* deny the externally proposed config format
2136 * and use the one specified in audio_hw layer configuration.
2137 * Note: out->format is returned by out->stream.common.get_format()
2138 * and is used to set config->format in the code several lines below.
2139 */
2140 out->format = audio_format_from_pcm_format(out->config.format);
2141 }
2142 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002143 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002144 }
Andy Hung6fcba9c2014-03-18 11:53:32 -07002145 ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n",
2146 __func__, use_case_table[out->usecase], config->format, out->config.format);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002147
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002148 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002149 if (adev->primary_output == NULL)
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002150 adev->primary_output = out;
2151 else {
2152 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002153 ret = -EEXIST;
2154 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002155 }
2156 }
2157
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002158 /* Check if this usecase is already existing */
2159 pthread_mutex_lock(&adev->lock);
2160 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2161 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002162 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002163 ret = -EEXIST;
2164 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002165 }
2166 pthread_mutex_unlock(&adev->lock);
2167
2168 out->stream.common.get_sample_rate = out_get_sample_rate;
2169 out->stream.common.set_sample_rate = out_set_sample_rate;
2170 out->stream.common.get_buffer_size = out_get_buffer_size;
2171 out->stream.common.get_channels = out_get_channels;
2172 out->stream.common.get_format = out_get_format;
2173 out->stream.common.set_format = out_set_format;
2174 out->stream.common.standby = out_standby;
2175 out->stream.common.dump = out_dump;
2176 out->stream.common.set_parameters = out_set_parameters;
2177 out->stream.common.get_parameters = out_get_parameters;
2178 out->stream.common.add_audio_effect = out_add_audio_effect;
2179 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2180 out->stream.get_latency = out_get_latency;
2181 out->stream.set_volume = out_set_volume;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002182#ifdef NO_AUDIO_OUT
2183 out->stream.write = out_write_for_no_output;
2184#else
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002185 out->stream.write = out_write;
Uday Kishore Pasupuleticec8ad82015-04-15 10:34:06 -07002186#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002187 out->stream.get_render_position = out_get_render_position;
2188 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002189 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002190
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002191 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002192 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002193 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002194
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002195 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2196 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2197
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002198 config->format = out->stream.common.get_format(&out->stream.common);
2199 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2200 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2201
2202 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002203 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002204 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002205
2206error_open:
2207 free(out);
2208 *stream_out = NULL;
2209 ALOGD("%s: exit: ret %d", __func__, ret);
2210 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002211}
2212
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002213static void adev_close_output_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002214 struct audio_stream_out *stream)
2215{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002216 struct stream_out *out = (struct stream_out *)stream;
2217 struct audio_device *adev = out->dev;
2218
Eric Laurent994a6932013-07-17 11:51:42 -07002219 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002220 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002221 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2222 destroy_offload_callback_thread(out);
2223
2224 if (out->compr_config.codec != NULL)
2225 free(out->compr_config.codec);
2226 }
Ravi Kumar Alamandaa4fc9022014-10-08 18:57:46 -07002227
2228 if (adev->voice_tx_output == out)
2229 adev->voice_tx_output = NULL;
2230
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002231 pthread_cond_destroy(&out->cond);
2232 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002233 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002234 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002235}
2236
2237static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2238{
2239 struct audio_device *adev = (struct audio_device *)dev;
2240 struct str_parms *parms;
2241 char *str;
2242 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002243 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002244 int ret;
Eric Laurent03f09432014-03-25 18:09:11 -07002245 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002246
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002247 ALOGD("%s: enter: %s", __func__, kvpairs);
2248
2249 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002250
2251 parms = str_parms_create_str(kvpairs);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002252 status = voice_set_parameters(adev, parms);
2253 if (status != 0) {
2254 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002255 }
2256
2257 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2258 if (ret >= 0) {
2259 /* When set to false, HAL should disable EC and NS
2260 * But it is currently not supported.
2261 */
2262 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2263 adev->bluetooth_nrec = true;
2264 else
2265 adev->bluetooth_nrec = false;
2266 }
2267
2268 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2269 if (ret >= 0) {
2270 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2271 adev->screen_off = false;
2272 else
2273 adev->screen_off = true;
2274 }
2275
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002276 ret = str_parms_get_int(parms, "rotation", &val);
2277 if (ret >= 0) {
2278 bool reverse_speakers = false;
2279 switch(val) {
2280 // FIXME: note that the code below assumes that the speakers are in the correct placement
2281 // relative to the user when the device is rotated 90deg from its default rotation. This
2282 // assumption is device-specific, not platform-specific like this code.
2283 case 270:
2284 reverse_speakers = true;
2285 break;
2286 case 0:
2287 case 90:
2288 case 180:
2289 break;
2290 default:
2291 ALOGE("%s: unexpected rotation of %d", __func__, val);
Eric Laurent03f09432014-03-25 18:09:11 -07002292 status = -EINVAL;
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002293 }
Eric Laurent03f09432014-03-25 18:09:11 -07002294 if (status == 0) {
Ravi Kumar Alamanda1f60cf82015-04-23 19:45:17 -07002295 platform_swap_lr_channels(adev, reverse_speakers);
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002296 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002297 }
2298
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002299 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value));
2300 if (ret >= 0) {
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002301 adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON);
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002302 }
2303
Ravi Kumar Alamanda8e6e98f2013-11-05 15:57:39 -08002304 audio_extn_hfp_set_parameters(adev, parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002305done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002306 str_parms_destroy(parms);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002307 pthread_mutex_unlock(&adev->lock);
Eric Laurent03f09432014-03-25 18:09:11 -07002308 ALOGV("%s: exit with code(%d)", __func__, status);
2309 return status;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002310}
2311
2312static char* adev_get_parameters(const struct audio_hw_device *dev,
2313 const char *keys)
2314{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002315 struct audio_device *adev = (struct audio_device *)dev;
2316 struct str_parms *reply = str_parms_create();
2317 struct str_parms *query = str_parms_create_str(keys);
2318 char *str;
2319
2320 pthread_mutex_lock(&adev->lock);
2321
2322 voice_get_parameters(adev, query, reply);
2323 str = str_parms_to_str(reply);
2324 str_parms_destroy(query);
2325 str_parms_destroy(reply);
2326
2327 pthread_mutex_unlock(&adev->lock);
2328 ALOGV("%s: exit: returns - %s", __func__, str);
2329 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002330}
2331
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002332static int adev_init_check(const struct audio_hw_device *dev __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002333{
2334 return 0;
2335}
2336
Haynes Mathew George5191a852013-09-11 14:19:36 -07002337static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2338{
2339 int ret;
2340 struct audio_device *adev = (struct audio_device *)dev;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002341
Eric Laurent4cc4ce12014-09-10 13:21:01 -05002342 audio_extn_extspk_set_voice_vol(adev->extspk, volume);
2343
Haynes Mathew George5191a852013-09-11 14:19:36 -07002344 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002345 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002346 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002347
Haynes Mathew George5191a852013-09-11 14:19:36 -07002348 return ret;
2349}
2350
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002351static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002352{
2353 return -ENOSYS;
2354}
2355
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002356static int adev_get_master_volume(struct audio_hw_device *dev __unused,
2357 float *volume __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002358{
2359 return -ENOSYS;
2360}
2361
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002362static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002363{
2364 return -ENOSYS;
2365}
2366
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002367static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002368{
2369 return -ENOSYS;
2370}
2371
2372static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2373{
2374 struct audio_device *adev = (struct audio_device *)dev;
2375
2376 pthread_mutex_lock(&adev->lock);
2377 if (adev->mode != mode) {
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002378 ALOGD("%s: mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002379 adev->mode = mode;
Ravi Kumar Alamanda36886fc2014-09-29 13:41:51 -07002380 if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) &&
2381 voice_is_in_call(adev)) {
2382 voice_stop_call(adev);
2383 adev->current_call_output = NULL;
2384 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002385 }
2386 pthread_mutex_unlock(&adev->lock);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002387
2388 audio_extn_extspk_set_mode(adev->extspk, mode);
2389
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002390 return 0;
2391}
2392
2393static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2394{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002395 int ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002396 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002397
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002398 ALOGD("%s: state %d\n", __func__, state);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002399 pthread_mutex_lock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002400 ret = voice_set_mic_mute(adev, state);
Eric Laurent7b2b5ab2014-09-14 12:29:59 -07002401 adev->mic_muted = state;
Eric Laurenta609e8e2014-06-18 02:15:17 +00002402 pthread_mutex_unlock(&adev->lock);
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002403
2404 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002405}
2406
2407static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2408{
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002409 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002410 return 0;
2411}
2412
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002413static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002414 const struct audio_config *config)
2415{
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002416 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002417
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002418 return get_input_buffer_size(config->sample_rate, config->format, channel_count,
2419 false /* is_low_latency: since we don't know, be conservative */);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002420}
2421
2422static int adev_open_input_stream(struct audio_hw_device *dev,
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002423 audio_io_handle_t handle,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002424 audio_devices_t devices,
2425 struct audio_config *config,
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002426 struct audio_stream_in **stream_in,
Eric Laurent91975a62014-07-27 17:15:50 -07002427 audio_input_flags_t flags,
2428 const char *address __unused,
Eric Laurentcefbbac2014-09-04 13:54:10 -05002429 audio_source_t source )
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002430{
2431 struct audio_device *adev = (struct audio_device *)dev;
2432 struct stream_in *in;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002433 int ret = 0, buffer_size, frame_size;
Eric Laurent0de8d1f2014-07-01 20:34:45 -07002434 int channel_count = audio_channel_count_from_in_mask(config->channel_mask);
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002435 bool is_low_latency = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002436
Eric Laurent994a6932013-07-17 11:51:42 -07002437 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002438 *stream_in = NULL;
2439 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2440 return -EINVAL;
2441
2442 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2443
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002444 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2445
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002446 in->stream.common.get_sample_rate = in_get_sample_rate;
2447 in->stream.common.set_sample_rate = in_set_sample_rate;
2448 in->stream.common.get_buffer_size = in_get_buffer_size;
2449 in->stream.common.get_channels = in_get_channels;
2450 in->stream.common.get_format = in_get_format;
2451 in->stream.common.set_format = in_set_format;
2452 in->stream.common.standby = in_standby;
2453 in->stream.common.dump = in_dump;
2454 in->stream.common.set_parameters = in_set_parameters;
2455 in->stream.common.get_parameters = in_get_parameters;
2456 in->stream.common.add_audio_effect = in_add_audio_effect;
2457 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2458 in->stream.set_gain = in_set_gain;
2459 in->stream.read = in_read;
2460 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2461
2462 in->device = devices;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002463 in->source = source;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002464 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002465 in->standby = 1;
2466 in->channel_mask = config->channel_mask;
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002467 in->capture_handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002468
2469 /* Update config params with the requested sample rate and channels */
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002470 if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
2471 if (config->sample_rate == 0)
2472 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2473 if (config->sample_rate != 48000 && config->sample_rate != 16000 &&
2474 config->sample_rate != 8000) {
2475 config->sample_rate = AFE_PROXY_SAMPLING_RATE;
2476 ret = -EINVAL;
2477 goto err_open;
2478 }
2479 if (config->format == AUDIO_FORMAT_DEFAULT)
2480 config->format = AUDIO_FORMAT_PCM_16_BIT;
2481 if (config->format != AUDIO_FORMAT_PCM_16_BIT) {
2482 config->format = AUDIO_FORMAT_PCM_16_BIT;
2483 ret = -EINVAL;
2484 goto err_open;
2485 }
2486
2487 in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY;
2488 in->config = pcm_config_afe_proxy_record;
2489 } else {
2490 in->usecase = USECASE_AUDIO_RECORD;
2491 if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE &&
2492 (flags & AUDIO_INPUT_FLAG_FAST) != 0) {
2493 is_low_latency = true;
Glenn Kasten4f993392014-05-14 07:30:48 -07002494#if LOW_LATENCY_CAPTURE_USE_CASE
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002495 in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY;
Glenn Kasten4f993392014-05-14 07:30:48 -07002496#endif
Ravi Kumar Alamanda99c752d2014-08-20 17:55:26 -07002497 }
2498 in->config = pcm_config_audio_capture;
2499
2500 frame_size = audio_stream_in_frame_size(&in->stream);
2501 buffer_size = get_input_buffer_size(config->sample_rate,
2502 config->format,
2503 channel_count,
2504 is_low_latency);
2505 in->config.period_size = buffer_size / frame_size;
Glenn Kasten68e79ce2014-07-15 10:56:59 -07002506 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002507 in->config.channels = channel_count;
2508 in->config.rate = config->sample_rate;
2509
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002510 /* This stream could be for sound trigger lab,
2511 get sound trigger pcm if present */
2512 audio_extn_sound_trigger_check_and_get_session(in);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002513
2514 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002515 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002516 return 0;
2517
2518err_open:
2519 free(in);
2520 *stream_in = NULL;
2521 return ret;
2522}
2523
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002524static void adev_close_input_stream(struct audio_hw_device *dev __unused,
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002525 struct audio_stream_in *stream)
2526{
Eric Laurent994a6932013-07-17 11:51:42 -07002527 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002528
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002529 in_standby(&stream->common);
2530 free(stream);
2531
2532 return;
2533}
2534
Haynes Mathew Georgecc9649b2014-06-10 15:08:39 -07002535static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002536{
2537 return 0;
2538}
2539
Andy Hung31aca912014-03-20 17:14:59 -07002540/* verifies input and output devices and their capabilities.
2541 *
2542 * This verification is required when enabling extended bit-depth or
2543 * sampling rates, as not all qcom products support it.
2544 *
2545 * Suitable for calling only on initialization such as adev_open().
2546 * It fills the audio_device use_case_table[] array.
2547 *
2548 * Has a side-effect that it needs to configure audio routing / devices
2549 * in order to power up the devices and read the device parameters.
2550 * It does not acquire any hw device lock. Should restore the devices
2551 * back to "normal state" upon completion.
2552 */
2553static int adev_verify_devices(struct audio_device *adev)
2554{
2555 /* enumeration is a bit difficult because one really wants to pull
2556 * the use_case, device id, etc from the hidden pcm_device_table[].
2557 * In this case there are the following use cases and device ids.
2558 *
2559 * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0},
2560 * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15},
2561 * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1},
2562 * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9},
2563 * [USECASE_AUDIO_RECORD] = {0, 0},
2564 * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15},
2565 * [USECASE_VOICE_CALL] = {2, 2},
2566 *
2567 * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted.
2568 * USECASE_VOICE_CALL omitted, but possible for either input or output.
2569 */
2570
2571 /* should be the usecases enabled in adev_open_input_stream() */
2572 static const int test_in_usecases[] = {
2573 USECASE_AUDIO_RECORD,
2574 USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */
2575 };
2576 /* should be the usecases enabled in adev_open_output_stream()*/
2577 static const int test_out_usecases[] = {
2578 USECASE_AUDIO_PLAYBACK_DEEP_BUFFER,
2579 USECASE_AUDIO_PLAYBACK_LOW_LATENCY,
2580 };
2581 static const usecase_type_t usecase_type_by_dir[] = {
2582 PCM_PLAYBACK,
2583 PCM_CAPTURE,
2584 };
2585 static const unsigned flags_by_dir[] = {
2586 PCM_OUT,
2587 PCM_IN,
2588 };
2589
2590 size_t i;
2591 unsigned dir;
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002592 const unsigned card_id = adev->snd_card;
Andy Hung31aca912014-03-20 17:14:59 -07002593 char info[512]; /* for possible debug info */
2594
2595 for (dir = 0; dir < 2; ++dir) {
2596 const usecase_type_t usecase_type = usecase_type_by_dir[dir];
2597 const unsigned flags_dir = flags_by_dir[dir];
2598 const size_t testsize =
2599 dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases);
2600 const int *testcases =
2601 dir ? test_in_usecases : test_out_usecases;
2602 const audio_devices_t audio_device =
2603 dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER;
2604
2605 for (i = 0; i < testsize; ++i) {
2606 const audio_usecase_t audio_usecase = testcases[i];
2607 int device_id;
2608 snd_device_t snd_device;
2609 struct pcm_params **pparams;
2610 struct stream_out out;
2611 struct stream_in in;
2612 struct audio_usecase uc_info;
2613 int retval;
2614
2615 pparams = &adev->use_case_table[audio_usecase];
2616 pcm_params_free(*pparams); /* can accept null input */
2617 *pparams = NULL;
2618
2619 /* find the device ID for the use case (signed, for error) */
2620 device_id = platform_get_pcm_device_id(audio_usecase, usecase_type);
2621 if (device_id < 0)
2622 continue;
2623
2624 /* prepare structures for device probing */
2625 memset(&uc_info, 0, sizeof(uc_info));
2626 uc_info.id = audio_usecase;
2627 uc_info.type = usecase_type;
2628 if (dir) {
2629 adev->active_input = &in;
2630 memset(&in, 0, sizeof(in));
2631 in.device = audio_device;
2632 in.source = AUDIO_SOURCE_VOICE_COMMUNICATION;
2633 uc_info.stream.in = &in;
2634 } else {
2635 adev->active_input = NULL;
2636 }
2637 memset(&out, 0, sizeof(out));
2638 out.devices = audio_device; /* only field needed in select_devices */
2639 uc_info.stream.out = &out;
2640 uc_info.devices = audio_device;
2641 uc_info.in_snd_device = SND_DEVICE_NONE;
2642 uc_info.out_snd_device = SND_DEVICE_NONE;
2643 list_add_tail(&adev->usecase_list, &uc_info.list);
2644
2645 /* select device - similar to start_(in/out)put_stream() */
2646 retval = select_devices(adev, audio_usecase);
2647 if (retval >= 0) {
2648 *pparams = pcm_params_get(card_id, device_id, flags_dir);
2649#if LOG_NDEBUG == 0
2650 if (*pparams) {
2651 ALOGV("%s: (%s) card %d device %d", __func__,
2652 dir ? "input" : "output", card_id, device_id);
2653 pcm_params_to_string(*pparams, info, ARRAY_SIZE(info));
2654 ALOGV(info); /* print parameters */
2655 } else {
2656 ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id);
2657 }
2658#endif
2659 }
2660
2661 /* deselect device - similar to stop_(in/out)put_stream() */
2662 /* 1. Get and set stream specific mixer controls */
Glenn Kastene7137302014-04-28 15:12:18 -07002663 retval = disable_audio_route(adev, &uc_info);
Andy Hung31aca912014-03-20 17:14:59 -07002664 /* 2. Disable the rx device */
2665 retval = disable_snd_device(adev,
Glenn Kastene7137302014-04-28 15:12:18 -07002666 dir ? uc_info.in_snd_device : uc_info.out_snd_device);
Andy Hung31aca912014-03-20 17:14:59 -07002667 list_remove(&uc_info.list);
2668 }
2669 }
2670 adev->active_input = NULL; /* restore adev state */
2671 return 0;
2672}
2673
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002674static int adev_close(hw_device_t *device)
2675{
Andy Hung31aca912014-03-20 17:14:59 -07002676 size_t i;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002677 struct audio_device *adev = (struct audio_device *)device;
2678 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002679 free(adev->snd_dev_ref_cnt);
2680 platform_deinit(adev->platform);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002681 audio_extn_extspk_deinit(adev->extspk);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002682 audio_extn_sound_trigger_deinit(adev);
Andy Hung31aca912014-03-20 17:14:59 -07002683 for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
2684 pcm_params_free(adev->use_case_table[i]);
2685 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002686 free(device);
2687 return 0;
2688}
2689
Glenn Kasten4f993392014-05-14 07:30:48 -07002690/* This returns 1 if the input parameter looks at all plausible as a low latency period size,
2691 * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work,
2692 * just that it _might_ work.
2693 */
2694static int period_size_is_plausible_for_low_latency(int period_size)
2695{
2696 switch (period_size) {
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002697 case 48:
2698 case 96:
2699 case 144:
Glenn Kasten4f993392014-05-14 07:30:48 -07002700 case 160:
Glenn Kasten5b5d04e2015-04-09 09:43:29 -07002701 case 192:
Glenn Kasten4f993392014-05-14 07:30:48 -07002702 case 240:
2703 case 320:
2704 case 480:
2705 return 1;
2706 default:
2707 return 0;
2708 }
2709}
2710
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002711static int adev_open(const hw_module_t *module, const char *name,
2712 hw_device_t **device)
2713{
2714 struct audio_device *adev;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002715 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002716
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002717 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002718 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2719
2720 adev = calloc(1, sizeof(struct audio_device));
2721
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002722 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2723
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002724 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2725 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2726 adev->device.common.module = (struct hw_module_t *)module;
2727 adev->device.common.close = adev_close;
2728
2729 adev->device.init_check = adev_init_check;
2730 adev->device.set_voice_volume = adev_set_voice_volume;
2731 adev->device.set_master_volume = adev_set_master_volume;
2732 adev->device.get_master_volume = adev_get_master_volume;
2733 adev->device.set_master_mute = adev_set_master_mute;
2734 adev->device.get_master_mute = adev_get_master_mute;
2735 adev->device.set_mode = adev_set_mode;
2736 adev->device.set_mic_mute = adev_set_mic_mute;
2737 adev->device.get_mic_mute = adev_get_mic_mute;
2738 adev->device.set_parameters = adev_set_parameters;
2739 adev->device.get_parameters = adev_get_parameters;
2740 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2741 adev->device.open_output_stream = adev_open_output_stream;
2742 adev->device.close_output_stream = adev_close_output_stream;
2743 adev->device.open_input_stream = adev_open_input_stream;
2744 adev->device.close_input_stream = adev_close_input_stream;
2745 adev->device.dump = adev_dump;
2746
2747 /* Set the default route before the PCM stream is opened */
2748 pthread_mutex_lock(&adev->lock);
2749 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002750 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002751 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002752 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002753 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002754 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002755 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Vineeta Srivastava4b89e372014-06-19 14:21:42 -07002756 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002757 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002758 pthread_mutex_unlock(&adev->lock);
2759
2760 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002761 adev->platform = platform_init(adev);
2762 if (!adev->platform) {
2763 free(adev->snd_dev_ref_cnt);
2764 free(adev);
2765 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2766 *device = NULL;
2767 return -EINVAL;
2768 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002769
Eric Laurent0499d4f2014-08-25 22:39:29 -05002770 adev->extspk = audio_extn_extspk_init(adev);
Ravi Kumar Alamandaa417cc52015-05-01 16:41:56 -07002771 audio_extn_sound_trigger_init(adev);
Eric Laurent0499d4f2014-08-25 22:39:29 -05002772
Eric Laurentc4aef752013-09-12 17:45:53 -07002773 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2774 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2775 if (adev->visualizer_lib == NULL) {
2776 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2777 } else {
2778 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2779 adev->visualizer_start_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002780 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002781 "visualizer_hal_start_output");
2782 adev->visualizer_stop_output =
Haynes Mathew George41f86652014-06-17 14:22:15 -07002783 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002784 "visualizer_hal_stop_output");
2785 }
2786 }
2787
Haynes Mathew George41f86652014-06-17 14:22:15 -07002788 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2789 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2790 if (adev->offload_effects_lib == NULL) {
2791 ALOGE("%s: DLOPEN failed for %s", __func__,
2792 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2793 } else {
2794 ALOGV("%s: DLOPEN successful for %s", __func__,
2795 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2796 adev->offload_effects_start_output =
2797 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2798 "offload_effects_bundle_hal_start_output");
2799 adev->offload_effects_stop_output =
2800 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2801 "offload_effects_bundle_hal_stop_output");
2802 }
2803 }
2804
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002805 adev->bt_wb_speech_enabled = false;
Eric Laurentcefbbac2014-09-04 13:54:10 -05002806 adev->enable_voicerx = false;
Ravi Kumar Alamanda9f306542014-04-02 15:11:49 -07002807
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002808 *device = &adev->device.common;
Andy Hung31aca912014-03-20 17:14:59 -07002809 if (k_enable_extended_precision)
2810 adev_verify_devices(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002811
Glenn Kasten4f993392014-05-14 07:30:48 -07002812 char value[PROPERTY_VALUE_MAX];
2813 int trial;
2814 if (property_get("audio_hal.period_size", value, NULL) > 0) {
2815 trial = atoi(value);
2816 if (period_size_is_plausible_for_low_latency(trial)) {
2817 pcm_config_low_latency.period_size = trial;
2818 pcm_config_low_latency.start_threshold = trial / 4;
2819 pcm_config_low_latency.avail_min = trial / 4;
2820 configured_low_latency_capture_period_size = trial;
2821 }
2822 }
2823 if (property_get("audio_hal.in_period_size", value, NULL) > 0) {
2824 trial = atoi(value);
2825 if (period_size_is_plausible_for_low_latency(trial)) {
2826 configured_low_latency_capture_period_size = trial;
2827 }
2828 }
2829
Eric Laurent994a6932013-07-17 11:51:42 -07002830 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002831 return 0;
2832}
2833
2834static struct hw_module_methods_t hal_module_methods = {
2835 .open = adev_open,
2836};
2837
2838struct audio_module HAL_MODULE_INFO_SYM = {
2839 .common = {
2840 .tag = HARDWARE_MODULE_TAG,
2841 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2842 .hal_api_version = HARDWARE_HAL_API_VERSION,
2843 .id = AUDIO_HARDWARE_MODULE_ID,
2844 .name = "QCOM Audio HAL",
2845 .author = "Code Aurora Forum",
2846 .methods = &hal_module_methods,
2847 },
2848};