blob: bc3cd0d7ca48834b89ecd032fdf9f8c309569dce [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
Eric Laurentb23d5282013-05-14 15:27:20 -070064struct pcm_config pcm_config_deep_buffer = {
65 .channels = 2,
66 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
67 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
68 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
69 .format = PCM_FORMAT_S16_LE,
70 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
71 .stop_threshold = INT_MAX,
72 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
73};
74
75struct pcm_config pcm_config_low_latency = {
76 .channels = 2,
77 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
78 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
79 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
80 .format = PCM_FORMAT_S16_LE,
81 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
82 .stop_threshold = INT_MAX,
83 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
84};
85
86struct pcm_config pcm_config_hdmi_multi = {
87 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
88 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
89 .period_size = HDMI_MULTI_PERIOD_SIZE,
90 .period_count = HDMI_MULTI_PERIOD_COUNT,
91 .format = PCM_FORMAT_S16_LE,
92 .start_threshold = 0,
93 .stop_threshold = INT_MAX,
94 .avail_min = 0,
95};
96
97struct pcm_config pcm_config_audio_capture = {
98 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070099 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
100 .format = PCM_FORMAT_S16_LE,
101};
102
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800103const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700104 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
105 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
106 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700107 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700108 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700109 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700110 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700111 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700112 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800113 [USECASE_AUDIO_HFP_SCO] = "hfp-sco",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700114 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700115
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700116 [USECASE_VOICE2_CALL] = "voice2-call",
117 [USECASE_VOLTE_CALL] = "volte-call",
118 [USECASE_QCHAT_CALL] = "qchat-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800119 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700120 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
121 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
122 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700123 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
124 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700125 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
126 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700127};
128
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800129
130#define STRING_TO_ENUM(string) { #string, string }
131
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800132struct string_to_enum {
133 const char *name;
134 uint32_t value;
135};
136
137static const struct string_to_enum out_channels_name_to_enum_table[] = {
138 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
139 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
140 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
141};
142
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700143static struct audio_device *adev = NULL;
144static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700145static unsigned int audio_device_ref_count;
146
Haynes Mathew George5191a852013-09-11 14:19:36 -0700147static int set_voice_volume_l(struct audio_device *adev, float volume);
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -0800148static uint32_t get_offload_buffer_size();
Haynes Mathew George5191a852013-09-11 14:19:36 -0700149
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700150static bool is_supported_format(audio_format_t format)
151{
Eric Laurent86e17132013-09-12 17:49:30 -0700152 if (format == AUDIO_FORMAT_MP3 ||
153 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700154 return true;
155
156 return false;
157}
158
159static int get_snd_codec_id(audio_format_t format)
160{
161 int id = 0;
162
163 switch (format) {
164 case AUDIO_FORMAT_MP3:
165 id = SND_AUDIOCODEC_MP3;
166 break;
167 case AUDIO_FORMAT_AAC:
168 id = SND_AUDIOCODEC_AAC;
169 break;
170 default:
Mingming Yin90310102013-11-13 16:57:00 -0800171 ALOGE("%s: Unsupported audio format :%x", __func__, format);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700172 }
173
174 return id;
175}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800176
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700177int enable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700178 struct audio_usecase *usecase,
179 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800180{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700181 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700182 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800183
184 if (usecase == NULL)
185 return -EINVAL;
186
187 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
188
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800189 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700190 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800191 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700192 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800193
194 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700195 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700196 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700197 audio_route_apply_path(adev->audio_route, mixer_path);
198 if (update_mixer)
199 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800200
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800201 ALOGV("%s: exit", __func__);
202 return 0;
203}
204
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700205int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700206 struct audio_usecase *usecase,
207 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800208{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700209 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700210 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800211
212 if (usecase == NULL)
213 return -EINVAL;
214
215 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700216 if (usecase->type == PCM_CAPTURE)
217 snd_device = usecase->in_snd_device;
218 else
219 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800220 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700221 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700222 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700223 audio_route_reset_path(adev->audio_route, mixer_path);
224 if (update_mixer)
225 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800226
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800227 ALOGV("%s: exit", __func__);
228 return 0;
229}
230
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700231int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700232 snd_device_t snd_device,
233 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800234{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700235 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
236
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800237 if (snd_device < SND_DEVICE_MIN ||
238 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800239 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800240 return -EINVAL;
241 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700242
243 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700244
245 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
246 ALOGE("%s: Invalid sound device returned", __func__);
247 return -EINVAL;
248 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700250 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700251 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700252 return 0;
253 }
254
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700255 /* start usb playback thread */
256 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
257 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
258 audio_extn_usb_start_playback(adev);
259
260 /* start usb capture thread */
261 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
262 audio_extn_usb_start_capture(adev);
263
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700264 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
265 audio_extn_spkr_prot_is_enabled()) {
266 if (audio_extn_spkr_prot_start_processing(snd_device)) {
267 ALOGE("%s: spkr_start_processing failed", __func__);
268 return -EINVAL;
269 }
270 } else {
271 ALOGV("%s: snd_device(%d: %s)", __func__,
272 snd_device, device_name);
273 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
274 adev->snd_dev_ref_cnt[snd_device]--;
275 return -EINVAL;
276 }
Kiran Kandide144c82013-11-20 15:58:32 -0800277 audio_extn_listen_update_status(snd_device,
278 LISTEN_EVENT_SND_DEVICE_BUSY);
279
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700280 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800281 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700282 if (update_mixer)
283 audio_route_update_mixer(adev->audio_route);
284
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800285 return 0;
286}
287
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700288int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700289 snd_device_t snd_device,
290 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800291{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700292 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
293
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800294 if (snd_device < SND_DEVICE_MIN ||
295 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800296 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800297 return -EINVAL;
298 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700299 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
300 ALOGE("%s: device ref cnt is already 0", __func__);
301 return -EINVAL;
302 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700303
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700304 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700305
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700306 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
307 ALOGE("%s: Invalid sound device returned", __func__);
308 return -EINVAL;
309 }
310
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700311 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700312 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700313 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800314 /* exit usb play back thread */
315 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
316 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
317 audio_extn_usb_stop_playback();
318
319 /* exit usb capture thread */
320 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
321 audio_extn_usb_stop_capture(adev);
322
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700323 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
324 audio_extn_spkr_prot_is_enabled()) {
325 audio_extn_spkr_prot_stop_processing();
326 } else
327 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800328
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700329 if (update_mixer)
330 audio_route_update_mixer(adev->audio_route);
Kiran Kandide144c82013-11-20 15:58:32 -0800331
332 audio_extn_listen_update_status(snd_device,
333 LISTEN_EVENT_SND_DEVICE_FREE);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700334 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700335
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800336 return 0;
337}
338
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700339static void check_usecases_codec_backend(struct audio_device *adev,
340 struct audio_usecase *uc_info,
341 snd_device_t snd_device)
342{
343 struct listnode *node;
344 struct audio_usecase *usecase;
345 bool switch_device[AUDIO_USECASE_MAX];
346 int i, num_uc_to_switch = 0;
347
348 /*
349 * This function is to make sure that all the usecases that are active on
350 * the hardware codec backend are always routed to any one device that is
351 * handled by the hardware codec.
352 * For example, if low-latency and deep-buffer usecases are currently active
353 * on speaker and out_set_parameters(headset) is received on low-latency
354 * output, then we have to make sure deep-buffer is also switched to headset,
355 * because of the limitation that both the devices cannot be enabled
356 * at the same time as they share the same backend.
357 */
358 /* Disable all the usecases on the shared backend other than the
359 specified usecase */
360 for (i = 0; i < AUDIO_USECASE_MAX; i++)
361 switch_device[i] = false;
362
363 list_for_each(node, &adev->usecase_list) {
364 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700365 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700366 usecase != uc_info &&
367 usecase->out_snd_device != snd_device &&
368 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
369 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
370 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700371 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700372 disable_audio_route(adev, usecase, false);
373 switch_device[usecase->id] = true;
374 num_uc_to_switch++;
375 }
376 }
377
378 if (num_uc_to_switch) {
379 /* Make sure all the streams are de-routed before disabling the device */
380 audio_route_update_mixer(adev->audio_route);
381
382 list_for_each(node, &adev->usecase_list) {
383 usecase = node_to_item(node, struct audio_usecase, list);
384 if (switch_device[usecase->id]) {
385 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700386 }
387 }
388
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700389 list_for_each(node, &adev->usecase_list) {
390 usecase = node_to_item(node, struct audio_usecase, list);
391 if (switch_device[usecase->id]) {
392 enable_snd_device(adev, snd_device, false);
393 }
394 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700395
Shruthi Krishna116f1ec2013-12-06 11:05:42 -0800396 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 /* Make sure new snd device is enabled before re-routing the streams */
403 audio_route_update_mixer(adev->audio_route);
404
405 /* Re-route all the usecases on the shared backend other than the
406 specified usecase to new snd devices */
407 list_for_each(node, &adev->usecase_list) {
408 usecase = node_to_item(node, struct audio_usecase, list);
409 /* Update the out_snd_device only before enabling the audio route */
410 if (switch_device[usecase->id] ) {
411 usecase->out_snd_device = snd_device;
412 enable_audio_route(adev, usecase, false);
413 }
414 }
415
416 audio_route_update_mixer(adev->audio_route);
417 }
418}
419
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700420static void check_and_route_capture_usecases(struct audio_device *adev,
421 struct audio_usecase *uc_info,
422 snd_device_t snd_device)
423{
424 struct listnode *node;
425 struct audio_usecase *usecase;
426 bool switch_device[AUDIO_USECASE_MAX];
427 int i, num_uc_to_switch = 0;
428
429 /*
430 * This function is to make sure that all the active capture usecases
431 * are always routed to the same input sound device.
432 * For example, if audio-record and voice-call usecases are currently
433 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
434 * is received for voice call then we have to make sure that audio-record
435 * usecase is also switched to earpiece i.e. voice-dmic-ef,
436 * because of the limitation that two devices cannot be enabled
437 * at the same time if they share the same backend.
438 */
439 for (i = 0; i < AUDIO_USECASE_MAX; i++)
440 switch_device[i] = false;
441
442 list_for_each(node, &adev->usecase_list) {
443 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700444 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700445 usecase != uc_info &&
446 usecase->in_snd_device != snd_device) {
447 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
448 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700449 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700450 disable_audio_route(adev, usecase, false);
451 switch_device[usecase->id] = true;
452 num_uc_to_switch++;
453 }
454 }
455
456 if (num_uc_to_switch) {
457 /* Make sure all the streams are de-routed before disabling the device */
458 audio_route_update_mixer(adev->audio_route);
459
460 list_for_each(node, &adev->usecase_list) {
461 usecase = node_to_item(node, struct audio_usecase, list);
462 if (switch_device[usecase->id]) {
463 disable_snd_device(adev, usecase->in_snd_device, false);
464 enable_snd_device(adev, snd_device, false);
465 }
466 }
467
468 /* Make sure new snd device is enabled before re-routing the streams */
469 audio_route_update_mixer(adev->audio_route);
470
471 /* Re-route all the usecases on the shared backend other than the
472 specified usecase to new snd devices */
473 list_for_each(node, &adev->usecase_list) {
474 usecase = node_to_item(node, struct audio_usecase, list);
475 /* Update the in_snd_device only before enabling the audio route */
476 if (switch_device[usecase->id] ) {
477 usecase->in_snd_device = snd_device;
478 enable_audio_route(adev, usecase, false);
479 }
480 }
481
482 audio_route_update_mixer(adev->audio_route);
483 }
484}
485
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700486static int disable_all_usecases_of_type(struct audio_device *adev,
487 usecase_type_t usecase_type,
488 bool update_mixer)
489{
490 struct audio_usecase *usecase;
491 struct listnode *node;
492 int ret = 0;
493
494 list_for_each(node, &adev->usecase_list) {
495 usecase = node_to_item(node, struct audio_usecase, list);
496 if (usecase->type == usecase_type) {
497 ALOGV("%s: usecase id %d", __func__, usecase->id);
498 ret = disable_audio_route(adev, usecase, update_mixer);
499 if (ret) {
500 ALOGE("%s: Failed to disable usecase id %d",
501 __func__, usecase->id);
502 }
503 }
504 }
505
506 return ret;
507}
508
509static int enable_all_usecases_of_type(struct audio_device *adev,
510 usecase_type_t usecase_type,
511 bool update_mixer)
512{
513 struct audio_usecase *usecase;
514 struct listnode *node;
515 int ret = 0;
516
517 list_for_each(node, &adev->usecase_list) {
518 usecase = node_to_item(node, struct audio_usecase, list);
519 if (usecase->type == usecase_type) {
520 ALOGV("%s: usecase id %d", __func__, usecase->id);
521 ret = enable_audio_route(adev, usecase, update_mixer);
522 if (ret) {
523 ALOGE("%s: Failed to enable usecase id %d",
524 __func__, usecase->id);
525 }
526 }
527 }
528
529 return ret;
530}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800531
532/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700533static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800534{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700535 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700536 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800537
538 switch (channels) {
539 /*
540 * Do not handle stereo output in Multi-channel cases
541 * Stereo case is handled in normal playback path
542 */
543 case 6:
544 ALOGV("%s: HDMI supports 5.1", __func__);
545 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
546 break;
547 case 8:
548 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
549 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
550 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
551 break;
552 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700553 ALOGE("HDMI does not support multi channel playback");
554 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800555 break;
556 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700557 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800558}
559
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700560static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
561{
562 struct audio_usecase *usecase;
563 struct listnode *node;
564
565 list_for_each(node, &adev->usecase_list) {
566 usecase = node_to_item(node, struct audio_usecase, list);
567 if (usecase->type == VOICE_CALL) {
568 ALOGV("%s: usecase id %d", __func__, usecase->id);
569 return usecase->id;
570 }
571 }
572 return USECASE_INVALID;
573}
574
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700575struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700576 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700577{
578 struct audio_usecase *usecase;
579 struct listnode *node;
580
581 list_for_each(node, &adev->usecase_list) {
582 usecase = node_to_item(node, struct audio_usecase, list);
583 if (usecase->id == uc_id)
584 return usecase;
585 }
586 return NULL;
587}
588
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700589int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800590{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800591 snd_device_t out_snd_device = SND_DEVICE_NONE;
592 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700593 struct audio_usecase *usecase = NULL;
594 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800595 struct audio_usecase *voip_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800596 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700597 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800598
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700599 usecase = get_usecase_from_list(adev, uc_id);
600 if (usecase == NULL) {
601 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
602 return -EINVAL;
603 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800604
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800605 if ((usecase->type == VOICE_CALL) ||
Vimal Puthanveed5b4d3f12013-11-05 15:57:39 -0800606 (usecase->type == VOIP_CALL) ||
607 (usecase->type == PCM_HFP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700608 out_snd_device = platform_get_output_snd_device(adev->platform,
609 usecase->stream.out->devices);
610 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700611 usecase->devices = usecase->stream.out->devices;
612 } else {
613 /*
614 * If the voice call is active, use the sound devices of voice call usecase
615 * so that it would not result any device switch. All the usecases will
616 * be switched to new device when select_devices() is called for voice call
617 * usecase. This is to avoid switching devices for voice call when
618 * check_usecases_codec_backend() is called below.
619 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700620 if (voice_is_in_call(adev)) {
621 vc_usecase = get_usecase_from_list(adev,
622 get_voice_usecase_id_from_list(adev));
Helen Zeng067b96b2013-11-26 12:10:29 -0800623 if ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
624 (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL)) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700625 in_snd_device = vc_usecase->in_snd_device;
626 out_snd_device = vc_usecase->out_snd_device;
627 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800628 } else if (voice_extn_compress_voip_is_active(adev)) {
629 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
630 if (voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
631 in_snd_device = voip_usecase->in_snd_device;
632 out_snd_device = voip_usecase->out_snd_device;
633 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700634 }
635 if (usecase->type == PCM_PLAYBACK) {
636 usecase->devices = usecase->stream.out->devices;
637 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700638 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700639 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700640 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700641 if (usecase->stream.out == adev->primary_output &&
642 adev->active_input &&
643 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
644 select_devices(adev, adev->active_input->usecase);
645 }
646 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700647 } else if (usecase->type == PCM_CAPTURE) {
648 usecase->devices = usecase->stream.in->device;
649 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700650 if (in_snd_device == SND_DEVICE_NONE) {
651 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
652 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700653 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700654 adev->primary_output->devices);
655 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700656 in_snd_device = platform_get_input_snd_device(adev->platform,
657 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700658 }
659 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700660 }
661 }
662
663 if (out_snd_device == usecase->out_snd_device &&
664 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800665 return 0;
666 }
667
sangwoobc677242013-08-08 16:53:43 +0900668 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700669 out_snd_device, platform_get_snd_device_name(out_snd_device),
670 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800671
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800672 /*
673 * Limitation: While in call, to do a device switch we need to disable
674 * and enable both RX and TX devices though one of them is same as current
675 * device.
676 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800677 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700678 status = platform_switch_voice_call_device_pre(adev->platform);
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800679 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800680 }
681
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700682 /* Disable current sound devices */
683 if (usecase->out_snd_device != SND_DEVICE_NONE) {
684 disable_audio_route(adev, usecase, true);
685 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800686 }
687
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700688 if (usecase->in_snd_device != SND_DEVICE_NONE) {
689 disable_audio_route(adev, usecase, true);
690 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800691 }
692
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700693 /* Enable new sound devices */
694 if (out_snd_device != SND_DEVICE_NONE) {
695 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
696 check_usecases_codec_backend(adev, usecase, out_snd_device);
697 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800698 }
699
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700700 if (in_snd_device != SND_DEVICE_NONE) {
701 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700702 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700703 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700704
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800705 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700706 status = platform_switch_voice_call_device_post(adev->platform,
707 out_snd_device,
708 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800709
sangwoo170731f2013-06-08 15:36:36 +0900710 audio_route_update_mixer(adev->audio_route);
711
712 usecase->in_snd_device = in_snd_device;
713 usecase->out_snd_device = out_snd_device;
714
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800715 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
716 enable_all_usecases_of_type(adev, usecase->type, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700717 else
718 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900719
Vidyakumar Athota1fd21792013-11-15 14:50:57 -0800720 /* Applicable only on the targets that has external modem.
721 * Enable device command should be sent to modem only after
722 * enabling voice call mixer controls
723 */
724 if (usecase->type == VOICE_CALL)
725 status = platform_switch_voice_call_usecase_route_post(adev->platform,
726 out_snd_device,
727 in_snd_device);
728
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800729 return status;
730}
731
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800732static int stop_input_stream(struct stream_in *in)
733{
734 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800735 struct audio_usecase *uc_info;
736 struct audio_device *adev = in->dev;
737
Eric Laurentc8400632013-02-14 19:04:54 -0800738 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800739
Eric Laurent994a6932013-07-17 11:51:42 -0700740 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700741 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800742 uc_info = get_usecase_from_list(adev, in->usecase);
743 if (uc_info == NULL) {
744 ALOGE("%s: Could not find the usecase (%d) in the list",
745 __func__, in->usecase);
746 return -EINVAL;
747 }
748
Vidyakumar Athota2850d532013-11-19 16:02:12 -0800749 /* Close in-call recording streams */
750 voice_check_and_stop_incall_rec_usecase(adev, in);
751
Eric Laurent150dbfe2013-02-27 14:31:02 -0800752 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700753 disable_audio_route(adev, uc_info, true);
754
755 /* 2. Disable the tx device */
756 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800757
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800758 list_remove(&uc_info->list);
759 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800760
Eric Laurent994a6932013-07-17 11:51:42 -0700761 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800762 return ret;
763}
764
765int start_input_stream(struct stream_in *in)
766{
767 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800768 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800769 struct audio_usecase *uc_info;
770 struct audio_device *adev = in->dev;
771
Mingming Yine62d7842013-10-25 16:26:03 -0700772 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Eric Laurent994a6932013-07-17 11:51:42 -0700773 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700774
775 /* Check if source matches incall recording usecase criteria */
776 ret = voice_check_and_set_incall_rec_usecase(adev, in);
777 if (ret)
778 goto error_config;
779 else
780 ALOGV("%s: usecase(%d)", __func__, in->usecase);
781
Eric Laurentb23d5282013-05-14 15:27:20 -0700782 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800783 if (in->pcm_device_id < 0) {
784 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
785 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800786 ret = -EINVAL;
787 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800788 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700789
790 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800791 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
792 uc_info->id = in->usecase;
793 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800794 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700795 uc_info->devices = in->device;
796 uc_info->in_snd_device = SND_DEVICE_NONE;
797 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800798
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800799 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700800 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800801
Eric Laurentc8400632013-02-14 19:04:54 -0800802 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
803 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800804 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
805 PCM_IN, &in->config);
806 if (in->pcm && !pcm_is_ready(in->pcm)) {
807 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
808 pcm_close(in->pcm);
809 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800810 ret = -EIO;
811 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800812 }
Eric Laurent994a6932013-07-17 11:51:42 -0700813 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800814 return ret;
815
816error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800817 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800818
819error_config:
820 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700821 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800822
823 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800824}
825
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700826/* must be called with out->lock locked */
827static int send_offload_cmd_l(struct stream_out* out, int command)
828{
829 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
830
831 ALOGVV("%s %d", __func__, command);
832
833 cmd->cmd = command;
834 list_add_tail(&out->offload_cmd_list, &cmd->node);
835 pthread_cond_signal(&out->offload_cond);
836 return 0;
837}
838
839/* must be called iwth out->lock locked */
840static void stop_compressed_output_l(struct stream_out *out)
841{
842 out->offload_state = OFFLOAD_STATE_IDLE;
843 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700844 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700845 if (out->compr != NULL) {
846 compress_stop(out->compr);
847 while (out->offload_thread_blocked) {
848 pthread_cond_wait(&out->cond, &out->lock);
849 }
850 }
851}
852
853static void *offload_thread_loop(void *context)
854{
855 struct stream_out *out = (struct stream_out *) context;
856 struct listnode *item;
857
858 out->offload_state = OFFLOAD_STATE_IDLE;
859 out->playback_started = 0;
860
861 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
862 set_sched_policy(0, SP_FOREGROUND);
863 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
864
865 ALOGV("%s", __func__);
866 pthread_mutex_lock(&out->lock);
867 for (;;) {
868 struct offload_cmd *cmd = NULL;
869 stream_callback_event_t event;
870 bool send_callback = false;
871
872 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
873 __func__, list_empty(&out->offload_cmd_list),
874 out->offload_state);
875 if (list_empty(&out->offload_cmd_list)) {
876 ALOGV("%s SLEEPING", __func__);
877 pthread_cond_wait(&out->offload_cond, &out->lock);
878 ALOGV("%s RUNNING", __func__);
879 continue;
880 }
881
882 item = list_head(&out->offload_cmd_list);
883 cmd = node_to_item(item, struct offload_cmd, node);
884 list_remove(item);
885
886 ALOGVV("%s STATE %d CMD %d out->compr %p",
887 __func__, out->offload_state, cmd->cmd, out->compr);
888
889 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
890 free(cmd);
891 break;
892 }
893
894 if (out->compr == NULL) {
895 ALOGE("%s: Compress handle is NULL", __func__);
896 pthread_cond_signal(&out->cond);
897 continue;
898 }
899 out->offload_thread_blocked = true;
900 pthread_mutex_unlock(&out->lock);
901 send_callback = false;
902 switch(cmd->cmd) {
903 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
904 compress_wait(out->compr, -1);
905 send_callback = true;
906 event = STREAM_CBK_EVENT_WRITE_READY;
907 break;
908 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700909 compress_next_track(out->compr);
910 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700911 send_callback = true;
912 event = STREAM_CBK_EVENT_DRAIN_READY;
913 break;
914 case OFFLOAD_CMD_DRAIN:
915 compress_drain(out->compr);
916 send_callback = true;
917 event = STREAM_CBK_EVENT_DRAIN_READY;
918 break;
919 default:
920 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
921 break;
922 }
923 pthread_mutex_lock(&out->lock);
924 out->offload_thread_blocked = false;
925 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700926 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700927 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700928 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700929 free(cmd);
930 }
931
932 pthread_cond_signal(&out->cond);
933 while (!list_empty(&out->offload_cmd_list)) {
934 item = list_head(&out->offload_cmd_list);
935 list_remove(item);
936 free(node_to_item(item, struct offload_cmd, node));
937 }
938 pthread_mutex_unlock(&out->lock);
939
940 return NULL;
941}
942
943static int create_offload_callback_thread(struct stream_out *out)
944{
945 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
946 list_init(&out->offload_cmd_list);
947 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
948 offload_thread_loop, out);
949 return 0;
950}
951
952static int destroy_offload_callback_thread(struct stream_out *out)
953{
954 pthread_mutex_lock(&out->lock);
955 stop_compressed_output_l(out);
956 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
957
958 pthread_mutex_unlock(&out->lock);
959 pthread_join(out->offload_thread, (void **) NULL);
960 pthread_cond_destroy(&out->offload_cond);
961
962 return 0;
963}
964
Eric Laurent07eeafd2013-10-06 12:52:49 -0700965static bool allow_hdmi_channel_config(struct audio_device *adev)
966{
967 struct listnode *node;
968 struct audio_usecase *usecase;
969 bool ret = true;
970
971 list_for_each(node, &adev->usecase_list) {
972 usecase = node_to_item(node, struct audio_usecase, list);
973 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
974 /*
975 * If voice call is already existing, do not proceed further to avoid
976 * disabling/enabling both RX and TX devices, CSD calls, etc.
977 * Once the voice call done, the HDMI channels can be configured to
978 * max channels of remaining use cases.
979 */
980 if (usecase->id == USECASE_VOICE_CALL) {
981 ALOGD("%s: voice call is active, no change in HDMI channels",
982 __func__);
983 ret = false;
984 break;
985 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
986 ALOGD("%s: multi channel playback is active, "
987 "no change in HDMI channels", __func__);
988 ret = false;
989 break;
990 }
991 }
992 }
993 return ret;
994}
995
996static int check_and_set_hdmi_channels(struct audio_device *adev,
997 unsigned int channels)
998{
999 struct listnode *node;
1000 struct audio_usecase *usecase;
1001
1002 /* Check if change in HDMI channel config is allowed */
1003 if (!allow_hdmi_channel_config(adev))
1004 return 0;
1005
1006 if (channels == adev->cur_hdmi_channels) {
Mingming Yin10fef6a2013-11-26 17:17:01 -08001007 ALOGD("%s: Requested channels are same as current channels(%d)", __func__, channels);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001008 return 0;
1009 }
1010
1011 platform_set_hdmi_channels(adev->platform, channels);
1012 adev->cur_hdmi_channels = channels;
1013
1014 /*
1015 * Deroute all the playback streams routed to HDMI so that
1016 * the back end is deactivated. Note that backend will not
1017 * be deactivated if any one stream is connected to it.
1018 */
1019 list_for_each(node, &adev->usecase_list) {
1020 usecase = node_to_item(node, struct audio_usecase, list);
1021 if (usecase->type == PCM_PLAYBACK &&
1022 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1023 disable_audio_route(adev, usecase, true);
1024 }
1025 }
1026
1027 /*
1028 * Enable all the streams disabled above. Now the HDMI backend
1029 * will be activated with new channel configuration
1030 */
1031 list_for_each(node, &adev->usecase_list) {
1032 usecase = node_to_item(node, struct audio_usecase, list);
1033 if (usecase->type == PCM_PLAYBACK &&
1034 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1035 enable_audio_route(adev, usecase, true);
1036 }
1037 }
1038
1039 return 0;
1040}
1041
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001042static int stop_output_stream(struct stream_out *out)
1043{
1044 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001045 struct audio_usecase *uc_info;
1046 struct audio_device *adev = out->dev;
1047
Eric Laurent994a6932013-07-17 11:51:42 -07001048 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001049 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001050 uc_info = get_usecase_from_list(adev, out->usecase);
1051 if (uc_info == NULL) {
1052 ALOGE("%s: Could not find the usecase (%d) in the list",
1053 __func__, out->usecase);
1054 return -EINVAL;
1055 }
1056
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001057 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1058 if (adev->visualizer_stop_output != NULL)
1059 adev->visualizer_stop_output(out->handle, out->pcm_device_id);
1060 if (adev->offload_effects_stop_output != NULL)
1061 adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
1062 }
Eric Laurentc4aef752013-09-12 17:45:53 -07001063
Eric Laurent150dbfe2013-02-27 14:31:02 -08001064 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001065 disable_audio_route(adev, uc_info, true);
1066
1067 /* 2. Disable the rx device */
1068 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001069
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001070 list_remove(&uc_info->list);
1071 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001072
Eric Laurent07eeafd2013-10-06 12:52:49 -07001073 /* Must be called after removing the usecase from list */
1074 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1075 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1076
Eric Laurent994a6932013-07-17 11:51:42 -07001077 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001078 return ret;
1079}
1080
1081int start_output_stream(struct stream_out *out)
1082{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001083 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001084 struct audio_usecase *uc_info;
1085 struct audio_device *adev = out->dev;
1086
Eric Laurent994a6932013-07-17 11:51:42 -07001087 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001088 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001089 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001090 if (out->pcm_device_id < 0) {
1091 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1092 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001093 ret = -EINVAL;
1094 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001095 }
1096
1097 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1098 uc_info->id = out->usecase;
1099 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001100 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001101 uc_info->devices = out->devices;
1102 uc_info->in_snd_device = SND_DEVICE_NONE;
1103 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001104
Eric Laurent07eeafd2013-10-06 12:52:49 -07001105 /* This must be called before adding this usecase to the list */
Mingming Yin10fef6a2013-11-26 17:17:01 -08001106 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1107 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1108 check_and_set_hdmi_channels(adev, out->compr_config.codec->ch_in);
1109 else
1110 check_and_set_hdmi_channels(adev, out->config.channels);
1111 }
Eric Laurent07eeafd2013-10-06 12:52:49 -07001112
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001113 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001114
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001115 select_devices(adev, out->usecase);
1116
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001117 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1118 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001119 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1120 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001121 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;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001131 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1132 COMPRESS_IN, &out->compr_config);
1133 if (out->compr && !is_compress_ready(out->compr)) {
1134 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1135 compress_close(out->compr);
1136 out->compr = NULL;
1137 ret = -EIO;
1138 goto error_open;
1139 }
1140 if (out->offload_callback)
1141 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001142
1143 if (adev->visualizer_start_output != NULL)
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001144 adev->visualizer_start_output(out->handle, out->pcm_device_id);
1145 if (adev->offload_effects_start_output != NULL)
1146 adev->offload_effects_start_output(out->handle, out->pcm_device_id);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001147 }
Eric Laurent994a6932013-07-17 11:51:42 -07001148 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001149 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001150error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001151 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001152error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001153 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001154}
1155
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001156static int check_input_parameters(uint32_t sample_rate,
1157 audio_format_t format,
1158 int channel_count)
1159{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001160 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001161
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001162 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001163 !voice_extn_compress_voip_is_format_supported(format) &&
1164 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001165
1166 switch (channel_count) {
1167 case 1:
1168 case 2:
1169 case 6:
1170 break;
1171 default:
1172 ret = -EINVAL;
1173 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001174
1175 switch (sample_rate) {
1176 case 8000:
1177 case 11025:
1178 case 12000:
1179 case 16000:
1180 case 22050:
1181 case 24000:
1182 case 32000:
1183 case 44100:
1184 case 48000:
1185 break;
1186 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001187 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001188 }
1189
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001190 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001191}
1192
1193static size_t get_input_buffer_size(uint32_t sample_rate,
1194 audio_format_t format,
1195 int channel_count)
1196{
1197 size_t size = 0;
1198
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001199 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1200 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001202 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1203 /* ToDo: should use frame_size computed based on the format and
1204 channel_count here. */
1205 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001206
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001207 /* make sure the size is multiple of 64 */
1208 size += 0x3f;
1209 size &= ~0x3f;
1210
1211 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001212}
1213
1214static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1215{
1216 struct stream_out *out = (struct stream_out *)stream;
1217
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001218 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001219}
1220
1221static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1222{
1223 return -ENOSYS;
1224}
1225
1226static size_t out_get_buffer_size(const struct audio_stream *stream)
1227{
1228 struct stream_out *out = (struct stream_out *)stream;
1229
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001230 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001231 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001232 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1233 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001234
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001235 return out->config.period_size * audio_stream_frame_size(stream);
1236}
1237
1238static uint32_t out_get_channels(const struct audio_stream *stream)
1239{
1240 struct stream_out *out = (struct stream_out *)stream;
1241
1242 return out->channel_mask;
1243}
1244
1245static audio_format_t out_get_format(const struct audio_stream *stream)
1246{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001247 struct stream_out *out = (struct stream_out *)stream;
1248
1249 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001250}
1251
1252static int out_set_format(struct audio_stream *stream, audio_format_t format)
1253{
1254 return -ENOSYS;
1255}
1256
1257static int out_standby(struct audio_stream *stream)
1258{
1259 struct stream_out *out = (struct stream_out *)stream;
1260 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001261
Eric Laurent994a6932013-07-17 11:51:42 -07001262 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001263 out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001264 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1265 /* Ignore standby in case of voip call because the voip output
1266 * stream is closed in adev_close_output_stream()
1267 */
1268 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1269 return 0;
1270 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001271
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001272 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001273 if (!out->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001274 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001275 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001276 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1277 if (out->pcm) {
1278 pcm_close(out->pcm);
1279 out->pcm = NULL;
1280 }
1281 } else {
1282 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001283 out->gapless_mdata.encoder_delay = 0;
1284 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001285 if (out->compr != NULL) {
1286 compress_close(out->compr);
1287 out->compr = NULL;
1288 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001289 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001290 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001291 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001292 }
1293 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001294 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001295 return 0;
1296}
1297
1298static int out_dump(const struct audio_stream *stream, int fd)
1299{
1300 return 0;
1301}
1302
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001303static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1304{
1305 int ret = 0;
1306 char value[32];
1307 struct compr_gapless_mdata tmp_mdata;
1308
1309 if (!out || !parms) {
1310 return -EINVAL;
1311 }
1312
1313 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1314 if (ret >= 0) {
1315 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1316 } else {
1317 return -EINVAL;
1318 }
1319
1320 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1321 if (ret >= 0) {
1322 tmp_mdata.encoder_padding = atoi(value);
1323 } else {
1324 return -EINVAL;
1325 }
1326
1327 out->gapless_mdata = tmp_mdata;
1328 out->send_new_metadata = 1;
1329 ALOGV("%s new encoder delay %u and padding %u", __func__,
1330 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1331
1332 return 0;
1333}
1334
1335
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001336static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1337{
1338 struct stream_out *out = (struct stream_out *)stream;
1339 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001340 struct audio_usecase *usecase;
1341 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001342 struct str_parms *parms;
1343 char value[32];
1344 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001345 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001346
sangwoobc677242013-08-08 16:53:43 +09001347 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001348 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001349 parms = str_parms_create_str(kvpairs);
1350 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1351 if (ret >= 0) {
1352 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001353 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001354 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001355
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001356 /*
1357 * When HDMI cable is unplugged the music playback is paused and
1358 * the policy manager sends routing=0. But the audioflinger
1359 * continues to write data until standby time (3sec).
1360 * As the HDMI core is turned off, the write gets blocked.
1361 * Avoid this by routing audio to speaker until standby.
1362 */
1363 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1364 val == AUDIO_DEVICE_NONE) {
1365 val = AUDIO_DEVICE_OUT_SPEAKER;
1366 }
1367
1368 /*
1369 * select_devices() call below switches all the usecases on the same
1370 * backend to the new device. Refer to check_usecases_codec_backend() in
1371 * the select_devices(). But how do we undo this?
1372 *
1373 * For example, music playback is active on headset (deep-buffer usecase)
1374 * and if we go to ringtones and select a ringtone, low-latency usecase
1375 * will be started on headset+speaker. As we can't enable headset+speaker
1376 * and headset devices at the same time, select_devices() switches the music
1377 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1378 * So when the ringtone playback is completed, how do we undo the same?
1379 *
1380 * We are relying on the out_set_parameters() call on deep-buffer output,
1381 * once the ringtone playback is ended.
1382 * NOTE: We should not check if the current devices are same as new devices.
1383 * Because select_devices() must be called to switch back the music
1384 * playback to headset.
1385 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001386 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001387 out->devices = val;
1388
1389 if (!out->standby)
1390 select_devices(adev, out->usecase);
1391
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001392 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1393 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001394 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001395 voice_start_call(adev);
1396 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1397 voice_is_in_call(adev) &&
1398 (out == adev->primary_output)) {
1399 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001400 }
1401 }
1402
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001403 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1404 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001405 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001406 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001407 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001408
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001410 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001411 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001412
1413 if (out == adev->primary_output) {
1414 pthread_mutex_lock(&adev->lock);
1415 audio_extn_set_parameters(adev, parms);
1416 pthread_mutex_unlock(&adev->lock);
1417 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001418 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1419 parse_compress_metadata(out, parms);
1420 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001421
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001422 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001423 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001424 return ret;
1425}
1426
1427static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1428{
1429 struct stream_out *out = (struct stream_out *)stream;
1430 struct str_parms *query = str_parms_create_str(keys);
1431 char *str;
1432 char value[256];
1433 struct str_parms *reply = str_parms_create();
1434 size_t i, j;
1435 int ret;
1436 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001437 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001438 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1439 if (ret >= 0) {
1440 value[0] = '\0';
1441 i = 0;
1442 while (out->supported_channel_masks[i] != 0) {
1443 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1444 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1445 if (!first) {
1446 strcat(value, "|");
1447 }
1448 strcat(value, out_channels_name_to_enum_table[j].name);
1449 first = false;
1450 break;
1451 }
1452 }
1453 i++;
1454 }
1455 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1456 str = str_parms_to_str(reply);
1457 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001458 voice_extn_out_get_parameters(out, query, reply);
1459 str = str_parms_to_str(reply);
1460 if (!strncmp(str, "", sizeof(""))) {
1461 str = strdup(keys);
1462 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001463 }
1464 str_parms_destroy(query);
1465 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001466 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001467 return str;
1468}
1469
1470static uint32_t out_get_latency(const struct audio_stream_out *stream)
1471{
1472 struct stream_out *out = (struct stream_out *)stream;
1473
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001474 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1475 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1476
1477 return (out->config.period_count * out->config.period_size * 1000) /
1478 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001479}
1480
1481static int out_set_volume(struct audio_stream_out *stream, float left,
1482 float right)
1483{
Eric Laurenta9024de2013-04-04 09:19:12 -07001484 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001485 int volume[2];
1486
Eric Laurenta9024de2013-04-04 09:19:12 -07001487 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1488 /* only take left channel into account: the API is for stereo anyway */
1489 out->muted = (left == 0.0f);
1490 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001491 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001492 char mixer_ctl_name[128];
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001493 struct audio_device *adev = out->dev;
1494 struct mixer_ctl *ctl;
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001495 int pcm_device_id = platform_get_pcm_device_id(out->usecase,
1496 PCM_PLAYBACK);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001497
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08001498 snprintf(mixer_ctl_name, sizeof(mixer_ctl_name),
1499 "Compress Playback %d Volume", pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001500 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1501 if (!ctl) {
1502 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1503 __func__, mixer_ctl_name);
1504 return -EINVAL;
1505 }
1506 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1507 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1508 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1509 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001510 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001511
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001512 return -ENOSYS;
1513}
1514
1515static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1516 size_t bytes)
1517{
1518 struct stream_out *out = (struct stream_out *)stream;
1519 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001520 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001521
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001522 pthread_mutex_lock(&out->lock);
1523 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001524 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001525 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001526 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1527 ret = voice_extn_compress_voip_start_output_stream(out);
1528 else
1529 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001530 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001531 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001532 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001533 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001534 goto exit;
1535 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001536 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001537
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001538 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001539 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1540 if (out->send_new_metadata) {
1541 ALOGVV("send new gapless metadata");
1542 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1543 out->send_new_metadata = 0;
1544 }
1545
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001546 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001547 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001548 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001549 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1550 }
1551 if (!out->playback_started) {
1552 compress_start(out->compr);
1553 out->playback_started = 1;
1554 out->offload_state = OFFLOAD_STATE_PLAYING;
1555 }
1556 pthread_mutex_unlock(&out->lock);
1557 return ret;
1558 } else {
1559 if (out->pcm) {
1560 if (out->muted)
1561 memset((void *)buffer, 0, bytes);
1562 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1563 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001564 if (ret == 0)
1565 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001566 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001567 }
1568
1569exit:
1570 pthread_mutex_unlock(&out->lock);
1571
1572 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001573 if (out->pcm)
1574 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001575 out_standby(&out->stream.common);
1576 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1577 out_get_sample_rate(&out->stream.common));
1578 }
1579 return bytes;
1580}
1581
1582static int out_get_render_position(const struct audio_stream_out *stream,
1583 uint32_t *dsp_frames)
1584{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001585 struct stream_out *out = (struct stream_out *)stream;
1586 *dsp_frames = 0;
1587 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1588 pthread_mutex_lock(&out->lock);
1589 if (out->compr != NULL) {
1590 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1591 &out->sample_rate);
1592 ALOGVV("%s rendered frames %d sample_rate %d",
1593 __func__, *dsp_frames, out->sample_rate);
1594 }
1595 pthread_mutex_unlock(&out->lock);
1596 return 0;
1597 } else
1598 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001599}
1600
1601static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1602{
1603 return 0;
1604}
1605
1606static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1607{
1608 return 0;
1609}
1610
1611static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1612 int64_t *timestamp)
1613{
1614 return -EINVAL;
1615}
1616
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001617static int out_get_presentation_position(const struct audio_stream_out *stream,
1618 uint64_t *frames, struct timespec *timestamp)
1619{
1620 struct stream_out *out = (struct stream_out *)stream;
1621 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001622 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001623
1624 pthread_mutex_lock(&out->lock);
1625
Eric Laurent949a0892013-09-20 09:20:13 -07001626 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1627 if (out->compr != NULL) {
1628 compress_get_tstamp(out->compr, &dsp_frames,
1629 &out->sample_rate);
1630 ALOGVV("%s rendered frames %ld sample_rate %d",
1631 __func__, dsp_frames, out->sample_rate);
1632 *frames = dsp_frames;
1633 ret = 0;
1634 /* this is the best we can do */
1635 clock_gettime(CLOCK_MONOTONIC, timestamp);
1636 }
1637 } else {
1638 if (out->pcm) {
1639 size_t avail;
1640 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1641 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001642 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001643 // This adjustment accounts for buffering after app processor.
1644 // It is based on estimated DSP latency per use case, rather than exact.
1645 signed_frames -=
1646 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1647
Eric Laurent949a0892013-09-20 09:20:13 -07001648 // It would be unusual for this value to be negative, but check just in case ...
1649 if (signed_frames >= 0) {
1650 *frames = signed_frames;
1651 ret = 0;
1652 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001653 }
1654 }
1655 }
1656
1657 pthread_mutex_unlock(&out->lock);
1658
1659 return ret;
1660}
1661
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001662static int out_set_callback(struct audio_stream_out *stream,
1663 stream_callback_t callback, void *cookie)
1664{
1665 struct stream_out *out = (struct stream_out *)stream;
1666
1667 ALOGV("%s", __func__);
1668 pthread_mutex_lock(&out->lock);
1669 out->offload_callback = callback;
1670 out->offload_cookie = cookie;
1671 pthread_mutex_unlock(&out->lock);
1672 return 0;
1673}
1674
1675static int out_pause(struct audio_stream_out* stream)
1676{
1677 struct stream_out *out = (struct stream_out *)stream;
1678 int status = -ENOSYS;
1679 ALOGV("%s", __func__);
1680 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1681 pthread_mutex_lock(&out->lock);
1682 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1683 status = compress_pause(out->compr);
1684 out->offload_state = OFFLOAD_STATE_PAUSED;
1685 }
1686 pthread_mutex_unlock(&out->lock);
1687 }
1688 return status;
1689}
1690
1691static int out_resume(struct audio_stream_out* stream)
1692{
1693 struct stream_out *out = (struct stream_out *)stream;
1694 int status = -ENOSYS;
1695 ALOGV("%s", __func__);
1696 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1697 status = 0;
1698 pthread_mutex_lock(&out->lock);
1699 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1700 status = compress_resume(out->compr);
1701 out->offload_state = OFFLOAD_STATE_PLAYING;
1702 }
1703 pthread_mutex_unlock(&out->lock);
1704 }
1705 return status;
1706}
1707
1708static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1709{
1710 struct stream_out *out = (struct stream_out *)stream;
1711 int status = -ENOSYS;
1712 ALOGV("%s", __func__);
1713 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1714 pthread_mutex_lock(&out->lock);
1715 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1716 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1717 else
1718 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1719 pthread_mutex_unlock(&out->lock);
1720 }
1721 return status;
1722}
1723
1724static int out_flush(struct audio_stream_out* stream)
1725{
1726 struct stream_out *out = (struct stream_out *)stream;
1727 ALOGV("%s", __func__);
1728 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1729 pthread_mutex_lock(&out->lock);
1730 stop_compressed_output_l(out);
1731 pthread_mutex_unlock(&out->lock);
1732 return 0;
1733 }
1734 return -ENOSYS;
1735}
1736
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001737/** audio_stream_in implementation **/
1738static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1739{
1740 struct stream_in *in = (struct stream_in *)stream;
1741
1742 return in->config.rate;
1743}
1744
1745static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1746{
1747 return -ENOSYS;
1748}
1749
1750static size_t in_get_buffer_size(const struct audio_stream *stream)
1751{
1752 struct stream_in *in = (struct stream_in *)stream;
1753
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001754 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1755 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001756 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1757 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001758
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001759 return in->config.period_size * audio_stream_frame_size(stream);
1760}
1761
1762static uint32_t in_get_channels(const struct audio_stream *stream)
1763{
1764 struct stream_in *in = (struct stream_in *)stream;
1765
1766 return in->channel_mask;
1767}
1768
1769static audio_format_t in_get_format(const struct audio_stream *stream)
1770{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001771 struct stream_in *in = (struct stream_in *)stream;
1772
1773 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001774}
1775
1776static int in_set_format(struct audio_stream *stream, audio_format_t format)
1777{
1778 return -ENOSYS;
1779}
1780
1781static int in_standby(struct audio_stream *stream)
1782{
1783 struct stream_in *in = (struct stream_in *)stream;
1784 struct audio_device *adev = in->dev;
1785 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001786 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001787
1788 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1789 /* Ignore standby in case of voip call because the voip input
1790 * stream is closed in adev_close_input_stream()
1791 */
1792 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1793 return status;
1794 }
1795
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001796 pthread_mutex_lock(&in->lock);
1797 if (!in->standby) {
Ravi Kumar Alamanda8bba9e92013-11-11 21:09:07 -08001798 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001799 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001800 if (in->pcm) {
1801 pcm_close(in->pcm);
1802 in->pcm = NULL;
1803 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001804 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001805 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001806 }
1807 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001808 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001809 return status;
1810}
1811
1812static int in_dump(const struct audio_stream *stream, int fd)
1813{
1814 return 0;
1815}
1816
1817static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1818{
1819 struct stream_in *in = (struct stream_in *)stream;
1820 struct audio_device *adev = in->dev;
1821 struct str_parms *parms;
1822 char *str;
1823 char value[32];
1824 int ret, val = 0;
1825
Eric Laurent994a6932013-07-17 11:51:42 -07001826 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001827 parms = str_parms_create_str(kvpairs);
1828
1829 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1830
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001831 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001832 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001833 if (ret >= 0) {
1834 val = atoi(value);
1835 /* no audio source uses val == 0 */
1836 if ((in->source != val) && (val != 0)) {
1837 in->source = val;
1838 }
1839 }
1840
1841 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1842 if (ret >= 0) {
1843 val = atoi(value);
1844 if ((in->device != val) && (val != 0)) {
1845 in->device = val;
1846 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001847 if (!in->standby)
1848 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001849 }
1850 }
1851
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001852 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001853 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001854
1855 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001856 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001857 return ret;
1858}
1859
1860static char* in_get_parameters(const struct audio_stream *stream,
1861 const char *keys)
1862{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001863 struct stream_in *in = (struct stream_in *)stream;
1864 struct str_parms *query = str_parms_create_str(keys);
1865 char *str;
1866 char value[256];
1867 struct str_parms *reply = str_parms_create();
1868 ALOGV("%s: enter: keys - %s", __func__, keys);
1869
1870 voice_extn_in_get_parameters(in, query, reply);
1871
1872 str = str_parms_to_str(reply);
1873 str_parms_destroy(query);
1874 str_parms_destroy(reply);
1875
1876 ALOGV("%s: exit: returns - %s", __func__, str);
1877 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001878}
1879
1880static int in_set_gain(struct audio_stream_in *stream, float gain)
1881{
1882 return 0;
1883}
1884
1885static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1886 size_t bytes)
1887{
1888 struct stream_in *in = (struct stream_in *)stream;
1889 struct audio_device *adev = in->dev;
1890 int i, ret = -1;
1891
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001892 pthread_mutex_lock(&in->lock);
1893 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001894 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001895 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
1896 ret = voice_extn_compress_voip_start_input_stream(in);
1897 else
1898 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001899 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001900 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001901 goto exit;
1902 }
1903 in->standby = 0;
1904 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001905
1906 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001907 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1908 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07001909 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
1910 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001911 else
1912 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001913 }
1914
1915 /*
1916 * Instead of writing zeroes here, we could trust the hardware
1917 * to always provide zeroes when muted.
1918 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001919 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001920 memset(buffer, 0, bytes);
1921
1922exit:
1923 pthread_mutex_unlock(&in->lock);
1924
1925 if (ret != 0) {
1926 in_standby(&in->stream.common);
1927 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1928 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1929 in_get_sample_rate(&in->stream.common));
1930 }
1931 return bytes;
1932}
1933
1934static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1935{
1936 return 0;
1937}
1938
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001939static int add_remove_audio_effect(const struct audio_stream *stream,
1940 effect_handle_t effect,
1941 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001942{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001943 struct stream_in *in = (struct stream_in *)stream;
1944 int status = 0;
1945 effect_descriptor_t desc;
1946
1947 status = (*effect)->get_descriptor(effect, &desc);
1948 if (status != 0)
1949 return status;
1950
1951 pthread_mutex_lock(&in->lock);
1952 pthread_mutex_lock(&in->dev->lock);
1953 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1954 in->enable_aec != enable &&
1955 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1956 in->enable_aec = enable;
1957 if (!in->standby)
1958 select_devices(in->dev, in->usecase);
1959 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08001960 if (in->enable_ns != enable &&
1961 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1962 in->enable_ns = enable;
1963 if (!in->standby)
1964 select_devices(in->dev, in->usecase);
1965 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001966 pthread_mutex_unlock(&in->dev->lock);
1967 pthread_mutex_unlock(&in->lock);
1968
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001969 return 0;
1970}
1971
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001972static int in_add_audio_effect(const struct audio_stream *stream,
1973 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001974{
Eric Laurent994a6932013-07-17 11:51:42 -07001975 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001976 return add_remove_audio_effect(stream, effect, true);
1977}
1978
1979static int in_remove_audio_effect(const struct audio_stream *stream,
1980 effect_handle_t effect)
1981{
Eric Laurent994a6932013-07-17 11:51:42 -07001982 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001983 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001984}
1985
1986static int adev_open_output_stream(struct audio_hw_device *dev,
1987 audio_io_handle_t handle,
1988 audio_devices_t devices,
1989 audio_output_flags_t flags,
1990 struct audio_config *config,
1991 struct audio_stream_out **stream_out)
1992{
1993 struct audio_device *adev = (struct audio_device *)dev;
1994 struct stream_out *out;
1995 int i, ret;
1996
Eric Laurent994a6932013-07-17 11:51:42 -07001997 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001998 __func__, config->sample_rate, config->channel_mask, devices, flags);
1999 *stream_out = NULL;
2000 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
2001
2002 if (devices == AUDIO_DEVICE_NONE)
2003 devices = AUDIO_DEVICE_OUT_SPEAKER;
2004
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002005 out->flags = flags;
2006 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07002007 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002008 out->format = config->format;
2009 out->sample_rate = config->sample_rate;
2010 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
2011 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07002012 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002013
2014 /* Init use case and pcm_config */
Ravi Kumar Alamanda86b0e2b2013-11-20 14:52:30 -08002015 if (out->flags == AUDIO_OUTPUT_FLAG_DIRECT &&
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002016 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002017 pthread_mutex_lock(&adev->lock);
2018 ret = read_hdmi_channel_masks(out);
2019 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07002020 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002021 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002022
2023 if (config->sample_rate == 0)
2024 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
2025 if (config->channel_mask == 0)
2026 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
2027
2028 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002029 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002030 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
2031 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002032 out->config.rate = config->sample_rate;
2033 out->config.channels = popcount(out->channel_mask);
2034 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002035 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2036 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002037 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002038 ret = voice_extn_compress_voip_open_output_stream(out);
2039 if (ret != 0) {
2040 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2041 __func__, ret);
2042 goto error_open;
2043 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002044 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2045 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2046 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2047 ALOGE("%s: Unsupported Offload information", __func__);
2048 ret = -EINVAL;
2049 goto error_open;
2050 }
Mingming Yin90310102013-11-13 16:57:00 -08002051 if (!is_supported_format(config->offload_info.format) &&
2052 !audio_extn_dolby_is_supported_format(config->offload_info.format)) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002053 ALOGE("%s: Unsupported audio format", __func__);
2054 ret = -EINVAL;
2055 goto error_open;
2056 }
2057
2058 out->compr_config.codec = (struct snd_codec *)
2059 calloc(1, sizeof(struct snd_codec));
2060
2061 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2062 if (config->offload_info.channel_mask)
2063 out->channel_mask = config->offload_info.channel_mask;
2064 else if (config->channel_mask)
2065 out->channel_mask = config->channel_mask;
2066 out->format = config->offload_info.format;
2067 out->sample_rate = config->offload_info.sample_rate;
2068
2069 out->stream.set_callback = out_set_callback;
2070 out->stream.pause = out_pause;
2071 out->stream.resume = out_resume;
2072 out->stream.drain = out_drain;
2073 out->stream.flush = out_flush;
2074
Mingming Yin90310102013-11-13 16:57:00 -08002075 if (audio_extn_dolby_is_supported_format(config->offload_info.format))
2076 out->compr_config.codec->id =
2077 audio_extn_dolby_get_snd_codec_id(config->offload_info.format);
2078 else
2079 out->compr_config.codec->id =
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002080 get_snd_codec_id(config->offload_info.format);
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -08002081 out->compr_config.fragment_size = get_offload_buffer_size();
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002082 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2083 out->compr_config.codec->sample_rate =
2084 compress_get_alsa_rate(config->offload_info.sample_rate);
2085 out->compr_config.codec->bit_rate =
2086 config->offload_info.bit_rate;
2087 out->compr_config.codec->ch_in =
2088 popcount(config->channel_mask);
2089 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2090
2091 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2092 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002093
2094 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002095 create_offload_callback_thread(out);
2096 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2097 __func__, config->offload_info.version,
2098 config->offload_info.bit_rate);
Mingming Yin90310102013-11-13 16:57:00 -08002099
2100 if (audio_extn_dolby_is_supported_format(out->format)) {
2101 ret = audio_extn_dolby_set_DMID(adev);
2102 if (ret != 0) {
2103 ALOGE("%s: Dolby DMID cannot be set error:%d",
2104 __func__, ret);
2105 goto error_open;
2106 }
2107 }
2108
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002109 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2110 ret = voice_check_and_set_incall_music_usecase(adev, out);
2111 if (ret != 0) {
2112 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2113 __func__, ret);
2114 goto error_open;
2115 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002116 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002117 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2118 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002119 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002120 } else {
2121 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2122 out->config = pcm_config_deep_buffer;
2123 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002124 }
2125
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002126 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2127 if(adev->primary_output == NULL)
2128 adev->primary_output = out;
2129 else {
2130 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002131 ret = -EEXIST;
2132 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002133 }
2134 }
2135
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002136 /* Check if this usecase is already existing */
2137 pthread_mutex_lock(&adev->lock);
2138 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2139 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002140 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002141 ret = -EEXIST;
2142 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002143 }
2144 pthread_mutex_unlock(&adev->lock);
2145
2146 out->stream.common.get_sample_rate = out_get_sample_rate;
2147 out->stream.common.set_sample_rate = out_set_sample_rate;
2148 out->stream.common.get_buffer_size = out_get_buffer_size;
2149 out->stream.common.get_channels = out_get_channels;
2150 out->stream.common.get_format = out_get_format;
2151 out->stream.common.set_format = out_set_format;
2152 out->stream.common.standby = out_standby;
2153 out->stream.common.dump = out_dump;
2154 out->stream.common.set_parameters = out_set_parameters;
2155 out->stream.common.get_parameters = out_get_parameters;
2156 out->stream.common.add_audio_effect = out_add_audio_effect;
2157 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2158 out->stream.get_latency = out_get_latency;
2159 out->stream.set_volume = out_set_volume;
2160 out->stream.write = out_write;
2161 out->stream.get_render_position = out_get_render_position;
2162 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002163 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002164
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002165 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002166 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002167 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002168
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002169 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2170 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002171
2172 config->format = out->stream.common.get_format(&out->stream.common);
2173 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2174 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2175
2176 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002177 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002178 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002179
2180error_open:
2181 free(out);
2182 *stream_out = NULL;
2183 ALOGD("%s: exit: ret %d", __func__, ret);
2184 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002185}
2186
2187static void adev_close_output_stream(struct audio_hw_device *dev,
2188 struct audio_stream_out *stream)
2189{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002190 struct stream_out *out = (struct stream_out *)stream;
2191 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002192 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002193
Eric Laurent994a6932013-07-17 11:51:42 -07002194 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002195 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2196 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2197 if(ret != 0)
2198 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2199 __func__, ret);
2200 }
2201 else
2202 out_standby(&stream->common);
2203
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002204 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2205 destroy_offload_callback_thread(out);
2206
2207 if (out->compr_config.codec != NULL)
2208 free(out->compr_config.codec);
2209 }
2210 pthread_cond_destroy(&out->cond);
2211 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002212 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002213 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002214}
2215
2216static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2217{
2218 struct audio_device *adev = (struct audio_device *)dev;
2219 struct str_parms *parms;
2220 char *str;
2221 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002222 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002223 int ret;
2224
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002225 ALOGD("%s: enter: %s", __func__, kvpairs);
2226
2227 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002228 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002229
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002230 voice_set_parameters(adev, parms);
2231 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002232
2233 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2234 if (ret >= 0) {
2235 /* When set to false, HAL should disable EC and NS
2236 * But it is currently not supported.
2237 */
2238 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2239 adev->bluetooth_nrec = true;
2240 else
2241 adev->bluetooth_nrec = false;
2242 }
2243
2244 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2245 if (ret >= 0) {
2246 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2247 adev->screen_off = false;
2248 else
2249 adev->screen_off = true;
2250 }
2251
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002252 ret = str_parms_get_int(parms, "rotation", &val);
2253 if (ret >= 0) {
2254 bool reverse_speakers = false;
2255 switch(val) {
2256 // FIXME: note that the code below assumes that the speakers are in the correct placement
2257 // relative to the user when the device is rotated 90deg from its default rotation. This
2258 // assumption is device-specific, not platform-specific like this code.
2259 case 270:
2260 reverse_speakers = true;
2261 break;
2262 case 0:
2263 case 90:
2264 case 180:
2265 break;
2266 default:
2267 ALOGE("%s: unexpected rotation of %d", __func__, val);
2268 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002269 if (adev->speaker_lr_swap != reverse_speakers) {
2270 adev->speaker_lr_swap = reverse_speakers;
2271 // only update the selected device if there is active pcm playback
2272 struct audio_usecase *usecase;
2273 struct listnode *node;
2274 list_for_each(node, &adev->usecase_list) {
2275 usecase = node_to_item(node, struct audio_usecase, list);
2276 if (usecase->type == PCM_PLAYBACK) {
2277 select_devices(adev, usecase->id);
2278 break;
2279 }
2280 }
2281 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002282 }
2283
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002284 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002285 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002286
2287 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002288 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002289 return ret;
2290}
2291
2292static char* adev_get_parameters(const struct audio_hw_device *dev,
2293 const char *keys)
2294{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002295 struct audio_device *adev = (struct audio_device *)dev;
2296 struct str_parms *reply = str_parms_create();
2297 struct str_parms *query = str_parms_create_str(keys);
2298 char *str;
2299
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002300 pthread_mutex_lock(&adev->lock);
2301
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002302 audio_extn_get_parameters(adev, query, reply);
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002303 voice_extn_get_parameters(adev, query, reply);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002304 platform_get_parameters(adev->platform, query, reply);
2305 str = str_parms_to_str(reply);
2306 str_parms_destroy(query);
2307 str_parms_destroy(reply);
2308
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002309 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002310 ALOGV("%s: exit: returns - %s", __func__, str);
2311 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002312}
2313
2314static int adev_init_check(const struct audio_hw_device *dev)
2315{
2316 return 0;
2317}
2318
2319static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2320{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002321 int ret;
2322 struct audio_device *adev = (struct audio_device *)dev;
2323 pthread_mutex_lock(&adev->lock);
2324 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002325 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002326 pthread_mutex_unlock(&adev->lock);
2327 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002328}
2329
2330static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2331{
2332 return -ENOSYS;
2333}
2334
2335static int adev_get_master_volume(struct audio_hw_device *dev,
2336 float *volume)
2337{
2338 return -ENOSYS;
2339}
2340
2341static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2342{
2343 return -ENOSYS;
2344}
2345
2346static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2347{
2348 return -ENOSYS;
2349}
2350
2351static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2352{
2353 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002354 pthread_mutex_lock(&adev->lock);
2355 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002356 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002357 adev->mode = mode;
2358 }
2359 pthread_mutex_unlock(&adev->lock);
2360 return 0;
2361}
2362
2363static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2364{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002365 int ret;
2366
2367 pthread_mutex_lock(&adev->lock);
Vidyakumar Athota2850d532013-11-19 16:02:12 -08002368 ALOGD("%s state %d\n", __func__, state);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002369 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2370 pthread_mutex_unlock(&adev->lock);
2371
2372 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002373}
2374
2375static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2376{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002377 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002378 return 0;
2379}
2380
2381static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2382 const struct audio_config *config)
2383{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002384 int channel_count = popcount(config->channel_mask);
2385
2386 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2387}
2388
2389static int adev_open_input_stream(struct audio_hw_device *dev,
2390 audio_io_handle_t handle,
2391 audio_devices_t devices,
2392 struct audio_config *config,
2393 struct audio_stream_in **stream_in)
2394{
2395 struct audio_device *adev = (struct audio_device *)dev;
2396 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002397 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002398 int channel_count = popcount(config->channel_mask);
2399
Eric Laurent994a6932013-07-17 11:51:42 -07002400 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002401 *stream_in = NULL;
2402 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2403 return -EINVAL;
2404
2405 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2406
2407 in->stream.common.get_sample_rate = in_get_sample_rate;
2408 in->stream.common.set_sample_rate = in_set_sample_rate;
2409 in->stream.common.get_buffer_size = in_get_buffer_size;
2410 in->stream.common.get_channels = in_get_channels;
2411 in->stream.common.get_format = in_get_format;
2412 in->stream.common.set_format = in_set_format;
2413 in->stream.common.standby = in_standby;
2414 in->stream.common.dump = in_dump;
2415 in->stream.common.set_parameters = in_set_parameters;
2416 in->stream.common.get_parameters = in_get_parameters;
2417 in->stream.common.add_audio_effect = in_add_audio_effect;
2418 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2419 in->stream.set_gain = in_set_gain;
2420 in->stream.read = in_read;
2421 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2422
2423 in->device = devices;
2424 in->source = AUDIO_SOURCE_DEFAULT;
2425 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002426 in->standby = 1;
2427 in->channel_mask = config->channel_mask;
2428
2429 /* Update config params with the requested sample rate and channels */
2430 in->usecase = USECASE_AUDIO_RECORD;
2431 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002432 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002433 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002434
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002435 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
Narsinga Rao Chella1eceff82013-12-02 19:25:28 -08002436 (voice_extn_compress_voip_is_config_supported(config))) {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002437 ret = voice_extn_compress_voip_open_input_stream(in);
2438 if (ret != 0)
2439 {
2440 ALOGE("%s: Compress voip input cannot be opened, error:%d",
2441 __func__, ret);
2442 goto err_open;
2443 }
Mingming Yine62d7842013-10-25 16:26:03 -07002444 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002445 if(audio_extn_ssr_get_enabled()) {
2446 if(audio_extn_ssr_init(adev, in)) {
2447 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2448 ret = -EINVAL;
2449 goto err_open;
2450 }
2451 } else {
2452 ret = -EINVAL;
2453 goto err_open;
2454 }
Mingming Yine62d7842013-10-25 16:26:03 -07002455 } else if (audio_extn_compr_cap_enabled() &&
2456 audio_extn_compr_cap_format_supported(config->format)) {
2457 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002458 } else {
2459 in->config.channels = channel_count;
2460 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2461 buffer_size = get_input_buffer_size(config->sample_rate,
2462 config->format,
2463 channel_count);
2464 in->config.period_size = buffer_size / frame_size;
2465 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002466
2467 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002468 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002469 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002470
2471err_open:
2472 free(in);
2473 *stream_in = NULL;
2474 return ret;
2475}
2476
2477static void adev_close_input_stream(struct audio_hw_device *dev,
2478 struct audio_stream_in *stream)
2479{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002480 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002481 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002482 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002483
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002484 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2485 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2486 if (ret != 0)
2487 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2488 __func__, ret);
2489 } else
2490 in_standby(&stream->common);
2491
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002492 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2493 audio_extn_ssr_deinit();
2494 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002495 free(stream);
2496
Mingming Yine62d7842013-10-25 16:26:03 -07002497 if(audio_extn_compr_cap_enabled() &&
2498 audio_extn_compr_cap_format_supported(in->config.format))
2499 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002500 return;
2501}
2502
2503static int adev_dump(const audio_hw_device_t *device, int fd)
2504{
2505 return 0;
2506}
2507
2508static int adev_close(hw_device_t *device)
2509{
2510 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002511
2512 if (!adev)
2513 return 0;
2514
2515 pthread_mutex_lock(&adev_init_lock);
2516
2517 if ((--audio_device_ref_count) == 0) {
Kiran Kandide144c82013-11-20 15:58:32 -08002518 audio_extn_listen_deinit(adev);
Kiran Kandi910e1862013-10-29 13:29:42 -07002519 audio_route_free(adev->audio_route);
2520 free(adev->snd_dev_ref_cnt);
2521 platform_deinit(adev->platform);
Kiran Kandi910e1862013-10-29 13:29:42 -07002522 free(device);
2523 adev = NULL;
2524 }
2525 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002526 return 0;
2527}
2528
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002529static int adev_open(const hw_module_t *module, const char *name,
2530 hw_device_t **device)
2531{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002532 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002533
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002534 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002535 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2536
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002537 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002538 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002539 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002540 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002541 ALOGD("%s: returning existing instance of adev", __func__);
2542 ALOGD("%s: exit", __func__);
2543 pthread_mutex_unlock(&adev_init_lock);
2544 return 0;
2545 }
2546
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002547 adev = calloc(1, sizeof(struct audio_device));
2548
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002549 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2550 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2551 adev->device.common.module = (struct hw_module_t *)module;
2552 adev->device.common.close = adev_close;
2553
2554 adev->device.init_check = adev_init_check;
2555 adev->device.set_voice_volume = adev_set_voice_volume;
2556 adev->device.set_master_volume = adev_set_master_volume;
2557 adev->device.get_master_volume = adev_get_master_volume;
2558 adev->device.set_master_mute = adev_set_master_mute;
2559 adev->device.get_master_mute = adev_get_master_mute;
2560 adev->device.set_mode = adev_set_mode;
2561 adev->device.set_mic_mute = adev_set_mic_mute;
2562 adev->device.get_mic_mute = adev_get_mic_mute;
2563 adev->device.set_parameters = adev_set_parameters;
2564 adev->device.get_parameters = adev_get_parameters;
2565 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2566 adev->device.open_output_stream = adev_open_output_stream;
2567 adev->device.close_output_stream = adev_close_output_stream;
2568 adev->device.open_input_stream = adev_open_input_stream;
2569 adev->device.close_input_stream = adev_close_input_stream;
2570 adev->device.dump = adev_dump;
2571
2572 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002573 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002574 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002575 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002576 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002577 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002578 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002579 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002580 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002581 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002582 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002583
2584 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002585 adev->platform = platform_init(adev);
2586 if (!adev->platform) {
2587 free(adev->snd_dev_ref_cnt);
2588 free(adev);
2589 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2590 *device = NULL;
2591 return -EINVAL;
2592 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002593
2594 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2595 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2596 if (adev->visualizer_lib == NULL) {
2597 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2598 } else {
2599 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2600 adev->visualizer_start_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002601 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002602 "visualizer_hal_start_output");
2603 adev->visualizer_stop_output =
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002604 (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib,
Eric Laurentc4aef752013-09-12 17:45:53 -07002605 "visualizer_hal_stop_output");
2606 }
2607 }
Kiran Kandide144c82013-11-20 15:58:32 -08002608 audio_extn_listen_init(adev, SOUND_CARD);
Eric Laurentc4aef752013-09-12 17:45:53 -07002609
Subhash Chandra Bose Naripeddy1d089162013-11-13 13:31:50 -08002610 if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) {
2611 adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW);
2612 if (adev->offload_effects_lib == NULL) {
2613 ALOGE("%s: DLOPEN failed for %s", __func__,
2614 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2615 } else {
2616 ALOGV("%s: DLOPEN successful for %s", __func__,
2617 OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH);
2618 adev->offload_effects_start_output =
2619 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2620 "offload_effects_bundle_hal_start_output");
2621 adev->offload_effects_stop_output =
2622 (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib,
2623 "offload_effects_bundle_hal_stop_output");
2624 }
2625 }
2626
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002627 *device = &adev->device.common;
2628
Kiran Kandi910e1862013-10-29 13:29:42 -07002629 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002630 pthread_mutex_unlock(&adev_init_lock);
2631
Eric Laurent994a6932013-07-17 11:51:42 -07002632 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002633 return 0;
2634}
2635
Krishnankutty Kolathappillyeff07ef2013-11-21 20:39:59 -08002636/* Read offload buffer size from a property.
2637 * If value is not power of 2 round it to
2638 * power of 2.
2639 */
2640static uint32_t get_offload_buffer_size()
2641{
2642 char value[PROPERTY_VALUE_MAX] = {0};
2643 uint32_t fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2644 if((property_get("audio.offload.buffer.size.kb", value, "")) &&
2645 atoi(value)) {
2646 fragment_size = atoi(value) * 1024;
2647 //ring buffer size needs to be 4k aligned.
2648 CHECK(!(fragment_size * COMPRESS_OFFLOAD_NUM_FRAGMENTS % 4096));
2649 }
2650 if(fragment_size < MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
2651 fragment_size = MIN_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2652 else if(fragment_size > MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE)
2653 fragment_size = MAX_COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2654 ALOGVV("%s: fragment_size %d", __func__, fragment_size);
2655 return fragment_size;
2656}
2657
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002658static struct hw_module_methods_t hal_module_methods = {
2659 .open = adev_open,
2660};
2661
2662struct audio_module HAL_MODULE_INFO_SYM = {
2663 .common = {
2664 .tag = HARDWARE_MODULE_TAG,
2665 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2666 .hal_api_version = HARDWARE_HAL_API_VERSION,
2667 .id = AUDIO_HARDWARE_MODULE_ID,
2668 .name = "QCOM Audio HAL",
2669 .author = "Code Aurora Forum",
2670 .methods = &hal_module_methods,
2671 },
2672};