blob: 873e5e2fcdf21da5be711ea30701e04ef6d5dab1 [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"
56
57#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
58#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
59/* ToDo: Check and update a proper value in msec */
60#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
61#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
62
Eric Laurentb23d5282013-05-14 15:27:20 -070063struct pcm_config pcm_config_deep_buffer = {
64 .channels = 2,
65 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
66 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
67 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
68 .format = PCM_FORMAT_S16_LE,
69 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
70 .stop_threshold = INT_MAX,
71 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
72};
73
74struct pcm_config pcm_config_low_latency = {
75 .channels = 2,
76 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
77 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
78 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
79 .format = PCM_FORMAT_S16_LE,
80 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
81 .stop_threshold = INT_MAX,
82 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
83};
84
85struct pcm_config pcm_config_hdmi_multi = {
86 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
87 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
88 .period_size = HDMI_MULTI_PERIOD_SIZE,
89 .period_count = HDMI_MULTI_PERIOD_COUNT,
90 .format = PCM_FORMAT_S16_LE,
91 .start_threshold = 0,
92 .stop_threshold = INT_MAX,
93 .avail_min = 0,
94};
95
96struct pcm_config pcm_config_audio_capture = {
97 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070098 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
99 .format = PCM_FORMAT_S16_LE,
100};
101
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -0800102const char * const use_case_table[AUDIO_USECASE_MAX] = {
Eric Laurentb23d5282013-05-14 15:27:20 -0700103 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
104 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
105 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700106 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700107 [USECASE_AUDIO_RECORD] = "audio-record",
Mingming Yine62d7842013-10-25 16:26:03 -0700108 [USECASE_AUDIO_RECORD_COMPRESS] = "audio-record-compress",
Eric Laurentb23d5282013-05-14 15:27:20 -0700109 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Preetam Singh Ranawatde84f1a2013-11-01 14:58:16 -0700110 [USECASE_AUDIO_RECORD_FM_VIRTUAL] = "fm-virtual-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700111 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700112 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700113
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700114 [USECASE_VOICE2_CALL] = "voice2-call",
115 [USECASE_VOLTE_CALL] = "volte-call",
116 [USECASE_QCHAT_CALL] = "qchat-call",
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800117 [USECASE_COMPRESS_VOIP_CALL] = "compress-voip-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700118 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
119 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
120 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700121 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
122 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700123 [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
124 [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
Eric Laurentb23d5282013-05-14 15:27:20 -0700125};
126
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800127
128#define STRING_TO_ENUM(string) { #string, string }
129
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800130struct string_to_enum {
131 const char *name;
132 uint32_t value;
133};
134
135static const struct string_to_enum out_channels_name_to_enum_table[] = {
136 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
137 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
138 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
139};
140
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700141static struct audio_device *adev = NULL;
142static pthread_mutex_t adev_init_lock;
Kiran Kandi910e1862013-10-29 13:29:42 -0700143static unsigned int audio_device_ref_count;
144
Haynes Mathew George5191a852013-09-11 14:19:36 -0700145static int set_voice_volume_l(struct audio_device *adev, float volume);
146
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700147static bool is_supported_format(audio_format_t format)
148{
Eric Laurent86e17132013-09-12 17:49:30 -0700149 if (format == AUDIO_FORMAT_MP3 ||
150 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700151 return true;
152
153 return false;
154}
155
156static int get_snd_codec_id(audio_format_t format)
157{
158 int id = 0;
159
160 switch (format) {
161 case AUDIO_FORMAT_MP3:
162 id = SND_AUDIOCODEC_MP3;
163 break;
164 case AUDIO_FORMAT_AAC:
165 id = SND_AUDIOCODEC_AAC;
166 break;
167 default:
168 ALOGE("%s: Unsupported audio format", __func__);
169 }
170
171 return id;
172}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800173
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700174int enable_audio_route(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700175 struct audio_usecase *usecase,
176 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800177{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700178 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700179 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800180
181 if (usecase == NULL)
182 return -EINVAL;
183
184 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
185
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800186 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700187 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800188 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700189 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800190
191 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700192 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700193 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700194 audio_route_apply_path(adev->audio_route, mixer_path);
195 if (update_mixer)
196 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800197
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800198 ALOGV("%s: exit", __func__);
199 return 0;
200}
201
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700202int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700203 struct audio_usecase *usecase,
204 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800205{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700206 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700207 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800208
209 if (usecase == NULL)
210 return -EINVAL;
211
212 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700213 if (usecase->type == PCM_CAPTURE)
214 snd_device = usecase->in_snd_device;
215 else
216 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800217 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700218 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700219 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700220 audio_route_reset_path(adev->audio_route, mixer_path);
221 if (update_mixer)
222 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800223
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800224 ALOGV("%s: exit", __func__);
225 return 0;
226}
227
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700228int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700229 snd_device_t snd_device,
230 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800231{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700232 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
233
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800234 if (snd_device < SND_DEVICE_MIN ||
235 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800236 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800237 return -EINVAL;
238 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700239
240 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700241
242 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
243 ALOGE("%s: Invalid sound device returned", __func__);
244 return -EINVAL;
245 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700246 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700247 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700248 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700249 return 0;
250 }
251
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700252 /* start usb playback thread */
253 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
254 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
255 audio_extn_usb_start_playback(adev);
256
257 /* start usb capture thread */
258 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
259 audio_extn_usb_start_capture(adev);
260
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700261 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
262 audio_extn_spkr_prot_is_enabled()) {
263 if (audio_extn_spkr_prot_start_processing(snd_device)) {
264 ALOGE("%s: spkr_start_processing failed", __func__);
265 return -EINVAL;
266 }
267 } else {
268 ALOGV("%s: snd_device(%d: %s)", __func__,
269 snd_device, device_name);
270 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
271 adev->snd_dev_ref_cnt[snd_device]--;
272 return -EINVAL;
273 }
274 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800275 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700276 if (update_mixer)
277 audio_route_update_mixer(adev->audio_route);
278
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800279 return 0;
280}
281
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700282int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700283 snd_device_t snd_device,
284 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800285{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700286 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
287
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800288 if (snd_device < SND_DEVICE_MIN ||
289 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800290 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800291 return -EINVAL;
292 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700293 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
294 ALOGE("%s: device ref cnt is already 0", __func__);
295 return -EINVAL;
296 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700297
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700298 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700299
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700300 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
301 ALOGE("%s: Invalid sound device returned", __func__);
302 return -EINVAL;
303 }
304
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700305 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700306 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700307 snd_device, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800308 /* exit usb play back thread */
309 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
310 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
311 audio_extn_usb_stop_playback();
312
313 /* exit usb capture thread */
314 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
315 audio_extn_usb_stop_capture(adev);
316
Gopikrishnaiah Anandanf538cef2013-10-28 14:06:03 -0700317 if (snd_device == SND_DEVICE_OUT_SPEAKER &&
318 audio_extn_spkr_prot_is_enabled()) {
319 audio_extn_spkr_prot_stop_processing();
320 } else
321 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -0800322
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700323 if (update_mixer)
324 audio_route_update_mixer(adev->audio_route);
325 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700326
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800327 return 0;
328}
329
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700330static void check_usecases_codec_backend(struct audio_device *adev,
331 struct audio_usecase *uc_info,
332 snd_device_t snd_device)
333{
334 struct listnode *node;
335 struct audio_usecase *usecase;
336 bool switch_device[AUDIO_USECASE_MAX];
337 int i, num_uc_to_switch = 0;
338
339 /*
340 * This function is to make sure that all the usecases that are active on
341 * the hardware codec backend are always routed to any one device that is
342 * handled by the hardware codec.
343 * For example, if low-latency and deep-buffer usecases are currently active
344 * on speaker and out_set_parameters(headset) is received on low-latency
345 * output, then we have to make sure deep-buffer is also switched to headset,
346 * because of the limitation that both the devices cannot be enabled
347 * at the same time as they share the same backend.
348 */
349 /* Disable all the usecases on the shared backend other than the
350 specified usecase */
351 for (i = 0; i < AUDIO_USECASE_MAX; i++)
352 switch_device[i] = false;
353
354 list_for_each(node, &adev->usecase_list) {
355 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700356 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700357 usecase != uc_info &&
358 usecase->out_snd_device != snd_device &&
359 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
360 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
361 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700362 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700363 disable_audio_route(adev, usecase, false);
364 switch_device[usecase->id] = true;
365 num_uc_to_switch++;
366 }
367 }
368
369 if (num_uc_to_switch) {
370 /* Make sure all the streams are de-routed before disabling the device */
371 audio_route_update_mixer(adev->audio_route);
372
373 list_for_each(node, &adev->usecase_list) {
374 usecase = node_to_item(node, struct audio_usecase, list);
375 if (switch_device[usecase->id]) {
376 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700377 }
378 }
379
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -0700380 list_for_each(node, &adev->usecase_list) {
381 usecase = node_to_item(node, struct audio_usecase, list);
382 if (switch_device[usecase->id]) {
383 enable_snd_device(adev, snd_device, false);
384 }
385 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700386 /* Make sure new snd device is enabled before re-routing the streams */
387 audio_route_update_mixer(adev->audio_route);
388
389 /* Re-route all the usecases on the shared backend other than the
390 specified usecase to new snd devices */
391 list_for_each(node, &adev->usecase_list) {
392 usecase = node_to_item(node, struct audio_usecase, list);
393 /* Update the out_snd_device only before enabling the audio route */
394 if (switch_device[usecase->id] ) {
395 usecase->out_snd_device = snd_device;
396 enable_audio_route(adev, usecase, false);
397 }
398 }
399
400 audio_route_update_mixer(adev->audio_route);
401 }
402}
403
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700404static void check_and_route_capture_usecases(struct audio_device *adev,
405 struct audio_usecase *uc_info,
406 snd_device_t snd_device)
407{
408 struct listnode *node;
409 struct audio_usecase *usecase;
410 bool switch_device[AUDIO_USECASE_MAX];
411 int i, num_uc_to_switch = 0;
412
413 /*
414 * This function is to make sure that all the active capture usecases
415 * are always routed to the same input sound device.
416 * For example, if audio-record and voice-call usecases are currently
417 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
418 * is received for voice call then we have to make sure that audio-record
419 * usecase is also switched to earpiece i.e. voice-dmic-ef,
420 * because of the limitation that two devices cannot be enabled
421 * at the same time if they share the same backend.
422 */
423 for (i = 0; i < AUDIO_USECASE_MAX; i++)
424 switch_device[i] = false;
425
426 list_for_each(node, &adev->usecase_list) {
427 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700428 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700429 usecase != uc_info &&
430 usecase->in_snd_device != snd_device) {
431 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
432 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700433 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700434 disable_audio_route(adev, usecase, false);
435 switch_device[usecase->id] = true;
436 num_uc_to_switch++;
437 }
438 }
439
440 if (num_uc_to_switch) {
441 /* Make sure all the streams are de-routed before disabling the device */
442 audio_route_update_mixer(adev->audio_route);
443
444 list_for_each(node, &adev->usecase_list) {
445 usecase = node_to_item(node, struct audio_usecase, list);
446 if (switch_device[usecase->id]) {
447 disable_snd_device(adev, usecase->in_snd_device, false);
448 enable_snd_device(adev, snd_device, false);
449 }
450 }
451
452 /* Make sure new snd device is enabled before re-routing the streams */
453 audio_route_update_mixer(adev->audio_route);
454
455 /* Re-route all the usecases on the shared backend other than the
456 specified usecase to new snd devices */
457 list_for_each(node, &adev->usecase_list) {
458 usecase = node_to_item(node, struct audio_usecase, list);
459 /* Update the in_snd_device only before enabling the audio route */
460 if (switch_device[usecase->id] ) {
461 usecase->in_snd_device = snd_device;
462 enable_audio_route(adev, usecase, false);
463 }
464 }
465
466 audio_route_update_mixer(adev->audio_route);
467 }
468}
469
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700470static int disable_all_usecases_of_type(struct audio_device *adev,
471 usecase_type_t usecase_type,
472 bool update_mixer)
473{
474 struct audio_usecase *usecase;
475 struct listnode *node;
476 int ret = 0;
477
478 list_for_each(node, &adev->usecase_list) {
479 usecase = node_to_item(node, struct audio_usecase, list);
480 if (usecase->type == usecase_type) {
481 ALOGV("%s: usecase id %d", __func__, usecase->id);
482 ret = disable_audio_route(adev, usecase, update_mixer);
483 if (ret) {
484 ALOGE("%s: Failed to disable usecase id %d",
485 __func__, usecase->id);
486 }
487 }
488 }
489
490 return ret;
491}
492
493static int enable_all_usecases_of_type(struct audio_device *adev,
494 usecase_type_t usecase_type,
495 bool update_mixer)
496{
497 struct audio_usecase *usecase;
498 struct listnode *node;
499 int ret = 0;
500
501 list_for_each(node, &adev->usecase_list) {
502 usecase = node_to_item(node, struct audio_usecase, list);
503 if (usecase->type == usecase_type) {
504 ALOGV("%s: usecase id %d", __func__, usecase->id);
505 ret = enable_audio_route(adev, usecase, update_mixer);
506 if (ret) {
507 ALOGE("%s: Failed to enable usecase id %d",
508 __func__, usecase->id);
509 }
510 }
511 }
512
513 return ret;
514}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800515
516/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700517static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800518{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700519 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700520 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800521
522 switch (channels) {
523 /*
524 * Do not handle stereo output in Multi-channel cases
525 * Stereo case is handled in normal playback path
526 */
527 case 6:
528 ALOGV("%s: HDMI supports 5.1", __func__);
529 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
530 break;
531 case 8:
532 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
533 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
534 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
535 break;
536 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700537 ALOGE("HDMI does not support multi channel playback");
538 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800539 break;
540 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700541 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800542}
543
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700544static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
545{
546 struct audio_usecase *usecase;
547 struct listnode *node;
548
549 list_for_each(node, &adev->usecase_list) {
550 usecase = node_to_item(node, struct audio_usecase, list);
551 if (usecase->type == VOICE_CALL) {
552 ALOGV("%s: usecase id %d", __func__, usecase->id);
553 return usecase->id;
554 }
555 }
556 return USECASE_INVALID;
557}
558
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700559struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700560 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700561{
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->id == uc_id)
568 return usecase;
569 }
570 return NULL;
571}
572
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700573int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800574{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800575 snd_device_t out_snd_device = SND_DEVICE_NONE;
576 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700577 struct audio_usecase *usecase = NULL;
578 struct audio_usecase *vc_usecase = NULL;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800579 struct audio_usecase *voip_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800580 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700581 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800582
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700583 usecase = get_usecase_from_list(adev, uc_id);
584 if (usecase == NULL) {
585 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
586 return -EINVAL;
587 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800588
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800589 if ((usecase->type == VOICE_CALL) ||
590 (usecase->type == VOIP_CALL)) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700591 out_snd_device = platform_get_output_snd_device(adev->platform,
592 usecase->stream.out->devices);
593 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700594 usecase->devices = usecase->stream.out->devices;
595 } else {
596 /*
597 * If the voice call is active, use the sound devices of voice call usecase
598 * so that it would not result any device switch. All the usecases will
599 * be switched to new device when select_devices() is called for voice call
600 * usecase. This is to avoid switching devices for voice call when
601 * check_usecases_codec_backend() is called below.
602 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700603 if (voice_is_in_call(adev)) {
604 vc_usecase = get_usecase_from_list(adev,
605 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700606 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
607 in_snd_device = vc_usecase->in_snd_device;
608 out_snd_device = vc_usecase->out_snd_device;
609 }
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800610 } else if (voice_extn_compress_voip_is_active(adev)) {
611 voip_usecase = get_usecase_from_list(adev, USECASE_COMPRESS_VOIP_CALL);
612 if (voip_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
613 in_snd_device = voip_usecase->in_snd_device;
614 out_snd_device = voip_usecase->out_snd_device;
615 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700616 }
617 if (usecase->type == PCM_PLAYBACK) {
618 usecase->devices = usecase->stream.out->devices;
619 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700620 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700621 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700622 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700623 if (usecase->stream.out == adev->primary_output &&
624 adev->active_input &&
625 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
626 select_devices(adev, adev->active_input->usecase);
627 }
628 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700629 } else if (usecase->type == PCM_CAPTURE) {
630 usecase->devices = usecase->stream.in->device;
631 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700632 if (in_snd_device == SND_DEVICE_NONE) {
633 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
634 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700635 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700636 adev->primary_output->devices);
637 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700638 in_snd_device = platform_get_input_snd_device(adev->platform,
639 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700640 }
641 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700642 }
643 }
644
645 if (out_snd_device == usecase->out_snd_device &&
646 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800647 return 0;
648 }
649
sangwoobc677242013-08-08 16:53:43 +0900650 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700651 out_snd_device, platform_get_snd_device_name(out_snd_device),
652 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800653
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800654 /*
655 * Limitation: While in call, to do a device switch we need to disable
656 * and enable both RX and TX devices though one of them is same as current
657 * device.
658 */
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800659 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) {
660 disable_all_usecases_of_type(adev, usecase->type, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700661 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800662 }
663
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700664 /* Disable current sound devices */
665 if (usecase->out_snd_device != SND_DEVICE_NONE) {
666 disable_audio_route(adev, usecase, true);
667 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800668 }
669
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700670 if (usecase->in_snd_device != SND_DEVICE_NONE) {
671 disable_audio_route(adev, usecase, true);
672 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800673 }
674
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700675 /* Enable new sound devices */
676 if (out_snd_device != SND_DEVICE_NONE) {
677 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
678 check_usecases_codec_backend(adev, usecase, out_snd_device);
679 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800680 }
681
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700682 if (in_snd_device != SND_DEVICE_NONE) {
683 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700684 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700685 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700686
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800687 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
Eric Laurentb23d5282013-05-14 15:27:20 -0700688 status = platform_switch_voice_call_device_post(adev->platform,
689 out_snd_device,
690 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800691
sangwoo170731f2013-06-08 15:36:36 +0900692 audio_route_update_mixer(adev->audio_route);
693
694 usecase->in_snd_device = in_snd_device;
695 usecase->out_snd_device = out_snd_device;
696
Narsinga Rao Chella05573b72013-11-15 15:21:40 -0800697 if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL)
698 enable_all_usecases_of_type(adev, usecase->type, true);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700699 else
700 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900701
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800702 return status;
703}
704
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800705static int stop_input_stream(struct stream_in *in)
706{
707 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800708 struct audio_usecase *uc_info;
709 struct audio_device *adev = in->dev;
710
Eric Laurentc8400632013-02-14 19:04:54 -0800711 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800712
Eric Laurent994a6932013-07-17 11:51:42 -0700713 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700714 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800715 uc_info = get_usecase_from_list(adev, in->usecase);
716 if (uc_info == NULL) {
717 ALOGE("%s: Could not find the usecase (%d) in the list",
718 __func__, in->usecase);
719 return -EINVAL;
720 }
721
Eric Laurent150dbfe2013-02-27 14:31:02 -0800722 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700723 disable_audio_route(adev, uc_info, true);
724
725 /* 2. Disable the tx device */
726 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800727
Kiran Kandi910e1862013-10-29 13:29:42 -0700728 audio_extn_listen_update_status(uc_info,
729 LISTEN_EVENT_AUDIO_CAPTURE_INACTIVE);
730
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800731 list_remove(&uc_info->list);
732 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800733
Eric Laurent994a6932013-07-17 11:51:42 -0700734 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800735 return ret;
736}
737
738int start_input_stream(struct stream_in *in)
739{
740 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800741 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800742 struct audio_usecase *uc_info;
743 struct audio_device *adev = in->dev;
744
Mingming Yine62d7842013-10-25 16:26:03 -0700745 in->usecase = platform_update_usecase_from_source(in->source,in->usecase);
Eric Laurent994a6932013-07-17 11:51:42 -0700746 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700747
748 /* Check if source matches incall recording usecase criteria */
749 ret = voice_check_and_set_incall_rec_usecase(adev, in);
750 if (ret)
751 goto error_config;
752 else
753 ALOGV("%s: usecase(%d)", __func__, in->usecase);
754
Eric Laurentb23d5282013-05-14 15:27:20 -0700755 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800756 if (in->pcm_device_id < 0) {
757 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
758 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800759 ret = -EINVAL;
760 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800761 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700762
763 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800764 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
765 uc_info->id = in->usecase;
766 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800767 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700768 uc_info->devices = in->device;
769 uc_info->in_snd_device = SND_DEVICE_NONE;
770 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800771
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800772 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700773 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800774
Kiran Kandi910e1862013-10-29 13:29:42 -0700775 audio_extn_listen_update_status(uc_info,
776 LISTEN_EVENT_AUDIO_CAPTURE_ACTIVE);
777
Eric Laurentc8400632013-02-14 19:04:54 -0800778 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
779 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800780 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
781 PCM_IN, &in->config);
782 if (in->pcm && !pcm_is_ready(in->pcm)) {
783 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
784 pcm_close(in->pcm);
785 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800786 ret = -EIO;
787 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800788 }
Eric Laurent994a6932013-07-17 11:51:42 -0700789 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800790 return ret;
791
792error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800793 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800794
795error_config:
796 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700797 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800798
799 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800800}
801
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700802/* must be called with out->lock locked */
803static int send_offload_cmd_l(struct stream_out* out, int command)
804{
805 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
806
807 ALOGVV("%s %d", __func__, command);
808
809 cmd->cmd = command;
810 list_add_tail(&out->offload_cmd_list, &cmd->node);
811 pthread_cond_signal(&out->offload_cond);
812 return 0;
813}
814
815/* must be called iwth out->lock locked */
816static void stop_compressed_output_l(struct stream_out *out)
817{
818 out->offload_state = OFFLOAD_STATE_IDLE;
819 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700820 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700821 if (out->compr != NULL) {
822 compress_stop(out->compr);
823 while (out->offload_thread_blocked) {
824 pthread_cond_wait(&out->cond, &out->lock);
825 }
826 }
827}
828
829static void *offload_thread_loop(void *context)
830{
831 struct stream_out *out = (struct stream_out *) context;
832 struct listnode *item;
833
834 out->offload_state = OFFLOAD_STATE_IDLE;
835 out->playback_started = 0;
836
837 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
838 set_sched_policy(0, SP_FOREGROUND);
839 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
840
841 ALOGV("%s", __func__);
842 pthread_mutex_lock(&out->lock);
843 for (;;) {
844 struct offload_cmd *cmd = NULL;
845 stream_callback_event_t event;
846 bool send_callback = false;
847
848 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
849 __func__, list_empty(&out->offload_cmd_list),
850 out->offload_state);
851 if (list_empty(&out->offload_cmd_list)) {
852 ALOGV("%s SLEEPING", __func__);
853 pthread_cond_wait(&out->offload_cond, &out->lock);
854 ALOGV("%s RUNNING", __func__);
855 continue;
856 }
857
858 item = list_head(&out->offload_cmd_list);
859 cmd = node_to_item(item, struct offload_cmd, node);
860 list_remove(item);
861
862 ALOGVV("%s STATE %d CMD %d out->compr %p",
863 __func__, out->offload_state, cmd->cmd, out->compr);
864
865 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
866 free(cmd);
867 break;
868 }
869
870 if (out->compr == NULL) {
871 ALOGE("%s: Compress handle is NULL", __func__);
872 pthread_cond_signal(&out->cond);
873 continue;
874 }
875 out->offload_thread_blocked = true;
876 pthread_mutex_unlock(&out->lock);
877 send_callback = false;
878 switch(cmd->cmd) {
879 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
880 compress_wait(out->compr, -1);
881 send_callback = true;
882 event = STREAM_CBK_EVENT_WRITE_READY;
883 break;
884 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700885 compress_next_track(out->compr);
886 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700887 send_callback = true;
888 event = STREAM_CBK_EVENT_DRAIN_READY;
889 break;
890 case OFFLOAD_CMD_DRAIN:
891 compress_drain(out->compr);
892 send_callback = true;
893 event = STREAM_CBK_EVENT_DRAIN_READY;
894 break;
895 default:
896 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
897 break;
898 }
899 pthread_mutex_lock(&out->lock);
900 out->offload_thread_blocked = false;
901 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700902 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700903 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700904 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700905 free(cmd);
906 }
907
908 pthread_cond_signal(&out->cond);
909 while (!list_empty(&out->offload_cmd_list)) {
910 item = list_head(&out->offload_cmd_list);
911 list_remove(item);
912 free(node_to_item(item, struct offload_cmd, node));
913 }
914 pthread_mutex_unlock(&out->lock);
915
916 return NULL;
917}
918
919static int create_offload_callback_thread(struct stream_out *out)
920{
921 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
922 list_init(&out->offload_cmd_list);
923 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
924 offload_thread_loop, out);
925 return 0;
926}
927
928static int destroy_offload_callback_thread(struct stream_out *out)
929{
930 pthread_mutex_lock(&out->lock);
931 stop_compressed_output_l(out);
932 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
933
934 pthread_mutex_unlock(&out->lock);
935 pthread_join(out->offload_thread, (void **) NULL);
936 pthread_cond_destroy(&out->offload_cond);
937
938 return 0;
939}
940
Eric Laurent07eeafd2013-10-06 12:52:49 -0700941static bool allow_hdmi_channel_config(struct audio_device *adev)
942{
943 struct listnode *node;
944 struct audio_usecase *usecase;
945 bool ret = true;
946
947 list_for_each(node, &adev->usecase_list) {
948 usecase = node_to_item(node, struct audio_usecase, list);
949 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
950 /*
951 * If voice call is already existing, do not proceed further to avoid
952 * disabling/enabling both RX and TX devices, CSD calls, etc.
953 * Once the voice call done, the HDMI channels can be configured to
954 * max channels of remaining use cases.
955 */
956 if (usecase->id == USECASE_VOICE_CALL) {
957 ALOGD("%s: voice call is active, no change in HDMI channels",
958 __func__);
959 ret = false;
960 break;
961 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
962 ALOGD("%s: multi channel playback is active, "
963 "no change in HDMI channels", __func__);
964 ret = false;
965 break;
966 }
967 }
968 }
969 return ret;
970}
971
972static int check_and_set_hdmi_channels(struct audio_device *adev,
973 unsigned int channels)
974{
975 struct listnode *node;
976 struct audio_usecase *usecase;
977
978 /* Check if change in HDMI channel config is allowed */
979 if (!allow_hdmi_channel_config(adev))
980 return 0;
981
982 if (channels == adev->cur_hdmi_channels) {
983 ALOGD("%s: Requested channels are same as current", __func__);
984 return 0;
985 }
986
987 platform_set_hdmi_channels(adev->platform, channels);
988 adev->cur_hdmi_channels = channels;
989
990 /*
991 * Deroute all the playback streams routed to HDMI so that
992 * the back end is deactivated. Note that backend will not
993 * be deactivated if any one stream is connected to it.
994 */
995 list_for_each(node, &adev->usecase_list) {
996 usecase = node_to_item(node, struct audio_usecase, list);
997 if (usecase->type == PCM_PLAYBACK &&
998 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
999 disable_audio_route(adev, usecase, true);
1000 }
1001 }
1002
1003 /*
1004 * Enable all the streams disabled above. Now the HDMI backend
1005 * will be activated with new channel configuration
1006 */
1007 list_for_each(node, &adev->usecase_list) {
1008 usecase = node_to_item(node, struct audio_usecase, list);
1009 if (usecase->type == PCM_PLAYBACK &&
1010 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
1011 enable_audio_route(adev, usecase, true);
1012 }
1013 }
1014
1015 return 0;
1016}
1017
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001018static int stop_output_stream(struct stream_out *out)
1019{
1020 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001021 struct audio_usecase *uc_info;
1022 struct audio_device *adev = out->dev;
1023
Eric Laurent994a6932013-07-17 11:51:42 -07001024 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001025 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026 uc_info = get_usecase_from_list(adev, out->usecase);
1027 if (uc_info == NULL) {
1028 ALOGE("%s: Could not find the usecase (%d) in the list",
1029 __func__, out->usecase);
1030 return -EINVAL;
1031 }
1032
Eric Laurentc4aef752013-09-12 17:45:53 -07001033 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
1034 adev->visualizer_stop_output != NULL)
1035 adev->visualizer_stop_output(out->handle);
1036
Eric Laurent150dbfe2013-02-27 14:31:02 -08001037 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001038 disable_audio_route(adev, uc_info, true);
1039
1040 /* 2. Disable the rx device */
1041 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001042
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001043 list_remove(&uc_info->list);
1044 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001045
Eric Laurent07eeafd2013-10-06 12:52:49 -07001046 /* Must be called after removing the usecase from list */
1047 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1048 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1049
Eric Laurent994a6932013-07-17 11:51:42 -07001050 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001051 return ret;
1052}
1053
1054int start_output_stream(struct stream_out *out)
1055{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001056 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001057 struct audio_usecase *uc_info;
1058 struct audio_device *adev = out->dev;
1059
Eric Laurent994a6932013-07-17 11:51:42 -07001060 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001061 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001062 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001063 if (out->pcm_device_id < 0) {
1064 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1065 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001066 ret = -EINVAL;
1067 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001068 }
1069
1070 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1071 uc_info->id = out->usecase;
1072 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001073 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001074 uc_info->devices = out->devices;
1075 uc_info->in_snd_device = SND_DEVICE_NONE;
1076 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001077
Eric Laurent07eeafd2013-10-06 12:52:49 -07001078 /* This must be called before adding this usecase to the list */
1079 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1080 check_and_set_hdmi_channels(adev, out->config.channels);
1081
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001082 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001083
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001084 select_devices(adev, out->usecase);
1085
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001086 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1087 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001088 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1089 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001090 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001091 if (out->pcm && !pcm_is_ready(out->pcm)) {
1092 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1093 pcm_close(out->pcm);
1094 out->pcm = NULL;
1095 ret = -EIO;
1096 goto error_open;
1097 }
1098 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001099 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001100 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1101 COMPRESS_IN, &out->compr_config);
1102 if (out->compr && !is_compress_ready(out->compr)) {
1103 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1104 compress_close(out->compr);
1105 out->compr = NULL;
1106 ret = -EIO;
1107 goto error_open;
1108 }
1109 if (out->offload_callback)
1110 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001111
1112 if (adev->visualizer_start_output != NULL)
1113 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001114 }
Eric Laurent994a6932013-07-17 11:51:42 -07001115 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001116 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001117error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001118 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001119error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001120 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001121}
1122
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001123static int check_input_parameters(uint32_t sample_rate,
1124 audio_format_t format,
1125 int channel_count)
1126{
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001127 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001128
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001129 if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
Mingming Yine62d7842013-10-25 16:26:03 -07001130 !voice_extn_compress_voip_is_format_supported(format) &&
1131 !audio_extn_compr_cap_format_supported(format)) ret = -EINVAL;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001132
1133 switch (channel_count) {
1134 case 1:
1135 case 2:
1136 case 6:
1137 break;
1138 default:
1139 ret = -EINVAL;
1140 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001141
1142 switch (sample_rate) {
1143 case 8000:
1144 case 11025:
1145 case 12000:
1146 case 16000:
1147 case 22050:
1148 case 24000:
1149 case 32000:
1150 case 44100:
1151 case 48000:
1152 break;
1153 default:
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001154 ret = -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001155 }
1156
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08001157 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001158}
1159
1160static size_t get_input_buffer_size(uint32_t sample_rate,
1161 audio_format_t format,
1162 int channel_count)
1163{
1164 size_t size = 0;
1165
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001166 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1167 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001168
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001169 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1170 /* ToDo: should use frame_size computed based on the format and
1171 channel_count here. */
1172 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001173
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001174 /* make sure the size is multiple of 64 */
1175 size += 0x3f;
1176 size &= ~0x3f;
1177
1178 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001179}
1180
1181static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1182{
1183 struct stream_out *out = (struct stream_out *)stream;
1184
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001185 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001186}
1187
1188static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1189{
1190 return -ENOSYS;
1191}
1192
1193static size_t out_get_buffer_size(const struct audio_stream *stream)
1194{
1195 struct stream_out *out = (struct stream_out *)stream;
1196
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001197 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001198 return out->compr_config.fragment_size;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001199 else if(out->usecase == USECASE_COMPRESS_VOIP_CALL)
1200 return voice_extn_compress_voip_out_get_buffer_size(out);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001201
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001202 return out->config.period_size * audio_stream_frame_size(stream);
1203}
1204
1205static uint32_t out_get_channels(const struct audio_stream *stream)
1206{
1207 struct stream_out *out = (struct stream_out *)stream;
1208
1209 return out->channel_mask;
1210}
1211
1212static audio_format_t out_get_format(const struct audio_stream *stream)
1213{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001214 struct stream_out *out = (struct stream_out *)stream;
1215
1216 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001217}
1218
1219static int out_set_format(struct audio_stream *stream, audio_format_t format)
1220{
1221 return -ENOSYS;
1222}
1223
1224static int out_standby(struct audio_stream *stream)
1225{
1226 struct stream_out *out = (struct stream_out *)stream;
1227 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001228
Eric Laurent994a6932013-07-17 11:51:42 -07001229 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001230 out->usecase, use_case_table[out->usecase]);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001231 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
1232 /* Ignore standby in case of voip call because the voip output
1233 * stream is closed in adev_close_output_stream()
1234 */
1235 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1236 return 0;
1237 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001238
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001239 pthread_mutex_lock(&out->lock);
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001240 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001241 if (!out->standby) {
1242 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001243 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1244 if (out->pcm) {
1245 pcm_close(out->pcm);
1246 out->pcm = NULL;
1247 }
1248 } else {
1249 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001250 out->gapless_mdata.encoder_delay = 0;
1251 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001252 if (out->compr != NULL) {
1253 compress_close(out->compr);
1254 out->compr = NULL;
1255 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001256 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001257 stop_output_stream(out);
1258 }
Krishnankutty Kolathappillydc4f7572013-11-01 20:07:13 -07001259 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001260 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001261 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262 return 0;
1263}
1264
1265static int out_dump(const struct audio_stream *stream, int fd)
1266{
1267 return 0;
1268}
1269
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001270static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1271{
1272 int ret = 0;
1273 char value[32];
1274 struct compr_gapless_mdata tmp_mdata;
1275
1276 if (!out || !parms) {
1277 return -EINVAL;
1278 }
1279
1280 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1281 if (ret >= 0) {
1282 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1283 } else {
1284 return -EINVAL;
1285 }
1286
1287 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1288 if (ret >= 0) {
1289 tmp_mdata.encoder_padding = atoi(value);
1290 } else {
1291 return -EINVAL;
1292 }
1293
1294 out->gapless_mdata = tmp_mdata;
1295 out->send_new_metadata = 1;
1296 ALOGV("%s new encoder delay %u and padding %u", __func__,
1297 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1298
1299 return 0;
1300}
1301
1302
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001303static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1304{
1305 struct stream_out *out = (struct stream_out *)stream;
1306 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001307 struct audio_usecase *usecase;
1308 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001309 struct str_parms *parms;
1310 char value[32];
1311 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001312 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001313
sangwoobc677242013-08-08 16:53:43 +09001314 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001315 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001316 parms = str_parms_create_str(kvpairs);
1317 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1318 if (ret >= 0) {
1319 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001320 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001321 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001322
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001323 /*
1324 * When HDMI cable is unplugged the music playback is paused and
1325 * the policy manager sends routing=0. But the audioflinger
1326 * continues to write data until standby time (3sec).
1327 * As the HDMI core is turned off, the write gets blocked.
1328 * Avoid this by routing audio to speaker until standby.
1329 */
1330 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1331 val == AUDIO_DEVICE_NONE) {
1332 val = AUDIO_DEVICE_OUT_SPEAKER;
1333 }
1334
1335 /*
1336 * select_devices() call below switches all the usecases on the same
1337 * backend to the new device. Refer to check_usecases_codec_backend() in
1338 * the select_devices(). But how do we undo this?
1339 *
1340 * For example, music playback is active on headset (deep-buffer usecase)
1341 * and if we go to ringtones and select a ringtone, low-latency usecase
1342 * will be started on headset+speaker. As we can't enable headset+speaker
1343 * and headset devices at the same time, select_devices() switches the music
1344 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1345 * So when the ringtone playback is completed, how do we undo the same?
1346 *
1347 * We are relying on the out_set_parameters() call on deep-buffer output,
1348 * once the ringtone playback is ended.
1349 * NOTE: We should not check if the current devices are same as new devices.
1350 * Because select_devices() must be called to switch back the music
1351 * playback to headset.
1352 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001353 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001354 out->devices = val;
1355
1356 if (!out->standby)
1357 select_devices(adev, out->usecase);
1358
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001359 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1360 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001361 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001362 voice_start_call(adev);
1363 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1364 voice_is_in_call(adev) &&
1365 (out == adev->primary_output)) {
1366 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001367 }
1368 }
1369
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001370 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1371 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001372 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001373 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001374 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001375
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001376 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001377 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001378 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001379
1380 if (out == adev->primary_output) {
1381 pthread_mutex_lock(&adev->lock);
1382 audio_extn_set_parameters(adev, parms);
1383 pthread_mutex_unlock(&adev->lock);
1384 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001385 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1386 parse_compress_metadata(out, parms);
1387 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001388
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001389 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001390 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001391 return ret;
1392}
1393
1394static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1395{
1396 struct stream_out *out = (struct stream_out *)stream;
1397 struct str_parms *query = str_parms_create_str(keys);
1398 char *str;
1399 char value[256];
1400 struct str_parms *reply = str_parms_create();
1401 size_t i, j;
1402 int ret;
1403 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001404 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001405 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1406 if (ret >= 0) {
1407 value[0] = '\0';
1408 i = 0;
1409 while (out->supported_channel_masks[i] != 0) {
1410 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1411 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1412 if (!first) {
1413 strcat(value, "|");
1414 }
1415 strcat(value, out_channels_name_to_enum_table[j].name);
1416 first = false;
1417 break;
1418 }
1419 }
1420 i++;
1421 }
1422 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1423 str = str_parms_to_str(reply);
1424 } else {
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001425 voice_extn_out_get_parameters(out, query, reply);
1426 str = str_parms_to_str(reply);
1427 if (!strncmp(str, "", sizeof(""))) {
1428 str = strdup(keys);
1429 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001430 }
1431 str_parms_destroy(query);
1432 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001433 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001434 return str;
1435}
1436
1437static uint32_t out_get_latency(const struct audio_stream_out *stream)
1438{
1439 struct stream_out *out = (struct stream_out *)stream;
1440
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001441 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1442 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1443
1444 return (out->config.period_count * out->config.period_size * 1000) /
1445 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001446}
1447
1448static int out_set_volume(struct audio_stream_out *stream, float left,
1449 float right)
1450{
Eric Laurenta9024de2013-04-04 09:19:12 -07001451 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001452 int volume[2];
1453
Eric Laurenta9024de2013-04-04 09:19:12 -07001454 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1455 /* only take left channel into account: the API is for stereo anyway */
1456 out->muted = (left == 0.0f);
1457 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001458 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1459 const char *mixer_ctl_name = "Compress Playback Volume";
1460 struct audio_device *adev = out->dev;
1461 struct mixer_ctl *ctl;
1462
1463 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1464 if (!ctl) {
1465 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1466 __func__, mixer_ctl_name);
1467 return -EINVAL;
1468 }
1469 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1470 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1471 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1472 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001473 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001474
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001475 return -ENOSYS;
1476}
1477
1478static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1479 size_t bytes)
1480{
1481 struct stream_out *out = (struct stream_out *)stream;
1482 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001483 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001484
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001485 pthread_mutex_lock(&out->lock);
1486 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001487 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001488 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001489 if (out->usecase == USECASE_COMPRESS_VOIP_CALL)
1490 ret = voice_extn_compress_voip_start_output_stream(out);
1491 else
1492 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001493 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001494 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001495 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001496 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001497 goto exit;
1498 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001499 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001500
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001501 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001502 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1503 if (out->send_new_metadata) {
1504 ALOGVV("send new gapless metadata");
1505 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1506 out->send_new_metadata = 0;
1507 }
1508
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001509 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001510 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001511 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001512 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1513 }
1514 if (!out->playback_started) {
1515 compress_start(out->compr);
1516 out->playback_started = 1;
1517 out->offload_state = OFFLOAD_STATE_PLAYING;
1518 }
1519 pthread_mutex_unlock(&out->lock);
1520 return ret;
1521 } else {
1522 if (out->pcm) {
1523 if (out->muted)
1524 memset((void *)buffer, 0, bytes);
1525 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1526 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001527 if (ret == 0)
1528 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001529 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001530 }
1531
1532exit:
1533 pthread_mutex_unlock(&out->lock);
1534
1535 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001536 if (out->pcm)
1537 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001538 out_standby(&out->stream.common);
1539 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1540 out_get_sample_rate(&out->stream.common));
1541 }
1542 return bytes;
1543}
1544
1545static int out_get_render_position(const struct audio_stream_out *stream,
1546 uint32_t *dsp_frames)
1547{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001548 struct stream_out *out = (struct stream_out *)stream;
1549 *dsp_frames = 0;
1550 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1551 pthread_mutex_lock(&out->lock);
1552 if (out->compr != NULL) {
1553 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1554 &out->sample_rate);
1555 ALOGVV("%s rendered frames %d sample_rate %d",
1556 __func__, *dsp_frames, out->sample_rate);
1557 }
1558 pthread_mutex_unlock(&out->lock);
1559 return 0;
1560 } else
1561 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001562}
1563
1564static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1565{
1566 return 0;
1567}
1568
1569static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1570{
1571 return 0;
1572}
1573
1574static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1575 int64_t *timestamp)
1576{
1577 return -EINVAL;
1578}
1579
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001580static int out_get_presentation_position(const struct audio_stream_out *stream,
1581 uint64_t *frames, struct timespec *timestamp)
1582{
1583 struct stream_out *out = (struct stream_out *)stream;
1584 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001585 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001586
1587 pthread_mutex_lock(&out->lock);
1588
Eric Laurent949a0892013-09-20 09:20:13 -07001589 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1590 if (out->compr != NULL) {
1591 compress_get_tstamp(out->compr, &dsp_frames,
1592 &out->sample_rate);
1593 ALOGVV("%s rendered frames %ld sample_rate %d",
1594 __func__, dsp_frames, out->sample_rate);
1595 *frames = dsp_frames;
1596 ret = 0;
1597 /* this is the best we can do */
1598 clock_gettime(CLOCK_MONOTONIC, timestamp);
1599 }
1600 } else {
1601 if (out->pcm) {
1602 size_t avail;
1603 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1604 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001605 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001606 // This adjustment accounts for buffering after app processor.
1607 // It is based on estimated DSP latency per use case, rather than exact.
1608 signed_frames -=
1609 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1610
Eric Laurent949a0892013-09-20 09:20:13 -07001611 // It would be unusual for this value to be negative, but check just in case ...
1612 if (signed_frames >= 0) {
1613 *frames = signed_frames;
1614 ret = 0;
1615 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001616 }
1617 }
1618 }
1619
1620 pthread_mutex_unlock(&out->lock);
1621
1622 return ret;
1623}
1624
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001625static int out_set_callback(struct audio_stream_out *stream,
1626 stream_callback_t callback, void *cookie)
1627{
1628 struct stream_out *out = (struct stream_out *)stream;
1629
1630 ALOGV("%s", __func__);
1631 pthread_mutex_lock(&out->lock);
1632 out->offload_callback = callback;
1633 out->offload_cookie = cookie;
1634 pthread_mutex_unlock(&out->lock);
1635 return 0;
1636}
1637
1638static int out_pause(struct audio_stream_out* stream)
1639{
1640 struct stream_out *out = (struct stream_out *)stream;
1641 int status = -ENOSYS;
1642 ALOGV("%s", __func__);
1643 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1644 pthread_mutex_lock(&out->lock);
1645 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1646 status = compress_pause(out->compr);
1647 out->offload_state = OFFLOAD_STATE_PAUSED;
1648 }
1649 pthread_mutex_unlock(&out->lock);
1650 }
1651 return status;
1652}
1653
1654static int out_resume(struct audio_stream_out* stream)
1655{
1656 struct stream_out *out = (struct stream_out *)stream;
1657 int status = -ENOSYS;
1658 ALOGV("%s", __func__);
1659 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1660 status = 0;
1661 pthread_mutex_lock(&out->lock);
1662 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1663 status = compress_resume(out->compr);
1664 out->offload_state = OFFLOAD_STATE_PLAYING;
1665 }
1666 pthread_mutex_unlock(&out->lock);
1667 }
1668 return status;
1669}
1670
1671static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1672{
1673 struct stream_out *out = (struct stream_out *)stream;
1674 int status = -ENOSYS;
1675 ALOGV("%s", __func__);
1676 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1677 pthread_mutex_lock(&out->lock);
1678 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1679 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1680 else
1681 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1682 pthread_mutex_unlock(&out->lock);
1683 }
1684 return status;
1685}
1686
1687static int out_flush(struct audio_stream_out* stream)
1688{
1689 struct stream_out *out = (struct stream_out *)stream;
1690 ALOGV("%s", __func__);
1691 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1692 pthread_mutex_lock(&out->lock);
1693 stop_compressed_output_l(out);
1694 pthread_mutex_unlock(&out->lock);
1695 return 0;
1696 }
1697 return -ENOSYS;
1698}
1699
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001700/** audio_stream_in implementation **/
1701static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1702{
1703 struct stream_in *in = (struct stream_in *)stream;
1704
1705 return in->config.rate;
1706}
1707
1708static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1709{
1710 return -ENOSYS;
1711}
1712
1713static size_t in_get_buffer_size(const struct audio_stream *stream)
1714{
1715 struct stream_in *in = (struct stream_in *)stream;
1716
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001717 if(in->usecase == USECASE_COMPRESS_VOIP_CALL)
1718 return voice_extn_compress_voip_in_get_buffer_size(in);
Mingming Yine62d7842013-10-25 16:26:03 -07001719 else if(audio_extn_compr_cap_usecase_supported(in->usecase))
1720 return audio_extn_compr_cap_get_buffer_size(in->config.format);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001721
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001722 return in->config.period_size * audio_stream_frame_size(stream);
1723}
1724
1725static uint32_t in_get_channels(const struct audio_stream *stream)
1726{
1727 struct stream_in *in = (struct stream_in *)stream;
1728
1729 return in->channel_mask;
1730}
1731
1732static audio_format_t in_get_format(const struct audio_stream *stream)
1733{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001734 struct stream_in *in = (struct stream_in *)stream;
1735
1736 return in->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001737}
1738
1739static int in_set_format(struct audio_stream *stream, audio_format_t format)
1740{
1741 return -ENOSYS;
1742}
1743
1744static int in_standby(struct audio_stream *stream)
1745{
1746 struct stream_in *in = (struct stream_in *)stream;
1747 struct audio_device *adev = in->dev;
1748 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001749 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001750
1751 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
1752 /* Ignore standby in case of voip call because the voip input
1753 * stream is closed in adev_close_input_stream()
1754 */
1755 ALOGV("%s: Ignore Standby in VOIP call", __func__);
1756 return status;
1757 }
1758
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001759 pthread_mutex_lock(&in->lock);
1760 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001761 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001762 if (in->pcm) {
1763 pcm_close(in->pcm);
1764 in->pcm = NULL;
1765 }
1766 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001767 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001768 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001769 }
1770 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001771 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001772 return status;
1773}
1774
1775static int in_dump(const struct audio_stream *stream, int fd)
1776{
1777 return 0;
1778}
1779
1780static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1781{
1782 struct stream_in *in = (struct stream_in *)stream;
1783 struct audio_device *adev = in->dev;
1784 struct str_parms *parms;
1785 char *str;
1786 char value[32];
1787 int ret, val = 0;
1788
Eric Laurent994a6932013-07-17 11:51:42 -07001789 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001790 parms = str_parms_create_str(kvpairs);
1791
1792 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1793
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001794 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001795 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001796 if (ret >= 0) {
1797 val = atoi(value);
1798 /* no audio source uses val == 0 */
1799 if ((in->source != val) && (val != 0)) {
1800 in->source = val;
1801 }
1802 }
1803
1804 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1805 if (ret >= 0) {
1806 val = atoi(value);
1807 if ((in->device != val) && (val != 0)) {
1808 in->device = val;
1809 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001810 if (!in->standby)
1811 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001812 }
1813 }
1814
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001815 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001816 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001817
1818 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001819 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001820 return ret;
1821}
1822
1823static char* in_get_parameters(const struct audio_stream *stream,
1824 const char *keys)
1825{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001826 struct stream_in *in = (struct stream_in *)stream;
1827 struct str_parms *query = str_parms_create_str(keys);
1828 char *str;
1829 char value[256];
1830 struct str_parms *reply = str_parms_create();
1831 ALOGV("%s: enter: keys - %s", __func__, keys);
1832
1833 voice_extn_in_get_parameters(in, query, reply);
1834
1835 str = str_parms_to_str(reply);
1836 str_parms_destroy(query);
1837 str_parms_destroy(reply);
1838
1839 ALOGV("%s: exit: returns - %s", __func__, str);
1840 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001841}
1842
1843static int in_set_gain(struct audio_stream_in *stream, float gain)
1844{
1845 return 0;
1846}
1847
1848static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1849 size_t bytes)
1850{
1851 struct stream_in *in = (struct stream_in *)stream;
1852 struct audio_device *adev = in->dev;
1853 int i, ret = -1;
1854
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001855 pthread_mutex_lock(&in->lock);
1856 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001857 pthread_mutex_lock(&adev->lock);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001858 if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
1859 ret = voice_extn_compress_voip_start_input_stream(in);
1860 else
1861 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001862 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001863 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001864 goto exit;
1865 }
1866 in->standby = 0;
1867 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001868
1869 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001870 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1871 ret = audio_extn_ssr_read(stream, buffer, bytes);
Mingming Yine62d7842013-10-25 16:26:03 -07001872 else if (audio_extn_compr_cap_usecase_supported(in->usecase))
1873 ret = audio_extn_compr_cap_read(in, buffer, bytes);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001874 else
1875 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001876 }
1877
1878 /*
1879 * Instead of writing zeroes here, we could trust the hardware
1880 * to always provide zeroes when muted.
1881 */
Helen Zenge0b186f2013-10-23 14:00:59 -07001882 if (ret == 0 && voice_get_mic_mute(adev) && !voice_is_in_call(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001883 memset(buffer, 0, bytes);
1884
1885exit:
1886 pthread_mutex_unlock(&in->lock);
1887
1888 if (ret != 0) {
1889 in_standby(&in->stream.common);
1890 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1891 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1892 in_get_sample_rate(&in->stream.common));
1893 }
1894 return bytes;
1895}
1896
1897static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1898{
1899 return 0;
1900}
1901
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001902static int add_remove_audio_effect(const struct audio_stream *stream,
1903 effect_handle_t effect,
1904 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001905{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001906 struct stream_in *in = (struct stream_in *)stream;
1907 int status = 0;
1908 effect_descriptor_t desc;
1909
1910 status = (*effect)->get_descriptor(effect, &desc);
1911 if (status != 0)
1912 return status;
1913
1914 pthread_mutex_lock(&in->lock);
1915 pthread_mutex_lock(&in->dev->lock);
1916 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1917 in->enable_aec != enable &&
1918 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1919 in->enable_aec = enable;
1920 if (!in->standby)
1921 select_devices(in->dev, in->usecase);
1922 }
Ravi Kumar Alamanda198185e2013-11-07 15:42:19 -08001923 if (in->enable_ns != enable &&
1924 (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) {
1925 in->enable_ns = enable;
1926 if (!in->standby)
1927 select_devices(in->dev, in->usecase);
1928 }
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001929 pthread_mutex_unlock(&in->dev->lock);
1930 pthread_mutex_unlock(&in->lock);
1931
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001932 return 0;
1933}
1934
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001935static int in_add_audio_effect(const struct audio_stream *stream,
1936 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001937{
Eric Laurent994a6932013-07-17 11:51:42 -07001938 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001939 return add_remove_audio_effect(stream, effect, true);
1940}
1941
1942static int in_remove_audio_effect(const struct audio_stream *stream,
1943 effect_handle_t effect)
1944{
Eric Laurent994a6932013-07-17 11:51:42 -07001945 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001946 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001947}
1948
1949static int adev_open_output_stream(struct audio_hw_device *dev,
1950 audio_io_handle_t handle,
1951 audio_devices_t devices,
1952 audio_output_flags_t flags,
1953 struct audio_config *config,
1954 struct audio_stream_out **stream_out)
1955{
1956 struct audio_device *adev = (struct audio_device *)dev;
1957 struct stream_out *out;
1958 int i, ret;
1959
Eric Laurent994a6932013-07-17 11:51:42 -07001960 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001961 __func__, config->sample_rate, config->channel_mask, devices, flags);
1962 *stream_out = NULL;
1963 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1964
1965 if (devices == AUDIO_DEVICE_NONE)
1966 devices = AUDIO_DEVICE_OUT_SPEAKER;
1967
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001968 out->flags = flags;
1969 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001970 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001971 out->format = config->format;
1972 out->sample_rate = config->sample_rate;
1973 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1974 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001975 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001976
1977 /* Init use case and pcm_config */
1978 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1979 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001980 pthread_mutex_lock(&adev->lock);
1981 ret = read_hdmi_channel_masks(out);
1982 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001983 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001984 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001985
1986 if (config->sample_rate == 0)
1987 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1988 if (config->channel_mask == 0)
1989 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1990
1991 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001992 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001993 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1994 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001995 out->config.rate = config->sample_rate;
1996 out->config.channels = popcount(out->channel_mask);
1997 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08001998 } else if ((out->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
1999 (out->flags == (AUDIO_OUTPUT_FLAG_DIRECT | AUDIO_OUTPUT_FLAG_VOIP_RX)) &&
2000 (voice_extn_compress_voip_is_format_supported(out->format))) {
2001 ret = voice_extn_compress_voip_open_output_stream(out);
2002 if (ret != 0) {
2003 ALOGE("%s: Compress voip output cannot be opened, error:%d",
2004 __func__, ret);
2005 goto error_open;
2006 }
2007 out->config.rate = config->sample_rate;
2008 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002009 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
2010 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
2011 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
2012 ALOGE("%s: Unsupported Offload information", __func__);
2013 ret = -EINVAL;
2014 goto error_open;
2015 }
2016 if (!is_supported_format(config->offload_info.format)) {
2017 ALOGE("%s: Unsupported audio format", __func__);
2018 ret = -EINVAL;
2019 goto error_open;
2020 }
2021
2022 out->compr_config.codec = (struct snd_codec *)
2023 calloc(1, sizeof(struct snd_codec));
2024
2025 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
2026 if (config->offload_info.channel_mask)
2027 out->channel_mask = config->offload_info.channel_mask;
2028 else if (config->channel_mask)
2029 out->channel_mask = config->channel_mask;
2030 out->format = config->offload_info.format;
2031 out->sample_rate = config->offload_info.sample_rate;
2032
2033 out->stream.set_callback = out_set_callback;
2034 out->stream.pause = out_pause;
2035 out->stream.resume = out_resume;
2036 out->stream.drain = out_drain;
2037 out->stream.flush = out_flush;
2038
2039 out->compr_config.codec->id =
2040 get_snd_codec_id(config->offload_info.format);
2041 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
2042 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
2043 out->compr_config.codec->sample_rate =
2044 compress_get_alsa_rate(config->offload_info.sample_rate);
2045 out->compr_config.codec->bit_rate =
2046 config->offload_info.bit_rate;
2047 out->compr_config.codec->ch_in =
2048 popcount(config->channel_mask);
2049 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
2050
2051 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
2052 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07002053
2054 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002055 create_offload_callback_thread(out);
2056 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
2057 __func__, config->offload_info.version,
2058 config->offload_info.bit_rate);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002059 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
2060 ret = voice_check_and_set_incall_music_usecase(adev, out);
2061 if (ret != 0) {
2062 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
2063 __func__, ret);
2064 goto error_open;
2065 }
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002066 } else if (out->flags & AUDIO_OUTPUT_FLAG_FAST) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002067 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
2068 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002069 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda8f715d92013-11-01 20:37:38 -07002070 } else {
2071 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
2072 out->config = pcm_config_deep_buffer;
2073 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002074 }
2075
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002076 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
2077 if(adev->primary_output == NULL)
2078 adev->primary_output = out;
2079 else {
2080 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002081 ret = -EEXIST;
2082 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002083 }
2084 }
2085
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002086 /* Check if this usecase is already existing */
2087 pthread_mutex_lock(&adev->lock);
2088 if (get_usecase_from_list(adev, out->usecase) != NULL) {
2089 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002090 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002091 ret = -EEXIST;
2092 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002093 }
2094 pthread_mutex_unlock(&adev->lock);
2095
2096 out->stream.common.get_sample_rate = out_get_sample_rate;
2097 out->stream.common.set_sample_rate = out_set_sample_rate;
2098 out->stream.common.get_buffer_size = out_get_buffer_size;
2099 out->stream.common.get_channels = out_get_channels;
2100 out->stream.common.get_format = out_get_format;
2101 out->stream.common.set_format = out_set_format;
2102 out->stream.common.standby = out_standby;
2103 out->stream.common.dump = out_dump;
2104 out->stream.common.set_parameters = out_set_parameters;
2105 out->stream.common.get_parameters = out_get_parameters;
2106 out->stream.common.add_audio_effect = out_add_audio_effect;
2107 out->stream.common.remove_audio_effect = out_remove_audio_effect;
2108 out->stream.get_latency = out_get_latency;
2109 out->stream.set_volume = out_set_volume;
2110 out->stream.write = out_write;
2111 out->stream.get_render_position = out_get_render_position;
2112 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002113 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002114
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002115 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07002116 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002117 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002118
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002119 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2120 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002121
2122 config->format = out->stream.common.get_format(&out->stream.common);
2123 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2124 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2125
2126 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002127 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002128 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002129
2130error_open:
2131 free(out);
2132 *stream_out = NULL;
2133 ALOGD("%s: exit: ret %d", __func__, ret);
2134 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002135}
2136
2137static void adev_close_output_stream(struct audio_hw_device *dev,
2138 struct audio_stream_out *stream)
2139{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002140 struct stream_out *out = (struct stream_out *)stream;
2141 struct audio_device *adev = out->dev;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002142 int ret = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002143
Eric Laurent994a6932013-07-17 11:51:42 -07002144 ALOGV("%s: enter", __func__);
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002145 if (out->usecase == USECASE_COMPRESS_VOIP_CALL) {
2146 ret = voice_extn_compress_voip_close_output_stream(&stream->common);
2147 if(ret != 0)
2148 ALOGE("%s: Compress voip output cannot be closed, error:%d",
2149 __func__, ret);
2150 }
2151 else
2152 out_standby(&stream->common);
2153
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002154 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2155 destroy_offload_callback_thread(out);
2156
2157 if (out->compr_config.codec != NULL)
2158 free(out->compr_config.codec);
2159 }
2160 pthread_cond_destroy(&out->cond);
2161 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002162 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002163 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002164}
2165
2166static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2167{
2168 struct audio_device *adev = (struct audio_device *)dev;
2169 struct str_parms *parms;
2170 char *str;
2171 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002172 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002173 int ret;
2174
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002175 ALOGD("%s: enter: %s", __func__, kvpairs);
2176
2177 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002178 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002179
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002180 voice_set_parameters(adev, parms);
2181 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002182
2183 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2184 if (ret >= 0) {
2185 /* When set to false, HAL should disable EC and NS
2186 * But it is currently not supported.
2187 */
2188 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2189 adev->bluetooth_nrec = true;
2190 else
2191 adev->bluetooth_nrec = false;
2192 }
2193
2194 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2195 if (ret >= 0) {
2196 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2197 adev->screen_off = false;
2198 else
2199 adev->screen_off = true;
2200 }
2201
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002202 ret = str_parms_get_int(parms, "rotation", &val);
2203 if (ret >= 0) {
2204 bool reverse_speakers = false;
2205 switch(val) {
2206 // FIXME: note that the code below assumes that the speakers are in the correct placement
2207 // relative to the user when the device is rotated 90deg from its default rotation. This
2208 // assumption is device-specific, not platform-specific like this code.
2209 case 270:
2210 reverse_speakers = true;
2211 break;
2212 case 0:
2213 case 90:
2214 case 180:
2215 break;
2216 default:
2217 ALOGE("%s: unexpected rotation of %d", __func__, val);
2218 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002219 if (adev->speaker_lr_swap != reverse_speakers) {
2220 adev->speaker_lr_swap = reverse_speakers;
2221 // only update the selected device if there is active pcm playback
2222 struct audio_usecase *usecase;
2223 struct listnode *node;
2224 list_for_each(node, &adev->usecase_list) {
2225 usecase = node_to_item(node, struct audio_usecase, list);
2226 if (usecase->type == PCM_PLAYBACK) {
2227 select_devices(adev, usecase->id);
2228 break;
2229 }
2230 }
2231 }
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002232 }
2233
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002234 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002235 str_parms_destroy(parms);
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002236
2237 pthread_mutex_unlock(&adev->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07002238 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002239 return ret;
2240}
2241
2242static char* adev_get_parameters(const struct audio_hw_device *dev,
2243 const char *keys)
2244{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002245 struct audio_device *adev = (struct audio_device *)dev;
2246 struct str_parms *reply = str_parms_create();
2247 struct str_parms *query = str_parms_create_str(keys);
2248 char *str;
2249
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002250 pthread_mutex_lock(&adev->lock);
2251
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002252 audio_extn_get_parameters(adev, query, reply);
2253 platform_get_parameters(adev->platform, query, reply);
2254 str = str_parms_to_str(reply);
2255 str_parms_destroy(query);
2256 str_parms_destroy(reply);
2257
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002258 pthread_mutex_unlock(&adev->lock);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002259 ALOGV("%s: exit: returns - %s", __func__, str);
2260 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002261}
2262
2263static int adev_init_check(const struct audio_hw_device *dev)
2264{
2265 return 0;
2266}
2267
2268static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2269{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002270 int ret;
2271 struct audio_device *adev = (struct audio_device *)dev;
2272 pthread_mutex_lock(&adev->lock);
2273 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002274 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002275 pthread_mutex_unlock(&adev->lock);
2276 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002277}
2278
2279static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2280{
2281 return -ENOSYS;
2282}
2283
2284static int adev_get_master_volume(struct audio_hw_device *dev,
2285 float *volume)
2286{
2287 return -ENOSYS;
2288}
2289
2290static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2291{
2292 return -ENOSYS;
2293}
2294
2295static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2296{
2297 return -ENOSYS;
2298}
2299
2300static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2301{
2302 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002303 pthread_mutex_lock(&adev->lock);
2304 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002305 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002306 adev->mode = mode;
2307 }
2308 pthread_mutex_unlock(&adev->lock);
2309 return 0;
2310}
2311
2312static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2313{
Shiv Maliyappanahalli3bb73582013-11-05 12:49:15 -08002314 int ret;
2315
2316 pthread_mutex_lock(&adev->lock);
2317 ret = voice_set_mic_mute((struct audio_device *)dev, state);
2318 pthread_mutex_unlock(&adev->lock);
2319
2320 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002321}
2322
2323static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2324{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002325 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002326 return 0;
2327}
2328
2329static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2330 const struct audio_config *config)
2331{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002332 int channel_count = popcount(config->channel_mask);
2333
2334 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2335}
2336
2337static int adev_open_input_stream(struct audio_hw_device *dev,
2338 audio_io_handle_t handle,
2339 audio_devices_t devices,
2340 struct audio_config *config,
2341 struct audio_stream_in **stream_in)
2342{
2343 struct audio_device *adev = (struct audio_device *)dev;
2344 struct stream_in *in;
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002345 int ret = 0, buffer_size, frame_size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002346 int channel_count = popcount(config->channel_mask);
2347
Eric Laurent994a6932013-07-17 11:51:42 -07002348 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002349 *stream_in = NULL;
2350 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2351 return -EINVAL;
2352
2353 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2354
2355 in->stream.common.get_sample_rate = in_get_sample_rate;
2356 in->stream.common.set_sample_rate = in_set_sample_rate;
2357 in->stream.common.get_buffer_size = in_get_buffer_size;
2358 in->stream.common.get_channels = in_get_channels;
2359 in->stream.common.get_format = in_get_format;
2360 in->stream.common.set_format = in_set_format;
2361 in->stream.common.standby = in_standby;
2362 in->stream.common.dump = in_dump;
2363 in->stream.common.set_parameters = in_set_parameters;
2364 in->stream.common.get_parameters = in_get_parameters;
2365 in->stream.common.add_audio_effect = in_add_audio_effect;
2366 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2367 in->stream.set_gain = in_set_gain;
2368 in->stream.read = in_read;
2369 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2370
2371 in->device = devices;
2372 in->source = AUDIO_SOURCE_DEFAULT;
2373 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002374 in->standby = 1;
2375 in->channel_mask = config->channel_mask;
2376
2377 /* Update config params with the requested sample rate and channels */
2378 in->usecase = USECASE_AUDIO_RECORD;
2379 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002380 in->config.rate = config->sample_rate;
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002381 in->format = config->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002382
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002383 if ((in->dev->mode == AUDIO_MODE_IN_COMMUNICATION) &&
2384 (voice_extn_compress_voip_is_format_supported(in->format))) {
2385 ret = voice_extn_compress_voip_open_input_stream(in);
2386 if (ret != 0)
2387 {
2388 ALOGE("%s: Compress voip input cannot be opened, error:%d",
2389 __func__, ret);
2390 goto err_open;
2391 }
Mingming Yine62d7842013-10-25 16:26:03 -07002392 } else if (channel_count == 6) {
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002393 if(audio_extn_ssr_get_enabled()) {
2394 if(audio_extn_ssr_init(adev, in)) {
2395 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2396 ret = -EINVAL;
2397 goto err_open;
2398 }
2399 } else {
2400 ret = -EINVAL;
2401 goto err_open;
2402 }
Mingming Yine62d7842013-10-25 16:26:03 -07002403 } else if (audio_extn_compr_cap_enabled() &&
2404 audio_extn_compr_cap_format_supported(config->format)) {
2405 audio_extn_compr_cap_init(adev, in);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002406 } else {
2407 in->config.channels = channel_count;
2408 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2409 buffer_size = get_input_buffer_size(config->sample_rate,
2410 config->format,
2411 channel_count);
2412 in->config.period_size = buffer_size / frame_size;
2413 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002414
2415 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002416 ALOGV("%s: exit", __func__);
Ravi Kumar Alamandafae42112013-11-07 23:31:54 -08002417 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002418
2419err_open:
2420 free(in);
2421 *stream_in = NULL;
2422 return ret;
2423}
2424
2425static void adev_close_input_stream(struct audio_hw_device *dev,
2426 struct audio_stream_in *stream)
2427{
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002428 int ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002429 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002430 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002431
Narsinga Rao Chella05573b72013-11-15 15:21:40 -08002432 if (in->usecase == USECASE_COMPRESS_VOIP_CALL) {
2433 ret = voice_extn_compress_voip_close_input_stream(&stream->common);
2434 if (ret != 0)
2435 ALOGE("%s: Compress voip input cannot be closed, error:%d",
2436 __func__, ret);
2437 } else
2438 in_standby(&stream->common);
2439
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002440 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2441 audio_extn_ssr_deinit();
2442 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002443 free(stream);
2444
Mingming Yine62d7842013-10-25 16:26:03 -07002445 if(audio_extn_compr_cap_enabled() &&
2446 audio_extn_compr_cap_format_supported(in->config.format))
2447 audio_extn_compr_cap_deinit();
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002448 return;
2449}
2450
2451static int adev_dump(const audio_hw_device_t *device, int fd)
2452{
2453 return 0;
2454}
2455
2456static int adev_close(hw_device_t *device)
2457{
2458 struct audio_device *adev = (struct audio_device *)device;
Kiran Kandi910e1862013-10-29 13:29:42 -07002459
2460 if (!adev)
2461 return 0;
2462
2463 pthread_mutex_lock(&adev_init_lock);
2464
2465 if ((--audio_device_ref_count) == 0) {
2466 audio_route_free(adev->audio_route);
2467 free(adev->snd_dev_ref_cnt);
2468 platform_deinit(adev->platform);
2469 audio_extn_listen_deinit(adev);
2470 free(device);
2471 adev = NULL;
2472 }
2473 pthread_mutex_unlock(&adev_init_lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002474 return 0;
2475}
2476
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002477static int adev_open(const hw_module_t *module, const char *name,
2478 hw_device_t **device)
2479{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002480 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002481
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002482 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002483 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2484
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002485 pthread_mutex_lock(&adev_init_lock);
Kiran Kandi910e1862013-10-29 13:29:42 -07002486 if (audio_device_ref_count != 0){
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002487 *device = &adev->device.common;
Kiran Kandi910e1862013-10-29 13:29:42 -07002488 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002489 ALOGD("%s: returning existing instance of adev", __func__);
2490 ALOGD("%s: exit", __func__);
2491 pthread_mutex_unlock(&adev_init_lock);
2492 return 0;
2493 }
2494
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002495 adev = calloc(1, sizeof(struct audio_device));
2496
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002497 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2498 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2499 adev->device.common.module = (struct hw_module_t *)module;
2500 adev->device.common.close = adev_close;
2501
2502 adev->device.init_check = adev_init_check;
2503 adev->device.set_voice_volume = adev_set_voice_volume;
2504 adev->device.set_master_volume = adev_set_master_volume;
2505 adev->device.get_master_volume = adev_get_master_volume;
2506 adev->device.set_master_mute = adev_set_master_mute;
2507 adev->device.get_master_mute = adev_get_master_mute;
2508 adev->device.set_mode = adev_set_mode;
2509 adev->device.set_mic_mute = adev_set_mic_mute;
2510 adev->device.get_mic_mute = adev_get_mic_mute;
2511 adev->device.set_parameters = adev_set_parameters;
2512 adev->device.get_parameters = adev_get_parameters;
2513 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2514 adev->device.open_output_stream = adev_open_output_stream;
2515 adev->device.close_output_stream = adev_close_output_stream;
2516 adev->device.open_input_stream = adev_open_input_stream;
2517 adev->device.close_input_stream = adev_close_input_stream;
2518 adev->device.dump = adev_dump;
2519
2520 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002521 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002522 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002523 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002524 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002525 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002526 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002527 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002528 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002529 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002530 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002531
2532 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002533 adev->platform = platform_init(adev);
2534 if (!adev->platform) {
2535 free(adev->snd_dev_ref_cnt);
2536 free(adev);
2537 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2538 *device = NULL;
2539 return -EINVAL;
2540 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002541
2542 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2543 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2544 if (adev->visualizer_lib == NULL) {
2545 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2546 } else {
2547 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2548 adev->visualizer_start_output =
2549 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2550 "visualizer_hal_start_output");
2551 adev->visualizer_stop_output =
2552 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2553 "visualizer_hal_stop_output");
2554 }
2555 }
Kiran Kandi910e1862013-10-29 13:29:42 -07002556 audio_extn_listen_init(adev);
Eric Laurentc4aef752013-09-12 17:45:53 -07002557
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002558 *device = &adev->device.common;
2559
Kiran Kandi910e1862013-10-29 13:29:42 -07002560 audio_device_ref_count++;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002561 pthread_mutex_unlock(&adev_init_lock);
2562
Eric Laurent994a6932013-07-17 11:51:42 -07002563 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002564 return 0;
2565}
2566
2567static struct hw_module_methods_t hal_module_methods = {
2568 .open = adev_open,
2569};
2570
2571struct audio_module HAL_MODULE_INFO_SYM = {
2572 .common = {
2573 .tag = HARDWARE_MODULE_TAG,
2574 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2575 .hal_api_version = HARDWARE_HAL_API_VERSION,
2576 .id = AUDIO_HARDWARE_MODULE_ID,
2577 .name = "QCOM Audio HAL",
2578 .author = "Code Aurora Forum",
2579 .methods = &hal_module_methods,
2580 },
2581};