blob: 28035d3c7ee25acbb98c5e833402f1acca0e828e [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"
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -080053
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -070054#include "sound/compress_params.h"
55
56#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (32 * 1024)
57#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 4
58/* ToDo: Check and update a proper value in msec */
59#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
60#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
61
Eric Laurentb23d5282013-05-14 15:27:20 -070062struct pcm_config pcm_config_deep_buffer = {
63 .channels = 2,
64 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
65 .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
66 .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
67 .format = PCM_FORMAT_S16_LE,
68 .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
69 .stop_threshold = INT_MAX,
70 .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
71};
72
73struct pcm_config pcm_config_low_latency = {
74 .channels = 2,
75 .rate = DEFAULT_OUTPUT_SAMPLING_RATE,
76 .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
77 .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
78 .format = PCM_FORMAT_S16_LE,
79 .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
80 .stop_threshold = INT_MAX,
81 .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
82};
83
84struct pcm_config pcm_config_hdmi_multi = {
85 .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
86 .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
87 .period_size = HDMI_MULTI_PERIOD_SIZE,
88 .period_count = HDMI_MULTI_PERIOD_COUNT,
89 .format = PCM_FORMAT_S16_LE,
90 .start_threshold = 0,
91 .stop_threshold = INT_MAX,
92 .avail_min = 0,
93};
94
95struct pcm_config pcm_config_audio_capture = {
96 .channels = 2,
Eric Laurentb23d5282013-05-14 15:27:20 -070097 .period_count = AUDIO_CAPTURE_PERIOD_COUNT,
98 .format = PCM_FORMAT_S16_LE,
99};
100
Eric Laurentb23d5282013-05-14 15:27:20 -0700101static const char * const use_case_table[AUDIO_USECASE_MAX] = {
102 [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
103 [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
104 [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700105 [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
Eric Laurentb23d5282013-05-14 15:27:20 -0700106 [USECASE_AUDIO_RECORD] = "audio-record",
107 [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700108 [USECASE_AUDIO_PLAYBACK_FM] = "play-fm",
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700109 [USECASE_VOICE_CALL] = "voice-call",
Shruthi Krishnaace10852013-10-25 14:32:12 -0700110
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700111 [USECASE_VOICE2_CALL] = "voice2-call",
112 [USECASE_VOLTE_CALL] = "volte-call",
113 [USECASE_QCHAT_CALL] = "qchat-call",
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700114 [USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
115 [USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
116 [USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -0700117 [USECASE_INCALL_MUSIC_UPLINK] = "incall_music_uplink",
118 [USECASE_INCALL_MUSIC_UPLINK2] = "incall_music_uplink2",
Eric Laurentb23d5282013-05-14 15:27:20 -0700119};
120
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800121
122#define STRING_TO_ENUM(string) { #string, string }
123
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800124struct string_to_enum {
125 const char *name;
126 uint32_t value;
127};
128
129static const struct string_to_enum out_channels_name_to_enum_table[] = {
130 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
131 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
132 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
133};
134
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700135static struct audio_device *adev = NULL;
136static pthread_mutex_t adev_init_lock;
137static bool is_adev_initialised = false;
Haynes Mathew George5191a852013-09-11 14:19:36 -0700138static int set_voice_volume_l(struct audio_device *adev, float volume);
139
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700140static bool is_supported_format(audio_format_t format)
141{
Eric Laurent86e17132013-09-12 17:49:30 -0700142 if (format == AUDIO_FORMAT_MP3 ||
143 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700144 return true;
145
146 return false;
147}
148
149static int get_snd_codec_id(audio_format_t format)
150{
151 int id = 0;
152
153 switch (format) {
154 case AUDIO_FORMAT_MP3:
155 id = SND_AUDIOCODEC_MP3;
156 break;
157 case AUDIO_FORMAT_AAC:
158 id = SND_AUDIOCODEC_AAC;
159 break;
160 default:
161 ALOGE("%s: Unsupported audio format", __func__);
162 }
163
164 return id;
165}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800166
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700167static int enable_audio_route(struct audio_device *adev,
168 struct audio_usecase *usecase,
169 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800170{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700171 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700172 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800173
174 if (usecase == NULL)
175 return -EINVAL;
176
177 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
178
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800179 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700180 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800181 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700182 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800183
184 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700185 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700186 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700187 audio_route_apply_path(adev->audio_route, mixer_path);
188 if (update_mixer)
189 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800190
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800191 ALOGV("%s: exit", __func__);
192 return 0;
193}
194
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700195int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700196 struct audio_usecase *usecase,
197 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800198{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700199 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700200 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800201
202 if (usecase == NULL)
203 return -EINVAL;
204
205 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700206 if (usecase->type == PCM_CAPTURE)
207 snd_device = usecase->in_snd_device;
208 else
209 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800210 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700211 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700212 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700213 audio_route_reset_path(adev->audio_route, mixer_path);
214 if (update_mixer)
215 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800216
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800217 ALOGV("%s: exit", __func__);
218 return 0;
219}
220
221static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700222 snd_device_t snd_device,
223 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800224{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700225 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
226
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800227 if (snd_device < SND_DEVICE_MIN ||
228 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800229 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800230 return -EINVAL;
231 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700232
233 adev->snd_dev_ref_cnt[snd_device]++;
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700234
235 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
236 ALOGE("%s: Invalid sound device returned", __func__);
237 return -EINVAL;
238 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700239 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700240 ALOGV("%s: snd_device(%d: %s) is already active",
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700241 __func__, snd_device, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700242 return 0;
243 }
244
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700245 /* start usb playback thread */
246 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
247 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
248 audio_extn_usb_start_playback(adev);
249
250 /* start usb capture thread */
251 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
252 audio_extn_usb_start_capture(adev);
253
Eric Laurentb23d5282013-05-14 15:27:20 -0700254 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700255 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800256 return -EINVAL;
257 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800258
Eric Laurent994a6932013-07-17 11:51:42 -0700259 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700260 snd_device, device_name);
261 audio_route_apply_path(adev->audio_route, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700262 if (update_mixer)
263 audio_route_update_mixer(adev->audio_route);
264
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800265 return 0;
266}
267
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700268int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700269 snd_device_t snd_device,
270 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800271{
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700272 char device_name[DEVICE_NAME_MAX_SIZE] = {0};
273
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800274 if (snd_device < SND_DEVICE_MIN ||
275 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800276 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800277 return -EINVAL;
278 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700279 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
280 ALOGE("%s: device ref cnt is already 0", __func__);
281 return -EINVAL;
282 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700283
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700284 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700285
286 /* exit usb play back thread */
287 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
288 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
289 audio_extn_usb_stop_playback();
290
291 /* exit usb capture thread */
292 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
293 audio_extn_usb_stop_capture(adev);
294
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700295 if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) {
296 ALOGE("%s: Invalid sound device returned", __func__);
297 return -EINVAL;
298 }
299
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700300 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700301 ALOGV("%s: snd_device(%d: %s)", __func__,
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700302 snd_device, device_name);
303 audio_route_reset_path(adev->audio_route, device_name);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700304 if (update_mixer)
305 audio_route_update_mixer(adev->audio_route);
306 }
Ravi Kumar Alamanda48c921d2013-10-29 06:07:44 -0700307
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800308 return 0;
309}
310
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700311static void check_usecases_codec_backend(struct audio_device *adev,
312 struct audio_usecase *uc_info,
313 snd_device_t snd_device)
314{
315 struct listnode *node;
316 struct audio_usecase *usecase;
317 bool switch_device[AUDIO_USECASE_MAX];
318 int i, num_uc_to_switch = 0;
319
320 /*
321 * This function is to make sure that all the usecases that are active on
322 * the hardware codec backend are always routed to any one device that is
323 * handled by the hardware codec.
324 * For example, if low-latency and deep-buffer usecases are currently active
325 * on speaker and out_set_parameters(headset) is received on low-latency
326 * output, then we have to make sure deep-buffer is also switched to headset,
327 * because of the limitation that both the devices cannot be enabled
328 * at the same time as they share the same backend.
329 */
330 /* Disable all the usecases on the shared backend other than the
331 specified usecase */
332 for (i = 0; i < AUDIO_USECASE_MAX; i++)
333 switch_device[i] = false;
334
335 list_for_each(node, &adev->usecase_list) {
336 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700337 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700338 usecase != uc_info &&
339 usecase->out_snd_device != snd_device &&
340 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
341 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
342 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700343 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700344 disable_audio_route(adev, usecase, false);
345 switch_device[usecase->id] = true;
346 num_uc_to_switch++;
347 }
348 }
349
350 if (num_uc_to_switch) {
351 /* Make sure all the streams are de-routed before disabling the device */
352 audio_route_update_mixer(adev->audio_route);
353
354 list_for_each(node, &adev->usecase_list) {
355 usecase = node_to_item(node, struct audio_usecase, list);
356 if (switch_device[usecase->id]) {
357 disable_snd_device(adev, usecase->out_snd_device, false);
358 enable_snd_device(adev, snd_device, false);
359 }
360 }
361
362 /* Make sure new snd device is enabled before re-routing the streams */
363 audio_route_update_mixer(adev->audio_route);
364
365 /* Re-route all the usecases on the shared backend other than the
366 specified usecase to new snd devices */
367 list_for_each(node, &adev->usecase_list) {
368 usecase = node_to_item(node, struct audio_usecase, list);
369 /* Update the out_snd_device only before enabling the audio route */
370 if (switch_device[usecase->id] ) {
371 usecase->out_snd_device = snd_device;
372 enable_audio_route(adev, usecase, false);
373 }
374 }
375
376 audio_route_update_mixer(adev->audio_route);
377 }
378}
379
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700380static void check_and_route_capture_usecases(struct audio_device *adev,
381 struct audio_usecase *uc_info,
382 snd_device_t snd_device)
383{
384 struct listnode *node;
385 struct audio_usecase *usecase;
386 bool switch_device[AUDIO_USECASE_MAX];
387 int i, num_uc_to_switch = 0;
388
389 /*
390 * This function is to make sure that all the active capture usecases
391 * are always routed to the same input sound device.
392 * For example, if audio-record and voice-call usecases are currently
393 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
394 * is received for voice call then we have to make sure that audio-record
395 * usecase is also switched to earpiece i.e. voice-dmic-ef,
396 * because of the limitation that two devices cannot be enabled
397 * at the same time if they share the same backend.
398 */
399 for (i = 0; i < AUDIO_USECASE_MAX; i++)
400 switch_device[i] = false;
401
402 list_for_each(node, &adev->usecase_list) {
403 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700404 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700405 usecase != uc_info &&
406 usecase->in_snd_device != snd_device) {
407 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
408 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700409 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700410 disable_audio_route(adev, usecase, false);
411 switch_device[usecase->id] = true;
412 num_uc_to_switch++;
413 }
414 }
415
416 if (num_uc_to_switch) {
417 /* Make sure all the streams are de-routed before disabling the device */
418 audio_route_update_mixer(adev->audio_route);
419
420 list_for_each(node, &adev->usecase_list) {
421 usecase = node_to_item(node, struct audio_usecase, list);
422 if (switch_device[usecase->id]) {
423 disable_snd_device(adev, usecase->in_snd_device, false);
424 enable_snd_device(adev, snd_device, false);
425 }
426 }
427
428 /* Make sure new snd device is enabled before re-routing the streams */
429 audio_route_update_mixer(adev->audio_route);
430
431 /* Re-route all the usecases on the shared backend other than the
432 specified usecase to new snd devices */
433 list_for_each(node, &adev->usecase_list) {
434 usecase = node_to_item(node, struct audio_usecase, list);
435 /* Update the in_snd_device only before enabling the audio route */
436 if (switch_device[usecase->id] ) {
437 usecase->in_snd_device = snd_device;
438 enable_audio_route(adev, usecase, false);
439 }
440 }
441
442 audio_route_update_mixer(adev->audio_route);
443 }
444}
445
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700446static int disable_all_usecases_of_type(struct audio_device *adev,
447 usecase_type_t usecase_type,
448 bool update_mixer)
449{
450 struct audio_usecase *usecase;
451 struct listnode *node;
452 int ret = 0;
453
454 list_for_each(node, &adev->usecase_list) {
455 usecase = node_to_item(node, struct audio_usecase, list);
456 if (usecase->type == usecase_type) {
457 ALOGV("%s: usecase id %d", __func__, usecase->id);
458 ret = disable_audio_route(adev, usecase, update_mixer);
459 if (ret) {
460 ALOGE("%s: Failed to disable usecase id %d",
461 __func__, usecase->id);
462 }
463 }
464 }
465
466 return ret;
467}
468
469static int enable_all_usecases_of_type(struct audio_device *adev,
470 usecase_type_t usecase_type,
471 bool update_mixer)
472{
473 struct audio_usecase *usecase;
474 struct listnode *node;
475 int ret = 0;
476
477 list_for_each(node, &adev->usecase_list) {
478 usecase = node_to_item(node, struct audio_usecase, list);
479 if (usecase->type == usecase_type) {
480 ALOGV("%s: usecase id %d", __func__, usecase->id);
481 ret = enable_audio_route(adev, usecase, update_mixer);
482 if (ret) {
483 ALOGE("%s: Failed to enable usecase id %d",
484 __func__, usecase->id);
485 }
486 }
487 }
488
489 return ret;
490}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800491
492/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700493static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800494{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700495 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700496 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800497
498 switch (channels) {
499 /*
500 * Do not handle stereo output in Multi-channel cases
501 * Stereo case is handled in normal playback path
502 */
503 case 6:
504 ALOGV("%s: HDMI supports 5.1", __func__);
505 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
506 break;
507 case 8:
508 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
509 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
510 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
511 break;
512 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700513 ALOGE("HDMI does not support multi channel playback");
514 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800515 break;
516 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700517 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800518}
519
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700520static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
521{
522 struct audio_usecase *usecase;
523 struct listnode *node;
524
525 list_for_each(node, &adev->usecase_list) {
526 usecase = node_to_item(node, struct audio_usecase, list);
527 if (usecase->type == VOICE_CALL) {
528 ALOGV("%s: usecase id %d", __func__, usecase->id);
529 return usecase->id;
530 }
531 }
532 return USECASE_INVALID;
533}
534
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700535struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700536 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700537{
538 struct audio_usecase *usecase;
539 struct listnode *node;
540
541 list_for_each(node, &adev->usecase_list) {
542 usecase = node_to_item(node, struct audio_usecase, list);
543 if (usecase->id == uc_id)
544 return usecase;
545 }
546 return NULL;
547}
548
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700549int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800550{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800551 snd_device_t out_snd_device = SND_DEVICE_NONE;
552 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700553 struct audio_usecase *usecase = NULL;
554 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800555 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700556 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800557
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700558 usecase = get_usecase_from_list(adev, uc_id);
559 if (usecase == NULL) {
560 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
561 return -EINVAL;
562 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800563
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700564 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700565 out_snd_device = platform_get_output_snd_device(adev->platform,
566 usecase->stream.out->devices);
567 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700568 usecase->devices = usecase->stream.out->devices;
569 } else {
570 /*
571 * If the voice call is active, use the sound devices of voice call usecase
572 * so that it would not result any device switch. All the usecases will
573 * be switched to new device when select_devices() is called for voice call
574 * usecase. This is to avoid switching devices for voice call when
575 * check_usecases_codec_backend() is called below.
576 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700577 if (voice_is_in_call(adev)) {
578 vc_usecase = get_usecase_from_list(adev,
579 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700580 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
581 in_snd_device = vc_usecase->in_snd_device;
582 out_snd_device = vc_usecase->out_snd_device;
583 }
584 }
585 if (usecase->type == PCM_PLAYBACK) {
586 usecase->devices = usecase->stream.out->devices;
587 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700588 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700589 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700590 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700591 if (usecase->stream.out == adev->primary_output &&
592 adev->active_input &&
593 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
594 select_devices(adev, adev->active_input->usecase);
595 }
596 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700597 } else if (usecase->type == PCM_CAPTURE) {
598 usecase->devices = usecase->stream.in->device;
599 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700600 if (in_snd_device == SND_DEVICE_NONE) {
601 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
602 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700603 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700604 adev->primary_output->devices);
605 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700606 in_snd_device = platform_get_input_snd_device(adev->platform,
607 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700608 }
609 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700610 }
611 }
612
613 if (out_snd_device == usecase->out_snd_device &&
614 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800615 return 0;
616 }
617
sangwoobc677242013-08-08 16:53:43 +0900618 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700619 out_snd_device, platform_get_snd_device_name(out_snd_device),
620 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800621
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800622 /*
623 * Limitation: While in call, to do a device switch we need to disable
624 * and enable both RX and TX devices though one of them is same as current
625 * device.
626 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700627 if (usecase->type == VOICE_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700628 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700629 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800630 }
631
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700632 /* Disable current sound devices */
633 if (usecase->out_snd_device != SND_DEVICE_NONE) {
634 disable_audio_route(adev, usecase, true);
635 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800636 }
637
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700638 if (usecase->in_snd_device != SND_DEVICE_NONE) {
639 disable_audio_route(adev, usecase, true);
640 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800641 }
642
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700643 /* Enable new sound devices */
644 if (out_snd_device != SND_DEVICE_NONE) {
645 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
646 check_usecases_codec_backend(adev, usecase, out_snd_device);
647 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800648 }
649
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700650 if (in_snd_device != SND_DEVICE_NONE) {
651 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700652 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700653 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700654
Eric Laurentb23d5282013-05-14 15:27:20 -0700655 if (usecase->type == VOICE_CALL)
656 status = platform_switch_voice_call_device_post(adev->platform,
657 out_snd_device,
658 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800659
sangwoo170731f2013-06-08 15:36:36 +0900660 audio_route_update_mixer(adev->audio_route);
661
662 usecase->in_snd_device = in_snd_device;
663 usecase->out_snd_device = out_snd_device;
664
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700665 if (usecase->type == VOICE_CALL)
666 enable_all_usecases_of_type(adev, VOICE_CALL, true);
667 else
668 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900669
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800670 return status;
671}
672
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800673static int stop_input_stream(struct stream_in *in)
674{
675 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800676 struct audio_usecase *uc_info;
677 struct audio_device *adev = in->dev;
678
Eric Laurentc8400632013-02-14 19:04:54 -0800679 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800680
Eric Laurent994a6932013-07-17 11:51:42 -0700681 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700682 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800683 uc_info = get_usecase_from_list(adev, in->usecase);
684 if (uc_info == NULL) {
685 ALOGE("%s: Could not find the usecase (%d) in the list",
686 __func__, in->usecase);
687 return -EINVAL;
688 }
689
Eric Laurent150dbfe2013-02-27 14:31:02 -0800690 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700691 disable_audio_route(adev, uc_info, true);
692
693 /* 2. Disable the tx device */
694 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800695
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800696 list_remove(&uc_info->list);
697 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800698
Eric Laurent994a6932013-07-17 11:51:42 -0700699 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800700 return ret;
701}
702
703int start_input_stream(struct stream_in *in)
704{
705 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800706 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800707 struct audio_usecase *uc_info;
708 struct audio_device *adev = in->dev;
709
Eric Laurent994a6932013-07-17 11:51:42 -0700710 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700711
712 /* Check if source matches incall recording usecase criteria */
713 ret = voice_check_and_set_incall_rec_usecase(adev, in);
714 if (ret)
715 goto error_config;
716 else
717 ALOGV("%s: usecase(%d)", __func__, in->usecase);
718
Eric Laurentb23d5282013-05-14 15:27:20 -0700719 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800720 if (in->pcm_device_id < 0) {
721 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
722 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800723 ret = -EINVAL;
724 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800725 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700726
727 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800728 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
729 uc_info->id = in->usecase;
730 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800731 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700732 uc_info->devices = in->device;
733 uc_info->in_snd_device = SND_DEVICE_NONE;
734 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800735
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800736 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700737 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800738
Eric Laurentc8400632013-02-14 19:04:54 -0800739 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
740 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800741 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
742 PCM_IN, &in->config);
743 if (in->pcm && !pcm_is_ready(in->pcm)) {
744 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
745 pcm_close(in->pcm);
746 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800747 ret = -EIO;
748 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800749 }
Eric Laurent994a6932013-07-17 11:51:42 -0700750 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800751 return ret;
752
753error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800754 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800755
756error_config:
757 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700758 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800759
760 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800761}
762
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700763/* must be called with out->lock locked */
764static int send_offload_cmd_l(struct stream_out* out, int command)
765{
766 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
767
768 ALOGVV("%s %d", __func__, command);
769
770 cmd->cmd = command;
771 list_add_tail(&out->offload_cmd_list, &cmd->node);
772 pthread_cond_signal(&out->offload_cond);
773 return 0;
774}
775
776/* must be called iwth out->lock locked */
777static void stop_compressed_output_l(struct stream_out *out)
778{
779 out->offload_state = OFFLOAD_STATE_IDLE;
780 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700781 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700782 if (out->compr != NULL) {
783 compress_stop(out->compr);
784 while (out->offload_thread_blocked) {
785 pthread_cond_wait(&out->cond, &out->lock);
786 }
787 }
788}
789
790static void *offload_thread_loop(void *context)
791{
792 struct stream_out *out = (struct stream_out *) context;
793 struct listnode *item;
794
795 out->offload_state = OFFLOAD_STATE_IDLE;
796 out->playback_started = 0;
797
798 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
799 set_sched_policy(0, SP_FOREGROUND);
800 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
801
802 ALOGV("%s", __func__);
803 pthread_mutex_lock(&out->lock);
804 for (;;) {
805 struct offload_cmd *cmd = NULL;
806 stream_callback_event_t event;
807 bool send_callback = false;
808
809 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
810 __func__, list_empty(&out->offload_cmd_list),
811 out->offload_state);
812 if (list_empty(&out->offload_cmd_list)) {
813 ALOGV("%s SLEEPING", __func__);
814 pthread_cond_wait(&out->offload_cond, &out->lock);
815 ALOGV("%s RUNNING", __func__);
816 continue;
817 }
818
819 item = list_head(&out->offload_cmd_list);
820 cmd = node_to_item(item, struct offload_cmd, node);
821 list_remove(item);
822
823 ALOGVV("%s STATE %d CMD %d out->compr %p",
824 __func__, out->offload_state, cmd->cmd, out->compr);
825
826 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
827 free(cmd);
828 break;
829 }
830
831 if (out->compr == NULL) {
832 ALOGE("%s: Compress handle is NULL", __func__);
833 pthread_cond_signal(&out->cond);
834 continue;
835 }
836 out->offload_thread_blocked = true;
837 pthread_mutex_unlock(&out->lock);
838 send_callback = false;
839 switch(cmd->cmd) {
840 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
841 compress_wait(out->compr, -1);
842 send_callback = true;
843 event = STREAM_CBK_EVENT_WRITE_READY;
844 break;
845 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700846 compress_next_track(out->compr);
847 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700848 send_callback = true;
849 event = STREAM_CBK_EVENT_DRAIN_READY;
850 break;
851 case OFFLOAD_CMD_DRAIN:
852 compress_drain(out->compr);
853 send_callback = true;
854 event = STREAM_CBK_EVENT_DRAIN_READY;
855 break;
856 default:
857 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
858 break;
859 }
860 pthread_mutex_lock(&out->lock);
861 out->offload_thread_blocked = false;
862 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700863 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700864 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700865 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700866 free(cmd);
867 }
868
869 pthread_cond_signal(&out->cond);
870 while (!list_empty(&out->offload_cmd_list)) {
871 item = list_head(&out->offload_cmd_list);
872 list_remove(item);
873 free(node_to_item(item, struct offload_cmd, node));
874 }
875 pthread_mutex_unlock(&out->lock);
876
877 return NULL;
878}
879
880static int create_offload_callback_thread(struct stream_out *out)
881{
882 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
883 list_init(&out->offload_cmd_list);
884 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
885 offload_thread_loop, out);
886 return 0;
887}
888
889static int destroy_offload_callback_thread(struct stream_out *out)
890{
891 pthread_mutex_lock(&out->lock);
892 stop_compressed_output_l(out);
893 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
894
895 pthread_mutex_unlock(&out->lock);
896 pthread_join(out->offload_thread, (void **) NULL);
897 pthread_cond_destroy(&out->offload_cond);
898
899 return 0;
900}
901
Eric Laurent07eeafd2013-10-06 12:52:49 -0700902static bool allow_hdmi_channel_config(struct audio_device *adev)
903{
904 struct listnode *node;
905 struct audio_usecase *usecase;
906 bool ret = true;
907
908 list_for_each(node, &adev->usecase_list) {
909 usecase = node_to_item(node, struct audio_usecase, list);
910 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
911 /*
912 * If voice call is already existing, do not proceed further to avoid
913 * disabling/enabling both RX and TX devices, CSD calls, etc.
914 * Once the voice call done, the HDMI channels can be configured to
915 * max channels of remaining use cases.
916 */
917 if (usecase->id == USECASE_VOICE_CALL) {
918 ALOGD("%s: voice call is active, no change in HDMI channels",
919 __func__);
920 ret = false;
921 break;
922 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
923 ALOGD("%s: multi channel playback is active, "
924 "no change in HDMI channels", __func__);
925 ret = false;
926 break;
927 }
928 }
929 }
930 return ret;
931}
932
933static int check_and_set_hdmi_channels(struct audio_device *adev,
934 unsigned int channels)
935{
936 struct listnode *node;
937 struct audio_usecase *usecase;
938
939 /* Check if change in HDMI channel config is allowed */
940 if (!allow_hdmi_channel_config(adev))
941 return 0;
942
943 if (channels == adev->cur_hdmi_channels) {
944 ALOGD("%s: Requested channels are same as current", __func__);
945 return 0;
946 }
947
948 platform_set_hdmi_channels(adev->platform, channels);
949 adev->cur_hdmi_channels = channels;
950
951 /*
952 * Deroute all the playback streams routed to HDMI so that
953 * the back end is deactivated. Note that backend will not
954 * be deactivated if any one stream is connected to it.
955 */
956 list_for_each(node, &adev->usecase_list) {
957 usecase = node_to_item(node, struct audio_usecase, list);
958 if (usecase->type == PCM_PLAYBACK &&
959 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
960 disable_audio_route(adev, usecase, true);
961 }
962 }
963
964 /*
965 * Enable all the streams disabled above. Now the HDMI backend
966 * will be activated with new channel configuration
967 */
968 list_for_each(node, &adev->usecase_list) {
969 usecase = node_to_item(node, struct audio_usecase, list);
970 if (usecase->type == PCM_PLAYBACK &&
971 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
972 enable_audio_route(adev, usecase, true);
973 }
974 }
975
976 return 0;
977}
978
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800979static int stop_output_stream(struct stream_out *out)
980{
981 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800982 struct audio_usecase *uc_info;
983 struct audio_device *adev = out->dev;
984
Eric Laurent994a6932013-07-17 11:51:42 -0700985 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700986 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800987 uc_info = get_usecase_from_list(adev, out->usecase);
988 if (uc_info == NULL) {
989 ALOGE("%s: Could not find the usecase (%d) in the list",
990 __func__, out->usecase);
991 return -EINVAL;
992 }
993
Eric Laurentc4aef752013-09-12 17:45:53 -0700994 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
995 adev->visualizer_stop_output != NULL)
996 adev->visualizer_stop_output(out->handle);
997
Eric Laurent150dbfe2013-02-27 14:31:02 -0800998 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700999 disable_audio_route(adev, uc_info, true);
1000
1001 /* 2. Disable the rx device */
1002 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001003
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001004 list_remove(&uc_info->list);
1005 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001006
Eric Laurent07eeafd2013-10-06 12:52:49 -07001007 /* Must be called after removing the usecase from list */
1008 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1009 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
1010
Eric Laurent994a6932013-07-17 11:51:42 -07001011 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001012 return ret;
1013}
1014
1015int start_output_stream(struct stream_out *out)
1016{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001017 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001018 struct audio_usecase *uc_info;
1019 struct audio_device *adev = out->dev;
1020
Eric Laurent994a6932013-07-17 11:51:42 -07001021 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001022 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001023 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001024 if (out->pcm_device_id < 0) {
1025 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1026 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001027 ret = -EINVAL;
1028 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001029 }
1030
1031 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1032 uc_info->id = out->usecase;
1033 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001034 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001035 uc_info->devices = out->devices;
1036 uc_info->in_snd_device = SND_DEVICE_NONE;
1037 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001038
Eric Laurent07eeafd2013-10-06 12:52:49 -07001039 /* This must be called before adding this usecase to the list */
1040 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1041 check_and_set_hdmi_channels(adev, out->config.channels);
1042
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001043 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001044
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001045 select_devices(adev, out->usecase);
1046
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001047 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1048 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001049 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1050 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001051 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001052 if (out->pcm && !pcm_is_ready(out->pcm)) {
1053 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1054 pcm_close(out->pcm);
1055 out->pcm = NULL;
1056 ret = -EIO;
1057 goto error_open;
1058 }
1059 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001060 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001061 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1062 COMPRESS_IN, &out->compr_config);
1063 if (out->compr && !is_compress_ready(out->compr)) {
1064 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1065 compress_close(out->compr);
1066 out->compr = NULL;
1067 ret = -EIO;
1068 goto error_open;
1069 }
1070 if (out->offload_callback)
1071 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001072
1073 if (adev->visualizer_start_output != NULL)
1074 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001075 }
Eric Laurent994a6932013-07-17 11:51:42 -07001076 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001077 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001078error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001079 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001080error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001081 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001082}
1083
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001084static int check_input_parameters(uint32_t sample_rate,
1085 audio_format_t format,
1086 int channel_count)
1087{
1088 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1089
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001090 if ((channel_count < 1) || (channel_count > 6)) return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001091
1092 switch (sample_rate) {
1093 case 8000:
1094 case 11025:
1095 case 12000:
1096 case 16000:
1097 case 22050:
1098 case 24000:
1099 case 32000:
1100 case 44100:
1101 case 48000:
1102 break;
1103 default:
1104 return -EINVAL;
1105 }
1106
1107 return 0;
1108}
1109
1110static size_t get_input_buffer_size(uint32_t sample_rate,
1111 audio_format_t format,
1112 int channel_count)
1113{
1114 size_t size = 0;
1115
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001116 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1117 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001118
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001119 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1120 /* ToDo: should use frame_size computed based on the format and
1121 channel_count here. */
1122 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001123
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001124 /* make sure the size is multiple of 64 */
1125 size += 0x3f;
1126 size &= ~0x3f;
1127
1128 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001129}
1130
1131static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1132{
1133 struct stream_out *out = (struct stream_out *)stream;
1134
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001135 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001136}
1137
1138static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1139{
1140 return -ENOSYS;
1141}
1142
1143static size_t out_get_buffer_size(const struct audio_stream *stream)
1144{
1145 struct stream_out *out = (struct stream_out *)stream;
1146
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001147 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1148 return out->compr_config.fragment_size;
1149 }
1150
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001151 return out->config.period_size * audio_stream_frame_size(stream);
1152}
1153
1154static uint32_t out_get_channels(const struct audio_stream *stream)
1155{
1156 struct stream_out *out = (struct stream_out *)stream;
1157
1158 return out->channel_mask;
1159}
1160
1161static audio_format_t out_get_format(const struct audio_stream *stream)
1162{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001163 struct stream_out *out = (struct stream_out *)stream;
1164
1165 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001166}
1167
1168static int out_set_format(struct audio_stream *stream, audio_format_t format)
1169{
1170 return -ENOSYS;
1171}
1172
1173static int out_standby(struct audio_stream *stream)
1174{
1175 struct stream_out *out = (struct stream_out *)stream;
1176 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001177
Eric Laurent994a6932013-07-17 11:51:42 -07001178 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001179 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001180
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001181 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001182 if (!out->standby) {
1183 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001184 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1185 if (out->pcm) {
1186 pcm_close(out->pcm);
1187 out->pcm = NULL;
1188 }
1189 } else {
1190 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001191 out->gapless_mdata.encoder_delay = 0;
1192 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001193 if (out->compr != NULL) {
1194 compress_close(out->compr);
1195 out->compr = NULL;
1196 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001197 }
1198 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001199 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001200 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001201 }
1202 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001203 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001204 return 0;
1205}
1206
1207static int out_dump(const struct audio_stream *stream, int fd)
1208{
1209 return 0;
1210}
1211
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001212static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1213{
1214 int ret = 0;
1215 char value[32];
1216 struct compr_gapless_mdata tmp_mdata;
1217
1218 if (!out || !parms) {
1219 return -EINVAL;
1220 }
1221
1222 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1223 if (ret >= 0) {
1224 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1225 } else {
1226 return -EINVAL;
1227 }
1228
1229 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1230 if (ret >= 0) {
1231 tmp_mdata.encoder_padding = atoi(value);
1232 } else {
1233 return -EINVAL;
1234 }
1235
1236 out->gapless_mdata = tmp_mdata;
1237 out->send_new_metadata = 1;
1238 ALOGV("%s new encoder delay %u and padding %u", __func__,
1239 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1240
1241 return 0;
1242}
1243
1244
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001245static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1246{
1247 struct stream_out *out = (struct stream_out *)stream;
1248 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001249 struct audio_usecase *usecase;
1250 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001251 struct str_parms *parms;
1252 char value[32];
1253 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001254 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001255
sangwoobc677242013-08-08 16:53:43 +09001256 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001257 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001258 parms = str_parms_create_str(kvpairs);
1259 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1260 if (ret >= 0) {
1261 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001262 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001263 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001264
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001265 /*
1266 * When HDMI cable is unplugged the music playback is paused and
1267 * the policy manager sends routing=0. But the audioflinger
1268 * continues to write data until standby time (3sec).
1269 * As the HDMI core is turned off, the write gets blocked.
1270 * Avoid this by routing audio to speaker until standby.
1271 */
1272 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1273 val == AUDIO_DEVICE_NONE) {
1274 val = AUDIO_DEVICE_OUT_SPEAKER;
1275 }
1276
1277 /*
1278 * select_devices() call below switches all the usecases on the same
1279 * backend to the new device. Refer to check_usecases_codec_backend() in
1280 * the select_devices(). But how do we undo this?
1281 *
1282 * For example, music playback is active on headset (deep-buffer usecase)
1283 * and if we go to ringtones and select a ringtone, low-latency usecase
1284 * will be started on headset+speaker. As we can't enable headset+speaker
1285 * and headset devices at the same time, select_devices() switches the music
1286 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1287 * So when the ringtone playback is completed, how do we undo the same?
1288 *
1289 * We are relying on the out_set_parameters() call on deep-buffer output,
1290 * once the ringtone playback is ended.
1291 * NOTE: We should not check if the current devices are same as new devices.
1292 * Because select_devices() must be called to switch back the music
1293 * playback to headset.
1294 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001295 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001296 out->devices = val;
1297
1298 if (!out->standby)
1299 select_devices(adev, out->usecase);
1300
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001301 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1302 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001303 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001304 voice_start_call(adev);
1305 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1306 voice_is_in_call(adev) &&
1307 (out == adev->primary_output)) {
1308 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001309 }
1310 }
1311
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001312 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1313 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001314 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001315 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001316 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001317
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001318 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001319 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001320 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001321
1322 if (out == adev->primary_output) {
1323 pthread_mutex_lock(&adev->lock);
1324 audio_extn_set_parameters(adev, parms);
1325 pthread_mutex_unlock(&adev->lock);
1326 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001327 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1328 parse_compress_metadata(out, parms);
1329 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001330
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001331 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001332 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001333 return ret;
1334}
1335
1336static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1337{
1338 struct stream_out *out = (struct stream_out *)stream;
1339 struct str_parms *query = str_parms_create_str(keys);
1340 char *str;
1341 char value[256];
1342 struct str_parms *reply = str_parms_create();
1343 size_t i, j;
1344 int ret;
1345 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001346 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001347 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1348 if (ret >= 0) {
1349 value[0] = '\0';
1350 i = 0;
1351 while (out->supported_channel_masks[i] != 0) {
1352 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1353 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1354 if (!first) {
1355 strcat(value, "|");
1356 }
1357 strcat(value, out_channels_name_to_enum_table[j].name);
1358 first = false;
1359 break;
1360 }
1361 }
1362 i++;
1363 }
1364 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1365 str = str_parms_to_str(reply);
1366 } else {
1367 str = strdup(keys);
1368 }
1369 str_parms_destroy(query);
1370 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001371 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001372 return str;
1373}
1374
1375static uint32_t out_get_latency(const struct audio_stream_out *stream)
1376{
1377 struct stream_out *out = (struct stream_out *)stream;
1378
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001379 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1380 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1381
1382 return (out->config.period_count * out->config.period_size * 1000) /
1383 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001384}
1385
1386static int out_set_volume(struct audio_stream_out *stream, float left,
1387 float right)
1388{
Eric Laurenta9024de2013-04-04 09:19:12 -07001389 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001390 int volume[2];
1391
Eric Laurenta9024de2013-04-04 09:19:12 -07001392 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1393 /* only take left channel into account: the API is for stereo anyway */
1394 out->muted = (left == 0.0f);
1395 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001396 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1397 const char *mixer_ctl_name = "Compress Playback Volume";
1398 struct audio_device *adev = out->dev;
1399 struct mixer_ctl *ctl;
1400
1401 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1402 if (!ctl) {
1403 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1404 __func__, mixer_ctl_name);
1405 return -EINVAL;
1406 }
1407 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1408 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1409 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1410 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001411 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001412
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001413 return -ENOSYS;
1414}
1415
1416static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1417 size_t bytes)
1418{
1419 struct stream_out *out = (struct stream_out *)stream;
1420 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001421 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001422
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001423 pthread_mutex_lock(&out->lock);
1424 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001425 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001426 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001427 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001428 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001429 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001430 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001431 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001432 goto exit;
1433 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001434 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001435
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001436 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001437 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1438 if (out->send_new_metadata) {
1439 ALOGVV("send new gapless metadata");
1440 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1441 out->send_new_metadata = 0;
1442 }
1443
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001444 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001445 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001446 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001447 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1448 }
1449 if (!out->playback_started) {
1450 compress_start(out->compr);
1451 out->playback_started = 1;
1452 out->offload_state = OFFLOAD_STATE_PLAYING;
1453 }
1454 pthread_mutex_unlock(&out->lock);
1455 return ret;
1456 } else {
1457 if (out->pcm) {
1458 if (out->muted)
1459 memset((void *)buffer, 0, bytes);
1460 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1461 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001462 if (ret == 0)
1463 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001464 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001465 }
1466
1467exit:
1468 pthread_mutex_unlock(&out->lock);
1469
1470 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001471 if (out->pcm)
1472 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001473 out_standby(&out->stream.common);
1474 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1475 out_get_sample_rate(&out->stream.common));
1476 }
1477 return bytes;
1478}
1479
1480static int out_get_render_position(const struct audio_stream_out *stream,
1481 uint32_t *dsp_frames)
1482{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001483 struct stream_out *out = (struct stream_out *)stream;
1484 *dsp_frames = 0;
1485 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1486 pthread_mutex_lock(&out->lock);
1487 if (out->compr != NULL) {
1488 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1489 &out->sample_rate);
1490 ALOGVV("%s rendered frames %d sample_rate %d",
1491 __func__, *dsp_frames, out->sample_rate);
1492 }
1493 pthread_mutex_unlock(&out->lock);
1494 return 0;
1495 } else
1496 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001497}
1498
1499static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1500{
1501 return 0;
1502}
1503
1504static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1505{
1506 return 0;
1507}
1508
1509static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1510 int64_t *timestamp)
1511{
1512 return -EINVAL;
1513}
1514
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001515static int out_get_presentation_position(const struct audio_stream_out *stream,
1516 uint64_t *frames, struct timespec *timestamp)
1517{
1518 struct stream_out *out = (struct stream_out *)stream;
1519 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001520 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001521
1522 pthread_mutex_lock(&out->lock);
1523
Eric Laurent949a0892013-09-20 09:20:13 -07001524 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1525 if (out->compr != NULL) {
1526 compress_get_tstamp(out->compr, &dsp_frames,
1527 &out->sample_rate);
1528 ALOGVV("%s rendered frames %ld sample_rate %d",
1529 __func__, dsp_frames, out->sample_rate);
1530 *frames = dsp_frames;
1531 ret = 0;
1532 /* this is the best we can do */
1533 clock_gettime(CLOCK_MONOTONIC, timestamp);
1534 }
1535 } else {
1536 if (out->pcm) {
1537 size_t avail;
1538 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1539 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001540 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001541 // This adjustment accounts for buffering after app processor.
1542 // It is based on estimated DSP latency per use case, rather than exact.
1543 signed_frames -=
1544 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1545
Eric Laurent949a0892013-09-20 09:20:13 -07001546 // It would be unusual for this value to be negative, but check just in case ...
1547 if (signed_frames >= 0) {
1548 *frames = signed_frames;
1549 ret = 0;
1550 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001551 }
1552 }
1553 }
1554
1555 pthread_mutex_unlock(&out->lock);
1556
1557 return ret;
1558}
1559
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001560static int out_set_callback(struct audio_stream_out *stream,
1561 stream_callback_t callback, void *cookie)
1562{
1563 struct stream_out *out = (struct stream_out *)stream;
1564
1565 ALOGV("%s", __func__);
1566 pthread_mutex_lock(&out->lock);
1567 out->offload_callback = callback;
1568 out->offload_cookie = cookie;
1569 pthread_mutex_unlock(&out->lock);
1570 return 0;
1571}
1572
1573static int out_pause(struct audio_stream_out* stream)
1574{
1575 struct stream_out *out = (struct stream_out *)stream;
1576 int status = -ENOSYS;
1577 ALOGV("%s", __func__);
1578 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1579 pthread_mutex_lock(&out->lock);
1580 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1581 status = compress_pause(out->compr);
1582 out->offload_state = OFFLOAD_STATE_PAUSED;
1583 }
1584 pthread_mutex_unlock(&out->lock);
1585 }
1586 return status;
1587}
1588
1589static int out_resume(struct audio_stream_out* stream)
1590{
1591 struct stream_out *out = (struct stream_out *)stream;
1592 int status = -ENOSYS;
1593 ALOGV("%s", __func__);
1594 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1595 status = 0;
1596 pthread_mutex_lock(&out->lock);
1597 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1598 status = compress_resume(out->compr);
1599 out->offload_state = OFFLOAD_STATE_PLAYING;
1600 }
1601 pthread_mutex_unlock(&out->lock);
1602 }
1603 return status;
1604}
1605
1606static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1607{
1608 struct stream_out *out = (struct stream_out *)stream;
1609 int status = -ENOSYS;
1610 ALOGV("%s", __func__);
1611 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1612 pthread_mutex_lock(&out->lock);
1613 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1614 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1615 else
1616 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1617 pthread_mutex_unlock(&out->lock);
1618 }
1619 return status;
1620}
1621
1622static int out_flush(struct audio_stream_out* stream)
1623{
1624 struct stream_out *out = (struct stream_out *)stream;
1625 ALOGV("%s", __func__);
1626 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1627 pthread_mutex_lock(&out->lock);
1628 stop_compressed_output_l(out);
1629 pthread_mutex_unlock(&out->lock);
1630 return 0;
1631 }
1632 return -ENOSYS;
1633}
1634
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001635/** audio_stream_in implementation **/
1636static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1637{
1638 struct stream_in *in = (struct stream_in *)stream;
1639
1640 return in->config.rate;
1641}
1642
1643static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1644{
1645 return -ENOSYS;
1646}
1647
1648static size_t in_get_buffer_size(const struct audio_stream *stream)
1649{
1650 struct stream_in *in = (struct stream_in *)stream;
1651
1652 return in->config.period_size * audio_stream_frame_size(stream);
1653}
1654
1655static uint32_t in_get_channels(const struct audio_stream *stream)
1656{
1657 struct stream_in *in = (struct stream_in *)stream;
1658
1659 return in->channel_mask;
1660}
1661
1662static audio_format_t in_get_format(const struct audio_stream *stream)
1663{
1664 return AUDIO_FORMAT_PCM_16_BIT;
1665}
1666
1667static int in_set_format(struct audio_stream *stream, audio_format_t format)
1668{
1669 return -ENOSYS;
1670}
1671
1672static int in_standby(struct audio_stream *stream)
1673{
1674 struct stream_in *in = (struct stream_in *)stream;
1675 struct audio_device *adev = in->dev;
1676 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001677 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001678 pthread_mutex_lock(&in->lock);
1679 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001680 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001681 if (in->pcm) {
1682 pcm_close(in->pcm);
1683 in->pcm = NULL;
1684 }
1685 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001686 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001687 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001688 }
1689 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001690 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001691 return status;
1692}
1693
1694static int in_dump(const struct audio_stream *stream, int fd)
1695{
1696 return 0;
1697}
1698
1699static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1700{
1701 struct stream_in *in = (struct stream_in *)stream;
1702 struct audio_device *adev = in->dev;
1703 struct str_parms *parms;
1704 char *str;
1705 char value[32];
1706 int ret, val = 0;
1707
Eric Laurent994a6932013-07-17 11:51:42 -07001708 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001709 parms = str_parms_create_str(kvpairs);
1710
1711 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1712
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001713 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001714 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001715 if (ret >= 0) {
1716 val = atoi(value);
1717 /* no audio source uses val == 0 */
1718 if ((in->source != val) && (val != 0)) {
1719 in->source = val;
1720 }
1721 }
1722
1723 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1724 if (ret >= 0) {
1725 val = atoi(value);
1726 if ((in->device != val) && (val != 0)) {
1727 in->device = val;
1728 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001729 if (!in->standby)
1730 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001731 }
1732 }
1733
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001734 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001735 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001736
1737 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001738 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001739 return ret;
1740}
1741
1742static char* in_get_parameters(const struct audio_stream *stream,
1743 const char *keys)
1744{
1745 return strdup("");
1746}
1747
1748static int in_set_gain(struct audio_stream_in *stream, float gain)
1749{
1750 return 0;
1751}
1752
1753static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1754 size_t bytes)
1755{
1756 struct stream_in *in = (struct stream_in *)stream;
1757 struct audio_device *adev = in->dev;
1758 int i, ret = -1;
1759
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001760 pthread_mutex_lock(&in->lock);
1761 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001762 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001763 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001764 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001765 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001766 goto exit;
1767 }
1768 in->standby = 0;
1769 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001770
1771 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001772 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1773 ret = audio_extn_ssr_read(stream, buffer, bytes);
1774 else
1775 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001776 }
1777
1778 /*
1779 * Instead of writing zeroes here, we could trust the hardware
1780 * to always provide zeroes when muted.
1781 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001782 if (ret == 0 && voice_get_mic_mute(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001783 memset(buffer, 0, bytes);
1784
1785exit:
1786 pthread_mutex_unlock(&in->lock);
1787
1788 if (ret != 0) {
1789 in_standby(&in->stream.common);
1790 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1791 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1792 in_get_sample_rate(&in->stream.common));
1793 }
1794 return bytes;
1795}
1796
1797static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1798{
1799 return 0;
1800}
1801
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001802static int add_remove_audio_effect(const struct audio_stream *stream,
1803 effect_handle_t effect,
1804 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001805{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001806 struct stream_in *in = (struct stream_in *)stream;
1807 int status = 0;
1808 effect_descriptor_t desc;
1809
1810 status = (*effect)->get_descriptor(effect, &desc);
1811 if (status != 0)
1812 return status;
1813
1814 pthread_mutex_lock(&in->lock);
1815 pthread_mutex_lock(&in->dev->lock);
1816 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1817 in->enable_aec != enable &&
1818 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1819 in->enable_aec = enable;
1820 if (!in->standby)
1821 select_devices(in->dev, in->usecase);
1822 }
1823 pthread_mutex_unlock(&in->dev->lock);
1824 pthread_mutex_unlock(&in->lock);
1825
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001826 return 0;
1827}
1828
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001829static int in_add_audio_effect(const struct audio_stream *stream,
1830 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001831{
Eric Laurent994a6932013-07-17 11:51:42 -07001832 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001833 return add_remove_audio_effect(stream, effect, true);
1834}
1835
1836static int in_remove_audio_effect(const struct audio_stream *stream,
1837 effect_handle_t effect)
1838{
Eric Laurent994a6932013-07-17 11:51:42 -07001839 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001840 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001841}
1842
1843static int adev_open_output_stream(struct audio_hw_device *dev,
1844 audio_io_handle_t handle,
1845 audio_devices_t devices,
1846 audio_output_flags_t flags,
1847 struct audio_config *config,
1848 struct audio_stream_out **stream_out)
1849{
1850 struct audio_device *adev = (struct audio_device *)dev;
1851 struct stream_out *out;
1852 int i, ret;
1853
Eric Laurent994a6932013-07-17 11:51:42 -07001854 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001855 __func__, config->sample_rate, config->channel_mask, devices, flags);
1856 *stream_out = NULL;
1857 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1858
1859 if (devices == AUDIO_DEVICE_NONE)
1860 devices = AUDIO_DEVICE_OUT_SPEAKER;
1861
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001862 out->flags = flags;
1863 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001864 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001865 out->format = config->format;
1866 out->sample_rate = config->sample_rate;
1867 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1868 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001869 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001870
1871 /* Init use case and pcm_config */
1872 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1873 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001874 pthread_mutex_lock(&adev->lock);
1875 ret = read_hdmi_channel_masks(out);
1876 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001877 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001878 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001879
1880 if (config->sample_rate == 0)
1881 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1882 if (config->channel_mask == 0)
1883 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1884
1885 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001886 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001887 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1888 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001889 out->config.rate = config->sample_rate;
1890 out->config.channels = popcount(out->channel_mask);
1891 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001892 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1893 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1894 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001895 out->sample_rate = out->config.rate;
1896 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1897 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1898 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1899 ALOGE("%s: Unsupported Offload information", __func__);
1900 ret = -EINVAL;
1901 goto error_open;
1902 }
1903 if (!is_supported_format(config->offload_info.format)) {
1904 ALOGE("%s: Unsupported audio format", __func__);
1905 ret = -EINVAL;
1906 goto error_open;
1907 }
1908
1909 out->compr_config.codec = (struct snd_codec *)
1910 calloc(1, sizeof(struct snd_codec));
1911
1912 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1913 if (config->offload_info.channel_mask)
1914 out->channel_mask = config->offload_info.channel_mask;
1915 else if (config->channel_mask)
1916 out->channel_mask = config->channel_mask;
1917 out->format = config->offload_info.format;
1918 out->sample_rate = config->offload_info.sample_rate;
1919
1920 out->stream.set_callback = out_set_callback;
1921 out->stream.pause = out_pause;
1922 out->stream.resume = out_resume;
1923 out->stream.drain = out_drain;
1924 out->stream.flush = out_flush;
1925
1926 out->compr_config.codec->id =
1927 get_snd_codec_id(config->offload_info.format);
1928 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1929 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1930 out->compr_config.codec->sample_rate =
1931 compress_get_alsa_rate(config->offload_info.sample_rate);
1932 out->compr_config.codec->bit_rate =
1933 config->offload_info.bit_rate;
1934 out->compr_config.codec->ch_in =
1935 popcount(config->channel_mask);
1936 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1937
1938 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1939 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001940
1941 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001942 create_offload_callback_thread(out);
1943 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1944 __func__, config->offload_info.version,
1945 config->offload_info.bit_rate);
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07001946 } else if (out->flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) {
1947 ret = voice_check_and_set_incall_music_usecase(adev, out);
1948 if (ret != 0) {
1949 ALOGE("%s: Incall music delivery usecase cannot be set error:%d",
1950 __func__, ret);
1951 goto error_open;
1952 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001953 } else {
1954 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1955 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001956 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001957 }
1958
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001959 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1960 if(adev->primary_output == NULL)
1961 adev->primary_output = out;
1962 else {
1963 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001964 ret = -EEXIST;
1965 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001966 }
1967 }
1968
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001969 /* Check if this usecase is already existing */
1970 pthread_mutex_lock(&adev->lock);
1971 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1972 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001973 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001974 ret = -EEXIST;
1975 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001976 }
1977 pthread_mutex_unlock(&adev->lock);
1978
1979 out->stream.common.get_sample_rate = out_get_sample_rate;
1980 out->stream.common.set_sample_rate = out_set_sample_rate;
1981 out->stream.common.get_buffer_size = out_get_buffer_size;
1982 out->stream.common.get_channels = out_get_channels;
1983 out->stream.common.get_format = out_get_format;
1984 out->stream.common.set_format = out_set_format;
1985 out->stream.common.standby = out_standby;
1986 out->stream.common.dump = out_dump;
1987 out->stream.common.set_parameters = out_set_parameters;
1988 out->stream.common.get_parameters = out_get_parameters;
1989 out->stream.common.add_audio_effect = out_add_audio_effect;
1990 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1991 out->stream.get_latency = out_get_latency;
1992 out->stream.set_volume = out_set_volume;
1993 out->stream.write = out_write;
1994 out->stream.get_render_position = out_get_render_position;
1995 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001996 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001997
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001998 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001999 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07002000 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002001
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002002 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
2003 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002004
2005 config->format = out->stream.common.get_format(&out->stream.common);
2006 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
2007 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
2008
2009 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002010 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002011 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07002012
2013error_open:
2014 free(out);
2015 *stream_out = NULL;
2016 ALOGD("%s: exit: ret %d", __func__, ret);
2017 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002018}
2019
2020static void adev_close_output_stream(struct audio_hw_device *dev,
2021 struct audio_stream_out *stream)
2022{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002023 struct stream_out *out = (struct stream_out *)stream;
2024 struct audio_device *adev = out->dev;
2025
Eric Laurent994a6932013-07-17 11:51:42 -07002026 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002027 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002028 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2029 destroy_offload_callback_thread(out);
2030
2031 if (out->compr_config.codec != NULL)
2032 free(out->compr_config.codec);
2033 }
2034 pthread_cond_destroy(&out->cond);
2035 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002036 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002037 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002038}
2039
2040static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2041{
2042 struct audio_device *adev = (struct audio_device *)dev;
2043 struct str_parms *parms;
2044 char *str;
2045 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002046 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002047 int ret;
2048
Eric Laurent994a6932013-07-17 11:51:42 -07002049 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002050 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002051
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002052 voice_set_parameters(adev, parms);
2053 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002054
2055 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2056 if (ret >= 0) {
2057 /* When set to false, HAL should disable EC and NS
2058 * But it is currently not supported.
2059 */
2060 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2061 adev->bluetooth_nrec = true;
2062 else
2063 adev->bluetooth_nrec = false;
2064 }
2065
2066 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2067 if (ret >= 0) {
2068 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2069 adev->screen_off = false;
2070 else
2071 adev->screen_off = true;
2072 }
2073
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002074 ret = str_parms_get_int(parms, "rotation", &val);
2075 if (ret >= 0) {
2076 bool reverse_speakers = false;
2077 switch(val) {
2078 // FIXME: note that the code below assumes that the speakers are in the correct placement
2079 // relative to the user when the device is rotated 90deg from its default rotation. This
2080 // assumption is device-specific, not platform-specific like this code.
2081 case 270:
2082 reverse_speakers = true;
2083 break;
2084 case 0:
2085 case 90:
2086 case 180:
2087 break;
2088 default:
2089 ALOGE("%s: unexpected rotation of %d", __func__, val);
2090 }
2091 pthread_mutex_lock(&adev->lock);
2092 if (adev->speaker_lr_swap != reverse_speakers) {
2093 adev->speaker_lr_swap = reverse_speakers;
2094 // only update the selected device if there is active pcm playback
2095 struct audio_usecase *usecase;
2096 struct listnode *node;
2097 list_for_each(node, &adev->usecase_list) {
2098 usecase = node_to_item(node, struct audio_usecase, list);
2099 if (usecase->type == PCM_PLAYBACK) {
2100 select_devices(adev, usecase->id);
2101 break;
2102 }
2103 }
2104 }
2105 pthread_mutex_unlock(&adev->lock);
2106 }
2107
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002108 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002109 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002110 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002111 return ret;
2112}
2113
2114static char* adev_get_parameters(const struct audio_hw_device *dev,
2115 const char *keys)
2116{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002117 struct audio_device *adev = (struct audio_device *)dev;
2118 struct str_parms *reply = str_parms_create();
2119 struct str_parms *query = str_parms_create_str(keys);
2120 char *str;
2121
2122 audio_extn_get_parameters(adev, query, reply);
2123 platform_get_parameters(adev->platform, query, reply);
2124 str = str_parms_to_str(reply);
2125 str_parms_destroy(query);
2126 str_parms_destroy(reply);
2127
2128 ALOGV("%s: exit: returns - %s", __func__, str);
2129 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002130}
2131
2132static int adev_init_check(const struct audio_hw_device *dev)
2133{
2134 return 0;
2135}
2136
2137static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2138{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002139 int ret;
2140 struct audio_device *adev = (struct audio_device *)dev;
2141 pthread_mutex_lock(&adev->lock);
2142 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002143 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002144 pthread_mutex_unlock(&adev->lock);
2145 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002146}
2147
2148static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2149{
2150 return -ENOSYS;
2151}
2152
2153static int adev_get_master_volume(struct audio_hw_device *dev,
2154 float *volume)
2155{
2156 return -ENOSYS;
2157}
2158
2159static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2160{
2161 return -ENOSYS;
2162}
2163
2164static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2165{
2166 return -ENOSYS;
2167}
2168
2169static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2170{
2171 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002172 pthread_mutex_lock(&adev->lock);
2173 if (adev->mode != mode) {
Shiv Maliyappanahallif3b9a422013-10-22 16:38:08 -07002174 ALOGD("%s mode %d\n", __func__, mode);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002175 adev->mode = mode;
2176 }
2177 pthread_mutex_unlock(&adev->lock);
2178 return 0;
2179}
2180
2181static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2182{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002183 return voice_set_mic_mute((struct audio_device *)dev, state);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002184}
2185
2186static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2187{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002188 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002189 return 0;
2190}
2191
2192static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2193 const struct audio_config *config)
2194{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002195 int channel_count = popcount(config->channel_mask);
2196
2197 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2198}
2199
2200static int adev_open_input_stream(struct audio_hw_device *dev,
2201 audio_io_handle_t handle,
2202 audio_devices_t devices,
2203 struct audio_config *config,
2204 struct audio_stream_in **stream_in)
2205{
2206 struct audio_device *adev = (struct audio_device *)dev;
2207 struct stream_in *in;
2208 int ret, buffer_size, frame_size;
2209 int channel_count = popcount(config->channel_mask);
2210
Eric Laurent994a6932013-07-17 11:51:42 -07002211 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002212 *stream_in = NULL;
2213 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2214 return -EINVAL;
2215
2216 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2217
2218 in->stream.common.get_sample_rate = in_get_sample_rate;
2219 in->stream.common.set_sample_rate = in_set_sample_rate;
2220 in->stream.common.get_buffer_size = in_get_buffer_size;
2221 in->stream.common.get_channels = in_get_channels;
2222 in->stream.common.get_format = in_get_format;
2223 in->stream.common.set_format = in_set_format;
2224 in->stream.common.standby = in_standby;
2225 in->stream.common.dump = in_dump;
2226 in->stream.common.set_parameters = in_set_parameters;
2227 in->stream.common.get_parameters = in_get_parameters;
2228 in->stream.common.add_audio_effect = in_add_audio_effect;
2229 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2230 in->stream.set_gain = in_set_gain;
2231 in->stream.read = in_read;
2232 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2233
2234 in->device = devices;
2235 in->source = AUDIO_SOURCE_DEFAULT;
2236 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002237 in->standby = 1;
2238 in->channel_mask = config->channel_mask;
2239
2240 /* Update config params with the requested sample rate and channels */
2241 in->usecase = USECASE_AUDIO_RECORD;
2242 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002243 in->config.rate = config->sample_rate;
2244
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002245 if (audio_extn_ssr_get_enabled()&& channel_count == 6) {
2246 if(audio_extn_ssr_init(adev, in))
2247 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2248 } else {
2249 in->config.channels = channel_count;
2250 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2251 buffer_size = get_input_buffer_size(config->sample_rate,
2252 config->format,
2253 channel_count);
2254 in->config.period_size = buffer_size / frame_size;
2255 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002256
2257 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002258 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002259 return 0;
2260
2261err_open:
2262 free(in);
2263 *stream_in = NULL;
2264 return ret;
2265}
2266
2267static void adev_close_input_stream(struct audio_hw_device *dev,
2268 struct audio_stream_in *stream)
2269{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002270 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002271 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002272
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002273 in_standby(&stream->common);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002274 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2275 audio_extn_ssr_deinit();
2276 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002277 free(stream);
2278
2279 return;
2280}
2281
2282static int adev_dump(const audio_hw_device_t *device, int fd)
2283{
2284 return 0;
2285}
2286
2287static int adev_close(hw_device_t *device)
2288{
2289 struct audio_device *adev = (struct audio_device *)device;
2290 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002291 free(adev->snd_dev_ref_cnt);
2292 platform_deinit(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002293 free(device);
2294 return 0;
2295}
2296
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002297static int adev_open(const hw_module_t *module, const char *name,
2298 hw_device_t **device)
2299{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002300 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002301
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002302 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002303 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2304
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002305 pthread_mutex_lock(&adev_init_lock);
2306 if (is_adev_initialised == true){
2307 *device = &adev->device.common;
2308 ALOGD("%s: returning existing instance of adev", __func__);
2309 ALOGD("%s: exit", __func__);
2310 pthread_mutex_unlock(&adev_init_lock);
2311 return 0;
2312 }
2313
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002314 adev = calloc(1, sizeof(struct audio_device));
2315
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002316 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2317 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2318 adev->device.common.module = (struct hw_module_t *)module;
2319 adev->device.common.close = adev_close;
2320
2321 adev->device.init_check = adev_init_check;
2322 adev->device.set_voice_volume = adev_set_voice_volume;
2323 adev->device.set_master_volume = adev_set_master_volume;
2324 adev->device.get_master_volume = adev_get_master_volume;
2325 adev->device.set_master_mute = adev_set_master_mute;
2326 adev->device.get_master_mute = adev_get_master_mute;
2327 adev->device.set_mode = adev_set_mode;
2328 adev->device.set_mic_mute = adev_set_mic_mute;
2329 adev->device.get_mic_mute = adev_get_mic_mute;
2330 adev->device.set_parameters = adev_set_parameters;
2331 adev->device.get_parameters = adev_get_parameters;
2332 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2333 adev->device.open_output_stream = adev_open_output_stream;
2334 adev->device.close_output_stream = adev_close_output_stream;
2335 adev->device.open_input_stream = adev_open_input_stream;
2336 adev->device.close_input_stream = adev_close_input_stream;
2337 adev->device.dump = adev_dump;
2338
2339 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002340 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002341 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002342 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002343 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002344 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002345 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002346 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002347 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002348 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002349 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002350
2351 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002352 adev->platform = platform_init(adev);
2353 if (!adev->platform) {
2354 free(adev->snd_dev_ref_cnt);
2355 free(adev);
2356 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2357 *device = NULL;
2358 return -EINVAL;
2359 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002360
2361 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2362 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2363 if (adev->visualizer_lib == NULL) {
2364 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2365 } else {
2366 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2367 adev->visualizer_start_output =
2368 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2369 "visualizer_hal_start_output");
2370 adev->visualizer_stop_output =
2371 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2372 "visualizer_hal_stop_output");
2373 }
2374 }
2375
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002376 *device = &adev->device.common;
2377
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002378 /* update init flag*/
2379 is_adev_initialised = true;
2380 pthread_mutex_unlock(&adev_init_lock);
2381
Eric Laurent994a6932013-07-17 11:51:42 -07002382 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002383 return 0;
2384}
2385
2386static struct hw_module_methods_t hal_module_methods = {
2387 .open = adev_open,
2388};
2389
2390struct audio_module HAL_MODULE_INFO_SYM = {
2391 .common = {
2392 .tag = HARDWARE_MODULE_TAG,
2393 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2394 .hal_api_version = HARDWARE_HAL_API_VERSION,
2395 .id = AUDIO_HARDWARE_MODULE_ID,
2396 .name = "QCOM Audio HAL",
2397 .author = "Code Aurora Forum",
2398 .methods = &hal_module_methods,
2399 },
2400};