blob: da128b3f482bc0b23d8c986f1e60c56f9e3a1e42 [file] [log] [blame]
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001/*
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
4 *
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08005 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_primary"
21/*#define LOG_NDEBUG 0*/
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070022/*#define VERY_VERY_VERBOSE_LOGGING*/
23#ifdef VERY_VERY_VERBOSE_LOGGING
24#define ALOGVV ALOGV
25#else
26#define ALOGVV(a...) do { } while(0)
27#endif
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080028
29#include <errno.h>
30#include <pthread.h>
31#include <stdint.h>
32#include <sys/time.h>
33#include <stdlib.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080034#include <math.h>
Eric Laurentc4aef752013-09-12 17:45:53 -070035#include <dlfcn.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070036#include <sys/resource.h>
37#include <sys/prctl.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080038
39#include <cutils/log.h>
40#include <cutils/str_parms.h>
41#include <cutils/properties.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070042#include <cutils/atomic.h>
43#include <cutils/sched_policy.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080044
Eric Laurentb23d5282013-05-14 15:27:20 -070045#include <hardware/audio_effect.h>
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070046#include <system/thread_defs.h>
Eric Laurentb23d5282013-05-14 15:27:20 -070047#include <audio_effects/effect_aec.h>
48#include <audio_effects/effect_ns.h>
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080049#include "audio_hw.h"
Eric Laurentb23d5282013-05-14 15:27:20 -070050#include "platform_api.h"
51#include <platform.h>
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -070052#include "audio_extn.h"
Narsinga Rao Chella05573b72013-11-15 15:21:40 -080053#include "voice_extn.h"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080054
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070055#include "sound/compress_params.h"
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -080056#define MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024)
57#define MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE (8 * 1024)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070058#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
59#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
60/* ToDo: Check and update a proper value in msec */
61#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
62#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
63
Haynes Mathew Georgebf143712013-12-03 13:02:53 -080064#define USECASE_AUDIO_PLAYBACK_PRIMARY USECASE_AUDIO_PLAYBACK_DEEP_BUFFER
65
Eric Laurentb23d5282013-05-14 15:27:20 -070066struct pcm_config pcm_config_deep_buffer = {
67 .channels = 2,
68 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
69 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
70 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
71 .format = PCM_FORMAT_S16_LE,
72 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
73 .stop_threshold = INT_MAX,
74 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
75};
76
77struct pcm_config pcm_config_low_latency = {
78 .channels = 2,
79 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
80 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
81 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
82 .format = PCM_FORMAT_S16_LE,
83 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
84 .stop_threshold = INT_MAX,
85 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
86};
87
88struct pcm_config pcm_config_hdmi_multi = {
89 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
90 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
91 .period_size = HDMI_MULTI_PERIOD_SIZE,
92 .period_count = HDMI_MULTI_PERIOD_COUNT,
93 .format = PCM_FORMAT_S16_LE,
94 .start_threshold = 0,
95 .stop_threshold = INT_MAX,
96 .avail_min = 0,
97};
98
99struct pcm_config pcm_config_audio_capture = {
100 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -0700101 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
102 .format = PCM_FORMAT_S16_LE,
103};
104
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800105const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700106 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
107 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
108 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700109 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700110 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700111 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700112 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700113 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700114 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800115 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Vimal Puthanveed47e64852013-12-20 13:23:39 -0800116 [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700117 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700118
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700119 [USECASE_VOICE2_CALL] = "voice2-call",
120 [USECASE_VOLTE_CALL] = "volte-call",
121 [USECASE_QCHAT_CALL] = "qchat-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800122 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700123 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
124 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
125 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Helen Zenge56b4852013-12-03 16:54:40 -0800126 [USECASE_INCALL_REC_UPLINK_COMPRESS] = "incall-rec-uplink-compress",
127 [USECASE_INCALL_REC_DOWNLINK_COMPRESS] = "incall-rec-downlink-compress",
128 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK_COMPRESS] = "incall-rec-uplink-and-downlink-compress",
129
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700130 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
131 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700132 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
133 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700134};
135
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800136
137#define STRING_TO_ENUM(string) { #string, string }
138
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800139struct string_to_enum {
140 const char *name;
141 uint32_t value;
142};
143
144static const struct string_to_enum out_channels_name_to_enum_table[] = {
145 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
146 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
147 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
148};
149
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700150static struct audio_device *adev = NULL;
151static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700152static unsigned int audio_device_ref_count;
153
Haynes Mathew George5191a852013-09-11 14:19:36 -0700154static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -0800155static uint32_t get_offload_buffer_size();
Haynes Mathew George5191a852013-09-11 14:19:36 -0700156
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700157static bool is_supported_format(audio_format_t format)
158{
Eric Laurent86e17132013-09-12 17:49:30 -0700159 if (format == AUDIO_FORMAT_MP3 ||
160 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700161 return true;
162
163 return false;
164}
165
166static int get_snd_codec_id(audio_format_t format)
167{
168 int id = 0;
169
170 switch (format) {
171 case AUDIO_FORMAT_MP3:
172 id = SND_AUDIOCODEC_MP3;
173 break;
174 case AUDIO_FORMAT_AAC:
175 id = SND_AUDIOCODEC_AAC;
176 break;
177 default:
Mingming Yin90310102013-11-13 16:57:00 -0800178 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700179 }
180
181 return id;
182}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800183
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700184int enable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700185 struct audio_usecase *usecase,
186 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800187{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700188 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700189 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800190
191 if (usecase == NULL)
192 return -EINVAL;
193
194 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
195
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800196 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700197 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800198 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700199 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800200
201 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700202 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700203 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700204 audio_route_apply_path(adev->audio_route, mixer_path);
205 if (update_mixer)
206 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800207
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800208 ALOGV("%s: exit", __func__);
209 return 0;
210}
211
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700212int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700213 struct audio_usecase *usecase,
214 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800215{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700216 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700217 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800218
219 if (usecase == NULL)
220 return -EINVAL;
221
222 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700223 if (usecase->type == PCM_CAPTURE)
224 snd_device = usecase->in_snd_device;
225 else
226 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800227 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700228 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700229 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700230 audio_route_reset_path(adev->audio_route, mixer_path);
231 if (update_mixer)
232 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800233
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800234 ALOGV("%s: exit", __func__);
235 return 0;
236}
237
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700238int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700239 snd_device_t snd_device,
240 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800241{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700242 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
243
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800244 if (snd_device < SND_DEVICE_MIN ||
245 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800246 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800247 return -EINVAL;
248 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249
250 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700251
252 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
253 ALOGE("%s: Invalid sound device returned", __func__);
254 return -EINVAL;
255 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700256 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700257 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700258 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700259 return 0;
260 }
261
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700262 /* start usb playback thread */
263 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
264 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
265 audio_extn_usb_start_playback(adev);
266
267 /* start usb capture thread */
268 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
269 audio_extn_usb_start_capture(adev);
270
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700271 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
272 audio_extn_spkr_prot_is_enabled()) {
273 if (audio_extn_spkr_prot_start_processing(snd_device)) {
274 ALOGE("%s: spkr_start_processing failed", __func__);
275 return -EINVAL;
276 }
277 } else {
278 ALOGV("%s: snd_device(%d: %s)", __func__,
279 snd_device, device_name);
280 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
281 adev->snd_dev_ref_cnt[snd_device]--;
282 return -EINVAL;
283 }
Kiran Kandide144c82013-11-20 15:58:32 -0800284 audio_extn_listen_update_status(snd_device,
285 LISTEN_EVENT_SND_DEVICE_BUSY);
286
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700287 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800288 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700289 if (update_mixer)
290 audio_route_update_mixer(adev->audio_route);
291
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800292 return 0;
293}
294
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700295int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700296 snd_device_t snd_device,
297 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800298{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700299 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
300
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800301 if (snd_device < SND_DEVICE_MIN ||
302 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800303 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800304 return -EINVAL;
305 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700306 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
307 ALOGE("%s: device ref cnt is already 0", __func__);
308 return -EINVAL;
309 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700310
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700311 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700312
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700313 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
314 ALOGE("%s: Invalid sound device returned", __func__);
315 return -EINVAL;
316 }
317
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700318 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700319 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700320 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800321 /* exit usb play back thread */
322 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
323 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
324 audio_extn_usb_stop_playback();
325
326 /* exit usb capture thread */
327 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
328 audio_extn_usb_stop_capture(adev);
329
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700330 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
331 audio_extn_spkr_prot_is_enabled()) {
332 audio_extn_spkr_prot_stop_processing();
333 } else
334 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800335
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700336 if (update_mixer)
337 audio_route_update_mixer(adev->audio_route);
Kiran Kandide144c82013-11-20 15:58:32 -0800338
339 audio_extn_listen_update_status(snd_device,
340 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700341 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700342
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800343 return 0;
344}
345
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700346static void check_usecases_codec_backend(struct audio_device *adev,
347 struct audio_usecase *uc_info,
348 snd_device_t snd_device)
349{
350 struct listnode *node;
351 struct audio_usecase *usecase;
352 bool switch_device[AUDIO_USECASE_MAX];
353 int i, num_uc_to_switch = 0;
354
355 /*
356 * This function is to make sure that all the usecases that are active on
357 * the hardware codec backend are always routed to any one device that is
358 * handled by the hardware codec.
359 * For example, if low-latency and deep-buffer usecases are currently active
360 * on speaker and out_set_parameters(headset) is received on low-latency
361 * output, then we have to make sure deep-buffer is also switched to headset,
362 * because of the limitation that both the devices cannot be enabled
363 * at the same time as they share the same backend.
364 */
365 /* Disable all the usecases on the shared backend other than the
366 specified usecase */
367 for (i = 0; i < AUDIO_USECASE_MAX; i++)
368 switch_device[i] = false;
369
370 list_for_each(node, &adev->usecase_list) {
371 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700372 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700373 usecase != uc_info &&
374 usecase->out_snd_device != snd_device &&
375 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
376 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
377 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700378 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700379 disable_audio_route(adev, usecase, false);
380 switch_device[usecase->id] = true;
381 num_uc_to_switch++;
382 }
383 }
384
385 if (num_uc_to_switch) {
386 /* Make sure all the streams are de-routed before disabling the device */
387 audio_route_update_mixer(adev->audio_route);
388
389 list_for_each(node, &adev->usecase_list) {
390 usecase = node_to_item(node, struct audio_usecase, list);
391 if (switch_device[usecase->id]) {
392 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700393 }
394 }
395
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700396 list_for_each(node, &adev->usecase_list) {
397 usecase = node_to_item(node, struct audio_usecase, list);
398 if (switch_device[usecase->id]) {
399 enable_snd_device(adev, snd_device, false);
400 }
401 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700402
403 /* Make sure new snd device is enabled before re-routing the streams */
404 audio_route_update_mixer(adev->audio_route);
405
406 /* Re-route all the usecases on the shared backend other than the
407 specified usecase to new snd devices */
408 list_for_each(node, &adev->usecase_list) {
409 usecase = node_to_item(node, struct audio_usecase, list);
410 /* Update the out_snd_device only before enabling the audio route */
411 if (switch_device[usecase->id] ) {
412 usecase->out_snd_device = snd_device;
413 enable_audio_route(adev, usecase, false);
414 }
415 }
416
417 audio_route_update_mixer(adev->audio_route);
418 }
419}
420
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700421static void check_and_route_capture_usecases(struct audio_device *adev,
422 struct audio_usecase *uc_info,
423 snd_device_t snd_device)
424{
425 struct listnode *node;
426 struct audio_usecase *usecase;
427 bool switch_device[AUDIO_USECASE_MAX];
428 int i, num_uc_to_switch = 0;
429
430 /*
431 * This function is to make sure that all the active capture usecases
432 * are always routed to the same input sound device.
433 * For example, if audio-record and voice-call usecases are currently
434 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
435 * is received for voice call then we have to make sure that audio-record
436 * usecase is also switched to earpiece i.e. voice-dmic-ef,
437 * because of the limitation that two devices cannot be enabled
438 * at the same time if they share the same backend.
439 */
440 for (i = 0; i < AUDIO_USECASE_MAX; i++)
441 switch_device[i] = false;
442
443 list_for_each(node, &adev->usecase_list) {
444 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700445 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700446 usecase != uc_info &&
447 usecase->in_snd_device != snd_device) {
448 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
449 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700450 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700451 disable_audio_route(adev, usecase, false);
452 switch_device[usecase->id] = true;
453 num_uc_to_switch++;
454 }
455 }
456
457 if (num_uc_to_switch) {
458 /* Make sure all the streams are de-routed before disabling the device */
459 audio_route_update_mixer(adev->audio_route);
460
461 list_for_each(node, &adev->usecase_list) {
462 usecase = node_to_item(node, struct audio_usecase, list);
463 if (switch_device[usecase->id]) {
464 disable_snd_device(adev, usecase->in_snd_device, false);
465 enable_snd_device(adev, snd_device, false);
466 }
467 }
468
469 /* Make sure new snd device is enabled before re-routing the streams */
470 audio_route_update_mixer(adev->audio_route);
471
472 /* Re-route all the usecases on the shared backend other than the
473 specified usecase to new snd devices */
474 list_for_each(node, &adev->usecase_list) {
475 usecase = node_to_item(node, struct audio_usecase, list);
476 /* Update the in_snd_device only before enabling the audio route */
477 if (switch_device[usecase->id] ) {
478 usecase->in_snd_device = snd_device;
479 enable_audio_route(adev, usecase, false);
480 }
481 }
482
483 audio_route_update_mixer(adev->audio_route);
484 }
485}
486
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700487static int disable_all_usecases_of_type(struct audio_device *adev,
488 usecase_type_t usecase_type,
489 bool update_mixer)
490{
491 struct audio_usecase *usecase;
492 struct listnode *node;
493 int ret = 0;
494
495 list_for_each(node, &adev->usecase_list) {
496 usecase = node_to_item(node, struct audio_usecase, list);
497 if (usecase->type == usecase_type) {
498 ALOGV("%s: usecase id %d", __func__, usecase->id);
499 ret = disable_audio_route(adev, usecase, update_mixer);
500 if (ret) {
501 ALOGE("%s: Failed to disable usecase id %d",
502 __func__, usecase->id);
503 }
504 }
505 }
506
507 return ret;
508}
509
510static int enable_all_usecases_of_type(struct audio_device *adev,
511 usecase_type_t usecase_type,
512 bool update_mixer)
513{
514 struct audio_usecase *usecase;
515 struct listnode *node;
516 int ret = 0;
517
518 list_for_each(node, &adev->usecase_list) {
519 usecase = node_to_item(node, struct audio_usecase, list);
520 if (usecase->type == usecase_type) {
521 ALOGV("%s: usecase id %d", __func__, usecase->id);
522 ret = enable_audio_route(adev, usecase, update_mixer);
523 if (ret) {
524 ALOGE("%s: Failed to enable usecase id %d",
525 __func__, usecase->id);
526 }
527 }
528 }
529
530 return ret;
531}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800532
533/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700534static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800535{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700536 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700537 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800538
539 switch (channels) {
540 /*
541 * Do not handle stereo output in Multi-channel cases
542 * Stereo case is handled in normal playback path
543 */
544 case 6:
545 ALOGV("%s: HDMI supports 5.1", __func__);
546 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
547 break;
548 case 8:
549 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
550 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
551 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
552 break;
553 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700554 ALOGE("HDMI does not support multi channel playback");
555 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800556 break;
557 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700558 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800559}
560
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700561static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
562{
563 struct audio_usecase *usecase;
564 struct listnode *node;
565
566 list_for_each(node, &adev->usecase_list) {
567 usecase = node_to_item(node, struct audio_usecase, list);
568 if (usecase->type == VOICE_CALL) {
569 ALOGV("%s: usecase id %d", __func__, usecase->id);
570 return usecase->id;
571 }
572 }
573 return USECASE_INVALID;
574}
575
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700576struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700577 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700578{
579 struct audio_usecase *usecase;
580 struct listnode *node;
581
582 list_for_each(node, &adev->usecase_list) {
583 usecase = node_to_item(node, struct audio_usecase, list);
584 if (usecase->id == uc_id)
585 return usecase;
586 }
587 return NULL;
588}
589
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700590int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800591{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800592 snd_device_t out_snd_device = SND_DEVICE_NONE;
593 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700594 struct audio_usecase *usecase = NULL;
595 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800596 struct audio_usecase *voip_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800597 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700598 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800599
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700600 usecase = get_usecase_from_list(adev, uc_id);
601 if (usecase == NULL) {
602 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
603 return -EINVAL;
604 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800605
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800606 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800607 (usecase->type == VOIP_CALL) ||
608 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700609 out_snd_device = platform_get_output_snd_device(adev->platform,
610 usecase->stream.out->devices);
611 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700612 usecase->devices = usecase->stream.out->devices;
613 } else {
614 /*
615 * If the voice call is active, use the sound devices of voice call usecase
616 * so that it would not result any device switch. All the usecases will
617 * be switched to new device when select_devices() is called for voice call
618 * usecase. This is to avoid switching devices for voice call when
619 * check_usecases_codec_backend() is called below.
620 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700621 if (voice_is_in_call(adev)) {
622 vc_usecase = get_usecase_from_list(adev,
623 get_voice_usecase_id_from_list(adev));
Helen Zeng067b96b2013-11-26 12:10:29 -0800624 if ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
625 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700626 in_snd_device = vc_usecase->in_snd_device;
627 out_snd_device = vc_usecase->out_snd_device;
628 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800629 } else if (voice_extn_compress_voip_is_active(adev)) {
630 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
631 if (voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
632 in_snd_device = voip_usecase->in_snd_device;
633 out_snd_device = voip_usecase->out_snd_device;
634 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700635 }
636 if (usecase->type == PCM_PLAYBACK) {
637 usecase->devices = usecase->stream.out->devices;
638 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700639 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700640 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700641 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700642 if (usecase->stream.out == adev->primary_output &&
643 adev->active_input &&
644 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
645 select_devices(adev, adev->active_input->usecase);
646 }
647 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700648 } else if (usecase->type == PCM_CAPTURE) {
649 usecase->devices = usecase->stream.in->device;
650 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700651 if (in_snd_device == SND_DEVICE_NONE) {
652 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
653 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700654 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700655 adev->primary_output->devices);
656 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700657 in_snd_device = platform_get_input_snd_device(adev->platform,
658 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700659 }
660 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700661 }
662 }
663
664 if (out_snd_device == usecase->out_snd_device &&
665 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800666 return 0;
667 }
668
sangwoobc677242013-08-08 16:53:43 +0900669 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700670 out_snd_device, platform_get_snd_device_name(out_snd_device),
671 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800672
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800673 /*
674 * Limitation: While in call, to do a device switch we need to disable
675 * and enable both RX and TX devices though one of them is same as current
676 * device.
677 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800678 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700679 status = platform_switch_voice_call_device_pre(adev->platform);
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800680 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800681 }
682
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700683 /* Disable current sound devices */
684 if (usecase->out_snd_device != SND_DEVICE_NONE) {
685 disable_audio_route(adev, usecase, true);
686 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800687 }
688
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700689 if (usecase->in_snd_device != SND_DEVICE_NONE) {
690 disable_audio_route(adev, usecase, true);
691 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800692 }
693
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700694 /* Enable new sound devices */
695 if (out_snd_device != SND_DEVICE_NONE) {
696 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
697 check_usecases_codec_backend(adev, usecase, out_snd_device);
698 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800699 }
700
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700701 if (in_snd_device != SND_DEVICE_NONE) {
702 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700703 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700704 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700705
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800706 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700707 status = platform_switch_voice_call_device_post(adev->platform,
708 out_snd_device,
709 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800710
sangwoo170731f2013-06-08 15:36:36 +0900711 audio_route_update_mixer(adev->audio_route);
712
713 usecase->in_snd_device = in_snd_device;
714 usecase->out_snd_device = out_snd_device;
715
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800716 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
717 enable_all_usecases_of_type(adev, usecase->type, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700718 else
719 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900720
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800721 /* Applicable only on the targets that has external modem.
722 * Enable device command should be sent to modem only after
723 * enabling voice call mixer controls
724 */
725 if (usecase->type == VOICE_CALL)
726 status = platform_switch_voice_call_usecase_route_post(adev->platform,
727 out_snd_device,
728 in_snd_device);
729
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800730 return status;
731}
732
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800733static int stop_input_stream(struct stream_in *in)
734{
735 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800736 struct audio_usecase *uc_info;
737 struct audio_device *adev = in->dev;
738
Eric Laurentc8400632013-02-14 19:04:54 -0800739 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800740
Eric Laurent994a6932013-07-17 11:51:42 -0700741 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700742 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800743 uc_info = get_usecase_from_list(adev, in->usecase);
744 if (uc_info == NULL) {
745 ALOGE("%s: Could not find the usecase (%d) in the list",
746 __func__, in->usecase);
747 return -EINVAL;
748 }
749
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800750 /* Close in-call recording streams */
751 voice_check_and_stop_incall_rec_usecase(adev, in);
752
Eric Laurent150dbfe2013-02-27 14:31:02 -0800753 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700754 disable_audio_route(adev, uc_info, true);
755
756 /* 2. Disable the tx device */
757 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800758
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800759 list_remove(&uc_info->list);
760 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800761
Eric Laurent994a6932013-07-17 11:51:42 -0700762 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800763 return ret;
764}
765
766int start_input_stream(struct stream_in *in)
767{
768 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800769 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800770 struct audio_usecase *uc_info;
771 struct audio_device *adev = in->dev;
772
Mingming Yine62d7842013-10-25 16:26:03 -0700773 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Eric Laurent994a6932013-07-17 11:51:42 -0700774 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700775
776 /* Check if source matches incall recording usecase criteria */
777 ret = voice_check_and_set_incall_rec_usecase(adev, in);
778 if (ret)
779 goto error_config;
780 else
781 ALOGV("%s: usecase(%d)", __func__, in->usecase);
782
Eric Laurentb23d5282013-05-14 15:27:20 -0700783 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800784 if (in->pcm_device_id < 0) {
785 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
786 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800787 ret = -EINVAL;
788 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800789 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700790
791 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800792 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
793 uc_info->id = in->usecase;
794 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800795 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700796 uc_info->devices = in->device;
797 uc_info->in_snd_device = SND_DEVICE_NONE;
798 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800799
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800800 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700801 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800802
Eric Laurentc8400632013-02-14 19:04:54 -0800803 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -0800804 __func__, adev->snd_card,
805 in->pcm_device_id, in->config.channels);
806 in->pcm = pcm_open(adev->snd_card,
807 in->pcm_device_id, PCM_IN, &in->config);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800808 if (in->pcm && !pcm_is_ready(in->pcm)) {
809 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
810 pcm_close(in->pcm);
811 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800812 ret = -EIO;
813 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800814 }
Eric Laurent994a6932013-07-17 11:51:42 -0700815 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800816 return ret;
817
818error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800819 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800820
821error_config:
822 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700823 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800824
825 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800826}
827
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700828/* must be called with out->lock locked */
829static int send_offload_cmd_l(struct stream_out* out, int command)
830{
831 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
832
833 ALOGVV("%s %d", __func__, command);
834
835 cmd->cmd = command;
836 list_add_tail(&out->offload_cmd_list, &cmd->node);
837 pthread_cond_signal(&out->offload_cond);
838 return 0;
839}
840
841/* must be called iwth out->lock locked */
842static void stop_compressed_output_l(struct stream_out *out)
843{
844 out->offload_state = OFFLOAD_STATE_IDLE;
845 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700846 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700847 if (out->compr != NULL) {
848 compress_stop(out->compr);
849 while (out->offload_thread_blocked) {
850 pthread_cond_wait(&out->cond, &out->lock);
851 }
852 }
853}
854
855static void *offload_thread_loop(void *context)
856{
857 struct stream_out *out = (struct stream_out *) context;
858 struct listnode *item;
859
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700860 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
861 set_sched_policy(0, SP_FOREGROUND);
862 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
863
864 ALOGV("%s", __func__);
865 pthread_mutex_lock(&out->lock);
866 for (;;) {
867 struct offload_cmd *cmd = NULL;
868 stream_callback_event_t event;
869 bool send_callback = false;
870
871 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
872 __func__, list_empty(&out->offload_cmd_list),
873 out->offload_state);
874 if (list_empty(&out->offload_cmd_list)) {
875 ALOGV("%s SLEEPING", __func__);
876 pthread_cond_wait(&out->offload_cond, &out->lock);
877 ALOGV("%s RUNNING", __func__);
878 continue;
879 }
880
881 item = list_head(&out->offload_cmd_list);
882 cmd = node_to_item(item, struct offload_cmd, node);
883 list_remove(item);
884
885 ALOGVV("%s STATE %d CMD %d out->compr %p",
886 __func__, out->offload_state, cmd->cmd, out->compr);
887
888 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
889 free(cmd);
890 break;
891 }
892
893 if (out->compr == NULL) {
894 ALOGE("%s: Compress handle is NULL", __func__);
895 pthread_cond_signal(&out->cond);
896 continue;
897 }
898 out->offload_thread_blocked = true;
899 pthread_mutex_unlock(&out->lock);
900 send_callback = false;
901 switch(cmd->cmd) {
902 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
903 compress_wait(out->compr, -1);
904 send_callback = true;
905 event = STREAM_CBK_EVENT_WRITE_READY;
906 break;
907 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700908 compress_next_track(out->compr);
909 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700910 send_callback = true;
911 event = STREAM_CBK_EVENT_DRAIN_READY;
912 break;
913 case OFFLOAD_CMD_DRAIN:
914 compress_drain(out->compr);
915 send_callback = true;
916 event = STREAM_CBK_EVENT_DRAIN_READY;
917 break;
918 default:
919 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
920 break;
921 }
922 pthread_mutex_lock(&out->lock);
923 out->offload_thread_blocked = false;
924 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700925 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700926 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700927 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700928 free(cmd);
929 }
930
931 pthread_cond_signal(&out->cond);
932 while (!list_empty(&out->offload_cmd_list)) {
933 item = list_head(&out->offload_cmd_list);
934 list_remove(item);
935 free(node_to_item(item, struct offload_cmd, node));
936 }
937 pthread_mutex_unlock(&out->lock);
938
939 return NULL;
940}
941
942static int create_offload_callback_thread(struct stream_out *out)
943{
944 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
945 list_init(&out->offload_cmd_list);
946 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
947 offload_thread_loop, out);
948 return 0;
949}
950
951static int destroy_offload_callback_thread(struct stream_out *out)
952{
953 pthread_mutex_lock(&out->lock);
954 stop_compressed_output_l(out);
955 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
956
957 pthread_mutex_unlock(&out->lock);
958 pthread_join(out->offload_thread, (void **) NULL);
959 pthread_cond_destroy(&out->offload_cond);
960
961 return 0;
962}
963
Eric Laurent07eeafd2013-10-06 12:52:49 -0700964static bool allow_hdmi_channel_config(struct audio_device *adev)
965{
966 struct listnode *node;
967 struct audio_usecase *usecase;
968 bool ret = true;
969
970 list_for_each(node, &adev->usecase_list) {
971 usecase = node_to_item(node, struct audio_usecase, list);
972 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
973 /*
974 * If voice call is already existing, do not proceed further to avoid
975 * disabling/enabling both RX and TX devices, CSD calls, etc.
976 * Once the voice call done, the HDMI channels can be configured to
977 * max channels of remaining use cases.
978 */
979 if (usecase->id == USECASE_VOICE_CALL) {
980 ALOGD("%s: voice call is active, no change in HDMI channels",
981 __func__);
982 ret = false;
983 break;
984 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
985 ALOGD("%s: multi channel playback is active, "
986 "no change in HDMI channels", __func__);
987 ret = false;
988 break;
989 }
990 }
991 }
992 return ret;
993}
994
995static int check_and_set_hdmi_channels(struct audio_device *adev,
996 unsigned int channels)
997{
998 struct listnode *node;
999 struct audio_usecase *usecase;
1000
1001 /* Check if change in HDMI channel config is allowed */
1002 if (!allow_hdmi_channel_config(adev))
1003 return 0;
1004
1005 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001006 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001007 return 0;
1008 }
1009
1010 platform_set_hdmi_channels(adev->platform, channels);
1011 adev->cur_hdmi_channels = channels;
1012
1013 /*
1014 * Deroute all the playback streams routed to HDMI so that
1015 * the back end is deactivated. Note that backend will not
1016 * be deactivated if any one stream is connected to it.
1017 */
1018 list_for_each(node, &adev->usecase_list) {
1019 usecase = node_to_item(node, struct audio_usecase, list);
1020 if (usecase->type == PCM_PLAYBACK &&
1021 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1022 disable_audio_route(adev, usecase, true);
1023 }
1024 }
1025
1026 /*
1027 * Enable all the streams disabled above. Now the HDMI backend
1028 * will be activated with new channel configuration
1029 */
1030 list_for_each(node, &adev->usecase_list) {
1031 usecase = node_to_item(node, struct audio_usecase, list);
1032 if (usecase->type == PCM_PLAYBACK &&
1033 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1034 enable_audio_route(adev, usecase, true);
1035 }
1036 }
1037
1038 return 0;
1039}
1040
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001041static int stop_output_stream(struct stream_out *out)
1042{
1043 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001044 struct audio_usecase *uc_info;
1045 struct audio_device *adev = out->dev;
1046
Eric Laurent994a6932013-07-17 11:51:42 -07001047 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001048 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001049 uc_info = get_usecase_from_list(adev, out->usecase);
1050 if (uc_info == NULL) {
1051 ALOGE("%s: Could not find the usecase (%d) in the list",
1052 __func__, out->usecase);
1053 return -EINVAL;
1054 }
1055
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001056 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1057 if (adev->visualizer_stop_output != NULL)
1058 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1059 if (adev->offload_effects_stop_output != NULL)
1060 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1061 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001062
Eric Laurent150dbfe2013-02-27 14:31:02 -08001063 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001064 disable_audio_route(adev, uc_info, true);
1065
1066 /* 2. Disable the rx device */
1067 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001068
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001069 list_remove(&uc_info->list);
1070 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001071
Eric Laurent07eeafd2013-10-06 12:52:49 -07001072 /* Must be called after removing the usecase from list */
1073 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1074 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1075
Eric Laurent994a6932013-07-17 11:51:42 -07001076 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001077 return ret;
1078}
1079
1080int start_output_stream(struct stream_out *out)
1081{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001082 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001083 struct audio_usecase *uc_info;
1084 struct audio_device *adev = out->dev;
1085
Eric Laurent994a6932013-07-17 11:51:42 -07001086 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001087 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001088 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001089 if (out->pcm_device_id < 0) {
1090 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1091 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001092 ret = -EINVAL;
1093 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001094 }
1095
1096 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1097 uc_info->id = out->usecase;
1098 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001099 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001100 uc_info->devices = out->devices;
1101 uc_info->in_snd_device = SND_DEVICE_NONE;
1102 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001103
Eric Laurent07eeafd2013-10-06 12:52:49 -07001104 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001105 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1106 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1107 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1108 else
1109 check_and_set_hdmi_channels(adev, out->config.channels);
1110 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001111
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001112 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001113
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001114 select_devices(adev, out->usecase);
1115
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001116 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1117 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001118 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001119 out->pcm = pcm_open(adev->snd_card,
1120 out->pcm_device_id,
1121 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001122 if (out->pcm && !pcm_is_ready(out->pcm)) {
1123 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1124 pcm_close(out->pcm);
1125 out->pcm = NULL;
1126 ret = -EIO;
1127 goto error_open;
1128 }
1129 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001130 out->pcm = NULL;
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08001131 out->compr = compress_open(adev->snd_card,
1132 out->pcm_device_id,
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001133 COMPRESS_IN, &out->compr_config);
1134 if (out->compr && !is_compress_ready(out->compr)) {
1135 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1136 compress_close(out->compr);
1137 out->compr = NULL;
1138 ret = -EIO;
1139 goto error_open;
1140 }
1141 if (out->offload_callback)
1142 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001143
1144 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001145 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1146 if (adev->offload_effects_start_output != NULL)
1147 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001148 }
Eric Laurent994a6932013-07-17 11:51:42 -07001149 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001150 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001151error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001152 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001153error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001154 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001155}
1156
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001157static int check_input_parameters(uint32_t sample_rate,
1158 audio_format_t format,
1159 int channel_count)
1160{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001161 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001162
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001163 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001164 !voice_extn_compress_voip_is_format_supported(format) &&
1165 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001166
1167 switch (channel_count) {
1168 case 1:
1169 case 2:
1170 case 6:
1171 break;
1172 default:
1173 ret = -EINVAL;
1174 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001175
1176 switch (sample_rate) {
1177 case 8000:
1178 case 11025:
1179 case 12000:
1180 case 16000:
1181 case 22050:
1182 case 24000:
1183 case 32000:
1184 case 44100:
1185 case 48000:
1186 break;
1187 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001188 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001189 }
1190
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001191 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001192}
1193
1194static size_t get_input_buffer_size(uint32_t sample_rate,
1195 audio_format_t format,
1196 int channel_count)
1197{
1198 size_t size = 0;
1199
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001200 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1201 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001202
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001203 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1204 /* ToDo: should use frame_size computed based on the format and
1205 channel_count here. */
1206 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001207
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001208 /* make sure the size is multiple of 64 */
1209 size += 0x3f;
1210 size &= ~0x3f;
1211
1212 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001213}
1214
1215static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1216{
1217 struct stream_out *out = (struct stream_out *)stream;
1218
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001219 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001220}
1221
1222static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1223{
1224 return -ENOSYS;
1225}
1226
1227static size_t out_get_buffer_size(const struct audio_stream *stream)
1228{
1229 struct stream_out *out = (struct stream_out *)stream;
1230
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001231 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001232 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001233 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1234 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001235
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001236 return out->config.period_size * audio_stream_frame_size(stream);
1237}
1238
1239static uint32_t out_get_channels(const struct audio_stream *stream)
1240{
1241 struct stream_out *out = (struct stream_out *)stream;
1242
1243 return out->channel_mask;
1244}
1245
1246static audio_format_t out_get_format(const struct audio_stream *stream)
1247{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001248 struct stream_out *out = (struct stream_out *)stream;
1249
1250 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001251}
1252
1253static int out_set_format(struct audio_stream *stream, audio_format_t format)
1254{
1255 return -ENOSYS;
1256}
1257
1258static int out_standby(struct audio_stream *stream)
1259{
1260 struct stream_out *out = (struct stream_out *)stream;
1261 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001262
Eric Laurent994a6932013-07-17 11:51:42 -07001263 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001264 out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001265 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1266 /* Ignore standby in case of voip call because the voip output
1267 * stream is closed in adev_close_output_stream()
1268 */
1269 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1270 return 0;
1271 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001272
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001273 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001274 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001275 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001276 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001277 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1278 if (out->pcm) {
1279 pcm_close(out->pcm);
1280 out->pcm = NULL;
1281 }
1282 } else {
1283 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001284 out->gapless_mdata.encoder_delay = 0;
1285 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001286 if (out->compr != NULL) {
1287 compress_close(out->compr);
1288 out->compr = NULL;
1289 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001290 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001291 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001292 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001293 }
1294 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001295 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001296 return 0;
1297}
1298
1299static int out_dump(const struct audio_stream *stream, int fd)
1300{
1301 return 0;
1302}
1303
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001304static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1305{
1306 int ret = 0;
1307 char value[32];
1308 struct compr_gapless_mdata tmp_mdata;
1309
1310 if (!out || !parms) {
1311 return -EINVAL;
1312 }
1313
1314 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1315 if (ret >= 0) {
1316 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1317 } else {
1318 return -EINVAL;
1319 }
1320
1321 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1322 if (ret >= 0) {
1323 tmp_mdata.encoder_padding = atoi(value);
1324 } else {
1325 return -EINVAL;
1326 }
1327
1328 out->gapless_mdata = tmp_mdata;
1329 out->send_new_metadata = 1;
1330 ALOGV("%s new encoder delay %u and padding %u", __func__,
1331 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1332
1333 return 0;
1334}
1335
1336
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001337static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1338{
1339 struct stream_out *out = (struct stream_out *)stream;
1340 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001341 struct audio_usecase *usecase;
1342 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001343 struct str_parms *parms;
1344 char value[32];
1345 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001346 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001347
sangwoobc677242013-08-08 16:53:43 +09001348 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001349 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001350 parms = str_parms_create_str(kvpairs);
1351 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1352 if (ret >= 0) {
1353 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001354 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001355 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001356
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001357 /*
1358 * When HDMI cable is unplugged the music playback is paused and
1359 * the policy manager sends routing=0. But the audioflinger
1360 * continues to write data until standby time (3sec).
1361 * As the HDMI core is turned off, the write gets blocked.
1362 * Avoid this by routing audio to speaker until standby.
1363 */
1364 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1365 val == AUDIO_DEVICE_NONE) {
1366 val = AUDIO_DEVICE_OUT_SPEAKER;
1367 }
1368
1369 /*
1370 * select_devices() call below switches all the usecases on the same
1371 * backend to the new device. Refer to check_usecases_codec_backend() in
1372 * the select_devices(). But how do we undo this?
1373 *
1374 * For example, music playback is active on headset (deep-buffer usecase)
1375 * and if we go to ringtones and select a ringtone, low-latency usecase
1376 * will be started on headset+speaker. As we can't enable headset+speaker
1377 * and headset devices at the same time, select_devices() switches the music
1378 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1379 * So when the ringtone playback is completed, how do we undo the same?
1380 *
1381 * We are relying on the out_set_parameters() call on deep-buffer output,
1382 * once the ringtone playback is ended.
1383 * NOTE: We should not check if the current devices are same as new devices.
1384 * Because select_devices() must be called to switch back the music
1385 * playback to headset.
1386 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001387 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001388 out->devices = val;
1389
1390 if (!out->standby)
1391 select_devices(adev, out->usecase);
1392
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001393 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1394 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001395 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001396 voice_start_call(adev);
1397 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1398 voice_is_in_call(adev) &&
1399 (out == adev->primary_output)) {
1400 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001401 }
1402 }
1403
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001404 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1405 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001406 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001407 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001408 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001410 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001411 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001412 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001413
1414 if (out == adev->primary_output) {
1415 pthread_mutex_lock(&adev->lock);
1416 audio_extn_set_parameters(adev, parms);
1417 pthread_mutex_unlock(&adev->lock);
1418 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001419 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1420 parse_compress_metadata(out, parms);
1421 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001422
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001423 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001424 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001425 return ret;
1426}
1427
1428static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1429{
1430 struct stream_out *out = (struct stream_out *)stream;
1431 struct str_parms *query = str_parms_create_str(keys);
1432 char *str;
1433 char value[256];
1434 struct str_parms *reply = str_parms_create();
1435 size_t i, j;
1436 int ret;
1437 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001438 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001439 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1440 if (ret >= 0) {
1441 value[0] = '\0';
1442 i = 0;
1443 while (out->supported_channel_masks[i] != 0) {
1444 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1445 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1446 if (!first) {
1447 strcat(value, "|");
1448 }
1449 strcat(value, out_channels_name_to_enum_table[j].name);
1450 first = false;
1451 break;
1452 }
1453 }
1454 i++;
1455 }
1456 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1457 str = str_parms_to_str(reply);
1458 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001459 voice_extn_out_get_parameters(out, query, reply);
1460 str = str_parms_to_str(reply);
1461 if (!strncmp(str, "", sizeof(""))) {
1462 str = strdup(keys);
1463 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001464 }
1465 str_parms_destroy(query);
1466 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001467 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001468 return str;
1469}
1470
1471static uint32_t out_get_latency(const struct audio_stream_out *stream)
1472{
1473 struct stream_out *out = (struct stream_out *)stream;
1474
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001475 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1476 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1477
1478 return (out->config.period_count * out->config.period_size * 1000) /
1479 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001480}
1481
1482static int out_set_volume(struct audio_stream_out *stream, float left,
1483 float right)
1484{
Eric Laurenta9024de2013-04-04 09:19:12 -07001485 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001486 int volume[2];
1487
Eric Laurenta9024de2013-04-04 09:19:12 -07001488 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1489 /* only take left channel into account: the API is for stereo anyway */
1490 out->muted = (left == 0.0f);
1491 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001492 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001493 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001494 struct audio_device *adev = out->dev;
1495 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001496 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1497 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001498
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001499 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1500 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001501 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1502 if (!ctl) {
1503 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1504 __func__, mixer_ctl_name);
1505 return -EINVAL;
1506 }
1507 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1508 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1509 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1510 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001511 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001512
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001513 return -ENOSYS;
1514}
1515
1516static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1517 size_t bytes)
1518{
1519 struct stream_out *out = (struct stream_out *)stream;
1520 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001521 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001522
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001523 pthread_mutex_lock(&out->lock);
1524 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001525 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001526 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001527 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1528 ret = voice_extn_compress_voip_start_output_stream(out);
1529 else
1530 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001531 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001532 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001533 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001534 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001535 goto exit;
1536 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001537 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001538
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001539 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001540 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1541 if (out->send_new_metadata) {
1542 ALOGVV("send new gapless metadata");
1543 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1544 out->send_new_metadata = 0;
1545 }
1546
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001547 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001548 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001549 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001550 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1551 }
1552 if (!out->playback_started) {
1553 compress_start(out->compr);
1554 out->playback_started = 1;
1555 out->offload_state = OFFLOAD_STATE_PLAYING;
1556 }
1557 pthread_mutex_unlock(&out->lock);
1558 return ret;
1559 } else {
1560 if (out->pcm) {
1561 if (out->muted)
1562 memset((void *)buffer, 0, bytes);
1563 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1564 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001565 if (ret == 0)
1566 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001567 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001568 }
1569
1570exit:
1571 pthread_mutex_unlock(&out->lock);
1572
1573 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001574 if (out->pcm)
1575 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001576 out_standby(&out->stream.common);
1577 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1578 out_get_sample_rate(&out->stream.common));
1579 }
1580 return bytes;
1581}
1582
1583static int out_get_render_position(const struct audio_stream_out *stream,
1584 uint32_t *dsp_frames)
1585{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001586 struct stream_out *out = (struct stream_out *)stream;
1587 *dsp_frames = 0;
1588 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1589 pthread_mutex_lock(&out->lock);
1590 if (out->compr != NULL) {
1591 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1592 &out->sample_rate);
1593 ALOGVV("%s rendered frames %d sample_rate %d",
1594 __func__, *dsp_frames, out->sample_rate);
1595 }
1596 pthread_mutex_unlock(&out->lock);
1597 return 0;
1598 } else
1599 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001600}
1601
1602static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1603{
1604 return 0;
1605}
1606
1607static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1608{
1609 return 0;
1610}
1611
1612static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1613 int64_t *timestamp)
1614{
1615 return -EINVAL;
1616}
1617
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001618static int out_get_presentation_position(const struct audio_stream_out *stream,
1619 uint64_t *frames, struct timespec *timestamp)
1620{
1621 struct stream_out *out = (struct stream_out *)stream;
1622 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001623 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001624
1625 pthread_mutex_lock(&out->lock);
1626
Eric Laurent949a0892013-09-20 09:20:13 -07001627 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1628 if (out->compr != NULL) {
1629 compress_get_tstamp(out->compr, &dsp_frames,
1630 &out->sample_rate);
1631 ALOGVV("%s rendered frames %ld sample_rate %d",
1632 __func__, dsp_frames, out->sample_rate);
1633 *frames = dsp_frames;
1634 ret = 0;
1635 /* this is the best we can do */
1636 clock_gettime(CLOCK_MONOTONIC, timestamp);
1637 }
1638 } else {
1639 if (out->pcm) {
1640 size_t avail;
1641 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1642 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001643 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001644 // This adjustment accounts for buffering after app processor.
1645 // It is based on estimated DSP latency per use case, rather than exact.
1646 signed_frames -=
1647 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1648
Eric Laurent949a0892013-09-20 09:20:13 -07001649 // It would be unusual for this value to be negative, but check just in case ...
1650 if (signed_frames >= 0) {
1651 *frames = signed_frames;
1652 ret = 0;
1653 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001654 }
1655 }
1656 }
1657
1658 pthread_mutex_unlock(&out->lock);
1659
1660 return ret;
1661}
1662
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001663static int out_set_callback(struct audio_stream_out *stream,
1664 stream_callback_t callback, void *cookie)
1665{
1666 struct stream_out *out = (struct stream_out *)stream;
1667
1668 ALOGV("%s", __func__);
1669 pthread_mutex_lock(&out->lock);
1670 out->offload_callback = callback;
1671 out->offload_cookie = cookie;
1672 pthread_mutex_unlock(&out->lock);
1673 return 0;
1674}
1675
1676static int out_pause(struct audio_stream_out* stream)
1677{
1678 struct stream_out *out = (struct stream_out *)stream;
1679 int status = -ENOSYS;
1680 ALOGV("%s", __func__);
1681 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1682 pthread_mutex_lock(&out->lock);
1683 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1684 status = compress_pause(out->compr);
1685 out->offload_state = OFFLOAD_STATE_PAUSED;
1686 }
1687 pthread_mutex_unlock(&out->lock);
1688 }
1689 return status;
1690}
1691
1692static int out_resume(struct audio_stream_out* stream)
1693{
1694 struct stream_out *out = (struct stream_out *)stream;
1695 int status = -ENOSYS;
1696 ALOGV("%s", __func__);
1697 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1698 status = 0;
1699 pthread_mutex_lock(&out->lock);
1700 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1701 status = compress_resume(out->compr);
1702 out->offload_state = OFFLOAD_STATE_PLAYING;
1703 }
1704 pthread_mutex_unlock(&out->lock);
1705 }
1706 return status;
1707}
1708
1709static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1710{
1711 struct stream_out *out = (struct stream_out *)stream;
1712 int status = -ENOSYS;
1713 ALOGV("%s", __func__);
1714 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1715 pthread_mutex_lock(&out->lock);
1716 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1717 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1718 else
1719 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1720 pthread_mutex_unlock(&out->lock);
1721 }
1722 return status;
1723}
1724
1725static int out_flush(struct audio_stream_out* stream)
1726{
1727 struct stream_out *out = (struct stream_out *)stream;
1728 ALOGV("%s", __func__);
1729 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1730 pthread_mutex_lock(&out->lock);
1731 stop_compressed_output_l(out);
1732 pthread_mutex_unlock(&out->lock);
1733 return 0;
1734 }
1735 return -ENOSYS;
1736}
1737
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001738/** audio_stream_in implementation **/
1739static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1740{
1741 struct stream_in *in = (struct stream_in *)stream;
1742
1743 return in->config.rate;
1744}
1745
1746static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1747{
1748 return -ENOSYS;
1749}
1750
1751static size_t in_get_buffer_size(const struct audio_stream *stream)
1752{
1753 struct stream_in *in = (struct stream_in *)stream;
1754
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001755 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1756 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001757 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1758 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001759
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001760 return in->config.period_size * audio_stream_frame_size(stream);
1761}
1762
1763static uint32_t in_get_channels(const struct audio_stream *stream)
1764{
1765 struct stream_in *in = (struct stream_in *)stream;
1766
1767 return in->channel_mask;
1768}
1769
1770static audio_format_t in_get_format(const struct audio_stream *stream)
1771{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001772 struct stream_in *in = (struct stream_in *)stream;
1773
1774 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001775}
1776
1777static int in_set_format(struct audio_stream *stream, audio_format_t format)
1778{
1779 return -ENOSYS;
1780}
1781
1782static int in_standby(struct audio_stream *stream)
1783{
1784 struct stream_in *in = (struct stream_in *)stream;
1785 struct audio_device *adev = in->dev;
1786 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001787 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001788
1789 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1790 /* Ignore standby in case of voip call because the voip input
1791 * stream is closed in adev_close_input_stream()
1792 */
1793 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1794 return status;
1795 }
1796
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001797 pthread_mutex_lock(&in->lock);
1798 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001799 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001800 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001801 if (in->pcm) {
1802 pcm_close(in->pcm);
1803 in->pcm = NULL;
1804 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001805 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001806 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001807 }
1808 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001809 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001810 return status;
1811}
1812
1813static int in_dump(const struct audio_stream *stream, int fd)
1814{
1815 return 0;
1816}
1817
1818static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1819{
1820 struct stream_in *in = (struct stream_in *)stream;
1821 struct audio_device *adev = in->dev;
1822 struct str_parms *parms;
1823 char *str;
1824 char value[32];
1825 int ret, val = 0;
1826
Eric Laurent994a6932013-07-17 11:51:42 -07001827 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001828 parms = str_parms_create_str(kvpairs);
1829
1830 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1831
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001832 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001833 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001834 if (ret >= 0) {
1835 val = atoi(value);
1836 /* no audio source uses val == 0 */
1837 if ((in->source != val) && (val != 0)) {
1838 in->source = val;
1839 }
1840 }
1841
1842 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1843 if (ret >= 0) {
1844 val = atoi(value);
1845 if ((in->device != val) && (val != 0)) {
1846 in->device = val;
1847 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001848 if (!in->standby)
1849 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001850 }
1851 }
1852
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001853 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001854 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001855
1856 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001857 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001858 return ret;
1859}
1860
1861static char* in_get_parameters(const struct audio_stream *stream,
1862 const char *keys)
1863{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001864 struct stream_in *in = (struct stream_in *)stream;
1865 struct str_parms *query = str_parms_create_str(keys);
1866 char *str;
1867 char value[256];
1868 struct str_parms *reply = str_parms_create();
1869 ALOGV("%s: enter: keys - %s", __func__, keys);
1870
1871 voice_extn_in_get_parameters(in, query, reply);
1872
1873 str = str_parms_to_str(reply);
1874 str_parms_destroy(query);
1875 str_parms_destroy(reply);
1876
1877 ALOGV("%s: exit: returns - %s", __func__, str);
1878 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001879}
1880
1881static int in_set_gain(struct audio_stream_in *stream, float gain)
1882{
1883 return 0;
1884}
1885
1886static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1887 size_t bytes)
1888{
1889 struct stream_in *in = (struct stream_in *)stream;
1890 struct audio_device *adev = in->dev;
1891 int i, ret = -1;
1892
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001893 pthread_mutex_lock(&in->lock);
1894 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001895 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001896 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
1897 ret = voice_extn_compress_voip_start_input_stream(in);
1898 else
1899 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001900 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001901 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001902 goto exit;
1903 }
1904 in->standby = 0;
1905 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001906
1907 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001908 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1909 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07001910 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
1911 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001912 else
1913 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001914 }
1915
1916 /*
1917 * Instead of writing zeroes here, we could trust the hardware
1918 * to always provide zeroes when muted.
1919 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001920 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001921 memset(buffer, 0, bytes);
1922
1923exit:
1924 pthread_mutex_unlock(&in->lock);
1925
1926 if (ret != 0) {
1927 in_standby(&in->stream.common);
1928 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1929 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1930 in_get_sample_rate(&in->stream.common));
1931 }
1932 return bytes;
1933}
1934
1935static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1936{
1937 return 0;
1938}
1939
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001940static int add_remove_audio_effect(const struct audio_stream *stream,
1941 effect_handle_t effect,
1942 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001943{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001944 struct stream_in *in = (struct stream_in *)stream;
1945 int status = 0;
1946 effect_descriptor_t desc;
1947
1948 status = (*effect)->get_descriptor(effect, &desc);
1949 if (status != 0)
1950 return status;
1951
1952 pthread_mutex_lock(&in->lock);
1953 pthread_mutex_lock(&in->dev->lock);
1954 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1955 in->enable_aec != enable &&
1956 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1957 in->enable_aec = enable;
1958 if (!in->standby)
1959 select_devices(in->dev, in->usecase);
1960 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08001961 if (in->enable_ns != enable &&
1962 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1963 in->enable_ns = enable;
1964 if (!in->standby)
1965 select_devices(in->dev, in->usecase);
1966 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001967 pthread_mutex_unlock(&in->dev->lock);
1968 pthread_mutex_unlock(&in->lock);
1969
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001970 return 0;
1971}
1972
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001973static int in_add_audio_effect(const struct audio_stream *stream,
1974 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001975{
Eric Laurent994a6932013-07-17 11:51:42 -07001976 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001977 return add_remove_audio_effect(stream, effect, true);
1978}
1979
1980static int in_remove_audio_effect(const struct audio_stream *stream,
1981 effect_handle_t effect)
1982{
Eric Laurent994a6932013-07-17 11:51:42 -07001983 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001984 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001985}
1986
1987static int adev_open_output_stream(struct audio_hw_device *dev,
1988 audio_io_handle_t handle,
1989 audio_devices_t devices,
1990 audio_output_flags_t flags,
1991 struct audio_config *config,
1992 struct audio_stream_out **stream_out)
1993{
1994 struct audio_device *adev = (struct audio_device *)dev;
1995 struct stream_out *out;
1996 int i, ret;
1997
Eric Laurent994a6932013-07-17 11:51:42 -07001998 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001999 __func__, config->sample_rate, config->channel_mask, devices, flags);
2000 *stream_out = NULL;
2001 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2002
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002003 if (!out) {
2004 return -ENOMEM;
2005 }
2006
2007 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2008 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
2009
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002010 if (devices == AUDIO_DEVICE_NONE)
2011 devices = AUDIO_DEVICE_OUT_SPEAKER;
2012
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002013 out->flags = flags;
2014 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002015 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002016 out->format = config->format;
2017 out->sample_rate = config->sample_rate;
2018 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2019 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002020 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002021
2022 /* Init use case and pcm_config */
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002023 if ((out->flags == AUDIO_OUTPUT_FLAG_DIRECT) &&
2024 (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2025 out->devices & AUDIO_DEVICE_OUT_PROXY)) {
2026
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002027 pthread_mutex_lock(&adev->lock);
Apoorv Raghuvanshi947cb902013-12-09 13:45:39 -08002028 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
2029 ret = read_hdmi_channel_masks(out);
2030
2031 if (out->devices & AUDIO_DEVICE_OUT_PROXY)
2032 ret = audio_extn_read_afe_proxy_channel_masks(out);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002033 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002034 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002035 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002036
2037 if (config->sample_rate == 0)
2038 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2039 if (config->channel_mask == 0)
2040 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2041
2042 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002043 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002044 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2045 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002046 out->config.rate = config->sample_rate;
2047 out->config.channels = popcount(out->channel_mask);
2048 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002049 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2050 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002051 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002052 ret = voice_extn_compress_voip_open_output_stream(out);
2053 if (ret != 0) {
2054 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2055 __func__, ret);
2056 goto error_open;
2057 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002058 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2059 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2060 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2061 ALOGE("%s: Unsupported Offload information", __func__);
2062 ret = -EINVAL;
2063 goto error_open;
2064 }
Mingming Yin90310102013-11-13 16:57:00 -08002065 if (!is_supported_format(config->offload_info.format) &&
2066 !audio_extn_dolby_is_supported_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002067 ALOGE("%s: Unsupported audio format", __func__);
2068 ret = -EINVAL;
2069 goto error_open;
2070 }
2071
2072 out->compr_config.codec = (struct snd_codec *)
2073 calloc(1, sizeof(struct snd_codec));
2074
2075 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2076 if (config->offload_info.channel_mask)
2077 out->channel_mask = config->offload_info.channel_mask;
2078 else if (config->channel_mask)
2079 out->channel_mask = config->channel_mask;
2080 out->format = config->offload_info.format;
2081 out->sample_rate = config->offload_info.sample_rate;
2082
2083 out->stream.set_callback = out_set_callback;
2084 out->stream.pause = out_pause;
2085 out->stream.resume = out_resume;
2086 out->stream.drain = out_drain;
2087 out->stream.flush = out_flush;
2088
Mingming Yin90310102013-11-13 16:57:00 -08002089 if (audio_extn_dolby_is_supported_format(config->offload_info.format))
2090 out->compr_config.codec->id =
2091 audio_extn_dolby_get_snd_codec_id(config->offload_info.format);
2092 else
2093 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002094 get_snd_codec_id(config->offload_info.format);
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -08002095 out->compr_config.fragment_size = get_offload_buffer_size();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002096 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2097 out->compr_config.codec->sample_rate =
2098 compress_get_alsa_rate(config->offload_info.sample_rate);
2099 out->compr_config.codec->bit_rate =
2100 config->offload_info.bit_rate;
2101 out->compr_config.codec->ch_in =
2102 popcount(config->channel_mask);
2103 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2104
2105 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2106 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002107
2108 out->send_new_metadata = 1;
Haynes Mathew Georgeb9012ab2013-12-10 13:44:56 -08002109 out->offload_state = OFFLOAD_STATE_IDLE;
2110 out->playback_started = 0;
2111
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002112 create_offload_callback_thread(out);
2113 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2114 __func__, config->offload_info.version,
2115 config->offload_info.bit_rate);
Mingming Yin90310102013-11-13 16:57:00 -08002116
2117 if (audio_extn_dolby_is_supported_format(out->format)) {
2118 ret = audio_extn_dolby_set_DMID(adev);
2119 if (ret != 0) {
2120 ALOGE("%s: Dolby DMID cannot be set error:%d",
2121 __func__, ret);
2122 goto error_open;
2123 }
2124 }
2125
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002126 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2127 ret = voice_check_and_set_incall_music_usecase(adev, out);
2128 if (ret != 0) {
2129 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2130 __func__, ret);
2131 goto error_open;
2132 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002133 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002134 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2135 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002136 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002137 } else {
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002138 /* primary path is the default path selected if no other outputs are available/suitable */
2139 out->usecase = USECASE_AUDIO_PLAYBACK_PRIMARY;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002140 out->config = pcm_config_deep_buffer;
2141 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002142 }
2143
Haynes Mathew Georgebf143712013-12-03 13:02:53 -08002144 if ((out->usecase == USECASE_AUDIO_PLAYBACK_PRIMARY) ||
2145 (flags & AUDIO_OUTPUT_FLAG_PRIMARY)) {
2146 /* Ensure the default output is not selected twice */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002147 if(adev->primary_output == NULL)
2148 adev->primary_output = out;
2149 else {
2150 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002151 ret = -EEXIST;
2152 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002153 }
2154 }
2155
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002156 /* Check if this usecase is already existing */
2157 pthread_mutex_lock(&adev->lock);
2158 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2159 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002160 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002161 ret = -EEXIST;
2162 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002163 }
2164 pthread_mutex_unlock(&adev->lock);
2165
2166 out->stream.common.get_sample_rate = out_get_sample_rate;
2167 out->stream.common.set_sample_rate = out_set_sample_rate;
2168 out->stream.common.get_buffer_size = out_get_buffer_size;
2169 out->stream.common.get_channels = out_get_channels;
2170 out->stream.common.get_format = out_get_format;
2171 out->stream.common.set_format = out_set_format;
2172 out->stream.common.standby = out_standby;
2173 out->stream.common.dump = out_dump;
2174 out->stream.common.set_parameters = out_set_parameters;
2175 out->stream.common.get_parameters = out_get_parameters;
2176 out->stream.common.add_audio_effect = out_add_audio_effect;
2177 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2178 out->stream.get_latency = out_get_latency;
2179 out->stream.set_volume = out_set_volume;
2180 out->stream.write = out_write;
2181 out->stream.get_render_position = out_get_render_position;
2182 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002183 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002184
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002185 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002186 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002187 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002188
2189 config->format = out->stream.common.get_format(&out->stream.common);
2190 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2191 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2192
2193 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002194 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002195 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002196
2197error_open:
2198 free(out);
2199 *stream_out = NULL;
2200 ALOGD("%s: exit: ret %d", __func__, ret);
2201 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002202}
2203
2204static void adev_close_output_stream(struct audio_hw_device *dev,
2205 struct audio_stream_out *stream)
2206{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002207 struct stream_out *out = (struct stream_out *)stream;
2208 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002209 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002210
Eric Laurent994a6932013-07-17 11:51:42 -07002211 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002212 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2213 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2214 if(ret != 0)
2215 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2216 __func__, ret);
2217 }
2218 else
2219 out_standby(&stream->common);
2220
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002221 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2222 destroy_offload_callback_thread(out);
2223
2224 if (out->compr_config.codec != NULL)
2225 free(out->compr_config.codec);
2226 }
2227 pthread_cond_destroy(&out->cond);
2228 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002229 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002230 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002231}
2232
2233static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2234{
2235 struct audio_device *adev = (struct audio_device *)dev;
2236 struct str_parms *parms;
2237 char *str;
2238 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002239 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002240 int ret;
2241
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002242 ALOGD("%s: enter: %s", __func__, kvpairs);
2243
2244 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002245 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002246
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002247 voice_set_parameters(adev, parms);
2248 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002249
2250 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2251 if (ret >= 0) {
2252 /* When set to false, HAL should disable EC and NS
2253 * But it is currently not supported.
2254 */
2255 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2256 adev->bluetooth_nrec = true;
2257 else
2258 adev->bluetooth_nrec = false;
2259 }
2260
2261 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2262 if (ret >= 0) {
2263 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2264 adev->screen_off = false;
2265 else
2266 adev->screen_off = true;
2267 }
2268
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002269 ret = str_parms_get_int(parms, "rotation", &val);
2270 if (ret >= 0) {
2271 bool reverse_speakers = false;
2272 switch(val) {
2273 // FIXME: note that the code below assumes that the speakers are in the correct placement
2274 // relative to the user when the device is rotated 90deg from its default rotation. This
2275 // assumption is device-specific, not platform-specific like this code.
2276 case 270:
2277 reverse_speakers = true;
2278 break;
2279 case 0:
2280 case 90:
2281 case 180:
2282 break;
2283 default:
2284 ALOGE("%s: unexpected rotation of %d", __func__, val);
2285 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002286 if (adev->speaker_lr_swap != reverse_speakers) {
2287 adev->speaker_lr_swap = reverse_speakers;
2288 // only update the selected device if there is active pcm playback
2289 struct audio_usecase *usecase;
2290 struct listnode *node;
2291 list_for_each(node, &adev->usecase_list) {
2292 usecase = node_to_item(node, struct audio_usecase, list);
2293 if (usecase->type == PCM_PLAYBACK) {
2294 select_devices(adev, usecase->id);
2295 break;
2296 }
2297 }
2298 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002299 }
2300
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002301 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002302 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002303
2304 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002305 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002306 return ret;
2307}
2308
2309static char* adev_get_parameters(const struct audio_hw_device *dev,
2310 const char *keys)
2311{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002312 struct audio_device *adev = (struct audio_device *)dev;
2313 struct str_parms *reply = str_parms_create();
2314 struct str_parms *query = str_parms_create_str(keys);
2315 char *str;
2316
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002317 pthread_mutex_lock(&adev->lock);
2318
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002319 audio_extn_get_parameters(adev, query, reply);
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002320 voice_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002321 platform_get_parameters(adev->platform, query, reply);
2322 str = str_parms_to_str(reply);
2323 str_parms_destroy(query);
2324 str_parms_destroy(reply);
2325
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002326 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002327 ALOGV("%s: exit: returns - %s", __func__, str);
2328 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002329}
2330
2331static int adev_init_check(const struct audio_hw_device *dev)
2332{
2333 return 0;
2334}
2335
2336static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2337{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002338 int ret;
2339 struct audio_device *adev = (struct audio_device *)dev;
2340 pthread_mutex_lock(&adev->lock);
2341 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002342 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002343 pthread_mutex_unlock(&adev->lock);
2344 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002345}
2346
2347static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2348{
2349 return -ENOSYS;
2350}
2351
2352static int adev_get_master_volume(struct audio_hw_device *dev,
2353 float *volume)
2354{
2355 return -ENOSYS;
2356}
2357
2358static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2359{
2360 return -ENOSYS;
2361}
2362
2363static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2364{
2365 return -ENOSYS;
2366}
2367
2368static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2369{
2370 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002371 pthread_mutex_lock(&adev->lock);
2372 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002373 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002374 adev->mode = mode;
2375 }
2376 pthread_mutex_unlock(&adev->lock);
2377 return 0;
2378}
2379
2380static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2381{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002382 int ret;
2383
2384 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002385 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002386 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2387 pthread_mutex_unlock(&adev->lock);
2388
2389 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002390}
2391
2392static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2393{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002394 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002395 return 0;
2396}
2397
2398static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2399 const struct audio_config *config)
2400{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002401 int channel_count = popcount(config->channel_mask);
2402
2403 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2404}
2405
2406static int adev_open_input_stream(struct audio_hw_device *dev,
2407 audio_io_handle_t handle,
2408 audio_devices_t devices,
2409 struct audio_config *config,
2410 struct audio_stream_in **stream_in)
2411{
2412 struct audio_device *adev = (struct audio_device *)dev;
2413 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002414 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002415 int channel_count = popcount(config->channel_mask);
2416
Eric Laurent994a6932013-07-17 11:51:42 -07002417 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002418 *stream_in = NULL;
2419 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2420 return -EINVAL;
2421
2422 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2423
2424 in->stream.common.get_sample_rate = in_get_sample_rate;
2425 in->stream.common.set_sample_rate = in_set_sample_rate;
2426 in->stream.common.get_buffer_size = in_get_buffer_size;
2427 in->stream.common.get_channels = in_get_channels;
2428 in->stream.common.get_format = in_get_format;
2429 in->stream.common.set_format = in_set_format;
2430 in->stream.common.standby = in_standby;
2431 in->stream.common.dump = in_dump;
2432 in->stream.common.set_parameters = in_set_parameters;
2433 in->stream.common.get_parameters = in_get_parameters;
2434 in->stream.common.add_audio_effect = in_add_audio_effect;
2435 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2436 in->stream.set_gain = in_set_gain;
2437 in->stream.read = in_read;
2438 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2439
2440 in->device = devices;
2441 in->source = AUDIO_SOURCE_DEFAULT;
2442 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002443 in->standby = 1;
2444 in->channel_mask = config->channel_mask;
2445
2446 /* Update config params with the requested sample rate and channels */
2447 in->usecase = USECASE_AUDIO_RECORD;
2448 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002449 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002450 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002451
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002452 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002453 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002454 ret = voice_extn_compress_voip_open_input_stream(in);
2455 if (ret != 0)
2456 {
2457 ALOGE("%s: Compress voip input cannot be opened, error:%d",
2458 __func__, ret);
2459 goto err_open;
2460 }
Mingming Yine62d7842013-10-25 16:26:03 -07002461 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002462 if(audio_extn_ssr_get_enabled()) {
2463 if(audio_extn_ssr_init(adev, in)) {
2464 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2465 ret = -EINVAL;
2466 goto err_open;
2467 }
2468 } else {
2469 ret = -EINVAL;
2470 goto err_open;
2471 }
Mingming Yine62d7842013-10-25 16:26:03 -07002472 } else if (audio_extn_compr_cap_enabled() &&
2473 audio_extn_compr_cap_format_supported(config->format)) {
2474 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002475 } else {
2476 in->config.channels = channel_count;
2477 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2478 buffer_size = get_input_buffer_size(config->sample_rate,
2479 config->format,
2480 channel_count);
2481 in->config.period_size = buffer_size / frame_size;
2482 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002483
2484 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002485 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002486 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002487
2488err_open:
2489 free(in);
2490 *stream_in = NULL;
2491 return ret;
2492}
2493
2494static void adev_close_input_stream(struct audio_hw_device *dev,
2495 struct audio_stream_in *stream)
2496{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002497 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002498 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002499 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002500
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002501 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2502 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2503 if (ret != 0)
2504 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2505 __func__, ret);
2506 } else
2507 in_standby(&stream->common);
2508
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002509 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2510 audio_extn_ssr_deinit();
2511 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002512 free(stream);
2513
Mingming Yine62d7842013-10-25 16:26:03 -07002514 if(audio_extn_compr_cap_enabled() &&
2515 audio_extn_compr_cap_format_supported(in->config.format))
2516 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002517 return;
2518}
2519
2520static int adev_dump(const audio_hw_device_t *device, int fd)
2521{
2522 return 0;
2523}
2524
2525static int adev_close(hw_device_t *device)
2526{
2527 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002528
2529 if (!adev)
2530 return 0;
2531
2532 pthread_mutex_lock(&adev_init_lock);
2533
2534 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002535 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07002536 audio_route_free(adev->audio_route);
2537 free(adev->snd_dev_ref_cnt);
2538 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002539 free(device);
2540 adev = NULL;
2541 }
2542 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002543 return 0;
2544}
2545
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002546static int adev_open(const hw_module_t *module, const char *name,
2547 hw_device_t **device)
2548{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002549 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002550
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002551 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002552 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2553
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002554 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002555 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002556 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002557 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002558 ALOGD("%s: returning existing instance of adev", __func__);
2559 ALOGD("%s: exit", __func__);
2560 pthread_mutex_unlock(&adev_init_lock);
2561 return 0;
2562 }
2563
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002564 adev = calloc(1, sizeof(struct audio_device));
2565
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002566 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2567 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2568 adev->device.common.module = (struct hw_module_t *)module;
2569 adev->device.common.close = adev_close;
2570
2571 adev->device.init_check = adev_init_check;
2572 adev->device.set_voice_volume = adev_set_voice_volume;
2573 adev->device.set_master_volume = adev_set_master_volume;
2574 adev->device.get_master_volume = adev_get_master_volume;
2575 adev->device.set_master_mute = adev_set_master_mute;
2576 adev->device.get_master_mute = adev_get_master_mute;
2577 adev->device.set_mode = adev_set_mode;
2578 adev->device.set_mic_mute = adev_set_mic_mute;
2579 adev->device.get_mic_mute = adev_get_mic_mute;
2580 adev->device.set_parameters = adev_set_parameters;
2581 adev->device.get_parameters = adev_get_parameters;
2582 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2583 adev->device.open_output_stream = adev_open_output_stream;
2584 adev->device.close_output_stream = adev_close_output_stream;
2585 adev->device.open_input_stream = adev_open_input_stream;
2586 adev->device.close_input_stream = adev_close_input_stream;
2587 adev->device.dump = adev_dump;
2588
2589 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002590 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002591 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002592 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002593 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002594 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002595 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002596 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002597 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002598 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002599 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002600
2601 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002602 adev->platform = platform_init(adev);
2603 if (!adev->platform) {
2604 free(adev->snd_dev_ref_cnt);
2605 free(adev);
2606 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2607 *device = NULL;
Apoorv Raghuvanshi6e57d7e2013-12-16 16:02:45 -08002608 pthread_mutex_unlock(&adev_init_lock);
Eric Laurentb23d5282013-05-14 15:27:20 -07002609 return -EINVAL;
2610 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002611
2612 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2613 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2614 if (adev->visualizer_lib == NULL) {
2615 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2616 } else {
2617 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2618 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002619 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002620 "visualizer_hal_start_output");
2621 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002622 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002623 "visualizer_hal_stop_output");
2624 }
2625 }
Apoorv Raghuvanshi84fa2fe2013-12-04 11:57:47 -08002626 audio_extn_listen_init(adev, adev->snd_card);
Eric Laurentc4aef752013-09-12 17:45:53 -07002627
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002628 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2629 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2630 if (adev->offload_effects_lib == NULL) {
2631 ALOGE("%s: DLOPEN failed for %s", __func__,
2632 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2633 } else {
2634 ALOGV("%s: DLOPEN successful for %s", __func__,
2635 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2636 adev->offload_effects_start_output =
2637 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2638 "offload_effects_bundle_hal_start_output");
2639 adev->offload_effects_stop_output =
2640 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2641 "offload_effects_bundle_hal_stop_output");
2642 }
2643 }
2644
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002645 *device = &adev->device.common;
2646
Kiran Kandi910e1862013-10-29 13:29:42 -07002647 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002648 pthread_mutex_unlock(&adev_init_lock);
2649
Eric Laurent994a6932013-07-17 11:51:42 -07002650 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002651 return 0;
2652}
2653
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -08002654/* Read offload buffer size from a property.
2655 * If value is not power of 2 round it to
2656 * power of 2.
2657 */
2658static uint32_t get_offload_buffer_size()
2659{
2660 char value[PROPERTY_VALUE_MAX] = {0};
2661 uint32_t fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2662 if((property_get("audio.offload.buffer.size.kb", value, "")) &&
2663 atoi(value)) {
2664 fragment_size = atoi(value) * 1024;
2665 //ring buffer size needs to be 4k aligned.
2666 CHECK(!(fragment_size * COMPRESS_OFFLOAD_NUM_FRAGMENTS % 4096));
2667 }
2668 if(fragment_size < MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
2669 fragment_size = MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2670 else if(fragment_size > MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
2671 fragment_size = MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2672 ALOGVV("%s: fragment_size %d", __func__, fragment_size);
2673 return fragment_size;
2674}
2675
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002676static struct hw_module_methods_t hal_module_methods = {
2677 .open = adev_open,
2678};
2679
2680struct audio_module HAL_MODULE_INFO_SYM = {
2681 .common = {
2682 .tag = HARDWARE_MODULE_TAG,
2683 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2684 .hal_api_version = HARDWARE_HAL_API_VERSION,
2685 .id = AUDIO_HARDWARE_MODULE_ID,
2686 .name = "QCOM Audio HAL",
Duy Truongfae19622013-11-24 02:17:54 -08002687 .author = "The Linux Foundation",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002688 .methods = &hal_module_methods,
2689 },
2690};