blob: ded07f4509a524ab20c1fa96c406928a976da863 [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"
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080056#include "sound/asound.h"
ApurupaPattapu2e084df2013-12-18 15:47:59 -080057
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070058#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
59/* ToDo: Check and update a proper value in msec */
60#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
61#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
62
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -080063
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",
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700110#ifdef MULTIPLE_OFFLOAD_ENABLED
111 [USECASE_AUDIO_PLAYBACK_OFFLOAD2] = "compress-offload-playback2",
112 [USECASE_AUDIO_PLAYBACK_OFFLOAD3] = "compress-offload-playback3",
113 [USECASE_AUDIO_PLAYBACK_OFFLOAD4] = "compress-offload-playback4",
114 [USECASE_AUDIO_PLAYBACK_OFFLOAD5] = "compress-offload-playback5",
115 [USECASE_AUDIO_PLAYBACK_OFFLOAD6] = "compress-offload-playback6",
116 [USECASE_AUDIO_PLAYBACK_OFFLOAD7] = "compress-offload-playback7",
117 [USECASE_AUDIO_PLAYBACK_OFFLOAD8] = "compress-offload-playback8",
118 [USECASE_AUDIO_PLAYBACK_OFFLOAD9] = "compress-offload-playback9",
119#endif
Eric Laurentb23d5282013-05-14 15:27:20 -0700120 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700121 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700122 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700123 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700124 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800125 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800126 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700127 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700128
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700129 [USECASE_VOICE2_CALL] = "voice2-call",
130 [USECASE_VOLTE_CALL] = "volte-call",
131 [USECASE_QCHAT_CALL] = "qchat-call",
Vicky Sehrawat7e4fc152014-02-12 17:58:59 -0800132 [USECASE_VOWLAN_CALL] = "vowlan-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800133 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700134 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
135 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
136 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800137 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
138 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
139 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
140
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700141 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
142 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700143 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
144 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700145};
146
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700147static const audio_usecase_t offload_usecases[] = {
148 USECASE_AUDIO_PLAYBACK_OFFLOAD,
149#ifdef MULTIPLE_OFFLOAD_ENABLED
150 USECASE_AUDIO_PLAYBACK_OFFLOAD2,
151 USECASE_AUDIO_PLAYBACK_OFFLOAD3,
152 USECASE_AUDIO_PLAYBACK_OFFLOAD4,
153 USECASE_AUDIO_PLAYBACK_OFFLOAD5,
154 USECASE_AUDIO_PLAYBACK_OFFLOAD6,
155 USECASE_AUDIO_PLAYBACK_OFFLOAD7,
156 USECASE_AUDIO_PLAYBACK_OFFLOAD8,
157 USECASE_AUDIO_PLAYBACK_OFFLOAD9,
158#endif
159};
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800160
161#define STRING_TO_ENUM(string) { #string, string }
162
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800163struct string_to_enum {
164 const char *name;
165 uint32_t value;
166};
167
168static const struct string_to_enum out_channels_name_to_enum_table[] = {
169 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
170 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
171 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
172};
173
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700174static struct audio_device *adev = NULL;
175static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700176static unsigned int audio_device_ref_count;
177
Haynes Mathew George5191a852013-09-11 14:19:36 -0700178static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800179
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -0800180static int check_and_set_gapless_mode(struct audio_device *adev) {
181
182
183 char value[PROPERTY_VALUE_MAX] = {0};
184 bool gapless_enabled = false;
185 const char *mixer_ctl_name = "Compress Gapless Playback";
186 struct mixer_ctl *ctl;
187
188 ALOGV("%s:", __func__);
189 property_get("audio.offload.gapless.enabled", value, NULL);
190 gapless_enabled = atoi(value) || !strncmp("true", value, 4);
191
192 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
193 if (!ctl) {
194 ALOGE("%s: Could not get ctl for mixer cmd - %s",
195 __func__, mixer_ctl_name);
196 return -EINVAL;
197 }
198
199 if (mixer_ctl_set_value(ctl, 0, gapless_enabled) < 0) {
200 ALOGE("%s: Could not set gapless mode %d",
201 __func__, gapless_enabled);
202 return -EINVAL;
203 }
204 return 0;
205}
Haynes Mathew George5191a852013-09-11 14:19:36 -0700206
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700207static bool is_supported_format(audio_format_t format)
208{
Eric Laurent86e17132013-09-12 17:49:30 -0700209 if (format == AUDIO_FORMAT_MP3 ||
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800210 format == AUDIO_FORMAT_AAC ||
211 format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD ||
212 format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
213 return true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700214
215 return false;
216}
217
218static int get_snd_codec_id(audio_format_t format)
219{
220 int id = 0;
221
222 switch (format) {
223 case AUDIO_FORMAT_MP3:
224 id = SND_AUDIOCODEC_MP3;
225 break;
226 case AUDIO_FORMAT_AAC:
227 id = SND_AUDIOCODEC_AAC;
228 break;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -0800229 case AUDIO_FORMAT_PCM_16_BIT_OFFLOAD:
230 case AUDIO_FORMAT_PCM_24_BIT_OFFLOAD:
231 id = SND_AUDIOCODEC_PCM;
232 break;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700233 default:
Mingming Yin90310102013-11-13 16:57:00 -0800234 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700235 }
236
237 return id;
238}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800239
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700240int enable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700241 struct audio_usecase *usecase,
242 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800243{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700244 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700245 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800246
247 if (usecase == NULL)
248 return -EINVAL;
249
250 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
251
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800252 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700253 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800254 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700255 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800256
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -0800257#ifdef DS1_DOLBY_DAP_ENABLED
258 audio_extn_dolby_set_dmid(adev);
259 audio_extn_dolby_set_endpoint(adev);
260#endif
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800261 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700262 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700263 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700264 audio_route_apply_path(adev->audio_route, mixer_path);
265 if (update_mixer)
266 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800267
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800268 ALOGV("%s: exit", __func__);
269 return 0;
270}
271
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700272int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700273 struct audio_usecase *usecase,
274 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800275{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700276 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700277 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800278
279 if (usecase == NULL)
280 return -EINVAL;
281
282 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700283 if (usecase->type == PCM_CAPTURE)
284 snd_device = usecase->in_snd_device;
285 else
286 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800287 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700288 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700289 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700290 audio_route_reset_path(adev->audio_route, mixer_path);
291 if (update_mixer)
292 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800293
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800294 ALOGV("%s: exit", __func__);
295 return 0;
296}
297
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700298int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700299 snd_device_t snd_device,
300 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800301{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700302 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
303
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800304 if (snd_device < SND_DEVICE_MIN ||
305 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800306 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800307 return -EINVAL;
308 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700309
310 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700311
312 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
313 ALOGE("%s: Invalid sound device returned", __func__);
314 return -EINVAL;
315 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700316 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700317 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700318 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700319 return 0;
320 }
321
Vidyakumar Athota76ecc0d2014-03-21 14:51:50 -0700322 /* Set BT sample rate before enabling the devices. Adding sample rate mixer
323 * control in use-case does not work because rate update takes place after
324 * AFE port open due to the limitation of mixer control order execution.
325 */
Vidyakumar Athota3c9fb7f2014-04-15 16:01:47 -0700326 if ((snd_device == SND_DEVICE_OUT_BT_SCO) ||
327 (snd_device == SND_DEVICE_IN_BT_SCO_MIC)) {
Vidyakumar Athota76ecc0d2014-03-21 14:51:50 -0700328 audio_route_apply_path(adev->audio_route, BT_SCO_SAMPLE_RATE);
329 audio_route_update_mixer(adev->audio_route);
Vidyakumar Athota3c9fb7f2014-04-15 16:01:47 -0700330 } else if ((snd_device == SND_DEVICE_OUT_BT_SCO_WB) ||
331 (snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB)) {
Vidyakumar Athota76ecc0d2014-03-21 14:51:50 -0700332 audio_route_apply_path(adev->audio_route, BT_SCO_WB_SAMPLE_RATE);
333 audio_route_update_mixer(adev->audio_route);
334 }
335
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700336 /* start usb playback thread */
337 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
338 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
339 audio_extn_usb_start_playback(adev);
340
341 /* start usb capture thread */
342 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
343 audio_extn_usb_start_capture(adev);
344
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800345 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
346 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700347 audio_extn_spkr_prot_is_enabled()) {
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800348 if (audio_extn_spkr_prot_start_processing(snd_device)) {
349 ALOGE("%s: spkr_start_processing failed", __func__);
350 return -EINVAL;
351 }
352 } else {
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700353 ALOGV("%s: snd_device(%d: %s)", __func__,
354 snd_device, device_name);
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -0700355 /* due to the possibility of calibration overwrite between listen
356 and audio, notify listen hal before audio calibration is sent */
357 audio_extn_listen_update_status(snd_device,
358 LISTEN_EVENT_SND_DEVICE_BUSY);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700359 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
360 adev->snd_dev_ref_cnt[snd_device]--;
Bharath Ramachandramurthy0de16782014-03-28 21:34:33 -0700361 audio_extn_listen_update_status(snd_device,
362 LISTEN_EVENT_SND_DEVICE_FREE);
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700363 return -EINVAL;
364 }
365 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800366 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700367 if (update_mixer)
368 audio_route_update_mixer(adev->audio_route);
369
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800370 return 0;
371}
372
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700373int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700374 snd_device_t snd_device,
375 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800376{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700377 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
378
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800379 if (snd_device < SND_DEVICE_MIN ||
380 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800381 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800382 return -EINVAL;
383 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700384 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
385 ALOGE("%s: device ref cnt is already 0", __func__);
386 return -EINVAL;
387 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700388
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700389 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700390
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700391 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
392 ALOGE("%s: Invalid sound device returned", __func__);
393 return -EINVAL;
394 }
395
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700396 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700397 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700398 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800399 /* exit usb play back thread */
400 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
401 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
402 audio_extn_usb_stop_playback();
403
404 /* exit usb capture thread */
405 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
406 audio_extn_usb_stop_capture(adev);
407
Vidyakumar Athota1c6419a2014-01-10 14:47:34 -0800408 if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
409 snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700410 audio_extn_spkr_prot_is_enabled()) {
411 audio_extn_spkr_prot_stop_processing();
412 } else
413 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800414
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700415 if (update_mixer)
416 audio_route_update_mixer(adev->audio_route);
Kiran Kandide144c82013-11-20 15:58:32 -0800417
418 audio_extn_listen_update_status(snd_device,
419 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700420 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700421
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800422 return 0;
423}
424
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700425static void check_usecases_codec_backend(struct audio_device *adev,
426 struct audio_usecase *uc_info,
427 snd_device_t snd_device)
428{
429 struct listnode *node;
430 struct audio_usecase *usecase;
431 bool switch_device[AUDIO_USECASE_MAX];
432 int i, num_uc_to_switch = 0;
433
434 /*
435 * This function is to make sure that all the usecases that are active on
436 * the hardware codec backend are always routed to any one device that is
437 * handled by the hardware codec.
438 * For example, if low-latency and deep-buffer usecases are currently active
439 * on speaker and out_set_parameters(headset) is received on low-latency
440 * output, then we have to make sure deep-buffer is also switched to headset,
441 * because of the limitation that both the devices cannot be enabled
442 * at the same time as they share the same backend.
443 */
444 /* Disable all the usecases on the shared backend other than the
445 specified usecase */
446 for (i = 0; i < AUDIO_USECASE_MAX; i++)
447 switch_device[i] = false;
448
449 list_for_each(node, &adev->usecase_list) {
450 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800451 if (usecase->type != PCM_CAPTURE &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700452 usecase != uc_info &&
453 usecase->out_snd_device != snd_device &&
454 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
455 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
456 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700457 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700458 disable_audio_route(adev, usecase, false);
459 switch_device[usecase->id] = true;
460 num_uc_to_switch++;
461 }
462 }
463
464 if (num_uc_to_switch) {
465 /* Make sure all the streams are de-routed before disabling the device */
466 audio_route_update_mixer(adev->audio_route);
467
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530468 /* Make sure the previous devices to be disabled first and then enable the
469 selected devices */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700470 list_for_each(node, &adev->usecase_list) {
471 usecase = node_to_item(node, struct audio_usecase, list);
472 if (switch_device[usecase->id]) {
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530473 disable_snd_device(adev, usecase->out_snd_device, true);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700474 }
475 }
476
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700477 list_for_each(node, &adev->usecase_list) {
478 usecase = node_to_item(node, struct audio_usecase, list);
479 if (switch_device[usecase->id]) {
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530480 enable_snd_device(adev, snd_device, true);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700481 }
482 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700483
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700484 /* Re-route all the usecases on the shared backend other than the
485 specified usecase to new snd devices */
486 list_for_each(node, &adev->usecase_list) {
487 usecase = node_to_item(node, struct audio_usecase, list);
488 /* Update the out_snd_device only before enabling the audio route */
489 if (switch_device[usecase->id] ) {
490 usecase->out_snd_device = snd_device;
491 enable_audio_route(adev, usecase, false);
492 }
493 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700494 }
495}
496
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700497static void check_and_route_capture_usecases(struct audio_device *adev,
498 struct audio_usecase *uc_info,
499 snd_device_t snd_device)
500{
501 struct listnode *node;
502 struct audio_usecase *usecase;
503 bool switch_device[AUDIO_USECASE_MAX];
504 int i, num_uc_to_switch = 0;
505
506 /*
507 * This function is to make sure that all the active capture usecases
508 * are always routed to the same input sound device.
509 * For example, if audio-record and voice-call usecases are currently
510 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
511 * is received for voice call then we have to make sure that audio-record
512 * usecase is also switched to earpiece i.e. voice-dmic-ef,
513 * because of the limitation that two devices cannot be enabled
514 * at the same time if they share the same backend.
515 */
516 for (i = 0; i < AUDIO_USECASE_MAX; i++)
517 switch_device[i] = false;
518
519 list_for_each(node, &adev->usecase_list) {
520 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800521 if (usecase->type != PCM_PLAYBACK &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700522 usecase != uc_info &&
523 usecase->in_snd_device != snd_device) {
524 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
525 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700526 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700527 disable_audio_route(adev, usecase, false);
528 switch_device[usecase->id] = true;
529 num_uc_to_switch++;
530 }
531 }
532
533 if (num_uc_to_switch) {
534 /* Make sure all the streams are de-routed before disabling the device */
535 audio_route_update_mixer(adev->audio_route);
536
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530537 /* Make sure the previous devices to be disabled first and then enable the
538 selected devices */
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700539 list_for_each(node, &adev->usecase_list) {
540 usecase = node_to_item(node, struct audio_usecase, list);
541 if (switch_device[usecase->id]) {
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530542 disable_snd_device(adev, usecase->in_snd_device, true);
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800543 }
544 }
545
546 list_for_each(node, &adev->usecase_list) {
547 usecase = node_to_item(node, struct audio_usecase, list);
548 if (switch_device[usecase->id]) {
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530549 enable_snd_device(adev, snd_device, true);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700550 }
551 }
552
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700553 /* Re-route all the usecases on the shared backend other than the
554 specified usecase to new snd devices */
555 list_for_each(node, &adev->usecase_list) {
556 usecase = node_to_item(node, struct audio_usecase, list);
557 /* Update the in_snd_device only before enabling the audio route */
558 if (switch_device[usecase->id] ) {
559 usecase->in_snd_device = snd_device;
560 enable_audio_route(adev, usecase, false);
561 }
562 }
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700563 }
564}
565
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800566/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700567static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800568{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700569 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700570 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800571
572 switch (channels) {
573 /*
574 * Do not handle stereo output in Multi-channel cases
575 * Stereo case is handled in normal playback path
576 */
577 case 6:
578 ALOGV("%s: HDMI supports 5.1", __func__);
579 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
580 break;
581 case 8:
582 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
583 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
584 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
585 break;
586 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700587 ALOGE("HDMI does not support multi channel playback");
588 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800589 break;
590 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700591 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800592}
593
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700594static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
595{
596 struct audio_usecase *usecase;
597 struct listnode *node;
598
599 list_for_each(node, &adev->usecase_list) {
600 usecase = node_to_item(node, struct audio_usecase, list);
601 if (usecase->type == VOICE_CALL) {
602 ALOGV("%s: usecase id %d", __func__, usecase->id);
603 return usecase->id;
604 }
605 }
606 return USECASE_INVALID;
607}
608
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700609struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700610 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700611{
612 struct audio_usecase *usecase;
613 struct listnode *node;
614
615 list_for_each(node, &adev->usecase_list) {
616 usecase = node_to_item(node, struct audio_usecase, list);
617 if (usecase->id == uc_id)
618 return usecase;
619 }
620 return NULL;
621}
622
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700623int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800624{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800625 snd_device_t out_snd_device = SND_DEVICE_NONE;
626 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700627 struct audio_usecase *usecase = NULL;
628 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800629 struct audio_usecase *voip_usecase = NULL;
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800630 struct audio_usecase *hfp_usecase = NULL;
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800631 audio_usecase_t hfp_ucid;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800632 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700633 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800634
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700635 usecase = get_usecase_from_list(adev, uc_id);
636 if (usecase == NULL) {
637 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
638 return -EINVAL;
639 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800640
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800641 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800642 (usecase->type == VOIP_CALL) ||
643 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700644 out_snd_device = platform_get_output_snd_device(adev->platform,
645 usecase->stream.out->devices);
646 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700647 usecase->devices = usecase->stream.out->devices;
648 } else {
649 /*
650 * If the voice call is active, use the sound devices of voice call usecase
651 * so that it would not result any device switch. All the usecases will
652 * be switched to new device when select_devices() is called for voice call
653 * usecase. This is to avoid switching devices for voice call when
654 * check_usecases_codec_backend() is called below.
655 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700656 if (voice_is_in_call(adev)) {
657 vc_usecase = get_usecase_from_list(adev,
658 get_voice_usecase_id_from_list(adev));
Helen Zeng067b96b2013-11-26 12:10:29 -0800659 if ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
660 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700661 in_snd_device = vc_usecase->in_snd_device;
662 out_snd_device = vc_usecase->out_snd_device;
663 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800664 } else if (voice_extn_compress_voip_is_active(adev)) {
665 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
Narsinga Rao Chellac17cab62014-04-11 17:20:46 -0700666 if ((voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) &&
667 (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800668 in_snd_device = voip_usecase->in_snd_device;
669 out_snd_device = voip_usecase->out_snd_device;
670 }
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800671 } else if (audio_extn_hfp_is_active(adev)) {
Vimal Puthanveed41fcff22014-01-23 15:56:53 -0800672 hfp_ucid = audio_extn_hfp_get_usecase();
673 hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
Vimal Puthanveed37b4a1c2014-01-07 16:47:47 -0800674 if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
675 in_snd_device = hfp_usecase->in_snd_device;
676 out_snd_device = hfp_usecase->out_snd_device;
677 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700678 }
679 if (usecase->type == PCM_PLAYBACK) {
680 usecase->devices = usecase->stream.out->devices;
681 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700682 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700683 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700684 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700685 if (usecase->stream.out == adev->primary_output &&
686 adev->active_input &&
687 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
688 select_devices(adev, adev->active_input->usecase);
689 }
690 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700691 } else if (usecase->type == PCM_CAPTURE) {
692 usecase->devices = usecase->stream.in->device;
693 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700694 if (in_snd_device == SND_DEVICE_NONE) {
695 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
696 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700697 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700698 adev->primary_output->devices);
699 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700700 in_snd_device = platform_get_input_snd_device(adev->platform,
701 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700702 }
703 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700704 }
705 }
706
707 if (out_snd_device == usecase->out_snd_device &&
708 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800709 return 0;
710 }
711
sangwoobc677242013-08-08 16:53:43 +0900712 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700713 out_snd_device, platform_get_snd_device_name(out_snd_device),
714 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800715
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800716 /*
717 * Limitation: While in call, to do a device switch we need to disable
718 * and enable both RX and TX devices though one of them is same as current
719 * device.
720 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800721 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700722 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800723 }
724
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700725 /* Disable current sound devices */
726 if (usecase->out_snd_device != SND_DEVICE_NONE) {
727 disable_audio_route(adev, usecase, true);
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530728 disable_snd_device(adev, usecase->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800729 }
730
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700731 if (usecase->in_snd_device != SND_DEVICE_NONE) {
732 disable_audio_route(adev, usecase, true);
Venkata Narendra Kumar Gutta7610e632014-04-14 23:16:38 +0530733 disable_snd_device(adev, usecase->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800734 }
735
Vidyakumar Athota545dbd32013-11-13 17:30:53 -0800736 /* Applicable only on the targets that has external modem.
737 * New device information should be sent to modem before enabling
738 * the devices to reduce in-call device switch time.
739 */
740 if (usecase->type == VOICE_CALL)
741 status = platform_switch_voice_call_enable_device_config(adev->platform,
742 out_snd_device,
743 in_snd_device);
744
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700745 /* Enable new sound devices */
746 if (out_snd_device != SND_DEVICE_NONE) {
747 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
748 check_usecases_codec_backend(adev, usecase, out_snd_device);
749 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800750 }
751
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700752 if (in_snd_device != SND_DEVICE_NONE) {
753 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700754 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700755 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700756
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800757 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700758 status = platform_switch_voice_call_device_post(adev->platform,
759 out_snd_device,
760 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800761
sangwoo170731f2013-06-08 15:36:36 +0900762 audio_route_update_mixer(adev->audio_route);
763
764 usecase->in_snd_device = in_snd_device;
765 usecase->out_snd_device = out_snd_device;
766
Shiv Maliyappanahalli80ac6282013-12-20 18:56:15 -0800767 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900768
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800769 /* Applicable only on the targets that has external modem.
770 * Enable device command should be sent to modem only after
771 * enabling voice call mixer controls
772 */
773 if (usecase->type == VOICE_CALL)
774 status = platform_switch_voice_call_usecase_route_post(adev->platform,
775 out_snd_device,
776 in_snd_device);
777
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800778 return status;
779}
780
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800781static int stop_input_stream(struct stream_in *in)
782{
783 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800784 struct audio_usecase *uc_info;
785 struct audio_device *adev = in->dev;
786
Eric Laurentc8400632013-02-14 19:04:54 -0800787 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800788
Eric Laurent994a6932013-07-17 11:51:42 -0700789 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700790 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800791 uc_info = get_usecase_from_list(adev, in->usecase);
792 if (uc_info == NULL) {
793 ALOGE("%s: Could not find the usecase (%d) in the list",
794 __func__, in->usecase);
795 return -EINVAL;
796 }
797
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800798 /* Close in-call recording streams */
799 voice_check_and_stop_incall_rec_usecase(adev, in);
800
Eric Laurent150dbfe2013-02-27 14:31:02 -0800801 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700802 disable_audio_route(adev, uc_info, true);
803
804 /* 2. Disable the tx device */
805 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800806
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800807 list_remove(&uc_info->list);
808 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800809
Eric Laurent994a6932013-07-17 11:51:42 -0700810 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800811 return ret;
812}
813
814int start_input_stream(struct stream_in *in)
815{
816 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800817 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800818 struct audio_usecase *uc_info;
819 struct audio_device *adev = in->dev;
820
Mingming Yine62d7842013-10-25 16:26:03 -0700821 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Eric Laurent994a6932013-07-17 11:51:42 -0700822 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700823
824 /* Check if source matches incall recording usecase criteria */
825 ret = voice_check_and_set_incall_rec_usecase(adev, in);
826 if (ret)
827 goto error_config;
828 else
829 ALOGV("%s: usecase(%d)", __func__, in->usecase);
830
Eric Laurentb23d5282013-05-14 15:27:20 -0700831 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800832 if (in->pcm_device_id < 0) {
833 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
834 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800835 ret = -EINVAL;
836 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800837 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700838
839 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800840 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
841 uc_info->id = in->usecase;
842 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800843 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700844 uc_info->devices = in->device;
845 uc_info->in_snd_device = SND_DEVICE_NONE;
846 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800847
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800848 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700849 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800850
Eric Laurentc8400632013-02-14 19:04:54 -0800851 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800852 __func__, adev->snd_card,
853 in->pcm_device_id, in->config.channels);
854 in->pcm = pcm_open(adev->snd_card,
855 in->pcm_device_id, PCM_IN, &in->config);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800856 if (in->pcm && !pcm_is_ready(in->pcm)) {
857 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
858 pcm_close(in->pcm);
859 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800860 ret = -EIO;
861 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800862 }
Eric Laurent994a6932013-07-17 11:51:42 -0700863 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800864 return ret;
865
866error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800867 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800868
869error_config:
870 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700871 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800872
873 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800874}
875
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700876/* must be called with out->lock locked */
877static int send_offload_cmd_l(struct stream_out* out, int command)
878{
879 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
880
881 ALOGVV("%s %d", __func__, command);
882
883 cmd->cmd = command;
884 list_add_tail(&out->offload_cmd_list, &cmd->node);
885 pthread_cond_signal(&out->offload_cond);
886 return 0;
887}
888
889/* must be called iwth out->lock locked */
890static void stop_compressed_output_l(struct stream_out *out)
891{
892 out->offload_state = OFFLOAD_STATE_IDLE;
893 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700894 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700895 if (out->compr != NULL) {
896 compress_stop(out->compr);
897 while (out->offload_thread_blocked) {
898 pthread_cond_wait(&out->cond, &out->lock);
899 }
900 }
901}
902
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -0700903bool is_offload_usecase(audio_usecase_t uc_id)
904{
905 unsigned int i;
906 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
907 if (uc_id == offload_usecases[i])
908 return true;
909 }
910 return false;
911}
912
913static audio_usecase_t get_offload_usecase(struct audio_device *adev)
914{
915 audio_usecase_t ret = USECASE_AUDIO_PLAYBACK_OFFLOAD;
916 unsigned int i, num_usecase = sizeof(offload_usecases)/sizeof(offload_usecases[0]);
917 char value[PROPERTY_VALUE_MAX] = {0};
918
919 property_get("audio.offload.multiple.enabled", value, NULL);
920 if (!(atoi(value) || !strncmp("true", value, 4)))
921 num_usecase = 1; /* If prop is not set, limit the num of offload usecases to 1 */
922
923 ALOGV("%s: num_usecase: %d", __func__, num_usecase);
924 for (i = 0; i < num_usecase; i++) {
925 if (!(adev->offload_usecases_state & (0x1<<i))) {
926 adev->offload_usecases_state |= 0x1 << i;
927 ret = offload_usecases[i];
928 break;
929 }
930 }
931 ALOGV("%s: offload usecase is %d", __func__, ret);
932 return ret;
933}
934
935static void free_offload_usecase(struct audio_device *adev,
936 audio_usecase_t uc_id)
937{
938 unsigned int i;
939 for (i = 0; i < sizeof(offload_usecases)/sizeof(offload_usecases[0]); i++) {
940 if (offload_usecases[i] == uc_id) {
941 adev->offload_usecases_state &= ~(0x1<<i);
942 break;
943 }
944 }
945 ALOGV("%s: free offload usecase %d", __func__, uc_id);
946}
947
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700948static void *offload_thread_loop(void *context)
949{
950 struct stream_out *out = (struct stream_out *) context;
951 struct listnode *item;
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -0800952 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700953
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700954 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
955 set_sched_policy(0, SP_FOREGROUND);
956 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
957
958 ALOGV("%s", __func__);
959 pthread_mutex_lock(&out->lock);
960 for (;;) {
961 struct offload_cmd *cmd = NULL;
962 stream_callback_event_t event;
963 bool send_callback = false;
964
965 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
966 __func__, list_empty(&out->offload_cmd_list),
967 out->offload_state);
968 if (list_empty(&out->offload_cmd_list)) {
969 ALOGV("%s SLEEPING", __func__);
970 pthread_cond_wait(&out->offload_cond, &out->lock);
971 ALOGV("%s RUNNING", __func__);
972 continue;
973 }
974
975 item = list_head(&out->offload_cmd_list);
976 cmd = node_to_item(item, struct offload_cmd, node);
977 list_remove(item);
978
979 ALOGVV("%s STATE %d CMD %d out->compr %p",
980 __func__, out->offload_state, cmd->cmd, out->compr);
981
982 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
983 free(cmd);
984 break;
985 }
986
987 if (out->compr == NULL) {
988 ALOGE("%s: Compress handle is NULL", __func__);
989 pthread_cond_signal(&out->cond);
990 continue;
991 }
992 out->offload_thread_blocked = true;
993 pthread_mutex_unlock(&out->lock);
994 send_callback = false;
995 switch(cmd->cmd) {
996 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
Sidipotu Ashok55820562014-02-10 16:16:38 +0530997 ALOGD("copl(%x):calling compress_wait", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700998 compress_wait(out->compr, -1);
Sidipotu Ashok55820562014-02-10 16:16:38 +0530999 ALOGD("copl(%x):out of compress_wait", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001000 send_callback = true;
1001 event = STREAM_CBK_EVENT_WRITE_READY;
1002 break;
1003 case OFFLOAD_CMD_PARTIAL_DRAIN:
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001004 ret = compress_next_track(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301005 if(ret == 0) {
1006 ALOGD("copl(%x):calling compress_partial_drain", (unsigned int)out);
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001007 compress_partial_drain(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301008 ALOGD("copl(%x):out of compress_partial_drain", (unsigned int)out);
1009 }
Krishnankutty Kolathappillyd4f1d132014-01-06 18:33:58 -08001010 else if(ret == -ETIMEDOUT)
1011 compress_drain(out->compr);
1012 else
1013 ALOGE("%s: Next track returned error %d",__func__, ret);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001014 send_callback = true;
1015 event = STREAM_CBK_EVENT_DRAIN_READY;
1016 break;
1017 case OFFLOAD_CMD_DRAIN:
Sidipotu Ashok55820562014-02-10 16:16:38 +05301018 ALOGD("copl(%x):calling compress_drain", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001019 compress_drain(out->compr);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301020 ALOGD("copl(%x):calling compress_drain", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001021 send_callback = true;
1022 event = STREAM_CBK_EVENT_DRAIN_READY;
1023 break;
1024 default:
1025 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
1026 break;
1027 }
1028 pthread_mutex_lock(&out->lock);
1029 out->offload_thread_blocked = false;
1030 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -07001031 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001032 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -07001033 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001034 free(cmd);
1035 }
1036
1037 pthread_cond_signal(&out->cond);
1038 while (!list_empty(&out->offload_cmd_list)) {
1039 item = list_head(&out->offload_cmd_list);
1040 list_remove(item);
1041 free(node_to_item(item, struct offload_cmd, node));
1042 }
1043 pthread_mutex_unlock(&out->lock);
1044
1045 return NULL;
1046}
1047
1048static int create_offload_callback_thread(struct stream_out *out)
1049{
1050 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
1051 list_init(&out->offload_cmd_list);
1052 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
1053 offload_thread_loop, out);
1054 return 0;
1055}
1056
1057static int destroy_offload_callback_thread(struct stream_out *out)
1058{
1059 pthread_mutex_lock(&out->lock);
1060 stop_compressed_output_l(out);
1061 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
1062
1063 pthread_mutex_unlock(&out->lock);
1064 pthread_join(out->offload_thread, (void **) NULL);
1065 pthread_cond_destroy(&out->offload_cond);
1066
1067 return 0;
1068}
1069
Eric Laurent07eeafd2013-10-06 12:52:49 -07001070static bool allow_hdmi_channel_config(struct audio_device *adev)
1071{
1072 struct listnode *node;
1073 struct audio_usecase *usecase;
1074 bool ret = true;
1075
1076 list_for_each(node, &adev->usecase_list) {
1077 usecase = node_to_item(node, struct audio_usecase, list);
1078 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1079 /*
1080 * If voice call is already existing, do not proceed further to avoid
1081 * disabling/enabling both RX and TX devices, CSD calls, etc.
1082 * Once the voice call done, the HDMI channels can be configured to
1083 * max channels of remaining use cases.
1084 */
1085 if (usecase->id == USECASE_VOICE_CALL) {
1086 ALOGD("%s: voice call is active, no change in HDMI channels",
1087 __func__);
1088 ret = false;
1089 break;
1090 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1091 ALOGD("%s: multi channel playback is active, "
1092 "no change in HDMI channels", __func__);
1093 ret = false;
1094 break;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001095 } else if (is_offload_usecase(usecase->id) &&
Mingming Yin139f1072014-02-24 17:56:01 -08001096 popcount(usecase->stream.out->channel_mask) > 2) {
1097 ALOGD("%s: multi-channel(%x) compress offload playback is active, "
1098 "no change in HDMI channels", __func__, usecase->stream.out->channel_mask);
1099 ret = false;
1100 break;
Eric Laurent07eeafd2013-10-06 12:52:49 -07001101 }
1102 }
1103 }
1104 return ret;
1105}
1106
1107static int check_and_set_hdmi_channels(struct audio_device *adev,
1108 unsigned int channels)
1109{
1110 struct listnode *node;
1111 struct audio_usecase *usecase;
1112
1113 /* Check if change in HDMI channel config is allowed */
1114 if (!allow_hdmi_channel_config(adev))
1115 return 0;
1116
1117 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001118 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001119 return 0;
1120 }
1121
1122 platform_set_hdmi_channels(adev->platform, channels);
1123 adev->cur_hdmi_channels = channels;
1124
1125 /*
1126 * Deroute all the playback streams routed to HDMI so that
1127 * the back end is deactivated. Note that backend will not
1128 * be deactivated if any one stream is connected to it.
1129 */
1130 list_for_each(node, &adev->usecase_list) {
1131 usecase = node_to_item(node, struct audio_usecase, list);
1132 if (usecase->type == PCM_PLAYBACK &&
1133 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1134 disable_audio_route(adev, usecase, true);
1135 }
1136 }
1137
1138 /*
1139 * Enable all the streams disabled above. Now the HDMI backend
1140 * will be activated with new channel configuration
1141 */
1142 list_for_each(node, &adev->usecase_list) {
1143 usecase = node_to_item(node, struct audio_usecase, list);
1144 if (usecase->type == PCM_PLAYBACK &&
1145 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1146 enable_audio_route(adev, usecase, true);
1147 }
1148 }
1149
1150 return 0;
1151}
1152
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001153static int stop_output_stream(struct stream_out *out)
1154{
1155 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001156 struct audio_usecase *uc_info;
1157 struct audio_device *adev = out->dev;
1158
Eric Laurent994a6932013-07-17 11:51:42 -07001159 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001160 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001161 uc_info = get_usecase_from_list(adev, out->usecase);
1162 if (uc_info == NULL) {
1163 ALOGE("%s: Could not find the usecase (%d) in the list",
1164 __func__, out->usecase);
1165 return -EINVAL;
1166 }
1167
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001168 if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001169 if (adev->visualizer_stop_output != NULL)
1170 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1171 if (adev->offload_effects_stop_output != NULL)
1172 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1173 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001174
Eric Laurent150dbfe2013-02-27 14:31:02 -08001175 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001176 disable_audio_route(adev, uc_info, true);
1177
1178 /* 2. Disable the rx device */
1179 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001180
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001181 list_remove(&uc_info->list);
1182 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001183
Eric Laurent07eeafd2013-10-06 12:52:49 -07001184 /* Must be called after removing the usecase from list */
1185 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1186 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1187
Eric Laurent994a6932013-07-17 11:51:42 -07001188 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001189 return ret;
1190}
1191
1192int start_output_stream(struct stream_out *out)
1193{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001194 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001195 struct audio_usecase *uc_info;
1196 struct audio_device *adev = out->dev;
1197
Sidipotu Ashok55820562014-02-10 16:16:38 +05301198 ALOGD("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001199 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001200 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201 if (out->pcm_device_id < 0) {
1202 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1203 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001204 ret = -EINVAL;
1205 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001206 }
1207
1208 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1209 uc_info->id = out->usecase;
1210 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001211 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001212 uc_info->devices = out->devices;
1213 uc_info->in_snd_device = SND_DEVICE_NONE;
1214 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001215
Eric Laurent07eeafd2013-10-06 12:52:49 -07001216 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001217 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001218 if (is_offload_usecase(out->usecase))
Mingming Yin10fef6a2013-11-26 17:17:01 -08001219 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1220 else
1221 check_and_set_hdmi_channels(adev, out->config.channels);
1222 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001223
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001224 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001225
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001226 select_devices(adev, out->usecase);
1227
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001228 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1229 __func__, 0, out->pcm_device_id);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001230 if (!is_offload_usecase(out->usecase)) {
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001231 out->pcm = pcm_open(adev->snd_card,
1232 out->pcm_device_id,
1233 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001234 if (out->pcm && !pcm_is_ready(out->pcm)) {
1235 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1236 pcm_close(out->pcm);
1237 out->pcm = NULL;
1238 ret = -EIO;
1239 goto error_open;
1240 }
1241 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001242 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001243 out->compr = compress_open(adev->snd_card,
1244 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001245 COMPRESS_IN, &out->compr_config);
1246 if (out->compr && !is_compress_ready(out->compr)) {
1247 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1248 compress_close(out->compr);
1249 out->compr = NULL;
1250 ret = -EIO;
1251 goto error_open;
1252 }
1253 if (out->offload_callback)
1254 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001255
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08001256#ifdef DS1_DOLBY_DDP_ENABLED
1257 if (audio_extn_is_dolby_format(out->format))
1258 audio_extn_dolby_send_ddp_endp_params(adev);
1259#endif
1260
Eric Laurentc4aef752013-09-12 17:45:53 -07001261 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001262 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1263 if (adev->offload_effects_start_output != NULL)
1264 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001265 }
Eric Laurent994a6932013-07-17 11:51:42 -07001266 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001267 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001268error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001269 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001270error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001271 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001272}
1273
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001274static int check_input_parameters(uint32_t sample_rate,
1275 audio_format_t format,
1276 int channel_count)
1277{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001278 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001279
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001280 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001281 !voice_extn_compress_voip_is_format_supported(format) &&
1282 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001283
1284 switch (channel_count) {
1285 case 1:
1286 case 2:
1287 case 6:
1288 break;
1289 default:
1290 ret = -EINVAL;
1291 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001292
1293 switch (sample_rate) {
1294 case 8000:
1295 case 11025:
1296 case 12000:
1297 case 16000:
1298 case 22050:
1299 case 24000:
1300 case 32000:
1301 case 44100:
1302 case 48000:
1303 break;
1304 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001305 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001306 }
1307
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001308 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001309}
1310
1311static size_t get_input_buffer_size(uint32_t sample_rate,
1312 audio_format_t format,
1313 int channel_count)
1314{
1315 size_t size = 0;
1316
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001317 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1318 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001319
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001320 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1321 /* ToDo: should use frame_size computed based on the format and
1322 channel_count here. */
1323 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001324
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001325 /* make sure the size is multiple of 64 */
1326 size += 0x3f;
1327 size &= ~0x3f;
1328
1329 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001330}
1331
1332static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1333{
1334 struct stream_out *out = (struct stream_out *)stream;
1335
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001336 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337}
1338
1339static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1340{
1341 return -ENOSYS;
1342}
1343
1344static size_t out_get_buffer_size(const struct audio_stream *stream)
1345{
1346 struct stream_out *out = (struct stream_out *)stream;
1347
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001348 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001349 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001350 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1351 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001352
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001353 return out->config.period_size * audio_stream_frame_size(stream);
1354}
1355
1356static uint32_t out_get_channels(const struct audio_stream *stream)
1357{
1358 struct stream_out *out = (struct stream_out *)stream;
1359
1360 return out->channel_mask;
1361}
1362
1363static audio_format_t out_get_format(const struct audio_stream *stream)
1364{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001365 struct stream_out *out = (struct stream_out *)stream;
1366
1367 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001368}
1369
1370static int out_set_format(struct audio_stream *stream, audio_format_t format)
1371{
1372 return -ENOSYS;
1373}
1374
1375static int out_standby(struct audio_stream *stream)
1376{
1377 struct stream_out *out = (struct stream_out *)stream;
1378 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001379
Eric Laurent994a6932013-07-17 11:51:42 -07001380 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001381 out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001382 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1383 /* Ignore standby in case of voip call because the voip output
1384 * stream is closed in adev_close_output_stream()
1385 */
1386 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1387 return 0;
1388 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001389
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001390 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001391 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001392 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001393 out->standby = true;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001394 if (!is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001395 if (out->pcm) {
1396 pcm_close(out->pcm);
1397 out->pcm = NULL;
1398 }
1399 } else {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301400 ALOGD("copl(%x):standby", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001401 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001402 out->gapless_mdata.encoder_delay = 0;
1403 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001404 if (out->compr != NULL) {
1405 compress_close(out->compr);
1406 out->compr = NULL;
1407 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001408 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001410 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001411 }
1412 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001413 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001414 return 0;
1415}
1416
1417static int out_dump(const struct audio_stream *stream, int fd)
1418{
1419 return 0;
1420}
1421
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001422static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1423{
1424 int ret = 0;
1425 char value[32];
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001426 bool is_meta_data_params = false;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001427 struct compr_gapless_mdata tmp_mdata;
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001428 tmp_mdata.encoder_delay = 0;
1429 tmp_mdata.encoder_padding = 0;
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001430
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001431 if (!out || !parms) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001432 ALOGE("%s: return invalid ",__func__);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001433 return -EINVAL;
1434 }
1435
ApurupaPattapu2e084df2013-12-18 15:47:59 -08001436 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_FORMAT, value, sizeof(value));
1437 if (ret >= 0) {
1438 if (atoi(value) == SND_AUDIOSTREAMFORMAT_MP4ADTS) {
1439 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_MP4ADTS;
1440 ALOGV("ADTS format is set in offload mode");
1441 }
1442 out->send_new_metadata = 1;
1443 }
1444
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001445 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_SAMPLE_RATE, value, sizeof(value));
1446 if(ret >= 0)
1447 is_meta_data_params = true;
1448 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_NUM_CHANNEL, value, sizeof(value));
1449 if(ret >= 0 )
1450 is_meta_data_params = true;
1451 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE, value, sizeof(value));
1452 if(ret >= 0 )
1453 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001454 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1455 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001456 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001457 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001458 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001459 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1460 if (ret >= 0) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001461 is_meta_data_params = true;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001462 tmp_mdata.encoder_padding = atoi(value);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001463 }
1464
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001465 if(!is_meta_data_params) {
1466 ALOGV("%s: Not gapless meta data params", __func__);
1467 return 0;
1468 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001469 out->gapless_mdata = tmp_mdata;
1470 out->send_new_metadata = 1;
1471 ALOGV("%s new encoder delay %u and padding %u", __func__,
1472 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1473
1474 return 0;
1475}
1476
1477
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001478static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1479{
1480 struct stream_out *out = (struct stream_out *)stream;
1481 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001482 struct audio_usecase *usecase;
1483 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001484 struct str_parms *parms;
1485 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001486 int ret = 0, val = 0, err;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001487 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001488
sangwoobc677242013-08-08 16:53:43 +09001489 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001490 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001491 parms = str_parms_create_str(kvpairs);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001492 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1493 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001494 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001495 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001496 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001497
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001498 /*
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301499 * When HDMI cable is unplugged/usb hs is disconnected the
1500 * music playback is paused and the policy manager sends routing=0
1501 * But the audioflingercontinues to write data until standby time
1502 * (3sec). As the HDMI core is turned off, the write gets blocked.
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001503 * Avoid this by routing audio to speaker until standby.
1504 */
Dhanalakshmi Siddani929a1f12014-04-18 22:26:56 +05301505 if ((out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
1506 out->devices == AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001507 val == AUDIO_DEVICE_NONE) {
1508 val = AUDIO_DEVICE_OUT_SPEAKER;
1509 }
1510
1511 /*
1512 * select_devices() call below switches all the usecases on the same
1513 * backend to the new device. Refer to check_usecases_codec_backend() in
1514 * the select_devices(). But how do we undo this?
1515 *
1516 * For example, music playback is active on headset (deep-buffer usecase)
1517 * and if we go to ringtones and select a ringtone, low-latency usecase
1518 * will be started on headset+speaker. As we can't enable headset+speaker
1519 * and headset devices at the same time, select_devices() switches the music
1520 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1521 * So when the ringtone playback is completed, how do we undo the same?
1522 *
1523 * We are relying on the out_set_parameters() call on deep-buffer output,
1524 * once the ringtone playback is ended.
1525 * NOTE: We should not check if the current devices are same as new devices.
1526 * Because select_devices() must be called to switch back the music
1527 * playback to headset.
1528 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001529 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001530 out->devices = val;
1531
1532 if (!out->standby)
1533 select_devices(adev, out->usecase);
1534
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001535 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1536 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001537 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001538 ret = voice_start_call(adev);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001539 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1540 voice_is_in_call(adev) &&
1541 (out == adev->primary_output)) {
Narsinga Rao Chella22d8d7a2014-02-06 14:05:14 -08001542 voice_update_devices_for_all_voice_usecases(adev);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001543 }
1544 }
1545
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001546 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1547 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001548 (out == adev->primary_output)) {
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001549 ret = voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001550 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001551
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001552 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001553 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001554 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001555
1556 if (out == adev->primary_output) {
1557 pthread_mutex_lock(&adev->lock);
1558 audio_extn_set_parameters(adev, parms);
1559 pthread_mutex_unlock(&adev->lock);
1560 }
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001561 if (is_offload_usecase(out->usecase)) {
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001562 pthread_mutex_lock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001563 parse_compress_metadata(out, parms);
Krishnankutty Kolathappillyeb78be72013-12-15 12:03:07 -08001564 pthread_mutex_unlock(&out->lock);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001565 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001566
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001567 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001568 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001569 return ret;
1570}
1571
1572static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1573{
1574 struct stream_out *out = (struct stream_out *)stream;
1575 struct str_parms *query = str_parms_create_str(keys);
1576 char *str;
1577 char value[256];
1578 struct str_parms *reply = str_parms_create();
1579 size_t i, j;
1580 int ret;
1581 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001582 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001583 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1584 if (ret >= 0) {
1585 value[0] = '\0';
1586 i = 0;
1587 while (out->supported_channel_masks[i] != 0) {
1588 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1589 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1590 if (!first) {
1591 strcat(value, "|");
1592 }
1593 strcat(value, out_channels_name_to_enum_table[j].name);
1594 first = false;
1595 break;
1596 }
1597 }
1598 i++;
1599 }
1600 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1601 str = str_parms_to_str(reply);
1602 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001603 voice_extn_out_get_parameters(out, query, reply);
1604 str = str_parms_to_str(reply);
1605 if (!strncmp(str, "", sizeof(""))) {
Narsinga Rao Chella29b8fc72014-01-29 12:52:19 -08001606 free(str);
1607 str = strdup(keys);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001608 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001609 }
1610 str_parms_destroy(query);
1611 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001612 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001613 return str;
1614}
1615
1616static uint32_t out_get_latency(const struct audio_stream_out *stream)
1617{
1618 struct stream_out *out = (struct stream_out *)stream;
1619
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001620 if (is_offload_usecase(out->usecase))
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001621 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1622
1623 return (out->config.period_count * out->config.period_size * 1000) /
1624 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001625}
1626
1627static int out_set_volume(struct audio_stream_out *stream, float left,
1628 float right)
1629{
Eric Laurenta9024de2013-04-04 09:19:12 -07001630 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001631 int volume[2];
1632
Eric Laurenta9024de2013-04-04 09:19:12 -07001633 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1634 /* only take left channel into account: the API is for stereo anyway */
1635 out->muted = (left == 0.0f);
1636 return 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001637 } else if (is_offload_usecase(out->usecase)) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001638 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001639 struct audio_device *adev = out->dev;
1640 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001641 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1642 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001643
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001644 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1645 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001646 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1647 if (!ctl) {
1648 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1649 __func__, mixer_ctl_name);
1650 return -EINVAL;
1651 }
1652 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1653 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1654 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1655 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001656 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001657
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001658 return -ENOSYS;
1659}
1660
1661static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1662 size_t bytes)
1663{
1664 struct stream_out *out = (struct stream_out *)stream;
1665 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001666 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001667
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001668 pthread_mutex_lock(&out->lock);
1669 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001670 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001671 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001672 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1673 ret = voice_extn_compress_voip_start_output_stream(out);
1674 else
1675 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001676 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001677 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001678 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001679 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001680 goto exit;
1681 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001682 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001683
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001684 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301685 ALOGD("copl(%x): writing buffer (%d bytes) to compress device", (unsigned int)out, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001686 if (out->send_new_metadata) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301687 ALOGD("copl(%x):send new gapless metadata", (unsigned int)out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001688 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1689 out->send_new_metadata = 0;
1690 }
1691
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001692 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001693 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001694 if (ret >= 0 && ret < (ssize_t)bytes) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301695 ALOGD("No space available in compress driver, post msg to cb thread");
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001696 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1697 }
1698 if (!out->playback_started) {
1699 compress_start(out->compr);
1700 out->playback_started = 1;
1701 out->offload_state = OFFLOAD_STATE_PLAYING;
1702 }
1703 pthread_mutex_unlock(&out->lock);
1704 return ret;
1705 } else {
1706 if (out->pcm) {
1707 if (out->muted)
1708 memset((void *)buffer, 0, bytes);
1709 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1710 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001711 if (ret == 0)
1712 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001713 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001714 }
1715
1716exit:
1717 pthread_mutex_unlock(&out->lock);
1718
1719 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001720 if (out->pcm)
1721 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001722 out_standby(&out->stream.common);
1723 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1724 out_get_sample_rate(&out->stream.common));
1725 }
1726 return bytes;
1727}
1728
1729static int out_get_render_position(const struct audio_stream_out *stream,
1730 uint32_t *dsp_frames)
1731{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001732 struct stream_out *out = (struct stream_out *)stream;
1733 *dsp_frames = 0;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001734 if (is_offload_usecase(out->usecase) && (dsp_frames != NULL)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001735 pthread_mutex_lock(&out->lock);
1736 if (out->compr != NULL) {
1737 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1738 &out->sample_rate);
1739 ALOGVV("%s rendered frames %d sample_rate %d",
1740 __func__, *dsp_frames, out->sample_rate);
1741 }
1742 pthread_mutex_unlock(&out->lock);
1743 return 0;
1744 } else
1745 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001746}
1747
1748static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1749{
1750 return 0;
1751}
1752
1753static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1754{
1755 return 0;
1756}
1757
1758static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1759 int64_t *timestamp)
1760{
1761 return -EINVAL;
1762}
1763
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001764static int out_get_presentation_position(const struct audio_stream_out *stream,
1765 uint64_t *frames, struct timespec *timestamp)
1766{
1767 struct stream_out *out = (struct stream_out *)stream;
1768 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001769 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001770
1771 pthread_mutex_lock(&out->lock);
1772
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001773 if (is_offload_usecase(out->usecase)) {
Eric Laurent949a0892013-09-20 09:20:13 -07001774 if (out->compr != NULL) {
1775 compress_get_tstamp(out->compr, &dsp_frames,
1776 &out->sample_rate);
1777 ALOGVV("%s rendered frames %ld sample_rate %d",
1778 __func__, dsp_frames, out->sample_rate);
1779 *frames = dsp_frames;
1780 ret = 0;
1781 /* this is the best we can do */
1782 clock_gettime(CLOCK_MONOTONIC, timestamp);
1783 }
1784 } else {
1785 if (out->pcm) {
1786 size_t avail;
1787 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1788 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001789 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001790 // This adjustment accounts for buffering after app processor.
1791 // It is based on estimated DSP latency per use case, rather than exact.
1792 signed_frames -=
1793 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1794
Eric Laurent949a0892013-09-20 09:20:13 -07001795 // It would be unusual for this value to be negative, but check just in case ...
1796 if (signed_frames >= 0) {
1797 *frames = signed_frames;
1798 ret = 0;
1799 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001800 }
1801 }
1802 }
1803
1804 pthread_mutex_unlock(&out->lock);
1805
1806 return ret;
1807}
1808
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001809static int out_set_callback(struct audio_stream_out *stream,
1810 stream_callback_t callback, void *cookie)
1811{
1812 struct stream_out *out = (struct stream_out *)stream;
1813
1814 ALOGV("%s", __func__);
1815 pthread_mutex_lock(&out->lock);
1816 out->offload_callback = callback;
1817 out->offload_cookie = cookie;
1818 pthread_mutex_unlock(&out->lock);
1819 return 0;
1820}
1821
1822static int out_pause(struct audio_stream_out* stream)
1823{
1824 struct stream_out *out = (struct stream_out *)stream;
1825 int status = -ENOSYS;
1826 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001827 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301828 ALOGD("copl(%x):pause compress driver", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001829 pthread_mutex_lock(&out->lock);
1830 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1831 status = compress_pause(out->compr);
1832 out->offload_state = OFFLOAD_STATE_PAUSED;
1833 }
1834 pthread_mutex_unlock(&out->lock);
1835 }
1836 return status;
1837}
1838
1839static int out_resume(struct audio_stream_out* stream)
1840{
1841 struct stream_out *out = (struct stream_out *)stream;
1842 int status = -ENOSYS;
1843 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001844 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301845 ALOGD("copl(%x):resume compress driver", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001846 status = 0;
1847 pthread_mutex_lock(&out->lock);
1848 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1849 status = compress_resume(out->compr);
1850 out->offload_state = OFFLOAD_STATE_PLAYING;
1851 }
1852 pthread_mutex_unlock(&out->lock);
1853 }
1854 return status;
1855}
1856
1857static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1858{
1859 struct stream_out *out = (struct stream_out *)stream;
1860 int status = -ENOSYS;
1861 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001862 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001863 pthread_mutex_lock(&out->lock);
1864 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1865 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1866 else
1867 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1868 pthread_mutex_unlock(&out->lock);
1869 }
1870 return status;
1871}
1872
1873static int out_flush(struct audio_stream_out* stream)
1874{
1875 struct stream_out *out = (struct stream_out *)stream;
1876 ALOGV("%s", __func__);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07001877 if (is_offload_usecase(out->usecase)) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05301878 ALOGD("copl(%x):calling compress flush", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001879 pthread_mutex_lock(&out->lock);
1880 stop_compressed_output_l(out);
1881 pthread_mutex_unlock(&out->lock);
Sidipotu Ashok55820562014-02-10 16:16:38 +05301882 ALOGD("copl(%x):out of compress flush", (unsigned int)out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001883 return 0;
1884 }
1885 return -ENOSYS;
1886}
1887
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001888/** audio_stream_in implementation **/
1889static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1890{
1891 struct stream_in *in = (struct stream_in *)stream;
1892
1893 return in->config.rate;
1894}
1895
1896static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1897{
1898 return -ENOSYS;
1899}
1900
1901static size_t in_get_buffer_size(const struct audio_stream *stream)
1902{
1903 struct stream_in *in = (struct stream_in *)stream;
1904
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001905 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1906 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001907 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1908 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001909
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001910 return in->config.period_size * audio_stream_frame_size(stream);
1911}
1912
1913static uint32_t in_get_channels(const struct audio_stream *stream)
1914{
1915 struct stream_in *in = (struct stream_in *)stream;
1916
1917 return in->channel_mask;
1918}
1919
1920static audio_format_t in_get_format(const struct audio_stream *stream)
1921{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001922 struct stream_in *in = (struct stream_in *)stream;
1923
1924 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001925}
1926
1927static int in_set_format(struct audio_stream *stream, audio_format_t format)
1928{
1929 return -ENOSYS;
1930}
1931
1932static int in_standby(struct audio_stream *stream)
1933{
1934 struct stream_in *in = (struct stream_in *)stream;
1935 struct audio_device *adev = in->dev;
1936 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001937 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001938
1939 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1940 /* Ignore standby in case of voip call because the voip input
1941 * stream is closed in adev_close_input_stream()
1942 */
1943 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1944 return status;
1945 }
1946
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001947 pthread_mutex_lock(&in->lock);
1948 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001949 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001950 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001951 if (in->pcm) {
1952 pcm_close(in->pcm);
1953 in->pcm = NULL;
1954 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001955 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001956 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001957 }
1958 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001959 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001960 return status;
1961}
1962
1963static int in_dump(const struct audio_stream *stream, int fd)
1964{
1965 return 0;
1966}
1967
1968static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1969{
1970 struct stream_in *in = (struct stream_in *)stream;
1971 struct audio_device *adev = in->dev;
1972 struct str_parms *parms;
1973 char *str;
1974 char value[32];
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001975 int ret = 0, val = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001976
Eric Laurent994a6932013-07-17 11:51:42 -07001977 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001978 parms = str_parms_create_str(kvpairs);
1979
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001980 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001981 pthread_mutex_lock(&adev->lock);
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08001982
1983 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1984 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001985 val = atoi(value);
1986 /* no audio source uses val == 0 */
1987 if ((in->source != val) && (val != 0)) {
1988 in->source = val;
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08001989 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1990 (in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
1991 (voice_extn_compress_voip_is_format_supported(in->format)) &&
1992 (in->config.rate == 8000 || in->config.rate == 16000) &&
1993 (popcount(in->channel_mask) == 1)) {
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08001994 err = voice_extn_compress_voip_open_input_stream(in);
1995 if (err != 0) {
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08001996 ALOGE("%s: Compress voip input cannot be opened, error:%d",
Narsinga Rao Chella7d5a3e82014-02-04 16:23:52 -08001997 __func__, err);
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08001998 }
1999 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002000 }
2001 }
2002
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002003 err = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
2004 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002005 val = atoi(value);
2006 if ((in->device != val) && (val != 0)) {
2007 in->device = val;
2008 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002009 if (!in->standby)
2010 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002011 }
2012 }
2013
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002014done:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002015 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002016 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002017
2018 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002019 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002020 return ret;
2021}
2022
2023static char* in_get_parameters(const struct audio_stream *stream,
2024 const char *keys)
2025{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002026 struct stream_in *in = (struct stream_in *)stream;
2027 struct str_parms *query = str_parms_create_str(keys);
2028 char *str;
2029 char value[256];
2030 struct str_parms *reply = str_parms_create();
2031 ALOGV("%s: enter: keys - %s", __func__, keys);
2032
2033 voice_extn_in_get_parameters(in, query, reply);
2034
2035 str = str_parms_to_str(reply);
2036 str_parms_destroy(query);
2037 str_parms_destroy(reply);
2038
2039 ALOGV("%s: exit: returns - %s", __func__, str);
2040 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002041}
2042
2043static int in_set_gain(struct audio_stream_in *stream, float gain)
2044{
2045 return 0;
2046}
2047
2048static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
2049 size_t bytes)
2050{
2051 struct stream_in *in = (struct stream_in *)stream;
2052 struct audio_device *adev = in->dev;
2053 int i, ret = -1;
2054
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002055 pthread_mutex_lock(&in->lock);
2056 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002057 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002058 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
2059 ret = voice_extn_compress_voip_start_input_stream(in);
2060 else
2061 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08002062 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002063 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002064 goto exit;
2065 }
2066 in->standby = 0;
2067 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002068
2069 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002070 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
2071 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07002072 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
2073 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002074 else
2075 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002076 }
2077
2078 /*
2079 * Instead of writing zeroes here, we could trust the hardware
2080 * to always provide zeroes when muted.
2081 */
Helen Zenge0b186f2013-10-23 14:00:59 -07002082 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002083 memset(buffer, 0, bytes);
2084
2085exit:
2086 pthread_mutex_unlock(&in->lock);
2087
2088 if (ret != 0) {
2089 in_standby(&in->stream.common);
2090 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
2091 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
2092 in_get_sample_rate(&in->stream.common));
2093 }
2094 return bytes;
2095}
2096
2097static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
2098{
2099 return 0;
2100}
2101
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002102static int add_remove_audio_effect(const struct audio_stream *stream,
2103 effect_handle_t effect,
2104 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002105{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002106 struct stream_in *in = (struct stream_in *)stream;
2107 int status = 0;
2108 effect_descriptor_t desc;
2109
2110 status = (*effect)->get_descriptor(effect, &desc);
2111 if (status != 0)
2112 return status;
2113
2114 pthread_mutex_lock(&in->lock);
2115 pthread_mutex_lock(&in->dev->lock);
2116 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
2117 in->enable_aec != enable &&
2118 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
2119 in->enable_aec = enable;
2120 if (!in->standby)
2121 select_devices(in->dev, in->usecase);
2122 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08002123 if (in->enable_ns != enable &&
2124 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
2125 in->enable_ns = enable;
2126 if (!in->standby)
2127 select_devices(in->dev, in->usecase);
2128 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002129 pthread_mutex_unlock(&in->dev->lock);
2130 pthread_mutex_unlock(&in->lock);
2131
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002132 return 0;
2133}
2134
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002135static int in_add_audio_effect(const struct audio_stream *stream,
2136 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002137{
Eric Laurent994a6932013-07-17 11:51:42 -07002138 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002139 return add_remove_audio_effect(stream, effect, true);
2140}
2141
2142static int in_remove_audio_effect(const struct audio_stream *stream,
2143 effect_handle_t effect)
2144{
Eric Laurent994a6932013-07-17 11:51:42 -07002145 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07002146 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002147}
2148
2149static int adev_open_output_stream(struct audio_hw_device *dev,
2150 audio_io_handle_t handle,
2151 audio_devices_t devices,
2152 audio_output_flags_t flags,
2153 struct audio_config *config,
2154 struct audio_stream_out **stream_out)
2155{
2156 struct audio_device *adev = (struct audio_device *)dev;
2157 struct stream_out *out;
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002158 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002159
Eric Laurent994a6932013-07-17 11:51:42 -07002160 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002161 __func__, config->sample_rate, config->channel_mask, devices, flags);
2162 *stream_out = NULL;
2163 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2164
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002165 if (!out) {
2166 return -ENOMEM;
2167 }
2168
2169 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2170 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2171
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002172 if (devices == AUDIO_DEVICE_NONE)
2173 devices = AUDIO_DEVICE_OUT_SPEAKER;
2174
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002175 out->flags = flags;
2176 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002177 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002178 out->format = config->format;
2179 out->sample_rate = config->sample_rate;
2180 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2181 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002182 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002183
2184 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002185 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2186 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2187 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2188
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002189 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002190 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2191 ret = read_hdmi_channel_masks(out);
2192
2193 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2194 ret = audio_extn_read_afe_proxy_channel_masks(out);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002195 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002196 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002197 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002198
2199 if (config->sample_rate == 0)
2200 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2201 if (config->channel_mask == 0)
2202 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2203
2204 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002205 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002206 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2207 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002208 out->config.rate = config->sample_rate;
2209 out->config.channels = popcount(out->channel_mask);
2210 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002211 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2212 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002213 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002214 ret = voice_extn_compress_voip_open_output_stream(out);
2215 if (ret != 0) {
2216 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2217 __func__, ret);
2218 goto error_open;
2219 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002220 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
Sidipotu Ashok55820562014-02-10 16:16:38 +05302221 ALOGD("%s: copl(%x): sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
2222 __func__, (unsigned int)out, config->sample_rate, config->channel_mask, devices, flags);
2223
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002224 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2225 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2226 ALOGE("%s: Unsupported Offload information", __func__);
2227 ret = -EINVAL;
2228 goto error_open;
2229 }
Mingming Yin90310102013-11-13 16:57:00 -08002230 if (!is_supported_format(config->offload_info.format) &&
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002231 !audio_extn_is_dolby_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002232 ALOGE("%s: Unsupported audio format", __func__);
2233 ret = -EINVAL;
2234 goto error_open;
2235 }
2236
2237 out->compr_config.codec = (struct snd_codec *)
2238 calloc(1, sizeof(struct snd_codec));
2239
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002240 out->usecase = get_offload_usecase(adev);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002241 if (config->offload_info.channel_mask)
2242 out->channel_mask = config->offload_info.channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002243 else if (config->channel_mask) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002244 out->channel_mask = config->channel_mask;
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002245 config->offload_info.channel_mask = config->channel_mask;
2246 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002247 out->format = config->offload_info.format;
2248 out->sample_rate = config->offload_info.sample_rate;
2249
2250 out->stream.set_callback = out_set_callback;
2251 out->stream.pause = out_pause;
2252 out->stream.resume = out_resume;
2253 out->stream.drain = out_drain;
2254 out->stream.flush = out_flush;
2255
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002256 if (audio_extn_is_dolby_format(config->offload_info.format))
Mingming Yin90310102013-11-13 16:57:00 -08002257 out->compr_config.codec->id =
Subhash Chandra Bose Naripeddy7690c562013-12-14 00:34:53 -08002258 audio_extn_dolby_get_snd_codec_id(adev, out,
2259 config->offload_info.format);
Mingming Yin90310102013-11-13 16:57:00 -08002260 else
2261 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002262 get_snd_codec_id(config->offload_info.format);
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002263 if (audio_is_offload_pcm(config->offload_info.format)) {
2264 out->compr_config.fragment_size =
2265 platform_get_pcm_offload_buffer_size(&config->offload_info);
2266 } else {
2267 out->compr_config.fragment_size =
2268 platform_get_compress_offload_buffer_size(&config->offload_info);
2269 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002270 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2271 out->compr_config.codec->sample_rate =
2272 compress_get_alsa_rate(config->offload_info.sample_rate);
2273 out->compr_config.codec->bit_rate =
2274 config->offload_info.bit_rate;
2275 out->compr_config.codec->ch_in =
2276 popcount(config->channel_mask);
2277 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
ApurupaPattapu2e084df2013-12-18 15:47:59 -08002278 out->compr_config.codec->format = SND_AUDIOSTREAMFORMAT_RAW;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002279
ApurupaPattapuc6a3a9e2014-01-10 14:46:02 -08002280 if (config->offload_info.format == AUDIO_FORMAT_PCM_16_BIT_OFFLOAD)
2281 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S16_LE;
2282 else if(config->offload_info.format == AUDIO_FORMAT_PCM_24_BIT_OFFLOAD)
2283 out->compr_config.codec->format = SNDRV_PCM_FORMAT_S24_LE;
2284
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002285 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2286 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002287
2288 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002289 out->offload_state = OFFLOAD_STATE_IDLE;
2290 out->playback_started = 0;
2291
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002292 create_offload_callback_thread(out);
2293 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2294 __func__, config->offload_info.version,
2295 config->offload_info.bit_rate);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002296 //Decide if we need to use gapless mode by default
Krishnankutty Kolathappilly6d8788b2014-01-09 12:45:31 -08002297 check_and_set_gapless_mode(adev);
Krishnankutty Kolathappillyb165a8a2014-01-07 11:25:51 -08002298
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002299 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2300 ret = voice_check_and_set_incall_music_usecase(adev, out);
2301 if (ret != 0) {
2302 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2303 __func__, ret);
2304 goto error_open;
2305 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002306 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002307 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2308 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002309 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002310 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002311 /* primary path is the default path selected if no other outputs are available/suitable */
2312 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002313 out->config = pcm_config_deep_buffer;
2314 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002315 }
2316
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002317 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2318 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2319 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002320 if(adev->primary_output == NULL)
2321 adev->primary_output = out;
2322 else {
2323 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002324 ret = -EEXIST;
2325 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002326 }
2327 }
2328
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002329 /* Check if this usecase is already existing */
2330 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella7ce05352014-04-17 20:00:41 -07002331 if ((get_usecase_from_list(adev, out->usecase) != NULL) &&
2332 (out->usecase != USECASE_COMPRESS_VOIP_CALL)) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002333 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002334 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002335 ret = -EEXIST;
2336 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002337 }
2338 pthread_mutex_unlock(&adev->lock);
2339
2340 out->stream.common.get_sample_rate = out_get_sample_rate;
2341 out->stream.common.set_sample_rate = out_set_sample_rate;
2342 out->stream.common.get_buffer_size = out_get_buffer_size;
2343 out->stream.common.get_channels = out_get_channels;
2344 out->stream.common.get_format = out_get_format;
2345 out->stream.common.set_format = out_set_format;
2346 out->stream.common.standby = out_standby;
2347 out->stream.common.dump = out_dump;
2348 out->stream.common.set_parameters = out_set_parameters;
2349 out->stream.common.get_parameters = out_get_parameters;
2350 out->stream.common.add_audio_effect = out_add_audio_effect;
2351 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2352 out->stream.get_latency = out_get_latency;
2353 out->stream.set_volume = out_set_volume;
2354 out->stream.write = out_write;
2355 out->stream.get_render_position = out_get_render_position;
2356 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002357 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002358
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002359 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002360 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002361 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002362
2363 config->format = out->stream.common.get_format(&out->stream.common);
2364 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2365 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2366
2367 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002368 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002369 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002370
2371error_open:
2372 free(out);
2373 *stream_out = NULL;
2374 ALOGD("%s: exit: ret %d", __func__, ret);
2375 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002376}
2377
2378static void adev_close_output_stream(struct audio_hw_device *dev,
2379 struct audio_stream_out *stream)
2380{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002381 struct stream_out *out = (struct stream_out *)stream;
2382 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002383 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002384
Eric Laurent994a6932013-07-17 11:51:42 -07002385 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002386 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2387 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2388 if(ret != 0)
2389 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2390 __func__, ret);
2391 }
2392 else
2393 out_standby(&stream->common);
2394
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002395 if (is_offload_usecase(out->usecase)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002396 destroy_offload_callback_thread(out);
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002397 free_offload_usecase(adev, out->usecase);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002398 if (out->compr_config.codec != NULL)
2399 free(out->compr_config.codec);
2400 }
2401 pthread_cond_destroy(&out->cond);
2402 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002403 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002404 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002405}
2406
2407static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2408{
2409 struct audio_device *adev = (struct audio_device *)dev;
2410 struct str_parms *parms;
2411 char *str;
2412 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002413 int val;
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002414 int ret = 0, err;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002415
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002416 ALOGD("%s: enter: %s", __func__, kvpairs);
2417
2418 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002419 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002420
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002421 ret = voice_set_parameters(adev, parms);
2422 if (ret != 0)
2423 goto done;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002424
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002425 ret = platform_set_parameters(adev->platform, parms);
2426 if (ret != 0)
2427 goto done;
2428
2429 err = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2430 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002431 /* When set to false, HAL should disable EC and NS
2432 * But it is currently not supported.
2433 */
2434 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2435 adev->bluetooth_nrec = true;
2436 else
2437 adev->bluetooth_nrec = false;
2438 }
2439
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002440 err = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2441 if (err >= 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002442 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2443 adev->screen_off = false;
2444 else
2445 adev->screen_off = true;
2446 }
2447
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002448 err = str_parms_get_int(parms, "rotation", &val);
2449 if (err >= 0) {
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002450 bool reverse_speakers = false;
2451 switch(val) {
2452 // FIXME: note that the code below assumes that the speakers are in the correct placement
2453 // relative to the user when the device is rotated 90deg from its default rotation. This
2454 // assumption is device-specific, not platform-specific like this code.
2455 case 270:
2456 reverse_speakers = true;
2457 break;
2458 case 0:
2459 case 90:
2460 case 180:
2461 break;
2462 default:
2463 ALOGE("%s: unexpected rotation of %d", __func__, val);
2464 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002465 if (adev->speaker_lr_swap != reverse_speakers) {
2466 adev->speaker_lr_swap = reverse_speakers;
2467 // only update the selected device if there is active pcm playback
2468 struct audio_usecase *usecase;
2469 struct listnode *node;
2470 list_for_each(node, &adev->usecase_list) {
2471 usecase = node_to_item(node, struct audio_usecase, list);
2472 if (usecase->type == PCM_PLAYBACK) {
2473 select_devices(adev, usecase->id);
2474 break;
2475 }
2476 }
2477 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002478 }
2479
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002480 audio_extn_set_parameters(adev, parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002481
Shiv Maliyappanahalli3e064fd2013-12-16 15:54:40 -08002482done:
2483 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002484 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002485 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002486 return ret;
2487}
2488
2489static char* adev_get_parameters(const struct audio_hw_device *dev,
2490 const char *keys)
2491{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002492 struct audio_device *adev = (struct audio_device *)dev;
2493 struct str_parms *reply = str_parms_create();
2494 struct str_parms *query = str_parms_create_str(keys);
2495 char *str;
2496
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002497 pthread_mutex_lock(&adev->lock);
2498
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002499 audio_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahallif9308492013-12-12 12:18:09 -08002500 voice_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002501 platform_get_parameters(adev->platform, query, reply);
2502 str = str_parms_to_str(reply);
2503 str_parms_destroy(query);
2504 str_parms_destroy(reply);
2505
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002506 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002507 ALOGV("%s: exit: returns - %s", __func__, str);
2508 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002509}
2510
2511static int adev_init_check(const struct audio_hw_device *dev)
2512{
2513 return 0;
2514}
2515
2516static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2517{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002518 int ret;
2519 struct audio_device *adev = (struct audio_device *)dev;
2520 pthread_mutex_lock(&adev->lock);
2521 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002522 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002523 pthread_mutex_unlock(&adev->lock);
2524 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002525}
2526
2527static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2528{
2529 return -ENOSYS;
2530}
2531
2532static int adev_get_master_volume(struct audio_hw_device *dev,
2533 float *volume)
2534{
2535 return -ENOSYS;
2536}
2537
2538static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2539{
2540 return -ENOSYS;
2541}
2542
2543static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2544{
2545 return -ENOSYS;
2546}
2547
2548static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2549{
2550 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002551 pthread_mutex_lock(&adev->lock);
2552 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002553 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002554 adev->mode = mode;
2555 }
2556 pthread_mutex_unlock(&adev->lock);
2557 return 0;
2558}
2559
2560static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2561{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002562 int ret;
2563
2564 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002565 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002566 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2567 pthread_mutex_unlock(&adev->lock);
2568
2569 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002570}
2571
2572static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2573{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002574 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002575 return 0;
2576}
2577
2578static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2579 const struct audio_config *config)
2580{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002581 int channel_count = popcount(config->channel_mask);
2582
2583 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2584}
2585
2586static int adev_open_input_stream(struct audio_hw_device *dev,
2587 audio_io_handle_t handle,
2588 audio_devices_t devices,
2589 struct audio_config *config,
2590 struct audio_stream_in **stream_in)
2591{
2592 struct audio_device *adev = (struct audio_device *)dev;
2593 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002594 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002595 int channel_count = popcount(config->channel_mask);
2596
Eric Laurent994a6932013-07-17 11:51:42 -07002597 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002598 *stream_in = NULL;
2599 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2600 return -EINVAL;
2601
2602 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2603
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07002604 pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
2605
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002606 in->stream.common.get_sample_rate = in_get_sample_rate;
2607 in->stream.common.set_sample_rate = in_set_sample_rate;
2608 in->stream.common.get_buffer_size = in_get_buffer_size;
2609 in->stream.common.get_channels = in_get_channels;
2610 in->stream.common.get_format = in_get_format;
2611 in->stream.common.set_format = in_set_format;
2612 in->stream.common.standby = in_standby;
2613 in->stream.common.dump = in_dump;
2614 in->stream.common.set_parameters = in_set_parameters;
2615 in->stream.common.get_parameters = in_get_parameters;
2616 in->stream.common.add_audio_effect = in_add_audio_effect;
2617 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2618 in->stream.set_gain = in_set_gain;
2619 in->stream.read = in_read;
2620 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2621
2622 in->device = devices;
2623 in->source = AUDIO_SOURCE_DEFAULT;
2624 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002625 in->standby = 1;
2626 in->channel_mask = config->channel_mask;
2627
2628 /* Update config params with the requested sample rate and channels */
2629 in->usecase = USECASE_AUDIO_RECORD;
2630 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002631 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002632 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002633
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002634 if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002635 if(audio_extn_ssr_get_enabled()) {
2636 if(audio_extn_ssr_init(adev, in)) {
2637 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2638 ret = -EINVAL;
2639 goto err_open;
2640 }
2641 } else {
2642 ret = -EINVAL;
2643 goto err_open;
2644 }
Mingming Yine62d7842013-10-25 16:26:03 -07002645 } else if (audio_extn_compr_cap_enabled() &&
Narsinga Rao Chella2a99dea2014-01-24 15:33:23 -08002646 audio_extn_compr_cap_format_supported(config->format) &&
2647 (in->dev->mode != AUDIO_MODE_IN_COMMUNICATION)) {
Mingming Yine62d7842013-10-25 16:26:03 -07002648 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002649 } else {
2650 in->config.channels = channel_count;
2651 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2652 buffer_size = get_input_buffer_size(config->sample_rate,
2653 config->format,
2654 channel_count);
2655 in->config.period_size = buffer_size / frame_size;
2656 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002657
2658 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002659 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002660 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002661
2662err_open:
2663 free(in);
2664 *stream_in = NULL;
2665 return ret;
2666}
2667
2668static void adev_close_input_stream(struct audio_hw_device *dev,
2669 struct audio_stream_in *stream)
2670{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002671 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002672 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002673 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002674
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002675 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2676 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2677 if (ret != 0)
2678 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2679 __func__, ret);
2680 } else
2681 in_standby(&stream->common);
2682
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002683 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2684 audio_extn_ssr_deinit();
2685 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002686 free(stream);
2687
Mingming Yine62d7842013-10-25 16:26:03 -07002688 if(audio_extn_compr_cap_enabled() &&
2689 audio_extn_compr_cap_format_supported(in->config.format))
2690 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002691 return;
2692}
2693
2694static int adev_dump(const audio_hw_device_t *device, int fd)
2695{
2696 return 0;
2697}
2698
2699static int adev_close(hw_device_t *device)
2700{
2701 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002702
2703 if (!adev)
2704 return 0;
2705
2706 pthread_mutex_lock(&adev_init_lock);
2707
2708 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002709 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07002710 audio_route_free(adev->audio_route);
2711 free(adev->snd_dev_ref_cnt);
2712 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002713 free(device);
2714 adev = NULL;
2715 }
2716 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002717 return 0;
2718}
2719
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002720static int adev_open(const hw_module_t *module, const char *name,
2721 hw_device_t **device)
2722{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002723 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002724
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002725 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002726 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2727
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002728 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002729 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002730 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002731 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002732 ALOGD("%s: returning existing instance of adev", __func__);
2733 ALOGD("%s: exit", __func__);
2734 pthread_mutex_unlock(&adev_init_lock);
2735 return 0;
2736 }
2737
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002738 adev = calloc(1, sizeof(struct audio_device));
2739
Ravi Kumar Alamanda40703102014-04-24 10:34:41 -07002740 pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL);
2741
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002742 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2743 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2744 adev->device.common.module = (struct hw_module_t *)module;
2745 adev->device.common.close = adev_close;
2746
2747 adev->device.init_check = adev_init_check;
2748 adev->device.set_voice_volume = adev_set_voice_volume;
2749 adev->device.set_master_volume = adev_set_master_volume;
2750 adev->device.get_master_volume = adev_get_master_volume;
2751 adev->device.set_master_mute = adev_set_master_mute;
2752 adev->device.get_master_mute = adev_get_master_mute;
2753 adev->device.set_mode = adev_set_mode;
2754 adev->device.set_mic_mute = adev_set_mic_mute;
2755 adev->device.get_mic_mute = adev_get_mic_mute;
2756 adev->device.set_parameters = adev_set_parameters;
2757 adev->device.get_parameters = adev_get_parameters;
2758 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2759 adev->device.open_output_stream = adev_open_output_stream;
2760 adev->device.close_output_stream = adev_close_output_stream;
2761 adev->device.open_input_stream = adev_open_input_stream;
2762 adev->device.close_input_stream = adev_close_input_stream;
2763 adev->device.dump = adev_dump;
2764
2765 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002766 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002767 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002768 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002769 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002770 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002771 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002772 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002773 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002774 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002775 list_init(&adev->usecase_list);
Krishnankutty Kolathappilly0b2de1c2014-02-14 14:45:49 -08002776 adev->cur_wfd_channels = 2;
Subhash Chandra Bose Naripeddy16ff4f82014-04-01 21:03:10 -07002777 adev->offload_usecases_state = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002778 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002779 adev->platform = platform_init(adev);
2780 if (!adev->platform) {
2781 free(adev->snd_dev_ref_cnt);
2782 free(adev);
2783 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2784 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08002785 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07002786 return -EINVAL;
2787 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002788
2789 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2790 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2791 if (adev->visualizer_lib == NULL) {
2792 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2793 } else {
2794 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2795 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002796 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002797 "visualizer_hal_start_output");
2798 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002799 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002800 "visualizer_hal_stop_output");
2801 }
2802 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08002803 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07002804
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002805 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2806 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2807 if (adev->offload_effects_lib == NULL) {
2808 ALOGE("%s: DLOPEN failed for %s", __func__,
2809 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2810 } else {
2811 ALOGV("%s: DLOPEN successful for %s", __func__,
2812 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2813 adev->offload_effects_start_output =
2814 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2815 "offload_effects_bundle_hal_start_output");
2816 adev->offload_effects_stop_output =
2817 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2818 "offload_effects_bundle_hal_stop_output");
2819 }
2820 }
2821
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002822 *device = &adev->device.common;
2823
Kiran Kandi910e1862013-10-29 13:29:42 -07002824 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002825 pthread_mutex_unlock(&adev_init_lock);
2826
Eric Laurent994a6932013-07-17 11:51:42 -07002827 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002828 return 0;
2829}
2830
2831static struct hw_module_methods_t hal_module_methods = {
2832 .open = adev_open,
2833};
2834
2835struct audio_module HAL_MODULE_INFO_SYM = {
2836 .common = {
2837 .tag = HARDWARE_MODULE_TAG,
2838 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2839 .hal_api_version = HARDWARE_HAL_API_VERSION,
2840 .id = AUDIO_HARDWARE_MODULE_ID,
2841 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08002842 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002843 .methods = &hal_module_methods,
2844 },
2845};