blob: cfcc5a5955dacd226cbe820208bd3ba34c01bc36 [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 Maliyappanahalli8911f282014-01-10 15:56:19 -08005 * Copyright (C) 2013 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",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800116 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700117 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700118
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700119 [USECASE_VOICE2_CALL] = "voice2-call",
120 [USECASE_VOLTE_CALL] = "volte-call",
121 [USECASE_QCHAT_CALL] = "qchat-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800122 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700123 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
124 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
125 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800126 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
127 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
128 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
129
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700130 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
131 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700132 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
133 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700134};
135
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800136
137#define STRING_TO_ENUM(string) { #string, string }
138
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800139struct string_to_enum {
140 const char *name;
141 uint32_t value;
142};
143
144static const struct string_to_enum out_channels_name_to_enum_table[] = {
145 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
146 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
147 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
148};
149
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700150static struct audio_device *adev = NULL;
151static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700152static unsigned int audio_device_ref_count;
153
Haynes Mathew George5191a852013-09-11 14:19:36 -0700154static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -0800155static uint32_t get_offload_buffer_size();
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -0800156static int set_gapless_mode(struct audio_device *adev);
Haynes Mathew George5191a852013-09-11 14:19:36 -0700157
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700158static bool is_supported_format(audio_format_t format)
159{
Eric Laurent86e17132013-09-12 17:49:30 -0700160 if (format == AUDIO_FORMAT_MP3 ||
161 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700162 return true;
163
164 return false;
165}
166
167static int get_snd_codec_id(audio_format_t format)
168{
169 int id = 0;
170
171 switch (format) {
172 case AUDIO_FORMAT_MP3:
173 id = SND_AUDIOCODEC_MP3;
174 break;
175 case AUDIO_FORMAT_AAC:
176 id = SND_AUDIOCODEC_AAC;
177 break;
178 default:
Mingming Yin90310102013-11-13 16:57:00 -0800179 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700180 }
181
182 return id;
183}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800184
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700185int enable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700186 struct audio_usecase *usecase,
187 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800188{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700189 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700190 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800191
192 if (usecase == NULL)
193 return -EINVAL;
194
195 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
196
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800197 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700198 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800199 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700200 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800201
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -0800202#ifdef DS1_DOLBY_DAP_ENABLED
203 audio_extn_dolby_set_dmid(adev);
204 audio_extn_dolby_set_endpoint(adev);
205#endif
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800206 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700207 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700208 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700209 audio_route_apply_path(adev->audio_route, mixer_path);
210 if (update_mixer)
211 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800212
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800213 ALOGV("%s: exit", __func__);
214 return 0;
215}
216
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700217int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700218 struct audio_usecase *usecase,
219 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800220{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700221 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700222 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800223
224 if (usecase == NULL)
225 return -EINVAL;
226
227 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700228 if (usecase->type == PCM_CAPTURE)
229 snd_device = usecase->in_snd_device;
230 else
231 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800232 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700233 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700234 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700235 audio_route_reset_path(adev->audio_route, mixer_path);
236 if (update_mixer)
237 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800238
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800239 ALOGV("%s: exit", __func__);
240 return 0;
241}
242
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700243int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700244 snd_device_t snd_device,
245 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800246{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700247 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
248
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800249 if (snd_device < SND_DEVICE_MIN ||
250 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800251 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800252 return -EINVAL;
253 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700254
255 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700256
257 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
258 ALOGE("%s: Invalid sound device returned", __func__);
259 return -EINVAL;
260 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700261 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700262 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700263 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700264 return 0;
265 }
266
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700267 /* start usb playback thread */
268 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
269 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
270 audio_extn_usb_start_playback(adev);
271
272 /* start usb capture thread */
273 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
274 audio_extn_usb_start_capture(adev);
275
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700276 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
277 audio_extn_spkr_prot_is_enabled()) {
278 if (audio_extn_spkr_prot_start_processing(snd_device)) {
279 ALOGE("%s: spkr_start_processing failed", __func__);
280 return -EINVAL;
281 }
282 } else {
283 ALOGV("%s: snd_device(%d: %s)", __func__,
284 snd_device, device_name);
285 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
286 adev->snd_dev_ref_cnt[snd_device]--;
287 return -EINVAL;
288 }
Kiran Kandide144c82013-11-20 15:58:32 -0800289 audio_extn_listen_update_status(snd_device,
290 LISTEN_EVENT_SND_DEVICE_BUSY);
291
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700292 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800293 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700294 if (update_mixer)
295 audio_route_update_mixer(adev->audio_route);
296
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800297 return 0;
298}
299
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700300int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700301 snd_device_t snd_device,
302 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800303{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700304 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
305
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800306 if (snd_device < SND_DEVICE_MIN ||
307 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800308 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800309 return -EINVAL;
310 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700311 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
312 ALOGE("%s: device ref cnt is already 0", __func__);
313 return -EINVAL;
314 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700315
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700316 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700317
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700318 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
319 ALOGE("%s: Invalid sound device returned", __func__);
320 return -EINVAL;
321 }
322
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700323 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700324 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700325 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800326 /* exit usb play back thread */
327 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
328 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
329 audio_extn_usb_stop_playback();
330
331 /* exit usb capture thread */
332 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
333 audio_extn_usb_stop_capture(adev);
334
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700335 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
336 audio_extn_spkr_prot_is_enabled()) {
337 audio_extn_spkr_prot_stop_processing();
338 } else
339 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800340
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700341 if (update_mixer)
342 audio_route_update_mixer(adev->audio_route);
Kiran Kandide144c82013-11-20 15:58:32 -0800343
344 audio_extn_listen_update_status(snd_device,
345 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700346 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700347
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800348 return 0;
349}
350
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700351static void check_usecases_codec_backend(struct audio_device *adev,
352 struct audio_usecase *uc_info,
353 snd_device_t snd_device)
354{
355 struct listnode *node;
356 struct audio_usecase *usecase;
357 bool switch_device[AUDIO_USECASE_MAX];
358 int i, num_uc_to_switch = 0;
359
360 /*
361 * This function is to make sure that all the usecases that are active on
362 * the hardware codec backend are always routed to any one device that is
363 * handled by the hardware codec.
364 * For example, if low-latency and deep-buffer usecases are currently active
365 * on speaker and out_set_parameters(headset) is received on low-latency
366 * output, then we have to make sure deep-buffer is also switched to headset,
367 * because of the limitation that both the devices cannot be enabled
368 * at the same time as they share the same backend.
369 */
370 /* Disable all the usecases on the shared backend other than the
371 specified usecase */
372 for (i = 0; i < AUDIO_USECASE_MAX; i++)
373 switch_device[i] = false;
374
375 list_for_each(node, &adev->usecase_list) {
376 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800377 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700378 usecase != uc_info &&
379 usecase->out_snd_device != snd_device &&
380 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
381 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
382 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700383 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700384 disable_audio_route(adev, usecase, false);
385 switch_device[usecase->id] = true;
386 num_uc_to_switch++;
387 }
388 }
389
390 if (num_uc_to_switch) {
391 /* Make sure all the streams are de-routed before disabling the device */
392 audio_route_update_mixer(adev->audio_route);
393
394 list_for_each(node, &adev->usecase_list) {
395 usecase = node_to_item(node, struct audio_usecase, list);
396 if (switch_device[usecase->id]) {
397 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700398 }
399 }
400
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700401 list_for_each(node, &adev->usecase_list) {
402 usecase = node_to_item(node, struct audio_usecase, list);
403 if (switch_device[usecase->id]) {
404 enable_snd_device(adev, snd_device, false);
405 }
406 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700407
408 /* Make sure new snd device is enabled before re-routing the streams */
409 audio_route_update_mixer(adev->audio_route);
410
411 /* Re-route all the usecases on the shared backend other than the
412 specified usecase to new snd devices */
413 list_for_each(node, &adev->usecase_list) {
414 usecase = node_to_item(node, struct audio_usecase, list);
415 /* Update the out_snd_device only before enabling the audio route */
416 if (switch_device[usecase->id] ) {
417 usecase->out_snd_device = snd_device;
418 enable_audio_route(adev, usecase, false);
419 }
420 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700421 }
422}
423
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700424static void check_and_route_capture_usecases(struct audio_device *adev,
425 struct audio_usecase *uc_info,
426 snd_device_t snd_device)
427{
428 struct listnode *node;
429 struct audio_usecase *usecase;
430 bool switch_device[AUDIO_USECASE_MAX];
431 int i, num_uc_to_switch = 0;
432
433 /*
434 * This function is to make sure that all the active capture usecases
435 * are always routed to the same input sound device.
436 * For example, if audio-record and voice-call usecases are currently
437 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
438 * is received for voice call then we have to make sure that audio-record
439 * usecase is also switched to earpiece i.e. voice-dmic-ef,
440 * because of the limitation that two devices cannot be enabled
441 * at the same time if they share the same backend.
442 */
443 for (i = 0; i < AUDIO_USECASE_MAX; i++)
444 switch_device[i] = false;
445
446 list_for_each(node, &adev->usecase_list) {
447 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800448 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700449 usecase != uc_info &&
450 usecase->in_snd_device != snd_device) {
451 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
452 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700453 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700454 disable_audio_route(adev, usecase, false);
455 switch_device[usecase->id] = true;
456 num_uc_to_switch++;
457 }
458 }
459
460 if (num_uc_to_switch) {
461 /* Make sure all the streams are de-routed before disabling the device */
462 audio_route_update_mixer(adev->audio_route);
463
464 list_for_each(node, &adev->usecase_list) {
465 usecase = node_to_item(node, struct audio_usecase, list);
466 if (switch_device[usecase->id]) {
467 disable_snd_device(adev, usecase->in_snd_device, false);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800468 }
469 }
470
471 list_for_each(node, &adev->usecase_list) {
472 usecase = node_to_item(node, struct audio_usecase, list);
473 if (switch_device[usecase->id]) {
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700474 enable_snd_device(adev, snd_device, false);
475 }
476 }
477
478 /* Make sure new snd device is enabled before re-routing the streams */
479 audio_route_update_mixer(adev->audio_route);
480
481 /* Re-route all the usecases on the shared backend other than the
482 specified usecase to new snd devices */
483 list_for_each(node, &adev->usecase_list) {
484 usecase = node_to_item(node, struct audio_usecase, list);
485 /* Update the in_snd_device only before enabling the audio route */
486 if (switch_device[usecase->id] ) {
487 usecase->in_snd_device = snd_device;
488 enable_audio_route(adev, usecase, false);
489 }
490 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700491 }
492}
493
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800494/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700495static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800496{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700497 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700498 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800499
500 switch (channels) {
501 /*
502 * Do not handle stereo output in Multi-channel cases
503 * Stereo case is handled in normal playback path
504 */
505 case 6:
506 ALOGV("%s: HDMI supports 5.1", __func__);
507 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
508 break;
509 case 8:
510 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
511 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
512 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
513 break;
514 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700515 ALOGE("HDMI does not support multi channel playback");
516 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800517 break;
518 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700519 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800520}
521
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800522static void update_devices_for_all_voice_usecases(struct audio_device *adev)
523{
524 struct listnode *node;
525 struct audio_usecase *usecase;
526
527 list_for_each(node, &adev->usecase_list) {
528 usecase = node_to_item(node, struct audio_usecase, list);
529 if (usecase->type == VOICE_CALL) {
530 ALOGV("%s: updating device for usecase:%s", __func__,
531 use_case_table[usecase->id]);
532 select_devices(adev, usecase->id);
533 }
534 }
535}
536
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700537static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
538{
539 struct audio_usecase *usecase;
540 struct listnode *node;
541
542 list_for_each(node, &adev->usecase_list) {
543 usecase = node_to_item(node, struct audio_usecase, list);
544 if (usecase->type == VOICE_CALL) {
545 ALOGV("%s: usecase id %d", __func__, usecase->id);
546 return usecase->id;
547 }
548 }
549 return USECASE_INVALID;
550}
551
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700552struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700553 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700554{
555 struct audio_usecase *usecase;
556 struct listnode *node;
557
558 list_for_each(node, &adev->usecase_list) {
559 usecase = node_to_item(node, struct audio_usecase, list);
560 if (usecase->id == uc_id)
561 return usecase;
562 }
563 return NULL;
564}
565
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700566int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800567{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800568 snd_device_t out_snd_device = SND_DEVICE_NONE;
569 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700570 struct audio_usecase *usecase = NULL;
571 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800572 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800573 struct audio_usecase *hfp_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800574 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700575 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800576
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700577 usecase = get_usecase_from_list(adev, uc_id);
578 if (usecase == NULL) {
579 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
580 return -EINVAL;
581 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800582
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800583 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800584 (usecase->type == VOIP_CALL) ||
585 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700586 out_snd_device = platform_get_output_snd_device(adev->platform,
587 usecase->stream.out->devices);
588 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700589 usecase->devices = usecase->stream.out->devices;
590 } else {
591 /*
592 * If the voice call is active, use the sound devices of voice call usecase
593 * so that it would not result any device switch. All the usecases will
594 * be switched to new device when select_devices() is called for voice call
595 * usecase. This is to avoid switching devices for voice call when
596 * check_usecases_codec_backend() is called below.
597 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700598 if (voice_is_in_call(adev)) {
599 vc_usecase = get_usecase_from_list(adev,
600 get_voice_usecase_id_from_list(adev));
Helen Zeng067b96b2013-11-26 12:10:29 -0800601 if ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
602 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700603 in_snd_device = vc_usecase->in_snd_device;
604 out_snd_device = vc_usecase->out_snd_device;
605 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800606 } else if (voice_extn_compress_voip_is_active(adev)) {
607 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
608 if (voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
609 in_snd_device = voip_usecase->in_snd_device;
610 out_snd_device = voip_usecase->out_snd_device;
611 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800612 } else if (audio_extn_hfp_is_active(adev)) {
613 hfp_usecase = get_usecase_from_list(adev, USECASE_AUDIO_HFP_SCO);
614 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
615 in_snd_device = hfp_usecase->in_snd_device;
616 out_snd_device = hfp_usecase->out_snd_device;
617 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700618 }
619 if (usecase->type == PCM_PLAYBACK) {
620 usecase->devices = usecase->stream.out->devices;
621 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700622 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700623 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700624 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700625 if (usecase->stream.out == adev->primary_output &&
626 adev->active_input &&
627 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
628 select_devices(adev, adev->active_input->usecase);
629 }
630 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700631 } else if (usecase->type == PCM_CAPTURE) {
632 usecase->devices = usecase->stream.in->device;
633 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700634 if (in_snd_device == SND_DEVICE_NONE) {
635 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
636 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700637 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700638 adev->primary_output->devices);
639 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700640 in_snd_device = platform_get_input_snd_device(adev->platform,
641 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700642 }
643 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700644 }
645 }
646
647 if (out_snd_device == usecase->out_snd_device &&
648 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800649 return 0;
650 }
651
sangwoobc677242013-08-08 16:53:43 +0900652 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700653 out_snd_device, platform_get_snd_device_name(out_snd_device),
654 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800655
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800656 /*
657 * Limitation: While in call, to do a device switch we need to disable
658 * and enable both RX and TX devices though one of them is same as current
659 * device.
660 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800661 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700662 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800663 }
664
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700665 /* Disable current sound devices */
666 if (usecase->out_snd_device != SND_DEVICE_NONE) {
667 disable_audio_route(adev, usecase, true);
668 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800669 }
670
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700671 if (usecase->in_snd_device != SND_DEVICE_NONE) {
672 disable_audio_route(adev, usecase, true);
673 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800674 }
675
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700676 /* Enable new sound devices */
677 if (out_snd_device != SND_DEVICE_NONE) {
678 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
679 check_usecases_codec_backend(adev, usecase, out_snd_device);
680 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800681 }
682
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700683 if (in_snd_device != SND_DEVICE_NONE) {
684 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700685 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700686 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700687
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800688 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700689 status = platform_switch_voice_call_device_post(adev->platform,
690 out_snd_device,
691 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800692
sangwoo170731f2013-06-08 15:36:36 +0900693 audio_route_update_mixer(adev->audio_route);
694
695 usecase->in_snd_device = in_snd_device;
696 usecase->out_snd_device = out_snd_device;
697
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800698 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900699
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800700 /* Applicable only on the targets that has external modem.
701 * Enable device command should be sent to modem only after
702 * enabling voice call mixer controls
703 */
704 if (usecase->type == VOICE_CALL)
705 status = platform_switch_voice_call_usecase_route_post(adev->platform,
706 out_snd_device,
707 in_snd_device);
708
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800709 return status;
710}
711
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800712static int stop_input_stream(struct stream_in *in)
713{
714 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800715 struct audio_usecase *uc_info;
716 struct audio_device *adev = in->dev;
717
Eric Laurentc8400632013-02-14 19:04:54 -0800718 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800719
Eric Laurent994a6932013-07-17 11:51:42 -0700720 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700721 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800722 uc_info = get_usecase_from_list(adev, in->usecase);
723 if (uc_info == NULL) {
724 ALOGE("%s: Could not find the usecase (%d) in the list",
725 __func__, in->usecase);
726 return -EINVAL;
727 }
728
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800729 /* Close in-call recording streams */
730 voice_check_and_stop_incall_rec_usecase(adev, in);
731
Eric Laurent150dbfe2013-02-27 14:31:02 -0800732 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700733 disable_audio_route(adev, uc_info, true);
734
735 /* 2. Disable the tx device */
736 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800737
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800738 list_remove(&uc_info->list);
739 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800740
Eric Laurent994a6932013-07-17 11:51:42 -0700741 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800742 return ret;
743}
744
745int start_input_stream(struct stream_in *in)
746{
747 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800748 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800749 struct audio_usecase *uc_info;
750 struct audio_device *adev = in->dev;
751
Mingming Yine62d7842013-10-25 16:26:03 -0700752 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Eric Laurent994a6932013-07-17 11:51:42 -0700753 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700754
755 /* Check if source matches incall recording usecase criteria */
756 ret = voice_check_and_set_incall_rec_usecase(adev, in);
757 if (ret)
758 goto error_config;
759 else
760 ALOGV("%s: usecase(%d)", __func__, in->usecase);
761
Eric Laurentb23d5282013-05-14 15:27:20 -0700762 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800763 if (in->pcm_device_id < 0) {
764 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
765 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800766 ret = -EINVAL;
767 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800768 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700769
770 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800771 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
772 uc_info->id = in->usecase;
773 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800774 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700775 uc_info->devices = in->device;
776 uc_info->in_snd_device = SND_DEVICE_NONE;
777 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800778
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800779 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700780 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800781
Eric Laurentc8400632013-02-14 19:04:54 -0800782 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800783 __func__, adev->snd_card,
784 in->pcm_device_id, in->config.channels);
785 in->pcm = pcm_open(adev->snd_card,
786 in->pcm_device_id, PCM_IN, &in->config);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800787 if (in->pcm && !pcm_is_ready(in->pcm)) {
788 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
789 pcm_close(in->pcm);
790 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800791 ret = -EIO;
792 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800793 }
Eric Laurent994a6932013-07-17 11:51:42 -0700794 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800795 return ret;
796
797error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800798 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800799
800error_config:
801 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700802 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800803
804 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800805}
806
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700807/* must be called with out->lock locked */
808static int send_offload_cmd_l(struct stream_out* out, int command)
809{
810 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
811
812 ALOGVV("%s %d", __func__, command);
813
814 cmd->cmd = command;
815 list_add_tail(&out->offload_cmd_list, &cmd->node);
816 pthread_cond_signal(&out->offload_cond);
817 return 0;
818}
819
820/* must be called iwth out->lock locked */
821static void stop_compressed_output_l(struct stream_out *out)
822{
823 out->offload_state = OFFLOAD_STATE_IDLE;
824 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700825 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700826 if (out->compr != NULL) {
827 compress_stop(out->compr);
828 while (out->offload_thread_blocked) {
829 pthread_cond_wait(&out->cond, &out->lock);
830 }
831 }
832}
833
834static void *offload_thread_loop(void *context)
835{
836 struct stream_out *out = (struct stream_out *) context;
837 struct listnode *item;
838
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700839 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
840 set_sched_policy(0, SP_FOREGROUND);
841 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
842
843 ALOGV("%s", __func__);
844 pthread_mutex_lock(&out->lock);
845 for (;;) {
846 struct offload_cmd *cmd = NULL;
847 stream_callback_event_t event;
848 bool send_callback = false;
849
850 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
851 __func__, list_empty(&out->offload_cmd_list),
852 out->offload_state);
853 if (list_empty(&out->offload_cmd_list)) {
854 ALOGV("%s SLEEPING", __func__);
855 pthread_cond_wait(&out->offload_cond, &out->lock);
856 ALOGV("%s RUNNING", __func__);
857 continue;
858 }
859
860 item = list_head(&out->offload_cmd_list);
861 cmd = node_to_item(item, struct offload_cmd, node);
862 list_remove(item);
863
864 ALOGVV("%s STATE %d CMD %d out->compr %p",
865 __func__, out->offload_state, cmd->cmd, out->compr);
866
867 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
868 free(cmd);
869 break;
870 }
871
872 if (out->compr == NULL) {
873 ALOGE("%s: Compress handle is NULL", __func__);
874 pthread_cond_signal(&out->cond);
875 continue;
876 }
877 out->offload_thread_blocked = true;
878 pthread_mutex_unlock(&out->lock);
879 send_callback = false;
880 switch(cmd->cmd) {
881 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
882 compress_wait(out->compr, -1);
883 send_callback = true;
884 event = STREAM_CBK_EVENT_WRITE_READY;
885 break;
886 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700887 compress_next_track(out->compr);
888 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700889 send_callback = true;
890 event = STREAM_CBK_EVENT_DRAIN_READY;
891 break;
892 case OFFLOAD_CMD_DRAIN:
893 compress_drain(out->compr);
894 send_callback = true;
895 event = STREAM_CBK_EVENT_DRAIN_READY;
896 break;
897 default:
898 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
899 break;
900 }
901 pthread_mutex_lock(&out->lock);
902 out->offload_thread_blocked = false;
903 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700904 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700905 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700906 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700907 free(cmd);
908 }
909
910 pthread_cond_signal(&out->cond);
911 while (!list_empty(&out->offload_cmd_list)) {
912 item = list_head(&out->offload_cmd_list);
913 list_remove(item);
914 free(node_to_item(item, struct offload_cmd, node));
915 }
916 pthread_mutex_unlock(&out->lock);
917
918 return NULL;
919}
920
921static int create_offload_callback_thread(struct stream_out *out)
922{
923 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
924 list_init(&out->offload_cmd_list);
925 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
926 offload_thread_loop, out);
927 return 0;
928}
929
930static int destroy_offload_callback_thread(struct stream_out *out)
931{
932 pthread_mutex_lock(&out->lock);
933 stop_compressed_output_l(out);
934 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
935
936 pthread_mutex_unlock(&out->lock);
937 pthread_join(out->offload_thread, (void **) NULL);
938 pthread_cond_destroy(&out->offload_cond);
939
940 return 0;
941}
942
Eric Laurent07eeafd2013-10-06 12:52:49 -0700943static bool allow_hdmi_channel_config(struct audio_device *adev)
944{
945 struct listnode *node;
946 struct audio_usecase *usecase;
947 bool ret = true;
948
949 list_for_each(node, &adev->usecase_list) {
950 usecase = node_to_item(node, struct audio_usecase, list);
951 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
952 /*
953 * If voice call is already existing, do not proceed further to avoid
954 * disabling/enabling both RX and TX devices, CSD calls, etc.
955 * Once the voice call done, the HDMI channels can be configured to
956 * max channels of remaining use cases.
957 */
958 if (usecase->id == USECASE_VOICE_CALL) {
959 ALOGD("%s: voice call is active, no change in HDMI channels",
960 __func__);
961 ret = false;
962 break;
963 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
964 ALOGD("%s: multi channel playback is active, "
965 "no change in HDMI channels", __func__);
966 ret = false;
967 break;
968 }
969 }
970 }
971 return ret;
972}
973
974static int check_and_set_hdmi_channels(struct audio_device *adev,
975 unsigned int channels)
976{
977 struct listnode *node;
978 struct audio_usecase *usecase;
979
980 /* Check if change in HDMI channel config is allowed */
981 if (!allow_hdmi_channel_config(adev))
982 return 0;
983
984 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -0800985 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -0700986 return 0;
987 }
988
989 platform_set_hdmi_channels(adev->platform, channels);
990 adev->cur_hdmi_channels = channels;
991
992 /*
993 * Deroute all the playback streams routed to HDMI so that
994 * the back end is deactivated. Note that backend will not
995 * be deactivated if any one stream is connected to it.
996 */
997 list_for_each(node, &adev->usecase_list) {
998 usecase = node_to_item(node, struct audio_usecase, list);
999 if (usecase->type == PCM_PLAYBACK &&
1000 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1001 disable_audio_route(adev, usecase, true);
1002 }
1003 }
1004
1005 /*
1006 * Enable all the streams disabled above. Now the HDMI backend
1007 * will be activated with new channel configuration
1008 */
1009 list_for_each(node, &adev->usecase_list) {
1010 usecase = node_to_item(node, struct audio_usecase, list);
1011 if (usecase->type == PCM_PLAYBACK &&
1012 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1013 enable_audio_route(adev, usecase, true);
1014 }
1015 }
1016
1017 return 0;
1018}
1019
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001020static int stop_output_stream(struct stream_out *out)
1021{
1022 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001023 struct audio_usecase *uc_info;
1024 struct audio_device *adev = out->dev;
1025
Eric Laurent994a6932013-07-17 11:51:42 -07001026 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001027 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001028 uc_info = get_usecase_from_list(adev, out->usecase);
1029 if (uc_info == NULL) {
1030 ALOGE("%s: Could not find the usecase (%d) in the list",
1031 __func__, out->usecase);
1032 return -EINVAL;
1033 }
1034
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001035 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1036 if (adev->visualizer_stop_output != NULL)
1037 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1038 if (adev->offload_effects_stop_output != NULL)
1039 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1040 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001041
Eric Laurent150dbfe2013-02-27 14:31:02 -08001042 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001043 disable_audio_route(adev, uc_info, true);
1044
1045 /* 2. Disable the rx device */
1046 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001047
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001048 list_remove(&uc_info->list);
1049 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001050
Eric Laurent07eeafd2013-10-06 12:52:49 -07001051 /* Must be called after removing the usecase from list */
1052 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1053 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1054
Eric Laurent994a6932013-07-17 11:51:42 -07001055 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001056 return ret;
1057}
1058
1059int start_output_stream(struct stream_out *out)
1060{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001061 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001062 struct audio_usecase *uc_info;
1063 struct audio_device *adev = out->dev;
1064
Eric Laurent994a6932013-07-17 11:51:42 -07001065 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001066 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001067 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001068 if (out->pcm_device_id < 0) {
1069 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1070 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001071 ret = -EINVAL;
1072 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001073 }
1074
1075 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1076 uc_info->id = out->usecase;
1077 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001078 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001079 uc_info->devices = out->devices;
1080 uc_info->in_snd_device = SND_DEVICE_NONE;
1081 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001082
Eric Laurent07eeafd2013-10-06 12:52:49 -07001083 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001084 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1085 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1086 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1087 else
1088 check_and_set_hdmi_channels(adev, out->config.channels);
1089 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001090
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001091 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001092
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001093 select_devices(adev, out->usecase);
1094
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001095 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1096 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001097 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001098 out->pcm = pcm_open(adev->snd_card,
1099 out->pcm_device_id,
1100 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001101 if (out->pcm && !pcm_is_ready(out->pcm)) {
1102 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1103 pcm_close(out->pcm);
1104 out->pcm = NULL;
1105 ret = -EIO;
1106 goto error_open;
1107 }
1108 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001109 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001110 out->compr = compress_open(adev->snd_card,
1111 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001112 COMPRESS_IN, &out->compr_config);
1113 if (out->compr && !is_compress_ready(out->compr)) {
1114 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1115 compress_close(out->compr);
1116 out->compr = NULL;
1117 ret = -EIO;
1118 goto error_open;
1119 }
1120 if (out->offload_callback)
1121 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001122
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001123#ifdef DS1_DOLBY_DDP_ENABLED
1124 if (audio_extn_is_dolby_format(out->format))
1125 audio_extn_dolby_send_ddp_endp_params(adev);
1126#endif
1127
Eric Laurentc4aef752013-09-12 17:45:53 -07001128 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001129 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1130 if (adev->offload_effects_start_output != NULL)
1131 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001132 }
Eric Laurent994a6932013-07-17 11:51:42 -07001133 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001134 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001135error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001136 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001137error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001138 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001139}
1140
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001141static int check_input_parameters(uint32_t sample_rate,
1142 audio_format_t format,
1143 int channel_count)
1144{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001145 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001146
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001147 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001148 !voice_extn_compress_voip_is_format_supported(format) &&
1149 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001150
1151 switch (channel_count) {
1152 case 1:
1153 case 2:
1154 case 6:
1155 break;
1156 default:
1157 ret = -EINVAL;
1158 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001159
1160 switch (sample_rate) {
1161 case 8000:
1162 case 11025:
1163 case 12000:
1164 case 16000:
1165 case 22050:
1166 case 24000:
1167 case 32000:
1168 case 44100:
1169 case 48000:
1170 break;
1171 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001172 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001173 }
1174
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001175 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001176}
1177
1178static size_t get_input_buffer_size(uint32_t sample_rate,
1179 audio_format_t format,
1180 int channel_count)
1181{
1182 size_t size = 0;
1183
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001184 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1185 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001186
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001187 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1188 /* ToDo: should use frame_size computed based on the format and
1189 channel_count here. */
1190 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001191
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001192 /* make sure the size is multiple of 64 */
1193 size += 0x3f;
1194 size &= ~0x3f;
1195
1196 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001197}
1198
1199static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1200{
1201 struct stream_out *out = (struct stream_out *)stream;
1202
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001203 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001204}
1205
1206static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1207{
1208 return -ENOSYS;
1209}
1210
1211static size_t out_get_buffer_size(const struct audio_stream *stream)
1212{
1213 struct stream_out *out = (struct stream_out *)stream;
1214
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001215 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001216 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001217 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1218 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001219
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001220 return out->config.period_size * audio_stream_frame_size(stream);
1221}
1222
1223static uint32_t out_get_channels(const struct audio_stream *stream)
1224{
1225 struct stream_out *out = (struct stream_out *)stream;
1226
1227 return out->channel_mask;
1228}
1229
1230static audio_format_t out_get_format(const struct audio_stream *stream)
1231{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001232 struct stream_out *out = (struct stream_out *)stream;
1233
1234 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001235}
1236
1237static int out_set_format(struct audio_stream *stream, audio_format_t format)
1238{
1239 return -ENOSYS;
1240}
1241
1242static int out_standby(struct audio_stream *stream)
1243{
1244 struct stream_out *out = (struct stream_out *)stream;
1245 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001246
Eric Laurent994a6932013-07-17 11:51:42 -07001247 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001248 out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001249 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1250 /* Ignore standby in case of voip call because the voip output
1251 * stream is closed in adev_close_output_stream()
1252 */
1253 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1254 return 0;
1255 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001256
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001257 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001258 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001259 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001260 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001261 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1262 if (out->pcm) {
1263 pcm_close(out->pcm);
1264 out->pcm = NULL;
1265 }
1266 } else {
1267 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001268 out->gapless_mdata.encoder_delay = 0;
1269 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001270 if (out->compr != NULL) {
1271 compress_close(out->compr);
1272 out->compr = NULL;
1273 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001274 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001275 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001276 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001277 }
1278 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001279 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001280 return 0;
1281}
1282
1283static int out_dump(const struct audio_stream *stream, int fd)
1284{
1285 return 0;
1286}
1287
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001288static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1289{
1290 int ret = 0;
1291 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001292 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001293 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001294 tmp_mdata.encoder_delay = 0;
1295 tmp_mdata.encoder_padding = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001296 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001297 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001298 return -EINVAL;
1299 }
1300
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001301 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1302 if(ret >= 0)
1303 is_meta_data_params = true;
1304 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1305 if(ret >= 0 )
1306 is_meta_data_params = true;
1307 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1308 if(ret >= 0 )
1309 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001310 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1311 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001312 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001313 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001314 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001315 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1316 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001317 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001318 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001319 }
1320
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001321 if(!is_meta_data_params) {
1322 ALOGV("%s: Not gapless meta data params", __func__);
1323 return 0;
1324 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001325 out->gapless_mdata = tmp_mdata;
1326 out->send_new_metadata = 1;
1327 ALOGV("%s new encoder delay %u and padding %u", __func__,
1328 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1329
1330 return 0;
1331}
1332
1333
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001334static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1335{
1336 struct stream_out *out = (struct stream_out *)stream;
1337 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001338 struct audio_usecase *usecase;
1339 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001340 struct str_parms *parms;
1341 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001342 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001343 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001344
sangwoobc677242013-08-08 16:53:43 +09001345 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001346 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001347 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001348 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1349 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001350 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001351 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001352 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001353
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001354 /*
1355 * When HDMI cable is unplugged the music playback is paused and
1356 * the policy manager sends routing=0. But the audioflinger
1357 * continues to write data until standby time (3sec).
1358 * As the HDMI core is turned off, the write gets blocked.
1359 * Avoid this by routing audio to speaker until standby.
1360 */
1361 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1362 val == AUDIO_DEVICE_NONE) {
1363 val = AUDIO_DEVICE_OUT_SPEAKER;
1364 }
1365
1366 /*
1367 * select_devices() call below switches all the usecases on the same
1368 * backend to the new device. Refer to check_usecases_codec_backend() in
1369 * the select_devices(). But how do we undo this?
1370 *
1371 * For example, music playback is active on headset (deep-buffer usecase)
1372 * and if we go to ringtones and select a ringtone, low-latency usecase
1373 * will be started on headset+speaker. As we can't enable headset+speaker
1374 * and headset devices at the same time, select_devices() switches the music
1375 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1376 * So when the ringtone playback is completed, how do we undo the same?
1377 *
1378 * We are relying on the out_set_parameters() call on deep-buffer output,
1379 * once the ringtone playback is ended.
1380 * NOTE: We should not check if the current devices are same as new devices.
1381 * Because select_devices() must be called to switch back the music
1382 * playback to headset.
1383 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001384 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001385 out->devices = val;
1386
1387 if (!out->standby)
1388 select_devices(adev, out->usecase);
1389
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001390 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1391 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001392 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001393 ret = voice_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001394 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1395 voice_is_in_call(adev) &&
1396 (out == adev->primary_output)) {
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -08001397 update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001398 }
1399 }
1400
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001401 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1402 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001403 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001404 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001405 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001406
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001407 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001408 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001410
1411 if (out == adev->primary_output) {
1412 pthread_mutex_lock(&adev->lock);
1413 audio_extn_set_parameters(adev, parms);
1414 pthread_mutex_unlock(&adev->lock);
1415 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001416 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001417 pthread_mutex_lock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001418 parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001419 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001420 }
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;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08001838 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1839 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
1840 (voice_extn_compress_voip_is_format_supported(in->format)) &&
1841 (in->config.rate == 8000 || in->config.rate == 16000) &&
1842 (popcount(in->channel_mask) == 1)) {
1843 ret = voice_extn_compress_voip_open_input_stream(in);
1844 if (ret != 0) {
1845 ALOGE("%s: Compress voip input cannot be opened, error:%d",
1846 __func__, ret);
1847 goto done;
1848 }
1849 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001850 }
1851 }
1852
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001853 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1854 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001855 val = atoi(value);
1856 if ((in->device != val) && (val != 0)) {
1857 in->device = val;
1858 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001859 if (!in->standby)
1860 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001861 }
1862 }
1863
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08001864done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001865 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001866 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001867
1868 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001869 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001870 return ret;
1871}
1872
1873static char* in_get_parameters(const struct audio_stream *stream,
1874 const char *keys)
1875{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001876 struct stream_in *in = (struct stream_in *)stream;
1877 struct str_parms *query = str_parms_create_str(keys);
1878 char *str;
1879 char value[256];
1880 struct str_parms *reply = str_parms_create();
1881 ALOGV("%s: enter: keys - %s", __func__, keys);
1882
1883 voice_extn_in_get_parameters(in, query, reply);
1884
1885 str = str_parms_to_str(reply);
1886 str_parms_destroy(query);
1887 str_parms_destroy(reply);
1888
1889 ALOGV("%s: exit: returns - %s", __func__, str);
1890 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001891}
1892
1893static int in_set_gain(struct audio_stream_in *stream, float gain)
1894{
1895 return 0;
1896}
1897
1898static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1899 size_t bytes)
1900{
1901 struct stream_in *in = (struct stream_in *)stream;
1902 struct audio_device *adev = in->dev;
1903 int i, ret = -1;
1904
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001905 pthread_mutex_lock(&in->lock);
1906 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001907 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001908 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
1909 ret = voice_extn_compress_voip_start_input_stream(in);
1910 else
1911 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001912 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001913 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001914 goto exit;
1915 }
1916 in->standby = 0;
1917 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001918
1919 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001920 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1921 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07001922 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
1923 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001924 else
1925 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001926 }
1927
1928 /*
1929 * Instead of writing zeroes here, we could trust the hardware
1930 * to always provide zeroes when muted.
1931 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001932 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001933 memset(buffer, 0, bytes);
1934
1935exit:
1936 pthread_mutex_unlock(&in->lock);
1937
1938 if (ret != 0) {
1939 in_standby(&in->stream.common);
1940 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1941 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1942 in_get_sample_rate(&in->stream.common));
1943 }
1944 return bytes;
1945}
1946
1947static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1948{
1949 return 0;
1950}
1951
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001952static int add_remove_audio_effect(const struct audio_stream *stream,
1953 effect_handle_t effect,
1954 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001955{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001956 struct stream_in *in = (struct stream_in *)stream;
1957 int status = 0;
1958 effect_descriptor_t desc;
1959
1960 status = (*effect)->get_descriptor(effect, &desc);
1961 if (status != 0)
1962 return status;
1963
1964 pthread_mutex_lock(&in->lock);
1965 pthread_mutex_lock(&in->dev->lock);
1966 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1967 in->enable_aec != enable &&
1968 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1969 in->enable_aec = enable;
1970 if (!in->standby)
1971 select_devices(in->dev, in->usecase);
1972 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08001973 if (in->enable_ns != enable &&
1974 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1975 in->enable_ns = enable;
1976 if (!in->standby)
1977 select_devices(in->dev, in->usecase);
1978 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001979 pthread_mutex_unlock(&in->dev->lock);
1980 pthread_mutex_unlock(&in->lock);
1981
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001982 return 0;
1983}
1984
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001985static int in_add_audio_effect(const struct audio_stream *stream,
1986 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001987{
Eric Laurent994a6932013-07-17 11:51:42 -07001988 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001989 return add_remove_audio_effect(stream, effect, true);
1990}
1991
1992static int in_remove_audio_effect(const struct audio_stream *stream,
1993 effect_handle_t effect)
1994{
Eric Laurent994a6932013-07-17 11:51:42 -07001995 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001996 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001997}
1998
1999static int adev_open_output_stream(struct audio_hw_device *dev,
2000 audio_io_handle_t handle,
2001 audio_devices_t devices,
2002 audio_output_flags_t flags,
2003 struct audio_config *config,
2004 struct audio_stream_out **stream_out)
2005{
2006 struct audio_device *adev = (struct audio_device *)dev;
2007 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002008 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002009
Eric Laurent994a6932013-07-17 11:51:42 -07002010 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002011 __func__, config->sample_rate, config->channel_mask, devices, flags);
2012 *stream_out = NULL;
2013 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2014
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002015 if (!out) {
2016 return -ENOMEM;
2017 }
2018
2019 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2020 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2021
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002022 if (devices == AUDIO_DEVICE_NONE)
2023 devices = AUDIO_DEVICE_OUT_SPEAKER;
2024
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002025 out->flags = flags;
2026 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002027 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002028 out->format = config->format;
2029 out->sample_rate = config->sample_rate;
2030 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2031 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002032 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002033
2034 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002035 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2036 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2037 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2038
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002039 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002040 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2041 ret = read_hdmi_channel_masks(out);
2042
2043 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2044 ret = audio_extn_read_afe_proxy_channel_masks(out);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002045 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002046 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002047 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002048
2049 if (config->sample_rate == 0)
2050 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2051 if (config->channel_mask == 0)
2052 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2053
2054 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002055 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002056 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2057 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002058 out->config.rate = config->sample_rate;
2059 out->config.channels = popcount(out->channel_mask);
2060 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002061 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2062 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002063 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002064 ret = voice_extn_compress_voip_open_output_stream(out);
2065 if (ret != 0) {
2066 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2067 __func__, ret);
2068 goto error_open;
2069 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002070 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2071 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2072 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2073 ALOGE("%s: Unsupported Offload information", __func__);
2074 ret = -EINVAL;
2075 goto error_open;
2076 }
Mingming Yin90310102013-11-13 16:57:00 -08002077 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002078 !audio_extn_is_dolby_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002079 ALOGE("%s: Unsupported audio format", __func__);
2080 ret = -EINVAL;
2081 goto error_open;
2082 }
2083
2084 out->compr_config.codec = (struct snd_codec *)
2085 calloc(1, sizeof(struct snd_codec));
2086
2087 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2088 if (config->offload_info.channel_mask)
2089 out->channel_mask = config->offload_info.channel_mask;
2090 else if (config->channel_mask)
2091 out->channel_mask = config->channel_mask;
2092 out->format = config->offload_info.format;
2093 out->sample_rate = config->offload_info.sample_rate;
2094
2095 out->stream.set_callback = out_set_callback;
2096 out->stream.pause = out_pause;
2097 out->stream.resume = out_resume;
2098 out->stream.drain = out_drain;
2099 out->stream.flush = out_flush;
2100
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002101 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002102 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002103 audio_extn_dolby_get_snd_codec_id(adev, out,
2104 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002105 else
2106 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002107 get_snd_codec_id(config->offload_info.format);
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -08002108 out->compr_config.fragment_size = get_offload_buffer_size();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002109 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2110 out->compr_config.codec->sample_rate =
2111 compress_get_alsa_rate(config->offload_info.sample_rate);
2112 out->compr_config.codec->bit_rate =
2113 config->offload_info.bit_rate;
2114 out->compr_config.codec->ch_in =
2115 popcount(config->channel_mask);
2116 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2117
2118 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2119 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002120
2121 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002122 out->offload_state = OFFLOAD_STATE_IDLE;
2123 out->playback_started = 0;
2124
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002125 create_offload_callback_thread(out);
2126 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2127 __func__, config->offload_info.version,
2128 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002129 //Decide if we need to use gapless mode by default
2130 set_gapless_mode(adev);
2131
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002132 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2133 ret = voice_check_and_set_incall_music_usecase(adev, out);
2134 if (ret != 0) {
2135 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2136 __func__, ret);
2137 goto error_open;
2138 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002139 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002140 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2141 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002142 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002143 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002144 /* primary path is the default path selected if no other outputs are available/suitable */
2145 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002146 out->config = pcm_config_deep_buffer;
2147 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002148 }
2149
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002150 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2151 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2152 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002153 if(adev->primary_output == NULL)
2154 adev->primary_output = out;
2155 else {
2156 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002157 ret = -EEXIST;
2158 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002159 }
2160 }
2161
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002162 /* Check if this usecase is already existing */
2163 pthread_mutex_lock(&adev->lock);
2164 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2165 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002166 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002167 ret = -EEXIST;
2168 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002169 }
2170 pthread_mutex_unlock(&adev->lock);
2171
2172 out->stream.common.get_sample_rate = out_get_sample_rate;
2173 out->stream.common.set_sample_rate = out_set_sample_rate;
2174 out->stream.common.get_buffer_size = out_get_buffer_size;
2175 out->stream.common.get_channels = out_get_channels;
2176 out->stream.common.get_format = out_get_format;
2177 out->stream.common.set_format = out_set_format;
2178 out->stream.common.standby = out_standby;
2179 out->stream.common.dump = out_dump;
2180 out->stream.common.set_parameters = out_set_parameters;
2181 out->stream.common.get_parameters = out_get_parameters;
2182 out->stream.common.add_audio_effect = out_add_audio_effect;
2183 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2184 out->stream.get_latency = out_get_latency;
2185 out->stream.set_volume = out_set_volume;
2186 out->stream.write = out_write;
2187 out->stream.get_render_position = out_get_render_position;
2188 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002189 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002190
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002191 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002192 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002193 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002194
2195 config->format = out->stream.common.get_format(&out->stream.common);
2196 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2197 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2198
2199 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002200 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002201 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002202
2203error_open:
2204 free(out);
2205 *stream_out = NULL;
2206 ALOGD("%s: exit: ret %d", __func__, ret);
2207 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002208}
2209
2210static void adev_close_output_stream(struct audio_hw_device *dev,
2211 struct audio_stream_out *stream)
2212{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002213 struct stream_out *out = (struct stream_out *)stream;
2214 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002215 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002216
Eric Laurent994a6932013-07-17 11:51:42 -07002217 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002218 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2219 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2220 if(ret != 0)
2221 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2222 __func__, ret);
2223 }
2224 else
2225 out_standby(&stream->common);
2226
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002227 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2228 destroy_offload_callback_thread(out);
2229
2230 if (out->compr_config.codec != NULL)
2231 free(out->compr_config.codec);
2232 }
2233 pthread_cond_destroy(&out->cond);
2234 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002235 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002236 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002237}
2238
2239static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2240{
2241 struct audio_device *adev = (struct audio_device *)dev;
2242 struct str_parms *parms;
2243 char *str;
2244 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002245 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002246 int ret = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002247
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002248 ALOGD("%s: enter: %s", __func__, kvpairs);
2249
2250 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002251 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002252
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002253 ret = voice_set_parameters(adev, parms);
2254 if (ret != 0)
2255 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002256
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002257 ret = platform_set_parameters(adev->platform, parms);
2258 if (ret != 0)
2259 goto done;
2260
2261 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2262 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002263 /* When set to false, HAL should disable EC and NS
2264 * But it is currently not supported.
2265 */
2266 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2267 adev->bluetooth_nrec = true;
2268 else
2269 adev->bluetooth_nrec = false;
2270 }
2271
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002272 err = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2273 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002274 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2275 adev->screen_off = false;
2276 else
2277 adev->screen_off = true;
2278 }
2279
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002280 err = str_parms_get_int(parms, "rotation", &val);
2281 if (err >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002282 bool reverse_speakers = false;
2283 switch(val) {
2284 // FIXME: note that the code below assumes that the speakers are in the correct placement
2285 // relative to the user when the device is rotated 90deg from its default rotation. This
2286 // assumption is device-specific, not platform-specific like this code.
2287 case 270:
2288 reverse_speakers = true;
2289 break;
2290 case 0:
2291 case 90:
2292 case 180:
2293 break;
2294 default:
2295 ALOGE("%s: unexpected rotation of %d", __func__, val);
2296 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002297 if (adev->speaker_lr_swap != reverse_speakers) {
2298 adev->speaker_lr_swap = reverse_speakers;
2299 // only update the selected device if there is active pcm playback
2300 struct audio_usecase *usecase;
2301 struct listnode *node;
2302 list_for_each(node, &adev->usecase_list) {
2303 usecase = node_to_item(node, struct audio_usecase, list);
2304 if (usecase->type == PCM_PLAYBACK) {
2305 select_devices(adev, usecase->id);
2306 break;
2307 }
2308 }
2309 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002310 }
2311
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002312 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002313
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002314done:
2315 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002316 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002317 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002318 return ret;
2319}
2320
2321static char* adev_get_parameters(const struct audio_hw_device *dev,
2322 const char *keys)
2323{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002324 struct audio_device *adev = (struct audio_device *)dev;
2325 struct str_parms *reply = str_parms_create();
2326 struct str_parms *query = str_parms_create_str(keys);
2327 char *str;
2328
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002329 pthread_mutex_lock(&adev->lock);
2330
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002331 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002332 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002333 platform_get_parameters(adev->platform, query, reply);
2334 str = str_parms_to_str(reply);
2335 str_parms_destroy(query);
2336 str_parms_destroy(reply);
2337
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002338 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002339 ALOGV("%s: exit: returns - %s", __func__, str);
2340 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002341}
2342
2343static int adev_init_check(const struct audio_hw_device *dev)
2344{
2345 return 0;
2346}
2347
2348static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2349{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002350 int ret;
2351 struct audio_device *adev = (struct audio_device *)dev;
2352 pthread_mutex_lock(&adev->lock);
2353 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002354 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002355 pthread_mutex_unlock(&adev->lock);
2356 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002357}
2358
2359static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2360{
2361 return -ENOSYS;
2362}
2363
2364static int adev_get_master_volume(struct audio_hw_device *dev,
2365 float *volume)
2366{
2367 return -ENOSYS;
2368}
2369
2370static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2371{
2372 return -ENOSYS;
2373}
2374
2375static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2376{
2377 return -ENOSYS;
2378}
2379
2380static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2381{
2382 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002383 pthread_mutex_lock(&adev->lock);
2384 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002385 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002386 adev->mode = mode;
2387 }
2388 pthread_mutex_unlock(&adev->lock);
2389 return 0;
2390}
2391
2392static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2393{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002394 int ret;
2395
2396 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002397 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002398 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2399 pthread_mutex_unlock(&adev->lock);
2400
2401 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002402}
2403
2404static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2405{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002406 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002407 return 0;
2408}
2409
2410static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2411 const struct audio_config *config)
2412{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002413 int channel_count = popcount(config->channel_mask);
2414
2415 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2416}
2417
2418static int adev_open_input_stream(struct audio_hw_device *dev,
2419 audio_io_handle_t handle,
2420 audio_devices_t devices,
2421 struct audio_config *config,
2422 struct audio_stream_in **stream_in)
2423{
2424 struct audio_device *adev = (struct audio_device *)dev;
2425 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002426 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002427 int channel_count = popcount(config->channel_mask);
2428
Eric Laurent994a6932013-07-17 11:51:42 -07002429 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002430 *stream_in = NULL;
2431 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2432 return -EINVAL;
2433
2434 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2435
2436 in->stream.common.get_sample_rate = in_get_sample_rate;
2437 in->stream.common.set_sample_rate = in_set_sample_rate;
2438 in->stream.common.get_buffer_size = in_get_buffer_size;
2439 in->stream.common.get_channels = in_get_channels;
2440 in->stream.common.get_format = in_get_format;
2441 in->stream.common.set_format = in_set_format;
2442 in->stream.common.standby = in_standby;
2443 in->stream.common.dump = in_dump;
2444 in->stream.common.set_parameters = in_set_parameters;
2445 in->stream.common.get_parameters = in_get_parameters;
2446 in->stream.common.add_audio_effect = in_add_audio_effect;
2447 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2448 in->stream.set_gain = in_set_gain;
2449 in->stream.read = in_read;
2450 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2451
2452 in->device = devices;
2453 in->source = AUDIO_SOURCE_DEFAULT;
2454 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002455 in->standby = 1;
2456 in->channel_mask = config->channel_mask;
2457
2458 /* Update config params with the requested sample rate and channels */
2459 in->usecase = USECASE_AUDIO_RECORD;
2460 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002461 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002462 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002463
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002464 if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002465 if(audio_extn_ssr_get_enabled()) {
2466 if(audio_extn_ssr_init(adev, in)) {
2467 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2468 ret = -EINVAL;
2469 goto err_open;
2470 }
2471 } else {
2472 ret = -EINVAL;
2473 goto err_open;
2474 }
Mingming Yine62d7842013-10-25 16:26:03 -07002475 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002476 audio_extn_compr_cap_format_supported(config->format) &&
2477 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Mingming Yine62d7842013-10-25 16:26:03 -07002478 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002479 } else {
2480 in->config.channels = channel_count;
2481 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2482 buffer_size = get_input_buffer_size(config->sample_rate,
2483 config->format,
2484 channel_count);
2485 in->config.period_size = buffer_size / frame_size;
2486 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002487
2488 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002489 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002490 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002491
2492err_open:
2493 free(in);
2494 *stream_in = NULL;
2495 return ret;
2496}
2497
2498static void adev_close_input_stream(struct audio_hw_device *dev,
2499 struct audio_stream_in *stream)
2500{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002501 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002502 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002503 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002504
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002505 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2506 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2507 if (ret != 0)
2508 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2509 __func__, ret);
2510 } else
2511 in_standby(&stream->common);
2512
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002513 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2514 audio_extn_ssr_deinit();
2515 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002516 free(stream);
2517
Mingming Yine62d7842013-10-25 16:26:03 -07002518 if(audio_extn_compr_cap_enabled() &&
2519 audio_extn_compr_cap_format_supported(in->config.format))
2520 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002521 return;
2522}
2523
2524static int adev_dump(const audio_hw_device_t *device, int fd)
2525{
2526 return 0;
2527}
2528
2529static int adev_close(hw_device_t *device)
2530{
2531 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002532
2533 if (!adev)
2534 return 0;
2535
2536 pthread_mutex_lock(&adev_init_lock);
2537
2538 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002539 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07002540 audio_route_free(adev->audio_route);
2541 free(adev->snd_dev_ref_cnt);
2542 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002543 free(device);
2544 adev = NULL;
2545 }
2546 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002547 return 0;
2548}
2549
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002550static int adev_open(const hw_module_t *module, const char *name,
2551 hw_device_t **device)
2552{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002553 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002554
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002555 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002556 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2557
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002558 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002559 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002560 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002561 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002562 ALOGD("%s: returning existing instance of adev", __func__);
2563 ALOGD("%s: exit", __func__);
2564 pthread_mutex_unlock(&adev_init_lock);
2565 return 0;
2566 }
2567
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002568 adev = calloc(1, sizeof(struct audio_device));
2569
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002570 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2571 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2572 adev->device.common.module = (struct hw_module_t *)module;
2573 adev->device.common.close = adev_close;
2574
2575 adev->device.init_check = adev_init_check;
2576 adev->device.set_voice_volume = adev_set_voice_volume;
2577 adev->device.set_master_volume = adev_set_master_volume;
2578 adev->device.get_master_volume = adev_get_master_volume;
2579 adev->device.set_master_mute = adev_set_master_mute;
2580 adev->device.get_master_mute = adev_get_master_mute;
2581 adev->device.set_mode = adev_set_mode;
2582 adev->device.set_mic_mute = adev_set_mic_mute;
2583 adev->device.get_mic_mute = adev_get_mic_mute;
2584 adev->device.set_parameters = adev_set_parameters;
2585 adev->device.get_parameters = adev_get_parameters;
2586 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2587 adev->device.open_output_stream = adev_open_output_stream;
2588 adev->device.close_output_stream = adev_close_output_stream;
2589 adev->device.open_input_stream = adev_open_input_stream;
2590 adev->device.close_input_stream = adev_close_input_stream;
2591 adev->device.dump = adev_dump;
2592
2593 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002594 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002595 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002596 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002597 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002598 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002599 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002600 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002601 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002602 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002603 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002604
2605 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002606 adev->platform = platform_init(adev);
2607 if (!adev->platform) {
2608 free(adev->snd_dev_ref_cnt);
2609 free(adev);
2610 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2611 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08002612 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07002613 return -EINVAL;
2614 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002615
2616 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2617 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2618 if (adev->visualizer_lib == NULL) {
2619 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2620 } else {
2621 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2622 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002623 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002624 "visualizer_hal_start_output");
2625 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002626 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002627 "visualizer_hal_stop_output");
2628 }
2629 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08002630 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07002631
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002632 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2633 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2634 if (adev->offload_effects_lib == NULL) {
2635 ALOGE("%s: DLOPEN failed for %s", __func__,
2636 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2637 } else {
2638 ALOGV("%s: DLOPEN successful for %s", __func__,
2639 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2640 adev->offload_effects_start_output =
2641 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2642 "offload_effects_bundle_hal_start_output");
2643 adev->offload_effects_stop_output =
2644 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2645 "offload_effects_bundle_hal_stop_output");
2646 }
2647 }
2648
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002649 *device = &adev->device.common;
2650
Kiran Kandi910e1862013-10-29 13:29:42 -07002651 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002652 pthread_mutex_unlock(&adev_init_lock);
2653
Eric Laurent994a6932013-07-17 11:51:42 -07002654 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002655 return 0;
2656}
2657
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -08002658/* Read offload buffer size from a property.
2659 * If value is not power of 2 round it to
2660 * power of 2.
2661 */
2662static uint32_t get_offload_buffer_size()
2663{
2664 char value[PROPERTY_VALUE_MAX] = {0};
2665 uint32_t fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2666 if((property_get("audio.offload.buffer.size.kb", value, "")) &&
2667 atoi(value)) {
2668 fragment_size = atoi(value) * 1024;
2669 //ring buffer size needs to be 4k aligned.
2670 CHECK(!(fragment_size * COMPRESS_OFFLOAD_NUM_FRAGMENTS % 4096));
2671 }
2672 if(fragment_size < MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
2673 fragment_size = MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2674 else if(fragment_size > MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
2675 fragment_size = MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2676 ALOGVV("%s: fragment_size %d", __func__, fragment_size);
2677 return fragment_size;
2678}
2679
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002680static int set_gapless_mode(struct audio_device *adev) {
2681
2682
2683 char value[PROPERTY_VALUE_MAX] = {0};
2684 bool gapless_enabled = false;
2685 const char *mixer_ctl_name = "Compress Gapless Playback";
2686 struct mixer_ctl *ctl;
2687
2688 ALOGV("%s:", __func__);
2689 property_get("audio.offload.gapless.enabled", value, NULL);
2690 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
2691
2692 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
2693 if (!ctl) {
2694 ALOGE("%s: Could not get ctl for mixer cmd - %s",
2695 __func__, mixer_ctl_name);
2696 return -EINVAL;
2697 }
2698
2699 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
2700 ALOGE("%s: Could not set gapless mode %d",
2701 __func__, gapless_enabled);
2702 return -EINVAL;
2703 }
2704 return 0;
2705
2706}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002707static struct hw_module_methods_t hal_module_methods = {
2708 .open = adev_open,
2709};
2710
2711struct audio_module HAL_MODULE_INFO_SYM = {
2712 .common = {
2713 .tag = HARDWARE_MODULE_TAG,
2714 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2715 .hal_api_version = HARDWARE_HAL_API_VERSION,
2716 .id = AUDIO_HARDWARE_MODULE_ID,
2717 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08002718 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002719 .methods = &hal_module_methods,
2720 },
2721};