blob: ae8bc87b040d5892b4b33e0bc420b4e7d23a7064 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07003 * Not a Contribution.
4 *
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08005 * Copyright (C) 2013-2014 The Android Open Source Project
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08006 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_primary"
21/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070022/*#define VERY_VERY_VERBOSE_LOGGING*/
23#ifdef VERY_VERY_VERBOSE_LOGGING
24#define ALOGVV ALOGV
25#else
26#define ALOGVV(a...) do { } while(0)
27#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080028
29#include <errno.h>
30#include <pthread.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080034#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070035#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070036#include <sys/resource.h>
37#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038
39#include <cutils/log.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070042#include <cutils/atomic.h>
43#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080044
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <hardware/audio_effect.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070050#include "platform_api.h"
51#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070052#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080053#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include "sound/compress_params.h"
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -080056#define MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024)
57#define MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE (8 * 1024)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070058#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
59#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
60/* ToDo: Check and update a proper value in msec */
61#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
62#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
63
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080064#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
65
Eric Laurentb23d5282013-05-14 15:27:20 -070066struct pcm_config pcm_config_deep_buffer = {
67 .channels = 2,
68 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
69 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
70 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
71 .format = PCM_FORMAT_S16_LE,
72 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
73 .stop_threshold = INT_MAX,
74 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
75};
76
77struct pcm_config pcm_config_low_latency = {
78 .channels = 2,
79 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
80 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
81 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
82 .format = PCM_FORMAT_S16_LE,
83 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
84 .stop_threshold = INT_MAX,
85 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
86};
87
88struct pcm_config pcm_config_hdmi_multi = {
89 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
90 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
91 .period_size = HDMI_MULTI_PERIOD_SIZE,
92 .period_count = HDMI_MULTI_PERIOD_COUNT,
93 .format = PCM_FORMAT_S16_LE,
94 .start_threshold = 0,
95 .stop_threshold = INT_MAX,
96 .avail_min = 0,
97};
98
99struct pcm_config pcm_config_audio_capture = {
100 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700101 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
102 .format = PCM_FORMAT_S16_LE,
103};
104
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800105const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700106 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
107 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
108 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700109 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700110 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700111 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700112 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700113 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700114 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800115 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700116 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700117
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700118 [USECASE_VOICE2_CALL] = "voice2-call",
119 [USECASE_VOLTE_CALL] = "volte-call",
120 [USECASE_QCHAT_CALL] = "qchat-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800121 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700122 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
123 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
124 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800125 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
126 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
127 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
128
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700129 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
130 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700131 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
132 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700133};
134
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800135
136#define STRING_TO_ENUM(string) { #string, string }
137
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800138struct string_to_enum {
139 const char *name;
140 uint32_t value;
141};
142
143static const struct string_to_enum out_channels_name_to_enum_table[] = {
144 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
145 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
146 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
147};
148
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700149static struct audio_device *adev = NULL;
150static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700151static unsigned int audio_device_ref_count;
152
Haynes Mathew George5191a852013-09-11 14:19:36 -0700153static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -0800154static uint32_t get_offload_buffer_size();
Haynes Mathew George5191a852013-09-11 14:19:36 -0700155
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700156static bool is_supported_format(audio_format_t format)
157{
Eric Laurent86e17132013-09-12 17:49:30 -0700158 if (format == AUDIO_FORMAT_MP3 ||
159 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700160 return true;
161
162 return false;
163}
164
165static int get_snd_codec_id(audio_format_t format)
166{
167 int id = 0;
168
169 switch (format) {
170 case AUDIO_FORMAT_MP3:
171 id = SND_AUDIOCODEC_MP3;
172 break;
173 case AUDIO_FORMAT_AAC:
174 id = SND_AUDIOCODEC_AAC;
175 break;
176 default:
Mingming Yin90310102013-11-13 16:57:00 -0800177 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700178 }
179
180 return id;
181}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800182
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700183int enable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700184 struct audio_usecase *usecase,
185 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800186{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700187 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700188 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800189
190 if (usecase == NULL)
191 return -EINVAL;
192
193 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
194
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800195 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700196 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800197 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700198 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800199
200 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700201 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700202 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700203 audio_route_apply_path(adev->audio_route, mixer_path);
204 if (update_mixer)
205 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800206
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800207 ALOGV("%s: exit", __func__);
208 return 0;
209}
210
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700211int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700212 struct audio_usecase *usecase,
213 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800214{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700215 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700216 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800217
218 if (usecase == NULL)
219 return -EINVAL;
220
221 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700222 if (usecase->type == PCM_CAPTURE)
223 snd_device = usecase->in_snd_device;
224 else
225 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800226 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700227 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700228 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700229 audio_route_reset_path(adev->audio_route, mixer_path);
230 if (update_mixer)
231 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800232
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800233 ALOGV("%s: exit", __func__);
234 return 0;
235}
236
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700237int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700238 snd_device_t snd_device,
239 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800240{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700241 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
242
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800243 if (snd_device < SND_DEVICE_MIN ||
244 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800245 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800246 return -EINVAL;
247 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700248
249 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700250
251 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
252 ALOGE("%s: Invalid sound device returned", __func__);
253 return -EINVAL;
254 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700255 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700256 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700257 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700258 return 0;
259 }
260
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700261 /* start usb playback thread */
262 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
263 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
264 audio_extn_usb_start_playback(adev);
265
266 /* start usb capture thread */
267 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
268 audio_extn_usb_start_capture(adev);
269
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700270 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
271 audio_extn_spkr_prot_is_enabled()) {
272 if (audio_extn_spkr_prot_start_processing(snd_device)) {
273 ALOGE("%s: spkr_start_processing failed", __func__);
274 return -EINVAL;
275 }
276 } else {
277 ALOGV("%s: snd_device(%d: %s)", __func__,
278 snd_device, device_name);
279 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
280 adev->snd_dev_ref_cnt[snd_device]--;
281 return -EINVAL;
282 }
Kiran Kandide144c82013-11-20 15:58:32 -0800283 audio_extn_listen_update_status(snd_device,
284 LISTEN_EVENT_SND_DEVICE_BUSY);
285
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700286 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800287 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700288 if (update_mixer)
289 audio_route_update_mixer(adev->audio_route);
290
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800291 return 0;
292}
293
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700294int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700295 snd_device_t snd_device,
296 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800297{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700298 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
299
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800300 if (snd_device < SND_DEVICE_MIN ||
301 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800302 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800303 return -EINVAL;
304 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700305 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
306 ALOGE("%s: device ref cnt is already 0", __func__);
307 return -EINVAL;
308 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700309
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700310 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700311
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700312 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
313 ALOGE("%s: Invalid sound device returned", __func__);
314 return -EINVAL;
315 }
316
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700317 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700318 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700319 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800320 /* exit usb play back thread */
321 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
322 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
323 audio_extn_usb_stop_playback();
324
325 /* exit usb capture thread */
326 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
327 audio_extn_usb_stop_capture(adev);
328
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700329 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
330 audio_extn_spkr_prot_is_enabled()) {
331 audio_extn_spkr_prot_stop_processing();
332 } else
333 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800334
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700335 if (update_mixer)
336 audio_route_update_mixer(adev->audio_route);
Kiran Kandide144c82013-11-20 15:58:32 -0800337
338 audio_extn_listen_update_status(snd_device,
339 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700340 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700341
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800342 return 0;
343}
344
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700345static void check_usecases_codec_backend(struct audio_device *adev,
346 struct audio_usecase *uc_info,
347 snd_device_t snd_device)
348{
349 struct listnode *node;
350 struct audio_usecase *usecase;
351 bool switch_device[AUDIO_USECASE_MAX];
352 int i, num_uc_to_switch = 0;
353
354 /*
355 * This function is to make sure that all the usecases that are active on
356 * the hardware codec backend are always routed to any one device that is
357 * handled by the hardware codec.
358 * For example, if low-latency and deep-buffer usecases are currently active
359 * on speaker and out_set_parameters(headset) is received on low-latency
360 * output, then we have to make sure deep-buffer is also switched to headset,
361 * because of the limitation that both the devices cannot be enabled
362 * at the same time as they share the same backend.
363 */
364 /* Disable all the usecases on the shared backend other than the
365 specified usecase */
366 for (i = 0; i < AUDIO_USECASE_MAX; i++)
367 switch_device[i] = false;
368
369 list_for_each(node, &adev->usecase_list) {
370 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700371 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700372 usecase != uc_info &&
373 usecase->out_snd_device != snd_device &&
374 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
375 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
376 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700377 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700378 disable_audio_route(adev, usecase, false);
379 switch_device[usecase->id] = true;
380 num_uc_to_switch++;
381 }
382 }
383
384 if (num_uc_to_switch) {
385 /* Make sure all the streams are de-routed before disabling the device */
386 audio_route_update_mixer(adev->audio_route);
387
388 list_for_each(node, &adev->usecase_list) {
389 usecase = node_to_item(node, struct audio_usecase, list);
390 if (switch_device[usecase->id]) {
391 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700392 }
393 }
394
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700395 list_for_each(node, &adev->usecase_list) {
396 usecase = node_to_item(node, struct audio_usecase, list);
397 if (switch_device[usecase->id]) {
398 enable_snd_device(adev, snd_device, false);
399 }
400 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700401
402 /* Make sure new snd device is enabled before re-routing the streams */
403 audio_route_update_mixer(adev->audio_route);
404
405 /* Re-route all the usecases on the shared backend other than the
406 specified usecase to new snd devices */
407 list_for_each(node, &adev->usecase_list) {
408 usecase = node_to_item(node, struct audio_usecase, list);
409 /* Update the out_snd_device only before enabling the audio route */
410 if (switch_device[usecase->id] ) {
411 usecase->out_snd_device = snd_device;
412 enable_audio_route(adev, usecase, false);
413 }
414 }
415
416 audio_route_update_mixer(adev->audio_route);
417 }
418}
419
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700420static void check_and_route_capture_usecases(struct audio_device *adev,
421 struct audio_usecase *uc_info,
422 snd_device_t snd_device)
423{
424 struct listnode *node;
425 struct audio_usecase *usecase;
426 bool switch_device[AUDIO_USECASE_MAX];
427 int i, num_uc_to_switch = 0;
428
429 /*
430 * This function is to make sure that all the active capture usecases
431 * are always routed to the same input sound device.
432 * For example, if audio-record and voice-call usecases are currently
433 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
434 * is received for voice call then we have to make sure that audio-record
435 * usecase is also switched to earpiece i.e. voice-dmic-ef,
436 * because of the limitation that two devices cannot be enabled
437 * at the same time if they share the same backend.
438 */
439 for (i = 0; i < AUDIO_USECASE_MAX; i++)
440 switch_device[i] = false;
441
442 list_for_each(node, &adev->usecase_list) {
443 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700444 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700445 usecase != uc_info &&
446 usecase->in_snd_device != snd_device) {
447 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
448 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700449 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700450 disable_audio_route(adev, usecase, false);
451 switch_device[usecase->id] = true;
452 num_uc_to_switch++;
453 }
454 }
455
456 if (num_uc_to_switch) {
457 /* Make sure all the streams are de-routed before disabling the device */
458 audio_route_update_mixer(adev->audio_route);
459
460 list_for_each(node, &adev->usecase_list) {
461 usecase = node_to_item(node, struct audio_usecase, list);
462 if (switch_device[usecase->id]) {
463 disable_snd_device(adev, usecase->in_snd_device, false);
464 enable_snd_device(adev, snd_device, false);
465 }
466 }
467
468 /* Make sure new snd device is enabled before re-routing the streams */
469 audio_route_update_mixer(adev->audio_route);
470
471 /* Re-route all the usecases on the shared backend other than the
472 specified usecase to new snd devices */
473 list_for_each(node, &adev->usecase_list) {
474 usecase = node_to_item(node, struct audio_usecase, list);
475 /* Update the in_snd_device only before enabling the audio route */
476 if (switch_device[usecase->id] ) {
477 usecase->in_snd_device = snd_device;
478 enable_audio_route(adev, usecase, false);
479 }
480 }
481
482 audio_route_update_mixer(adev->audio_route);
483 }
484}
485
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700486static int disable_all_usecases_of_type(struct audio_device *adev,
487 usecase_type_t usecase_type,
488 bool update_mixer)
489{
490 struct audio_usecase *usecase;
491 struct listnode *node;
492 int ret = 0;
493
494 list_for_each(node, &adev->usecase_list) {
495 usecase = node_to_item(node, struct audio_usecase, list);
496 if (usecase->type == usecase_type) {
497 ALOGV("%s: usecase id %d", __func__, usecase->id);
498 ret = disable_audio_route(adev, usecase, update_mixer);
499 if (ret) {
500 ALOGE("%s: Failed to disable usecase id %d",
501 __func__, usecase->id);
502 }
503 }
504 }
505
506 return ret;
507}
508
509static int enable_all_usecases_of_type(struct audio_device *adev,
510 usecase_type_t usecase_type,
511 bool update_mixer)
512{
513 struct audio_usecase *usecase;
514 struct listnode *node;
515 int ret = 0;
516
517 list_for_each(node, &adev->usecase_list) {
518 usecase = node_to_item(node, struct audio_usecase, list);
519 if (usecase->type == usecase_type) {
520 ALOGV("%s: usecase id %d", __func__, usecase->id);
521 ret = enable_audio_route(adev, usecase, update_mixer);
522 if (ret) {
523 ALOGE("%s: Failed to enable usecase id %d",
524 __func__, usecase->id);
525 }
526 }
527 }
528
529 return ret;
530}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800531
532/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700533static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800534{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700535 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700536 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800537
538 switch (channels) {
539 /*
540 * Do not handle stereo output in Multi-channel cases
541 * Stereo case is handled in normal playback path
542 */
543 case 6:
544 ALOGV("%s: HDMI supports 5.1", __func__);
545 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
546 break;
547 case 8:
548 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
549 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
550 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
551 break;
552 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700553 ALOGE("HDMI does not support multi channel playback");
554 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800555 break;
556 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700557 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800558}
559
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700560static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
561{
562 struct audio_usecase *usecase;
563 struct listnode *node;
564
565 list_for_each(node, &adev->usecase_list) {
566 usecase = node_to_item(node, struct audio_usecase, list);
567 if (usecase->type == VOICE_CALL) {
568 ALOGV("%s: usecase id %d", __func__, usecase->id);
569 return usecase->id;
570 }
571 }
572 return USECASE_INVALID;
573}
574
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700575struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700576 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700577{
578 struct audio_usecase *usecase;
579 struct listnode *node;
580
581 list_for_each(node, &adev->usecase_list) {
582 usecase = node_to_item(node, struct audio_usecase, list);
583 if (usecase->id == uc_id)
584 return usecase;
585 }
586 return NULL;
587}
588
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700589int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800590{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800591 snd_device_t out_snd_device = SND_DEVICE_NONE;
592 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700593 struct audio_usecase *usecase = NULL;
594 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800595 struct audio_usecase *voip_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800596 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700597 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800598
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700599 usecase = get_usecase_from_list(adev, uc_id);
600 if (usecase == NULL) {
601 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
602 return -EINVAL;
603 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800604
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800605 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800606 (usecase->type == VOIP_CALL) ||
607 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700608 out_snd_device = platform_get_output_snd_device(adev->platform,
609 usecase->stream.out->devices);
610 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700611 usecase->devices = usecase->stream.out->devices;
612 } else {
613 /*
614 * If the voice call is active, use the sound devices of voice call usecase
615 * so that it would not result any device switch. All the usecases will
616 * be switched to new device when select_devices() is called for voice call
617 * usecase. This is to avoid switching devices for voice call when
618 * check_usecases_codec_backend() is called below.
619 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700620 if (voice_is_in_call(adev)) {
621 vc_usecase = get_usecase_from_list(adev,
622 get_voice_usecase_id_from_list(adev));
Helen Zeng067b96b2013-11-26 12:10:29 -0800623 if ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
624 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700625 in_snd_device = vc_usecase->in_snd_device;
626 out_snd_device = vc_usecase->out_snd_device;
627 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800628 } else if (voice_extn_compress_voip_is_active(adev)) {
629 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
630 if (voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
631 in_snd_device = voip_usecase->in_snd_device;
632 out_snd_device = voip_usecase->out_snd_device;
633 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700634 }
635 if (usecase->type == PCM_PLAYBACK) {
636 usecase->devices = usecase->stream.out->devices;
637 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700638 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700639 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700640 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700641 if (usecase->stream.out == adev->primary_output &&
642 adev->active_input &&
643 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
644 select_devices(adev, adev->active_input->usecase);
645 }
646 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700647 } else if (usecase->type == PCM_CAPTURE) {
648 usecase->devices = usecase->stream.in->device;
649 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700650 if (in_snd_device == SND_DEVICE_NONE) {
651 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
652 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700653 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700654 adev->primary_output->devices);
655 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700656 in_snd_device = platform_get_input_snd_device(adev->platform,
657 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700658 }
659 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700660 }
661 }
662
663 if (out_snd_device == usecase->out_snd_device &&
664 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800665 return 0;
666 }
667
sangwoobc677242013-08-08 16:53:43 +0900668 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700669 out_snd_device, platform_get_snd_device_name(out_snd_device),
670 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800671
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800672 /*
673 * Limitation: While in call, to do a device switch we need to disable
674 * and enable both RX and TX devices though one of them is same as current
675 * device.
676 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800677 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700678 status = platform_switch_voice_call_device_pre(adev->platform);
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800679 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800680 }
681
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700682 /* Disable current sound devices */
683 if (usecase->out_snd_device != SND_DEVICE_NONE) {
684 disable_audio_route(adev, usecase, true);
685 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800686 }
687
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700688 if (usecase->in_snd_device != SND_DEVICE_NONE) {
689 disable_audio_route(adev, usecase, true);
690 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691 }
692
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700693 /* Enable new sound devices */
694 if (out_snd_device != SND_DEVICE_NONE) {
695 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
696 check_usecases_codec_backend(adev, usecase, out_snd_device);
697 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800698 }
699
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700700 if (in_snd_device != SND_DEVICE_NONE) {
701 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700702 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700703 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700704
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800705 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700706 status = platform_switch_voice_call_device_post(adev->platform,
707 out_snd_device,
708 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800709
sangwoo170731f2013-06-08 15:36:36 +0900710 audio_route_update_mixer(adev->audio_route);
711
712 usecase->in_snd_device = in_snd_device;
713 usecase->out_snd_device = out_snd_device;
714
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800715 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
716 enable_all_usecases_of_type(adev, usecase->type, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700717 else
718 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900719
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800720 /* Applicable only on the targets that has external modem.
721 * Enable device command should be sent to modem only after
722 * enabling voice call mixer controls
723 */
724 if (usecase->type == VOICE_CALL)
725 status = platform_switch_voice_call_usecase_route_post(adev->platform,
726 out_snd_device,
727 in_snd_device);
728
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800729 return status;
730}
731
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732static int stop_input_stream(struct stream_in *in)
733{
734 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800735 struct audio_usecase *uc_info;
736 struct audio_device *adev = in->dev;
737
Eric Laurentc8400632013-02-14 19:04:54 -0800738 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800739
Eric Laurent994a6932013-07-17 11:51:42 -0700740 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700741 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800742 uc_info = get_usecase_from_list(adev, in->usecase);
743 if (uc_info == NULL) {
744 ALOGE("%s: Could not find the usecase (%d) in the list",
745 __func__, in->usecase);
746 return -EINVAL;
747 }
748
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800749 /* Close in-call recording streams */
750 voice_check_and_stop_incall_rec_usecase(adev, in);
751
Eric Laurent150dbfe2013-02-27 14:31:02 -0800752 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700753 disable_audio_route(adev, uc_info, true);
754
755 /* 2. Disable the tx device */
756 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800757
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800758 list_remove(&uc_info->list);
759 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800760
Eric Laurent994a6932013-07-17 11:51:42 -0700761 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800762 return ret;
763}
764
765int start_input_stream(struct stream_in *in)
766{
767 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800768 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800769 struct audio_usecase *uc_info;
770 struct audio_device *adev = in->dev;
771
Mingming Yine62d7842013-10-25 16:26:03 -0700772 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Eric Laurent994a6932013-07-17 11:51:42 -0700773 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700774
775 /* Check if source matches incall recording usecase criteria */
776 ret = voice_check_and_set_incall_rec_usecase(adev, in);
777 if (ret)
778 goto error_config;
779 else
780 ALOGV("%s: usecase(%d)", __func__, in->usecase);
781
Eric Laurentb23d5282013-05-14 15:27:20 -0700782 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800783 if (in->pcm_device_id < 0) {
784 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
785 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800786 ret = -EINVAL;
787 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800788 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700789
790 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800791 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
792 uc_info->id = in->usecase;
793 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800794 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700795 uc_info->devices = in->device;
796 uc_info->in_snd_device = SND_DEVICE_NONE;
797 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800798
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800799 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700800 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800801
Eric Laurentc8400632013-02-14 19:04:54 -0800802 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800803 __func__, adev->snd_card,
804 in->pcm_device_id, in->config.channels);
805 in->pcm = pcm_open(adev->snd_card,
806 in->pcm_device_id, PCM_IN, &in->config);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800807 if (in->pcm && !pcm_is_ready(in->pcm)) {
808 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
809 pcm_close(in->pcm);
810 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800811 ret = -EIO;
812 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800813 }
Eric Laurent994a6932013-07-17 11:51:42 -0700814 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800815 return ret;
816
817error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800818 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800819
820error_config:
821 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700822 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800823
824 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800825}
826
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700827/* must be called with out->lock locked */
828static int send_offload_cmd_l(struct stream_out* out, int command)
829{
830 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
831
832 ALOGVV("%s %d", __func__, command);
833
834 cmd->cmd = command;
835 list_add_tail(&out->offload_cmd_list, &cmd->node);
836 pthread_cond_signal(&out->offload_cond);
837 return 0;
838}
839
840/* must be called iwth out->lock locked */
841static void stop_compressed_output_l(struct stream_out *out)
842{
843 out->offload_state = OFFLOAD_STATE_IDLE;
844 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700845 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700846 if (out->compr != NULL) {
847 compress_stop(out->compr);
848 while (out->offload_thread_blocked) {
849 pthread_cond_wait(&out->cond, &out->lock);
850 }
851 }
852}
853
854static void *offload_thread_loop(void *context)
855{
856 struct stream_out *out = (struct stream_out *) context;
857 struct listnode *item;
858
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700859 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
860 set_sched_policy(0, SP_FOREGROUND);
861 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
862
863 ALOGV("%s", __func__);
864 pthread_mutex_lock(&out->lock);
865 for (;;) {
866 struct offload_cmd *cmd = NULL;
867 stream_callback_event_t event;
868 bool send_callback = false;
869
870 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
871 __func__, list_empty(&out->offload_cmd_list),
872 out->offload_state);
873 if (list_empty(&out->offload_cmd_list)) {
874 ALOGV("%s SLEEPING", __func__);
875 pthread_cond_wait(&out->offload_cond, &out->lock);
876 ALOGV("%s RUNNING", __func__);
877 continue;
878 }
879
880 item = list_head(&out->offload_cmd_list);
881 cmd = node_to_item(item, struct offload_cmd, node);
882 list_remove(item);
883
884 ALOGVV("%s STATE %d CMD %d out->compr %p",
885 __func__, out->offload_state, cmd->cmd, out->compr);
886
887 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
888 free(cmd);
889 break;
890 }
891
892 if (out->compr == NULL) {
893 ALOGE("%s: Compress handle is NULL", __func__);
894 pthread_cond_signal(&out->cond);
895 continue;
896 }
897 out->offload_thread_blocked = true;
898 pthread_mutex_unlock(&out->lock);
899 send_callback = false;
900 switch(cmd->cmd) {
901 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
902 compress_wait(out->compr, -1);
903 send_callback = true;
904 event = STREAM_CBK_EVENT_WRITE_READY;
905 break;
906 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700907 compress_next_track(out->compr);
908 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700909 send_callback = true;
910 event = STREAM_CBK_EVENT_DRAIN_READY;
911 break;
912 case OFFLOAD_CMD_DRAIN:
913 compress_drain(out->compr);
914 send_callback = true;
915 event = STREAM_CBK_EVENT_DRAIN_READY;
916 break;
917 default:
918 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
919 break;
920 }
921 pthread_mutex_lock(&out->lock);
922 out->offload_thread_blocked = false;
923 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700924 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700925 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700926 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700927 free(cmd);
928 }
929
930 pthread_cond_signal(&out->cond);
931 while (!list_empty(&out->offload_cmd_list)) {
932 item = list_head(&out->offload_cmd_list);
933 list_remove(item);
934 free(node_to_item(item, struct offload_cmd, node));
935 }
936 pthread_mutex_unlock(&out->lock);
937
938 return NULL;
939}
940
941static int create_offload_callback_thread(struct stream_out *out)
942{
943 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
944 list_init(&out->offload_cmd_list);
945 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
946 offload_thread_loop, out);
947 return 0;
948}
949
950static int destroy_offload_callback_thread(struct stream_out *out)
951{
952 pthread_mutex_lock(&out->lock);
953 stop_compressed_output_l(out);
954 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
955
956 pthread_mutex_unlock(&out->lock);
957 pthread_join(out->offload_thread, (void **) NULL);
958 pthread_cond_destroy(&out->offload_cond);
959
960 return 0;
961}
962
Eric Laurent07eeafd2013-10-06 12:52:49 -0700963static bool allow_hdmi_channel_config(struct audio_device *adev)
964{
965 struct listnode *node;
966 struct audio_usecase *usecase;
967 bool ret = true;
968
969 list_for_each(node, &adev->usecase_list) {
970 usecase = node_to_item(node, struct audio_usecase, list);
971 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
972 /*
973 * If voice call is already existing, do not proceed further to avoid
974 * disabling/enabling both RX and TX devices, CSD calls, etc.
975 * Once the voice call done, the HDMI channels can be configured to
976 * max channels of remaining use cases.
977 */
978 if (usecase->id == USECASE_VOICE_CALL) {
979 ALOGD("%s: voice call is active, no change in HDMI channels",
980 __func__);
981 ret = false;
982 break;
983 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
984 ALOGD("%s: multi channel playback is active, "
985 "no change in HDMI channels", __func__);
986 ret = false;
987 break;
988 }
989 }
990 }
991 return ret;
992}
993
994static int check_and_set_hdmi_channels(struct audio_device *adev,
995 unsigned int channels)
996{
997 struct listnode *node;
998 struct audio_usecase *usecase;
999
1000 /* Check if change in HDMI channel config is allowed */
1001 if (!allow_hdmi_channel_config(adev))
1002 return 0;
1003
1004 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001005 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001006 return 0;
1007 }
1008
1009 platform_set_hdmi_channels(adev->platform, channels);
1010 adev->cur_hdmi_channels = channels;
1011
1012 /*
1013 * Deroute all the playback streams routed to HDMI so that
1014 * the back end is deactivated. Note that backend will not
1015 * be deactivated if any one stream is connected to it.
1016 */
1017 list_for_each(node, &adev->usecase_list) {
1018 usecase = node_to_item(node, struct audio_usecase, list);
1019 if (usecase->type == PCM_PLAYBACK &&
1020 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1021 disable_audio_route(adev, usecase, true);
1022 }
1023 }
1024
1025 /*
1026 * Enable all the streams disabled above. Now the HDMI backend
1027 * will be activated with new channel configuration
1028 */
1029 list_for_each(node, &adev->usecase_list) {
1030 usecase = node_to_item(node, struct audio_usecase, list);
1031 if (usecase->type == PCM_PLAYBACK &&
1032 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1033 enable_audio_route(adev, usecase, true);
1034 }
1035 }
1036
1037 return 0;
1038}
1039
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001040static int stop_output_stream(struct stream_out *out)
1041{
1042 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001043 struct audio_usecase *uc_info;
1044 struct audio_device *adev = out->dev;
1045
Eric Laurent994a6932013-07-17 11:51:42 -07001046 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001047 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001048 uc_info = get_usecase_from_list(adev, out->usecase);
1049 if (uc_info == NULL) {
1050 ALOGE("%s: Could not find the usecase (%d) in the list",
1051 __func__, out->usecase);
1052 return -EINVAL;
1053 }
1054
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001055 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1056 if (adev->visualizer_stop_output != NULL)
1057 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1058 if (adev->offload_effects_stop_output != NULL)
1059 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1060 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001061
Eric Laurent150dbfe2013-02-27 14:31:02 -08001062 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001063 disable_audio_route(adev, uc_info, true);
1064
1065 /* 2. Disable the rx device */
1066 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001067
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001068 list_remove(&uc_info->list);
1069 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001070
Eric Laurent07eeafd2013-10-06 12:52:49 -07001071 /* Must be called after removing the usecase from list */
1072 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1073 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1074
Eric Laurent994a6932013-07-17 11:51:42 -07001075 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001076 return ret;
1077}
1078
1079int start_output_stream(struct stream_out *out)
1080{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001081 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001082 struct audio_usecase *uc_info;
1083 struct audio_device *adev = out->dev;
1084
Eric Laurent994a6932013-07-17 11:51:42 -07001085 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001086 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001087 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001088 if (out->pcm_device_id < 0) {
1089 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1090 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001091 ret = -EINVAL;
1092 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001093 }
1094
1095 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1096 uc_info->id = out->usecase;
1097 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001098 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001099 uc_info->devices = out->devices;
1100 uc_info->in_snd_device = SND_DEVICE_NONE;
1101 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001102
Eric Laurent07eeafd2013-10-06 12:52:49 -07001103 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001104 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1105 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1106 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1107 else
1108 check_and_set_hdmi_channels(adev, out->config.channels);
1109 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001110
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001111 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001112
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001113 select_devices(adev, out->usecase);
1114
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001115 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1116 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001117 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001118 out->pcm = pcm_open(adev->snd_card,
1119 out->pcm_device_id,
1120 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001121 if (out->pcm && !pcm_is_ready(out->pcm)) {
1122 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1123 pcm_close(out->pcm);
1124 out->pcm = NULL;
1125 ret = -EIO;
1126 goto error_open;
1127 }
1128 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001129 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001130 out->compr = compress_open(adev->snd_card,
1131 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001132 COMPRESS_IN, &out->compr_config);
1133 if (out->compr && !is_compress_ready(out->compr)) {
1134 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1135 compress_close(out->compr);
1136 out->compr = NULL;
1137 ret = -EIO;
1138 goto error_open;
1139 }
1140 if (out->offload_callback)
1141 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001142
1143 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001144 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1145 if (adev->offload_effects_start_output != NULL)
1146 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001147 }
Eric Laurent994a6932013-07-17 11:51:42 -07001148 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001149 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001150error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001151 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001152error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001153 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001154}
1155
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001156static int check_input_parameters(uint32_t sample_rate,
1157 audio_format_t format,
1158 int channel_count)
1159{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001160 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001161
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001162 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001163 !voice_extn_compress_voip_is_format_supported(format) &&
1164 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001165
1166 switch (channel_count) {
1167 case 1:
1168 case 2:
1169 case 6:
1170 break;
1171 default:
1172 ret = -EINVAL;
1173 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001174
1175 switch (sample_rate) {
1176 case 8000:
1177 case 11025:
1178 case 12000:
1179 case 16000:
1180 case 22050:
1181 case 24000:
1182 case 32000:
1183 case 44100:
1184 case 48000:
1185 break;
1186 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001187 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001188 }
1189
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001190 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001191}
1192
1193static size_t get_input_buffer_size(uint32_t sample_rate,
1194 audio_format_t format,
1195 int channel_count)
1196{
1197 size_t size = 0;
1198
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001199 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1200 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001202 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1203 /* ToDo: should use frame_size computed based on the format and
1204 channel_count here. */
1205 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001206
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001207 /* make sure the size is multiple of 64 */
1208 size += 0x3f;
1209 size &= ~0x3f;
1210
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
1221static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1222{
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
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001230 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001231 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001232 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1233 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001234
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001235 return out->config.period_size * audio_stream_frame_size(stream);
1236}
1237
1238static uint32_t out_get_channels(const struct audio_stream *stream)
1239{
1240 struct stream_out *out = (struct stream_out *)stream;
1241
1242 return out->channel_mask;
1243}
1244
1245static audio_format_t out_get_format(const struct audio_stream *stream)
1246{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001247 struct stream_out *out = (struct stream_out *)stream;
1248
1249 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001250}
1251
1252static int out_set_format(struct audio_stream *stream, audio_format_t format)
1253{
1254 return -ENOSYS;
1255}
1256
1257static int out_standby(struct audio_stream *stream)
1258{
1259 struct stream_out *out = (struct stream_out *)stream;
1260 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001261
Eric Laurent994a6932013-07-17 11:51:42 -07001262 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001263 out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001264 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1265 /* Ignore standby in case of voip call because the voip output
1266 * stream is closed in adev_close_output_stream()
1267 */
1268 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1269 return 0;
1270 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001271
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001272 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001274 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001275 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001276 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1277 if (out->pcm) {
1278 pcm_close(out->pcm);
1279 out->pcm = NULL;
1280 }
1281 } else {
1282 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001283 out->gapless_mdata.encoder_delay = 0;
1284 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001285 if (out->compr != NULL) {
1286 compress_close(out->compr);
1287 out->compr = NULL;
1288 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001289 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001290 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001291 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001292 }
1293 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001294 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001295 return 0;
1296}
1297
1298static int out_dump(const struct audio_stream *stream, int fd)
1299{
1300 return 0;
1301}
1302
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001303static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1304{
1305 int ret = 0;
1306 char value[32];
1307 struct compr_gapless_mdata tmp_mdata;
1308
1309 if (!out || !parms) {
1310 return -EINVAL;
1311 }
1312
1313 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1314 if (ret >= 0) {
1315 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1316 } else {
1317 return -EINVAL;
1318 }
1319
1320 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1321 if (ret >= 0) {
1322 tmp_mdata.encoder_padding = atoi(value);
1323 } else {
1324 return -EINVAL;
1325 }
1326
1327 out->gapless_mdata = tmp_mdata;
1328 out->send_new_metadata = 1;
1329 ALOGV("%s new encoder delay %u and padding %u", __func__,
1330 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1331
1332 return 0;
1333}
1334
1335
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001336static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1337{
1338 struct stream_out *out = (struct stream_out *)stream;
1339 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001340 struct audio_usecase *usecase;
1341 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001342 struct str_parms *parms;
1343 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001344 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001345 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001346
sangwoobc677242013-08-08 16:53:43 +09001347 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001348 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001349 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001350 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1351 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001352 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001353 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001354 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001355
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001356 /*
1357 * When HDMI cable is unplugged the music playback is paused and
1358 * the policy manager sends routing=0. But the audioflinger
1359 * continues to write data until standby time (3sec).
1360 * As the HDMI core is turned off, the write gets blocked.
1361 * Avoid this by routing audio to speaker until standby.
1362 */
1363 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1364 val == AUDIO_DEVICE_NONE) {
1365 val = AUDIO_DEVICE_OUT_SPEAKER;
1366 }
1367
1368 /*
1369 * select_devices() call below switches all the usecases on the same
1370 * backend to the new device. Refer to check_usecases_codec_backend() in
1371 * the select_devices(). But how do we undo this?
1372 *
1373 * For example, music playback is active on headset (deep-buffer usecase)
1374 * and if we go to ringtones and select a ringtone, low-latency usecase
1375 * will be started on headset+speaker. As we can't enable headset+speaker
1376 * and headset devices at the same time, select_devices() switches the music
1377 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1378 * So when the ringtone playback is completed, how do we undo the same?
1379 *
1380 * We are relying on the out_set_parameters() call on deep-buffer output,
1381 * once the ringtone playback is ended.
1382 * NOTE: We should not check if the current devices are same as new devices.
1383 * Because select_devices() must be called to switch back the music
1384 * playback to headset.
1385 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001386 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001387 out->devices = val;
1388
1389 if (!out->standby)
1390 select_devices(adev, out->usecase);
1391
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001392 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1393 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001394 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001395 ret = voice_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001396 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1397 voice_is_in_call(adev) &&
1398 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001399 ret = select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001400 }
1401 }
1402
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001403 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1404 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001405 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001406 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001407 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001408
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001410 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001411 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001412
1413 if (out == adev->primary_output) {
1414 pthread_mutex_lock(&adev->lock);
1415 audio_extn_set_parameters(adev, parms);
1416 pthread_mutex_unlock(&adev->lock);
1417 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001418 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1419 parse_compress_metadata(out, parms);
1420 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001421
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001422 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001423 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001424 return ret;
1425}
1426
1427static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1428{
1429 struct stream_out *out = (struct stream_out *)stream;
1430 struct str_parms *query = str_parms_create_str(keys);
1431 char *str;
1432 char value[256];
1433 struct str_parms *reply = str_parms_create();
1434 size_t i, j;
1435 int ret;
1436 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001437 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001438 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1439 if (ret >= 0) {
1440 value[0] = '\0';
1441 i = 0;
1442 while (out->supported_channel_masks[i] != 0) {
1443 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1444 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1445 if (!first) {
1446 strcat(value, "|");
1447 }
1448 strcat(value, out_channels_name_to_enum_table[j].name);
1449 first = false;
1450 break;
1451 }
1452 }
1453 i++;
1454 }
1455 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1456 str = str_parms_to_str(reply);
1457 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001458 voice_extn_out_get_parameters(out, query, reply);
1459 str = str_parms_to_str(reply);
1460 if (!strncmp(str, "", sizeof(""))) {
1461 str = strdup(keys);
1462 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001463 }
1464 str_parms_destroy(query);
1465 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001466 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001467 return str;
1468}
1469
1470static uint32_t out_get_latency(const struct audio_stream_out *stream)
1471{
1472 struct stream_out *out = (struct stream_out *)stream;
1473
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001474 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1475 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1476
1477 return (out->config.period_count * out->config.period_size * 1000) /
1478 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001479}
1480
1481static int out_set_volume(struct audio_stream_out *stream, float left,
1482 float right)
1483{
Eric Laurenta9024de2013-04-04 09:19:12 -07001484 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001485 int volume[2];
1486
Eric Laurenta9024de2013-04-04 09:19:12 -07001487 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1488 /* only take left channel into account: the API is for stereo anyway */
1489 out->muted = (left == 0.0f);
1490 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001491 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001492 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001493 struct audio_device *adev = out->dev;
1494 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001495 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1496 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001497
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001498 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1499 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001500 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1501 if (!ctl) {
1502 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1503 __func__, mixer_ctl_name);
1504 return -EINVAL;
1505 }
1506 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1507 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1508 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1509 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001510 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001511
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001512 return -ENOSYS;
1513}
1514
1515static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1516 size_t bytes)
1517{
1518 struct stream_out *out = (struct stream_out *)stream;
1519 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001520 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001521
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001522 pthread_mutex_lock(&out->lock);
1523 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001524 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001525 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001526 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1527 ret = voice_extn_compress_voip_start_output_stream(out);
1528 else
1529 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001530 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001531 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001532 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001533 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001534 goto exit;
1535 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001536 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001537
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001538 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001539 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1540 if (out->send_new_metadata) {
1541 ALOGVV("send new gapless metadata");
1542 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1543 out->send_new_metadata = 0;
1544 }
1545
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001546 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001547 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001548 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001549 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1550 }
1551 if (!out->playback_started) {
1552 compress_start(out->compr);
1553 out->playback_started = 1;
1554 out->offload_state = OFFLOAD_STATE_PLAYING;
1555 }
1556 pthread_mutex_unlock(&out->lock);
1557 return ret;
1558 } else {
1559 if (out->pcm) {
1560 if (out->muted)
1561 memset((void *)buffer, 0, bytes);
1562 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1563 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001564 if (ret == 0)
1565 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001566 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001567 }
1568
1569exit:
1570 pthread_mutex_unlock(&out->lock);
1571
1572 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001573 if (out->pcm)
1574 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001575 out_standby(&out->stream.common);
1576 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1577 out_get_sample_rate(&out->stream.common));
1578 }
1579 return bytes;
1580}
1581
1582static int out_get_render_position(const struct audio_stream_out *stream,
1583 uint32_t *dsp_frames)
1584{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001585 struct stream_out *out = (struct stream_out *)stream;
1586 *dsp_frames = 0;
1587 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1588 pthread_mutex_lock(&out->lock);
1589 if (out->compr != NULL) {
1590 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1591 &out->sample_rate);
1592 ALOGVV("%s rendered frames %d sample_rate %d",
1593 __func__, *dsp_frames, out->sample_rate);
1594 }
1595 pthread_mutex_unlock(&out->lock);
1596 return 0;
1597 } else
1598 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001599}
1600
1601static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1602{
1603 return 0;
1604}
1605
1606static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1607{
1608 return 0;
1609}
1610
1611static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1612 int64_t *timestamp)
1613{
1614 return -EINVAL;
1615}
1616
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001617static int out_get_presentation_position(const struct audio_stream_out *stream,
1618 uint64_t *frames, struct timespec *timestamp)
1619{
1620 struct stream_out *out = (struct stream_out *)stream;
1621 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001622 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001623
1624 pthread_mutex_lock(&out->lock);
1625
Eric Laurent949a0892013-09-20 09:20:13 -07001626 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1627 if (out->compr != NULL) {
1628 compress_get_tstamp(out->compr, &dsp_frames,
1629 &out->sample_rate);
1630 ALOGVV("%s rendered frames %ld sample_rate %d",
1631 __func__, dsp_frames, out->sample_rate);
1632 *frames = dsp_frames;
1633 ret = 0;
1634 /* this is the best we can do */
1635 clock_gettime(CLOCK_MONOTONIC, timestamp);
1636 }
1637 } else {
1638 if (out->pcm) {
1639 size_t avail;
1640 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1641 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001642 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001643 // This adjustment accounts for buffering after app processor.
1644 // It is based on estimated DSP latency per use case, rather than exact.
1645 signed_frames -=
1646 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1647
Eric Laurent949a0892013-09-20 09:20:13 -07001648 // It would be unusual for this value to be negative, but check just in case ...
1649 if (signed_frames >= 0) {
1650 *frames = signed_frames;
1651 ret = 0;
1652 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001653 }
1654 }
1655 }
1656
1657 pthread_mutex_unlock(&out->lock);
1658
1659 return ret;
1660}
1661
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001662static int out_set_callback(struct audio_stream_out *stream,
1663 stream_callback_t callback, void *cookie)
1664{
1665 struct stream_out *out = (struct stream_out *)stream;
1666
1667 ALOGV("%s", __func__);
1668 pthread_mutex_lock(&out->lock);
1669 out->offload_callback = callback;
1670 out->offload_cookie = cookie;
1671 pthread_mutex_unlock(&out->lock);
1672 return 0;
1673}
1674
1675static int out_pause(struct audio_stream_out* stream)
1676{
1677 struct stream_out *out = (struct stream_out *)stream;
1678 int status = -ENOSYS;
1679 ALOGV("%s", __func__);
1680 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1681 pthread_mutex_lock(&out->lock);
1682 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1683 status = compress_pause(out->compr);
1684 out->offload_state = OFFLOAD_STATE_PAUSED;
1685 }
1686 pthread_mutex_unlock(&out->lock);
1687 }
1688 return status;
1689}
1690
1691static int out_resume(struct audio_stream_out* stream)
1692{
1693 struct stream_out *out = (struct stream_out *)stream;
1694 int status = -ENOSYS;
1695 ALOGV("%s", __func__);
1696 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1697 status = 0;
1698 pthread_mutex_lock(&out->lock);
1699 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1700 status = compress_resume(out->compr);
1701 out->offload_state = OFFLOAD_STATE_PLAYING;
1702 }
1703 pthread_mutex_unlock(&out->lock);
1704 }
1705 return status;
1706}
1707
1708static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1709{
1710 struct stream_out *out = (struct stream_out *)stream;
1711 int status = -ENOSYS;
1712 ALOGV("%s", __func__);
1713 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1714 pthread_mutex_lock(&out->lock);
1715 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1716 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1717 else
1718 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1719 pthread_mutex_unlock(&out->lock);
1720 }
1721 return status;
1722}
1723
1724static int out_flush(struct audio_stream_out* stream)
1725{
1726 struct stream_out *out = (struct stream_out *)stream;
1727 ALOGV("%s", __func__);
1728 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1729 pthread_mutex_lock(&out->lock);
1730 stop_compressed_output_l(out);
1731 pthread_mutex_unlock(&out->lock);
1732 return 0;
1733 }
1734 return -ENOSYS;
1735}
1736
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001737/** audio_stream_in implementation **/
1738static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1739{
1740 struct stream_in *in = (struct stream_in *)stream;
1741
1742 return in->config.rate;
1743}
1744
1745static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1746{
1747 return -ENOSYS;
1748}
1749
1750static size_t in_get_buffer_size(const struct audio_stream *stream)
1751{
1752 struct stream_in *in = (struct stream_in *)stream;
1753
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001754 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1755 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001756 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1757 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001758
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001759 return in->config.period_size * audio_stream_frame_size(stream);
1760}
1761
1762static uint32_t in_get_channels(const struct audio_stream *stream)
1763{
1764 struct stream_in *in = (struct stream_in *)stream;
1765
1766 return in->channel_mask;
1767}
1768
1769static audio_format_t in_get_format(const struct audio_stream *stream)
1770{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001771 struct stream_in *in = (struct stream_in *)stream;
1772
1773 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001774}
1775
1776static int in_set_format(struct audio_stream *stream, audio_format_t format)
1777{
1778 return -ENOSYS;
1779}
1780
1781static int in_standby(struct audio_stream *stream)
1782{
1783 struct stream_in *in = (struct stream_in *)stream;
1784 struct audio_device *adev = in->dev;
1785 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001786 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001787
1788 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1789 /* Ignore standby in case of voip call because the voip input
1790 * stream is closed in adev_close_input_stream()
1791 */
1792 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1793 return status;
1794 }
1795
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001796 pthread_mutex_lock(&in->lock);
1797 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001798 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001799 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001800 if (in->pcm) {
1801 pcm_close(in->pcm);
1802 in->pcm = NULL;
1803 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001804 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001805 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001806 }
1807 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001808 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001809 return status;
1810}
1811
1812static int in_dump(const struct audio_stream *stream, int fd)
1813{
1814 return 0;
1815}
1816
1817static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1818{
1819 struct stream_in *in = (struct stream_in *)stream;
1820 struct audio_device *adev = in->dev;
1821 struct str_parms *parms;
1822 char *str;
1823 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001824 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001825
Eric Laurent994a6932013-07-17 11:51:42 -07001826 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001827 parms = str_parms_create_str(kvpairs);
1828
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001829 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001830 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001831
1832 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1833 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001834 val = atoi(value);
1835 /* no audio source uses val == 0 */
1836 if ((in->source != val) && (val != 0)) {
1837 in->source = val;
1838 }
1839 }
1840
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001841 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1842 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001843 val = atoi(value);
1844 if ((in->device != val) && (val != 0)) {
1845 in->device = val;
1846 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001847 if (!in->standby)
1848 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001849 }
1850 }
1851
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001852 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001853 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001854
1855 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001856 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001857 return ret;
1858}
1859
1860static char* in_get_parameters(const struct audio_stream *stream,
1861 const char *keys)
1862{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001863 struct stream_in *in = (struct stream_in *)stream;
1864 struct str_parms *query = str_parms_create_str(keys);
1865 char *str;
1866 char value[256];
1867 struct str_parms *reply = str_parms_create();
1868 ALOGV("%s: enter: keys - %s", __func__, keys);
1869
1870 voice_extn_in_get_parameters(in, query, reply);
1871
1872 str = str_parms_to_str(reply);
1873 str_parms_destroy(query);
1874 str_parms_destroy(reply);
1875
1876 ALOGV("%s: exit: returns - %s", __func__, str);
1877 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001878}
1879
1880static int in_set_gain(struct audio_stream_in *stream, float gain)
1881{
1882 return 0;
1883}
1884
1885static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1886 size_t bytes)
1887{
1888 struct stream_in *in = (struct stream_in *)stream;
1889 struct audio_device *adev = in->dev;
1890 int i, ret = -1;
1891
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001892 pthread_mutex_lock(&in->lock);
1893 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001894 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001895 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
1896 ret = voice_extn_compress_voip_start_input_stream(in);
1897 else
1898 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001899 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001900 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001901 goto exit;
1902 }
1903 in->standby = 0;
1904 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001905
1906 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001907 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1908 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07001909 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
1910 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001911 else
1912 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001913 }
1914
1915 /*
1916 * Instead of writing zeroes here, we could trust the hardware
1917 * to always provide zeroes when muted.
1918 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001919 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001920 memset(buffer, 0, bytes);
1921
1922exit:
1923 pthread_mutex_unlock(&in->lock);
1924
1925 if (ret != 0) {
1926 in_standby(&in->stream.common);
1927 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1928 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1929 in_get_sample_rate(&in->stream.common));
1930 }
1931 return bytes;
1932}
1933
1934static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1935{
1936 return 0;
1937}
1938
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001939static int add_remove_audio_effect(const struct audio_stream *stream,
1940 effect_handle_t effect,
1941 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001942{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001943 struct stream_in *in = (struct stream_in *)stream;
1944 int status = 0;
1945 effect_descriptor_t desc;
1946
1947 status = (*effect)->get_descriptor(effect, &desc);
1948 if (status != 0)
1949 return status;
1950
1951 pthread_mutex_lock(&in->lock);
1952 pthread_mutex_lock(&in->dev->lock);
1953 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1954 in->enable_aec != enable &&
1955 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1956 in->enable_aec = enable;
1957 if (!in->standby)
1958 select_devices(in->dev, in->usecase);
1959 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08001960 if (in->enable_ns != enable &&
1961 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1962 in->enable_ns = enable;
1963 if (!in->standby)
1964 select_devices(in->dev, in->usecase);
1965 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001966 pthread_mutex_unlock(&in->dev->lock);
1967 pthread_mutex_unlock(&in->lock);
1968
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001969 return 0;
1970}
1971
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001972static int in_add_audio_effect(const struct audio_stream *stream,
1973 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001974{
Eric Laurent994a6932013-07-17 11:51:42 -07001975 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001976 return add_remove_audio_effect(stream, effect, true);
1977}
1978
1979static int in_remove_audio_effect(const struct audio_stream *stream,
1980 effect_handle_t effect)
1981{
Eric Laurent994a6932013-07-17 11:51:42 -07001982 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001983 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001984}
1985
1986static int adev_open_output_stream(struct audio_hw_device *dev,
1987 audio_io_handle_t handle,
1988 audio_devices_t devices,
1989 audio_output_flags_t flags,
1990 struct audio_config *config,
1991 struct audio_stream_out **stream_out)
1992{
1993 struct audio_device *adev = (struct audio_device *)dev;
1994 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08001995 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001996
Eric Laurent994a6932013-07-17 11:51:42 -07001997 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001998 __func__, config->sample_rate, config->channel_mask, devices, flags);
1999 *stream_out = NULL;
2000 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2001
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002002 if (!out) {
2003 return -ENOMEM;
2004 }
2005
2006 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2007 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2008
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002009 if (devices == AUDIO_DEVICE_NONE)
2010 devices = AUDIO_DEVICE_OUT_SPEAKER;
2011
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002012 out->flags = flags;
2013 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002014 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002015 out->format = config->format;
2016 out->sample_rate = config->sample_rate;
2017 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2018 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002019 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002020
2021 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002022 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2023 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2024 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2025
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002026 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002027 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2028 ret = read_hdmi_channel_masks(out);
2029
2030 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2031 ret = audio_extn_read_afe_proxy_channel_masks(out);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002032 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002033 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002034 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002035
2036 if (config->sample_rate == 0)
2037 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2038 if (config->channel_mask == 0)
2039 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2040
2041 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002042 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002043 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2044 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002045 out->config.rate = config->sample_rate;
2046 out->config.channels = popcount(out->channel_mask);
2047 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002048 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2049 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002050 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002051 ret = voice_extn_compress_voip_open_output_stream(out);
2052 if (ret != 0) {
2053 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2054 __func__, ret);
2055 goto error_open;
2056 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002057 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2058 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2059 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2060 ALOGE("%s: Unsupported Offload information", __func__);
2061 ret = -EINVAL;
2062 goto error_open;
2063 }
Mingming Yin90310102013-11-13 16:57:00 -08002064 if (!is_supported_format(config->offload_info.format) &&
2065 !audio_extn_dolby_is_supported_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002066 ALOGE("%s: Unsupported audio format", __func__);
2067 ret = -EINVAL;
2068 goto error_open;
2069 }
2070
2071 out->compr_config.codec = (struct snd_codec *)
2072 calloc(1, sizeof(struct snd_codec));
2073
2074 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2075 if (config->offload_info.channel_mask)
2076 out->channel_mask = config->offload_info.channel_mask;
2077 else if (config->channel_mask)
2078 out->channel_mask = config->channel_mask;
2079 out->format = config->offload_info.format;
2080 out->sample_rate = config->offload_info.sample_rate;
2081
2082 out->stream.set_callback = out_set_callback;
2083 out->stream.pause = out_pause;
2084 out->stream.resume = out_resume;
2085 out->stream.drain = out_drain;
2086 out->stream.flush = out_flush;
2087
Mingming Yin90310102013-11-13 16:57:00 -08002088 if (audio_extn_dolby_is_supported_format(config->offload_info.format))
2089 out->compr_config.codec->id =
2090 audio_extn_dolby_get_snd_codec_id(config->offload_info.format);
2091 else
2092 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002093 get_snd_codec_id(config->offload_info.format);
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -08002094 out->compr_config.fragment_size = get_offload_buffer_size();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002095 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2096 out->compr_config.codec->sample_rate =
2097 compress_get_alsa_rate(config->offload_info.sample_rate);
2098 out->compr_config.codec->bit_rate =
2099 config->offload_info.bit_rate;
2100 out->compr_config.codec->ch_in =
2101 popcount(config->channel_mask);
2102 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2103
2104 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2105 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002106
2107 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002108 out->offload_state = OFFLOAD_STATE_IDLE;
2109 out->playback_started = 0;
2110
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002111 create_offload_callback_thread(out);
2112 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2113 __func__, config->offload_info.version,
2114 config->offload_info.bit_rate);
Mingming Yin90310102013-11-13 16:57:00 -08002115
2116 if (audio_extn_dolby_is_supported_format(out->format)) {
2117 ret = audio_extn_dolby_set_DMID(adev);
2118 if (ret != 0) {
2119 ALOGE("%s: Dolby DMID cannot be set error:%d",
2120 __func__, ret);
2121 goto error_open;
2122 }
2123 }
2124
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002125 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2126 ret = voice_check_and_set_incall_music_usecase(adev, out);
2127 if (ret != 0) {
2128 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2129 __func__, ret);
2130 goto error_open;
2131 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002132 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002133 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2134 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002135 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002136 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002137 /* primary path is the default path selected if no other outputs are available/suitable */
2138 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002139 out->config = pcm_config_deep_buffer;
2140 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002141 }
2142
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002143 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2144 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2145 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002146 if(adev->primary_output == NULL)
2147 adev->primary_output = out;
2148 else {
2149 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002150 ret = -EEXIST;
2151 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002152 }
2153 }
2154
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002155 /* Check if this usecase is already existing */
2156 pthread_mutex_lock(&adev->lock);
2157 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2158 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002159 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002160 ret = -EEXIST;
2161 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002162 }
2163 pthread_mutex_unlock(&adev->lock);
2164
2165 out->stream.common.get_sample_rate = out_get_sample_rate;
2166 out->stream.common.set_sample_rate = out_set_sample_rate;
2167 out->stream.common.get_buffer_size = out_get_buffer_size;
2168 out->stream.common.get_channels = out_get_channels;
2169 out->stream.common.get_format = out_get_format;
2170 out->stream.common.set_format = out_set_format;
2171 out->stream.common.standby = out_standby;
2172 out->stream.common.dump = out_dump;
2173 out->stream.common.set_parameters = out_set_parameters;
2174 out->stream.common.get_parameters = out_get_parameters;
2175 out->stream.common.add_audio_effect = out_add_audio_effect;
2176 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2177 out->stream.get_latency = out_get_latency;
2178 out->stream.set_volume = out_set_volume;
2179 out->stream.write = out_write;
2180 out->stream.get_render_position = out_get_render_position;
2181 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002182 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002183
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002184 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002185 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002186 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002187
2188 config->format = out->stream.common.get_format(&out->stream.common);
2189 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2190 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2191
2192 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002193 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002194 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002195
2196error_open:
2197 free(out);
2198 *stream_out = NULL;
2199 ALOGD("%s: exit: ret %d", __func__, ret);
2200 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002201}
2202
2203static void adev_close_output_stream(struct audio_hw_device *dev,
2204 struct audio_stream_out *stream)
2205{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002206 struct stream_out *out = (struct stream_out *)stream;
2207 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002208 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002209
Eric Laurent994a6932013-07-17 11:51:42 -07002210 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002211 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2212 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2213 if(ret != 0)
2214 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2215 __func__, ret);
2216 }
2217 else
2218 out_standby(&stream->common);
2219
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002220 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2221 destroy_offload_callback_thread(out);
2222
2223 if (out->compr_config.codec != NULL)
2224 free(out->compr_config.codec);
2225 }
2226 pthread_cond_destroy(&out->cond);
2227 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002228 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002229 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002230}
2231
2232static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2233{
2234 struct audio_device *adev = (struct audio_device *)dev;
2235 struct str_parms *parms;
2236 char *str;
2237 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002238 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002239 int ret = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002240
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002241 ALOGD("%s: enter: %s", __func__, kvpairs);
2242
2243 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002244 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002245
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002246 ret = voice_set_parameters(adev, parms);
2247 if (ret != 0)
2248 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002249
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002250 ret = platform_set_parameters(adev->platform, parms);
2251 if (ret != 0)
2252 goto done;
2253
2254 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2255 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002256 /* When set to false, HAL should disable EC and NS
2257 * But it is currently not supported.
2258 */
2259 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2260 adev->bluetooth_nrec = true;
2261 else
2262 adev->bluetooth_nrec = false;
2263 }
2264
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002265 err = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2266 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002267 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2268 adev->screen_off = false;
2269 else
2270 adev->screen_off = true;
2271 }
2272
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002273 err = str_parms_get_int(parms, "rotation", &val);
2274 if (err >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002275 bool reverse_speakers = false;
2276 switch(val) {
2277 // FIXME: note that the code below assumes that the speakers are in the correct placement
2278 // relative to the user when the device is rotated 90deg from its default rotation. This
2279 // assumption is device-specific, not platform-specific like this code.
2280 case 270:
2281 reverse_speakers = true;
2282 break;
2283 case 0:
2284 case 90:
2285 case 180:
2286 break;
2287 default:
2288 ALOGE("%s: unexpected rotation of %d", __func__, val);
2289 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002290 if (adev->speaker_lr_swap != reverse_speakers) {
2291 adev->speaker_lr_swap = reverse_speakers;
2292 // only update the selected device if there is active pcm playback
2293 struct audio_usecase *usecase;
2294 struct listnode *node;
2295 list_for_each(node, &adev->usecase_list) {
2296 usecase = node_to_item(node, struct audio_usecase, list);
2297 if (usecase->type == PCM_PLAYBACK) {
2298 select_devices(adev, usecase->id);
2299 break;
2300 }
2301 }
2302 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002303 }
2304
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002305 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002306
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002307done:
2308 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002309 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002310 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002311 return ret;
2312}
2313
2314static char* adev_get_parameters(const struct audio_hw_device *dev,
2315 const char *keys)
2316{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002317 struct audio_device *adev = (struct audio_device *)dev;
2318 struct str_parms *reply = str_parms_create();
2319 struct str_parms *query = str_parms_create_str(keys);
2320 char *str;
2321
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002322 pthread_mutex_lock(&adev->lock);
2323
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002324 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002325 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002326 platform_get_parameters(adev->platform, query, reply);
2327 str = str_parms_to_str(reply);
2328 str_parms_destroy(query);
2329 str_parms_destroy(reply);
2330
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002331 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002332 ALOGV("%s: exit: returns - %s", __func__, str);
2333 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002334}
2335
2336static int adev_init_check(const struct audio_hw_device *dev)
2337{
2338 return 0;
2339}
2340
2341static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2342{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002343 int ret;
2344 struct audio_device *adev = (struct audio_device *)dev;
2345 pthread_mutex_lock(&adev->lock);
2346 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002347 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002348 pthread_mutex_unlock(&adev->lock);
2349 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002350}
2351
2352static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2353{
2354 return -ENOSYS;
2355}
2356
2357static int adev_get_master_volume(struct audio_hw_device *dev,
2358 float *volume)
2359{
2360 return -ENOSYS;
2361}
2362
2363static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2364{
2365 return -ENOSYS;
2366}
2367
2368static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2369{
2370 return -ENOSYS;
2371}
2372
2373static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2374{
2375 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002376 pthread_mutex_lock(&adev->lock);
2377 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002378 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002379 adev->mode = mode;
2380 }
2381 pthread_mutex_unlock(&adev->lock);
2382 return 0;
2383}
2384
2385static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2386{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002387 int ret;
2388
2389 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002390 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002391 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2392 pthread_mutex_unlock(&adev->lock);
2393
2394 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002395}
2396
2397static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2398{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002399 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002400 return 0;
2401}
2402
2403static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2404 const struct audio_config *config)
2405{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002406 int channel_count = popcount(config->channel_mask);
2407
2408 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2409}
2410
2411static int adev_open_input_stream(struct audio_hw_device *dev,
2412 audio_io_handle_t handle,
2413 audio_devices_t devices,
2414 struct audio_config *config,
2415 struct audio_stream_in **stream_in)
2416{
2417 struct audio_device *adev = (struct audio_device *)dev;
2418 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002419 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002420 int channel_count = popcount(config->channel_mask);
2421
Eric Laurent994a6932013-07-17 11:51:42 -07002422 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002423 *stream_in = NULL;
2424 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2425 return -EINVAL;
2426
2427 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2428
2429 in->stream.common.get_sample_rate = in_get_sample_rate;
2430 in->stream.common.set_sample_rate = in_set_sample_rate;
2431 in->stream.common.get_buffer_size = in_get_buffer_size;
2432 in->stream.common.get_channels = in_get_channels;
2433 in->stream.common.get_format = in_get_format;
2434 in->stream.common.set_format = in_set_format;
2435 in->stream.common.standby = in_standby;
2436 in->stream.common.dump = in_dump;
2437 in->stream.common.set_parameters = in_set_parameters;
2438 in->stream.common.get_parameters = in_get_parameters;
2439 in->stream.common.add_audio_effect = in_add_audio_effect;
2440 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2441 in->stream.set_gain = in_set_gain;
2442 in->stream.read = in_read;
2443 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2444
2445 in->device = devices;
2446 in->source = AUDIO_SOURCE_DEFAULT;
2447 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002448 in->standby = 1;
2449 in->channel_mask = config->channel_mask;
2450
2451 /* Update config params with the requested sample rate and channels */
2452 in->usecase = USECASE_AUDIO_RECORD;
2453 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002454 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002455 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002456
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002457 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002458 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002459 ret = voice_extn_compress_voip_open_input_stream(in);
2460 if (ret != 0)
2461 {
2462 ALOGE("%s: Compress voip input cannot be opened, error:%d",
2463 __func__, ret);
2464 goto err_open;
2465 }
Mingming Yine62d7842013-10-25 16:26:03 -07002466 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002467 if(audio_extn_ssr_get_enabled()) {
2468 if(audio_extn_ssr_init(adev, in)) {
2469 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2470 ret = -EINVAL;
2471 goto err_open;
2472 }
2473 } else {
2474 ret = -EINVAL;
2475 goto err_open;
2476 }
Mingming Yine62d7842013-10-25 16:26:03 -07002477 } else if (audio_extn_compr_cap_enabled() &&
2478 audio_extn_compr_cap_format_supported(config->format)) {
2479 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002480 } else {
2481 in->config.channels = channel_count;
2482 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2483 buffer_size = get_input_buffer_size(config->sample_rate,
2484 config->format,
2485 channel_count);
2486 in->config.period_size = buffer_size / frame_size;
2487 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002488
2489 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002490 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002491 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002492
2493err_open:
2494 free(in);
2495 *stream_in = NULL;
2496 return ret;
2497}
2498
2499static void adev_close_input_stream(struct audio_hw_device *dev,
2500 struct audio_stream_in *stream)
2501{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002502 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002503 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002504 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002505
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002506 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2507 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2508 if (ret != 0)
2509 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2510 __func__, ret);
2511 } else
2512 in_standby(&stream->common);
2513
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002514 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2515 audio_extn_ssr_deinit();
2516 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002517 free(stream);
2518
Mingming Yine62d7842013-10-25 16:26:03 -07002519 if(audio_extn_compr_cap_enabled() &&
2520 audio_extn_compr_cap_format_supported(in->config.format))
2521 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002522 return;
2523}
2524
2525static int adev_dump(const audio_hw_device_t *device, int fd)
2526{
2527 return 0;
2528}
2529
2530static int adev_close(hw_device_t *device)
2531{
2532 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002533
2534 if (!adev)
2535 return 0;
2536
2537 pthread_mutex_lock(&adev_init_lock);
2538
2539 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002540 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07002541 audio_route_free(adev->audio_route);
2542 free(adev->snd_dev_ref_cnt);
2543 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002544 free(device);
2545 adev = NULL;
2546 }
2547 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002548 return 0;
2549}
2550
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002551static int adev_open(const hw_module_t *module, const char *name,
2552 hw_device_t **device)
2553{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002554 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002555
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002556 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002557 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2558
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002559 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002560 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002561 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002562 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002563 ALOGD("%s: returning existing instance of adev", __func__);
2564 ALOGD("%s: exit", __func__);
2565 pthread_mutex_unlock(&adev_init_lock);
2566 return 0;
2567 }
2568
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002569 adev = calloc(1, sizeof(struct audio_device));
2570
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002571 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2572 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2573 adev->device.common.module = (struct hw_module_t *)module;
2574 adev->device.common.close = adev_close;
2575
2576 adev->device.init_check = adev_init_check;
2577 adev->device.set_voice_volume = adev_set_voice_volume;
2578 adev->device.set_master_volume = adev_set_master_volume;
2579 adev->device.get_master_volume = adev_get_master_volume;
2580 adev->device.set_master_mute = adev_set_master_mute;
2581 adev->device.get_master_mute = adev_get_master_mute;
2582 adev->device.set_mode = adev_set_mode;
2583 adev->device.set_mic_mute = adev_set_mic_mute;
2584 adev->device.get_mic_mute = adev_get_mic_mute;
2585 adev->device.set_parameters = adev_set_parameters;
2586 adev->device.get_parameters = adev_get_parameters;
2587 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2588 adev->device.open_output_stream = adev_open_output_stream;
2589 adev->device.close_output_stream = adev_close_output_stream;
2590 adev->device.open_input_stream = adev_open_input_stream;
2591 adev->device.close_input_stream = adev_close_input_stream;
2592 adev->device.dump = adev_dump;
2593
2594 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002595 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002596 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002597 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002598 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002599 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002600 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002601 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002602 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002603 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002604 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002605
2606 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002607 adev->platform = platform_init(adev);
2608 if (!adev->platform) {
2609 free(adev->snd_dev_ref_cnt);
2610 free(adev);
2611 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2612 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08002613 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07002614 return -EINVAL;
2615 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002616
2617 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2618 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2619 if (adev->visualizer_lib == NULL) {
2620 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2621 } else {
2622 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2623 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002624 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002625 "visualizer_hal_start_output");
2626 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002627 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002628 "visualizer_hal_stop_output");
2629 }
2630 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08002631 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07002632
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002633 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2634 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2635 if (adev->offload_effects_lib == NULL) {
2636 ALOGE("%s: DLOPEN failed for %s", __func__,
2637 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2638 } else {
2639 ALOGV("%s: DLOPEN successful for %s", __func__,
2640 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2641 adev->offload_effects_start_output =
2642 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2643 "offload_effects_bundle_hal_start_output");
2644 adev->offload_effects_stop_output =
2645 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2646 "offload_effects_bundle_hal_stop_output");
2647 }
2648 }
2649
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002650 *device = &adev->device.common;
2651
Kiran Kandi910e1862013-10-29 13:29:42 -07002652 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002653 pthread_mutex_unlock(&adev_init_lock);
2654
Eric Laurent994a6932013-07-17 11:51:42 -07002655 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002656 return 0;
2657}
2658
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -08002659/* Read offload buffer size from a property.
2660 * If value is not power of 2 round it to
2661 * power of 2.
2662 */
2663static uint32_t get_offload_buffer_size()
2664{
2665 char value[PROPERTY_VALUE_MAX] = {0};
2666 uint32_t fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2667 if((property_get("audio.offload.buffer.size.kb", value, "")) &&
2668 atoi(value)) {
2669 fragment_size = atoi(value) * 1024;
2670 //ring buffer size needs to be 4k aligned.
2671 CHECK(!(fragment_size * COMPRESS_OFFLOAD_NUM_FRAGMENTS % 4096));
2672 }
2673 if(fragment_size < MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
2674 fragment_size = MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2675 else if(fragment_size > MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
2676 fragment_size = MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2677 ALOGVV("%s: fragment_size %d", __func__, fragment_size);
2678 return fragment_size;
2679}
2680
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002681static struct hw_module_methods_t hal_module_methods = {
2682 .open = adev_open,
2683};
2684
2685struct audio_module HAL_MODULE_INFO_SYM = {
2686 .common = {
2687 .tag = HARDWARE_MODULE_TAG,
2688 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2689 .hal_api_version = HARDWARE_HAL_API_VERSION,
2690 .id = AUDIO_HARDWARE_MODULE_ID,
2691 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08002692 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002693 .methods = &hal_module_methods,
2694 },
2695};