blob: 1bcb25acaf3d8c02394c5b19de6a320cf83d26ee [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",
Eric Laurentb23d5282013-05-14 15:27:20 -0700117};
118
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800119
120#define STRING_TO_ENUM(string) { #string, string }
121
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800122struct string_to_enum {
123 const char *name;
124 uint32_t value;
125};
126
127static const struct string_to_enum out_channels_name_to_enum_table[] = {
128 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
129 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
130 STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
131};
132
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700133static struct audio_device *adev = NULL;
134static pthread_mutex_t adev_init_lock;
135static bool is_adev_initialised = false;
Haynes Mathew George5191a852013-09-11 14:19:36 -0700136static int set_voice_volume_l(struct audio_device *adev, float volume);
137
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700138static bool is_supported_format(audio_format_t format)
139{
Eric Laurent86e17132013-09-12 17:49:30 -0700140 if (format == AUDIO_FORMAT_MP3 ||
141 format == AUDIO_FORMAT_AAC)
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700142 return true;
143
144 return false;
145}
146
147static int get_snd_codec_id(audio_format_t format)
148{
149 int id = 0;
150
151 switch (format) {
152 case AUDIO_FORMAT_MP3:
153 id = SND_AUDIOCODEC_MP3;
154 break;
155 case AUDIO_FORMAT_AAC:
156 id = SND_AUDIOCODEC_AAC;
157 break;
158 default:
159 ALOGE("%s: Unsupported audio format", __func__);
160 }
161
162 return id;
163}
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -0800164
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700165static int enable_audio_route(struct audio_device *adev,
166 struct audio_usecase *usecase,
167 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800168{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700169 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700170 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800171
172 if (usecase == NULL)
173 return -EINVAL;
174
175 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
176
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800177 if (usecase->type == PCM_CAPTURE)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700178 snd_device = usecase->in_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800179 else
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700180 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800181
182 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700183 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700184 ALOGV("%s: apply mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700185 audio_route_apply_path(adev->audio_route, mixer_path);
186 if (update_mixer)
187 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800188
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800189 ALOGV("%s: exit", __func__);
190 return 0;
191}
192
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700193int disable_audio_route(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700194 struct audio_usecase *usecase,
195 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800196{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700197 snd_device_t snd_device;
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700198 char mixer_path[MIXER_PATH_MAX_LENGTH];
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800199
200 if (usecase == NULL)
201 return -EINVAL;
202
203 ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700204 if (usecase->type == PCM_CAPTURE)
205 snd_device = usecase->in_snd_device;
206 else
207 snd_device = usecase->out_snd_device;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800208 strcpy(mixer_path, use_case_table[usecase->id]);
Eric Laurentb23d5282013-05-14 15:27:20 -0700209 platform_add_backend_name(mixer_path, snd_device);
Eric Laurent994a6932013-07-17 11:51:42 -0700210 ALOGV("%s: reset mixer path: %s", __func__, mixer_path);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700211 audio_route_reset_path(adev->audio_route, mixer_path);
212 if (update_mixer)
213 audio_route_update_mixer(adev->audio_route);
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800214
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800215 ALOGV("%s: exit", __func__);
216 return 0;
217}
218
219static int enable_snd_device(struct audio_device *adev,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700220 snd_device_t snd_device,
221 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800222{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800223 if (snd_device < SND_DEVICE_MIN ||
224 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800225 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800226 return -EINVAL;
227 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700228
229 adev->snd_dev_ref_cnt[snd_device]++;
230 if (adev->snd_dev_ref_cnt[snd_device] > 1) {
Eric Laurent994a6932013-07-17 11:51:42 -0700231 ALOGV("%s: snd_device(%d: %s) is already active",
Eric Laurentb23d5282013-05-14 15:27:20 -0700232 __func__, snd_device, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700233 return 0;
234 }
235
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700236 /* start usb playback thread */
237 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
238 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
239 audio_extn_usb_start_playback(adev);
240
241 /* start usb capture thread */
242 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
243 audio_extn_usb_start_capture(adev);
244
Eric Laurentb23d5282013-05-14 15:27:20 -0700245 if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700246 adev->snd_dev_ref_cnt[snd_device]--;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800247 return -EINVAL;
248 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800249
Eric Laurent994a6932013-07-17 11:51:42 -0700250 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700251 snd_device, platform_get_snd_device_name(snd_device));
252 audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700253 if (update_mixer)
254 audio_route_update_mixer(adev->audio_route);
255
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800256 return 0;
257}
258
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700259int disable_snd_device(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700260 snd_device_t snd_device,
261 bool update_mixer)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800262{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800263 if (snd_device < SND_DEVICE_MIN ||
264 snd_device >= SND_DEVICE_MAX) {
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800265 ALOGE("%s: Invalid sound device %d", __func__, snd_device);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800266 return -EINVAL;
267 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700268 if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
269 ALOGE("%s: device ref cnt is already 0", __func__);
270 return -EINVAL;
271 }
272 adev->snd_dev_ref_cnt[snd_device]--;
Apoorv Raghuvanshi5792d4b2013-10-07 18:40:05 -0700273
274 /* exit usb play back thread */
275 if(SND_DEVICE_OUT_USB_HEADSET == snd_device ||
276 SND_DEVICE_OUT_SPEAKER_AND_USB_HEADSET == snd_device)
277 audio_extn_usb_stop_playback();
278
279 /* exit usb capture thread */
280 if(SND_DEVICE_IN_USB_HEADSET_MIC == snd_device)
281 audio_extn_usb_stop_capture(adev);
282
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700283 if (adev->snd_dev_ref_cnt[snd_device] == 0) {
Eric Laurent994a6932013-07-17 11:51:42 -0700284 ALOGV("%s: snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700285 snd_device, platform_get_snd_device_name(snd_device));
286 audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700287 if (update_mixer)
288 audio_route_update_mixer(adev->audio_route);
289 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800290 return 0;
291}
292
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700293static void check_usecases_codec_backend(struct audio_device *adev,
294 struct audio_usecase *uc_info,
295 snd_device_t snd_device)
296{
297 struct listnode *node;
298 struct audio_usecase *usecase;
299 bool switch_device[AUDIO_USECASE_MAX];
300 int i, num_uc_to_switch = 0;
301
302 /*
303 * This function is to make sure that all the usecases that are active on
304 * the hardware codec backend are always routed to any one device that is
305 * handled by the hardware codec.
306 * For example, if low-latency and deep-buffer usecases are currently active
307 * on speaker and out_set_parameters(headset) is received on low-latency
308 * output, then we have to make sure deep-buffer is also switched to headset,
309 * because of the limitation that both the devices cannot be enabled
310 * at the same time as they share the same backend.
311 */
312 /* Disable all the usecases on the shared backend other than the
313 specified usecase */
314 for (i = 0; i < AUDIO_USECASE_MAX; i++)
315 switch_device[i] = false;
316
317 list_for_each(node, &adev->usecase_list) {
318 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700319 if (usecase->type == PCM_PLAYBACK &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700320 usecase != uc_info &&
321 usecase->out_snd_device != snd_device &&
322 usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
323 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
324 __func__, use_case_table[usecase->id],
Eric Laurentb23d5282013-05-14 15:27:20 -0700325 platform_get_snd_device_name(usecase->out_snd_device));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700326 disable_audio_route(adev, usecase, false);
327 switch_device[usecase->id] = true;
328 num_uc_to_switch++;
329 }
330 }
331
332 if (num_uc_to_switch) {
333 /* Make sure all the streams are de-routed before disabling the device */
334 audio_route_update_mixer(adev->audio_route);
335
336 list_for_each(node, &adev->usecase_list) {
337 usecase = node_to_item(node, struct audio_usecase, list);
338 if (switch_device[usecase->id]) {
339 disable_snd_device(adev, usecase->out_snd_device, false);
340 enable_snd_device(adev, snd_device, false);
341 }
342 }
343
344 /* Make sure new snd device is enabled before re-routing the streams */
345 audio_route_update_mixer(adev->audio_route);
346
347 /* Re-route all the usecases on the shared backend other than the
348 specified usecase to new snd devices */
349 list_for_each(node, &adev->usecase_list) {
350 usecase = node_to_item(node, struct audio_usecase, list);
351 /* Update the out_snd_device only before enabling the audio route */
352 if (switch_device[usecase->id] ) {
353 usecase->out_snd_device = snd_device;
354 enable_audio_route(adev, usecase, false);
355 }
356 }
357
358 audio_route_update_mixer(adev->audio_route);
359 }
360}
361
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700362static void check_and_route_capture_usecases(struct audio_device *adev,
363 struct audio_usecase *uc_info,
364 snd_device_t snd_device)
365{
366 struct listnode *node;
367 struct audio_usecase *usecase;
368 bool switch_device[AUDIO_USECASE_MAX];
369 int i, num_uc_to_switch = 0;
370
371 /*
372 * This function is to make sure that all the active capture usecases
373 * are always routed to the same input sound device.
374 * For example, if audio-record and voice-call usecases are currently
375 * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
376 * is received for voice call then we have to make sure that audio-record
377 * usecase is also switched to earpiece i.e. voice-dmic-ef,
378 * because of the limitation that two devices cannot be enabled
379 * at the same time if they share the same backend.
380 */
381 for (i = 0; i < AUDIO_USECASE_MAX; i++)
382 switch_device[i] = false;
383
384 list_for_each(node, &adev->usecase_list) {
385 usecase = node_to_item(node, struct audio_usecase, list);
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700386 if (usecase->type == PCM_CAPTURE &&
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700387 usecase != uc_info &&
388 usecase->in_snd_device != snd_device) {
389 ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
390 __func__, use_case_table[usecase->id],
Devin Kim1e5f3532013-08-09 07:48:29 -0700391 platform_get_snd_device_name(usecase->in_snd_device));
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700392 disable_audio_route(adev, usecase, false);
393 switch_device[usecase->id] = true;
394 num_uc_to_switch++;
395 }
396 }
397
398 if (num_uc_to_switch) {
399 /* Make sure all the streams are de-routed before disabling the device */
400 audio_route_update_mixer(adev->audio_route);
401
402 list_for_each(node, &adev->usecase_list) {
403 usecase = node_to_item(node, struct audio_usecase, list);
404 if (switch_device[usecase->id]) {
405 disable_snd_device(adev, usecase->in_snd_device, false);
406 enable_snd_device(adev, snd_device, false);
407 }
408 }
409
410 /* Make sure new snd device is enabled before re-routing the streams */
411 audio_route_update_mixer(adev->audio_route);
412
413 /* Re-route all the usecases on the shared backend other than the
414 specified usecase to new snd devices */
415 list_for_each(node, &adev->usecase_list) {
416 usecase = node_to_item(node, struct audio_usecase, list);
417 /* Update the in_snd_device only before enabling the audio route */
418 if (switch_device[usecase->id] ) {
419 usecase->in_snd_device = snd_device;
420 enable_audio_route(adev, usecase, false);
421 }
422 }
423
424 audio_route_update_mixer(adev->audio_route);
425 }
426}
427
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700428static int disable_all_usecases_of_type(struct audio_device *adev,
429 usecase_type_t usecase_type,
430 bool update_mixer)
431{
432 struct audio_usecase *usecase;
433 struct listnode *node;
434 int ret = 0;
435
436 list_for_each(node, &adev->usecase_list) {
437 usecase = node_to_item(node, struct audio_usecase, list);
438 if (usecase->type == usecase_type) {
439 ALOGV("%s: usecase id %d", __func__, usecase->id);
440 ret = disable_audio_route(adev, usecase, update_mixer);
441 if (ret) {
442 ALOGE("%s: Failed to disable usecase id %d",
443 __func__, usecase->id);
444 }
445 }
446 }
447
448 return ret;
449}
450
451static int enable_all_usecases_of_type(struct audio_device *adev,
452 usecase_type_t usecase_type,
453 bool update_mixer)
454{
455 struct audio_usecase *usecase;
456 struct listnode *node;
457 int ret = 0;
458
459 list_for_each(node, &adev->usecase_list) {
460 usecase = node_to_item(node, struct audio_usecase, list);
461 if (usecase->type == usecase_type) {
462 ALOGV("%s: usecase id %d", __func__, usecase->id);
463 ret = enable_audio_route(adev, usecase, update_mixer);
464 if (ret) {
465 ALOGE("%s: Failed to enable usecase id %d",
466 __func__, usecase->id);
467 }
468 }
469 }
470
471 return ret;
472}
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800473
474/* must be called with hw device mutex locked */
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700475static int read_hdmi_channel_masks(struct stream_out *out)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800476{
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700477 int ret = 0;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -0700478 int channels = platform_edid_get_max_channels(out->dev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800479
480 switch (channels) {
481 /*
482 * Do not handle stereo output in Multi-channel cases
483 * Stereo case is handled in normal playback path
484 */
485 case 6:
486 ALOGV("%s: HDMI supports 5.1", __func__);
487 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
488 break;
489 case 8:
490 ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
491 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
492 out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
493 break;
494 default:
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700495 ALOGE("HDMI does not support multi channel playback");
496 ret = -ENOSYS;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800497 break;
498 }
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -0700499 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800500}
501
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700502static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
503{
504 struct audio_usecase *usecase;
505 struct listnode *node;
506
507 list_for_each(node, &adev->usecase_list) {
508 usecase = node_to_item(node, struct audio_usecase, list);
509 if (usecase->type == VOICE_CALL) {
510 ALOGV("%s: usecase id %d", __func__, usecase->id);
511 return usecase->id;
512 }
513 }
514 return USECASE_INVALID;
515}
516
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -0700517struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700518 audio_usecase_t uc_id)
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700519{
520 struct audio_usecase *usecase;
521 struct listnode *node;
522
523 list_for_each(node, &adev->usecase_list) {
524 usecase = node_to_item(node, struct audio_usecase, list);
525 if (usecase->id == uc_id)
526 return usecase;
527 }
528 return NULL;
529}
530
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700531int select_devices(struct audio_device *adev, audio_usecase_t uc_id)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800532{
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800533 snd_device_t out_snd_device = SND_DEVICE_NONE;
534 snd_device_t in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700535 struct audio_usecase *usecase = NULL;
536 struct audio_usecase *vc_usecase = NULL;
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800537 struct listnode *node;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700538 int status = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800539
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700540 usecase = get_usecase_from_list(adev, uc_id);
541 if (usecase == NULL) {
542 ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
543 return -EINVAL;
544 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800545
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700546 if (usecase->type == VOICE_CALL) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700547 out_snd_device = platform_get_output_snd_device(adev->platform,
548 usecase->stream.out->devices);
549 in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700550 usecase->devices = usecase->stream.out->devices;
551 } else {
552 /*
553 * If the voice call is active, use the sound devices of voice call usecase
554 * so that it would not result any device switch. All the usecases will
555 * be switched to new device when select_devices() is called for voice call
556 * usecase. This is to avoid switching devices for voice call when
557 * check_usecases_codec_backend() is called below.
558 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700559 if (voice_is_in_call(adev)) {
560 vc_usecase = get_usecase_from_list(adev,
561 get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700562 if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
563 in_snd_device = vc_usecase->in_snd_device;
564 out_snd_device = vc_usecase->out_snd_device;
565 }
566 }
567 if (usecase->type == PCM_PLAYBACK) {
568 usecase->devices = usecase->stream.out->devices;
569 in_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700570 if (out_snd_device == SND_DEVICE_NONE) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700571 out_snd_device = platform_get_output_snd_device(adev->platform,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700572 usecase->stream.out->devices);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700573 if (usecase->stream.out == adev->primary_output &&
574 adev->active_input &&
575 adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) {
576 select_devices(adev, adev->active_input->usecase);
577 }
578 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700579 } else if (usecase->type == PCM_CAPTURE) {
580 usecase->devices = usecase->stream.in->device;
581 out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700582 if (in_snd_device == SND_DEVICE_NONE) {
583 if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION &&
584 adev->primary_output && !adev->primary_output->standby) {
Eric Laurentb23d5282013-05-14 15:27:20 -0700585 in_snd_device = platform_get_input_snd_device(adev->platform,
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700586 adev->primary_output->devices);
587 } else {
Eric Laurentb23d5282013-05-14 15:27:20 -0700588 in_snd_device = platform_get_input_snd_device(adev->platform,
589 AUDIO_DEVICE_NONE);
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -0700590 }
591 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700592 }
593 }
594
595 if (out_snd_device == usecase->out_snd_device &&
596 in_snd_device == usecase->in_snd_device) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800597 return 0;
598 }
599
sangwoobc677242013-08-08 16:53:43 +0900600 ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__,
Eric Laurentb23d5282013-05-14 15:27:20 -0700601 out_snd_device, platform_get_snd_device_name(out_snd_device),
602 in_snd_device, platform_get_snd_device_name(in_snd_device));
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -0800603
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800604 /*
605 * Limitation: While in call, to do a device switch we need to disable
606 * and enable both RX and TX devices though one of them is same as current
607 * device.
608 */
Eric Laurentb23d5282013-05-14 15:27:20 -0700609 if (usecase->type == VOICE_CALL) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700610 disable_all_usecases_of_type(adev, VOICE_CALL, true);
Eric Laurentb23d5282013-05-14 15:27:20 -0700611 status = platform_switch_voice_call_device_pre(adev->platform);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800612 }
613
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700614 /* Disable current sound devices */
615 if (usecase->out_snd_device != SND_DEVICE_NONE) {
616 disable_audio_route(adev, usecase, true);
617 disable_snd_device(adev, usecase->out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800618 }
619
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700620 if (usecase->in_snd_device != SND_DEVICE_NONE) {
621 disable_audio_route(adev, usecase, true);
622 disable_snd_device(adev, usecase->in_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800623 }
624
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700625 /* Enable new sound devices */
626 if (out_snd_device != SND_DEVICE_NONE) {
627 if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND)
628 check_usecases_codec_backend(adev, usecase, out_snd_device);
629 enable_snd_device(adev, out_snd_device, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800630 }
631
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700632 if (in_snd_device != SND_DEVICE_NONE) {
633 check_and_route_capture_usecases(adev, usecase, in_snd_device);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700634 enable_snd_device(adev, in_snd_device, false);
Ravi Kumar Alamandac4ba7432013-06-05 14:11:39 -0700635 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700636
Eric Laurentb23d5282013-05-14 15:27:20 -0700637 if (usecase->type == VOICE_CALL)
638 status = platform_switch_voice_call_device_post(adev->platform,
639 out_snd_device,
640 in_snd_device);
Ravi Kumar Alamanda610e8cc2013-02-12 01:42:38 -0800641
sangwoo170731f2013-06-08 15:36:36 +0900642 audio_route_update_mixer(adev->audio_route);
643
644 usecase->in_snd_device = in_snd_device;
645 usecase->out_snd_device = out_snd_device;
646
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -0700647 if (usecase->type == VOICE_CALL)
648 enable_all_usecases_of_type(adev, VOICE_CALL, true);
649 else
650 enable_audio_route(adev, usecase, true);
sangwoo170731f2013-06-08 15:36:36 +0900651
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800652 return status;
653}
654
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800655static int stop_input_stream(struct stream_in *in)
656{
657 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800658 struct audio_usecase *uc_info;
659 struct audio_device *adev = in->dev;
660
Eric Laurentc8400632013-02-14 19:04:54 -0800661 adev->active_input = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800662
Eric Laurent994a6932013-07-17 11:51:42 -0700663 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700664 in->usecase, use_case_table[in->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800665 uc_info = get_usecase_from_list(adev, in->usecase);
666 if (uc_info == NULL) {
667 ALOGE("%s: Could not find the usecase (%d) in the list",
668 __func__, in->usecase);
669 return -EINVAL;
670 }
671
Eric Laurent150dbfe2013-02-27 14:31:02 -0800672 /* 1. Disable stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700673 disable_audio_route(adev, uc_info, true);
674
675 /* 2. Disable the tx device */
676 disable_snd_device(adev, uc_info->in_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800677
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800678 list_remove(&uc_info->list);
679 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800680
Eric Laurent994a6932013-07-17 11:51:42 -0700681 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800682 return ret;
683}
684
685int start_input_stream(struct stream_in *in)
686{
687 /* 1. Enable output device and stream routing controls */
Eric Laurentc8400632013-02-14 19:04:54 -0800688 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800689 struct audio_usecase *uc_info;
690 struct audio_device *adev = in->dev;
691
Eric Laurent994a6932013-07-17 11:51:42 -0700692 ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
Shiv Maliyappanahallida107642013-10-17 11:16:13 -0700693
694 /* Check if source matches incall recording usecase criteria */
695 ret = voice_check_and_set_incall_rec_usecase(adev, in);
696 if (ret)
697 goto error_config;
698 else
699 ALOGV("%s: usecase(%d)", __func__, in->usecase);
700
Eric Laurentb23d5282013-05-14 15:27:20 -0700701 in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800702 if (in->pcm_device_id < 0) {
703 ALOGE("%s: Could not find PCM device id for the usecase(%d)",
704 __func__, in->usecase);
Eric Laurentc8400632013-02-14 19:04:54 -0800705 ret = -EINVAL;
706 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800707 }
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700708
709 adev->active_input = in;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800710 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
711 uc_info->id = in->usecase;
712 uc_info->type = PCM_CAPTURE;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -0800713 uc_info->stream.in = in;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700714 uc_info->devices = in->device;
715 uc_info->in_snd_device = SND_DEVICE_NONE;
716 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800717
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800718 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700719 select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800720
Eric Laurentc8400632013-02-14 19:04:54 -0800721 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
722 __func__, SOUND_CARD, in->pcm_device_id, in->config.channels);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800723 in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id,
724 PCM_IN, &in->config);
725 if (in->pcm && !pcm_is_ready(in->pcm)) {
726 ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
727 pcm_close(in->pcm);
728 in->pcm = NULL;
Eric Laurentc8400632013-02-14 19:04:54 -0800729 ret = -EIO;
730 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800731 }
Eric Laurent994a6932013-07-17 11:51:42 -0700732 ALOGV("%s: exit", __func__);
Eric Laurentc8400632013-02-14 19:04:54 -0800733 return ret;
734
735error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800736 stop_input_stream(in);
Eric Laurentc8400632013-02-14 19:04:54 -0800737
738error_config:
739 adev->active_input = NULL;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700740 ALOGD("%s: exit: status(%d)", __func__, ret);
Eric Laurentc8400632013-02-14 19:04:54 -0800741
742 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800743}
744
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700745/* must be called with out->lock locked */
746static int send_offload_cmd_l(struct stream_out* out, int command)
747{
748 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
749
750 ALOGVV("%s %d", __func__, command);
751
752 cmd->cmd = command;
753 list_add_tail(&out->offload_cmd_list, &cmd->node);
754 pthread_cond_signal(&out->offload_cond);
755 return 0;
756}
757
758/* must be called iwth out->lock locked */
759static void stop_compressed_output_l(struct stream_out *out)
760{
761 out->offload_state = OFFLOAD_STATE_IDLE;
762 out->playback_started = 0;
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700763 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700764 if (out->compr != NULL) {
765 compress_stop(out->compr);
766 while (out->offload_thread_blocked) {
767 pthread_cond_wait(&out->cond, &out->lock);
768 }
769 }
770}
771
772static void *offload_thread_loop(void *context)
773{
774 struct stream_out *out = (struct stream_out *) context;
775 struct listnode *item;
776
777 out->offload_state = OFFLOAD_STATE_IDLE;
778 out->playback_started = 0;
779
780 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
781 set_sched_policy(0, SP_FOREGROUND);
782 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
783
784 ALOGV("%s", __func__);
785 pthread_mutex_lock(&out->lock);
786 for (;;) {
787 struct offload_cmd *cmd = NULL;
788 stream_callback_event_t event;
789 bool send_callback = false;
790
791 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
792 __func__, list_empty(&out->offload_cmd_list),
793 out->offload_state);
794 if (list_empty(&out->offload_cmd_list)) {
795 ALOGV("%s SLEEPING", __func__);
796 pthread_cond_wait(&out->offload_cond, &out->lock);
797 ALOGV("%s RUNNING", __func__);
798 continue;
799 }
800
801 item = list_head(&out->offload_cmd_list);
802 cmd = node_to_item(item, struct offload_cmd, node);
803 list_remove(item);
804
805 ALOGVV("%s STATE %d CMD %d out->compr %p",
806 __func__, out->offload_state, cmd->cmd, out->compr);
807
808 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
809 free(cmd);
810 break;
811 }
812
813 if (out->compr == NULL) {
814 ALOGE("%s: Compress handle is NULL", __func__);
815 pthread_cond_signal(&out->cond);
816 continue;
817 }
818 out->offload_thread_blocked = true;
819 pthread_mutex_unlock(&out->lock);
820 send_callback = false;
821 switch(cmd->cmd) {
822 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
823 compress_wait(out->compr, -1);
824 send_callback = true;
825 event = STREAM_CBK_EVENT_WRITE_READY;
826 break;
827 case OFFLOAD_CMD_PARTIAL_DRAIN:
Haynes Mathew George352f27b2013-07-26 00:00:15 -0700828 compress_next_track(out->compr);
829 compress_partial_drain(out->compr);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700830 send_callback = true;
831 event = STREAM_CBK_EVENT_DRAIN_READY;
832 break;
833 case OFFLOAD_CMD_DRAIN:
834 compress_drain(out->compr);
835 send_callback = true;
836 event = STREAM_CBK_EVENT_DRAIN_READY;
837 break;
838 default:
839 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
840 break;
841 }
842 pthread_mutex_lock(&out->lock);
843 out->offload_thread_blocked = false;
844 pthread_cond_signal(&out->cond);
Eric Laurent6e895242013-09-05 16:10:57 -0700845 if (send_callback) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700846 out->offload_callback(event, NULL, out->offload_cookie);
Eric Laurent6e895242013-09-05 16:10:57 -0700847 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -0700848 free(cmd);
849 }
850
851 pthread_cond_signal(&out->cond);
852 while (!list_empty(&out->offload_cmd_list)) {
853 item = list_head(&out->offload_cmd_list);
854 list_remove(item);
855 free(node_to_item(item, struct offload_cmd, node));
856 }
857 pthread_mutex_unlock(&out->lock);
858
859 return NULL;
860}
861
862static int create_offload_callback_thread(struct stream_out *out)
863{
864 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
865 list_init(&out->offload_cmd_list);
866 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
867 offload_thread_loop, out);
868 return 0;
869}
870
871static int destroy_offload_callback_thread(struct stream_out *out)
872{
873 pthread_mutex_lock(&out->lock);
874 stop_compressed_output_l(out);
875 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
876
877 pthread_mutex_unlock(&out->lock);
878 pthread_join(out->offload_thread, (void **) NULL);
879 pthread_cond_destroy(&out->offload_cond);
880
881 return 0;
882}
883
Eric Laurent07eeafd2013-10-06 12:52:49 -0700884static bool allow_hdmi_channel_config(struct audio_device *adev)
885{
886 struct listnode *node;
887 struct audio_usecase *usecase;
888 bool ret = true;
889
890 list_for_each(node, &adev->usecase_list) {
891 usecase = node_to_item(node, struct audio_usecase, list);
892 if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
893 /*
894 * If voice call is already existing, do not proceed further to avoid
895 * disabling/enabling both RX and TX devices, CSD calls, etc.
896 * Once the voice call done, the HDMI channels can be configured to
897 * max channels of remaining use cases.
898 */
899 if (usecase->id == USECASE_VOICE_CALL) {
900 ALOGD("%s: voice call is active, no change in HDMI channels",
901 __func__);
902 ret = false;
903 break;
904 } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
905 ALOGD("%s: multi channel playback is active, "
906 "no change in HDMI channels", __func__);
907 ret = false;
908 break;
909 }
910 }
911 }
912 return ret;
913}
914
915static int check_and_set_hdmi_channels(struct audio_device *adev,
916 unsigned int channels)
917{
918 struct listnode *node;
919 struct audio_usecase *usecase;
920
921 /* Check if change in HDMI channel config is allowed */
922 if (!allow_hdmi_channel_config(adev))
923 return 0;
924
925 if (channels == adev->cur_hdmi_channels) {
926 ALOGD("%s: Requested channels are same as current", __func__);
927 return 0;
928 }
929
930 platform_set_hdmi_channels(adev->platform, channels);
931 adev->cur_hdmi_channels = channels;
932
933 /*
934 * Deroute all the playback streams routed to HDMI so that
935 * the back end is deactivated. Note that backend will not
936 * be deactivated if any one stream is connected to it.
937 */
938 list_for_each(node, &adev->usecase_list) {
939 usecase = node_to_item(node, struct audio_usecase, list);
940 if (usecase->type == PCM_PLAYBACK &&
941 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
942 disable_audio_route(adev, usecase, true);
943 }
944 }
945
946 /*
947 * Enable all the streams disabled above. Now the HDMI backend
948 * will be activated with new channel configuration
949 */
950 list_for_each(node, &adev->usecase_list) {
951 usecase = node_to_item(node, struct audio_usecase, list);
952 if (usecase->type == PCM_PLAYBACK &&
953 usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
954 enable_audio_route(adev, usecase, true);
955 }
956 }
957
958 return 0;
959}
960
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800961static int stop_output_stream(struct stream_out *out)
962{
963 int i, ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800964 struct audio_usecase *uc_info;
965 struct audio_device *adev = out->dev;
966
Eric Laurent994a6932013-07-17 11:51:42 -0700967 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700968 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800969 uc_info = get_usecase_from_list(adev, out->usecase);
970 if (uc_info == NULL) {
971 ALOGE("%s: Could not find the usecase (%d) in the list",
972 __func__, out->usecase);
973 return -EINVAL;
974 }
975
Eric Laurentc4aef752013-09-12 17:45:53 -0700976 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD &&
977 adev->visualizer_stop_output != NULL)
978 adev->visualizer_stop_output(out->handle);
979
Eric Laurent150dbfe2013-02-27 14:31:02 -0800980 /* 1. Get and set stream specific mixer controls */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -0700981 disable_audio_route(adev, uc_info, true);
982
983 /* 2. Disable the rx device */
984 disable_snd_device(adev, uc_info->out_snd_device, true);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800985
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -0800986 list_remove(&uc_info->list);
987 free(uc_info);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800988
Eric Laurent07eeafd2013-10-06 12:52:49 -0700989 /* Must be called after removing the usecase from list */
990 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
991 check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
992
Eric Laurent994a6932013-07-17 11:51:42 -0700993 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800994 return ret;
995}
996
997int start_output_stream(struct stream_out *out)
998{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -0800999 int ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001000 struct audio_usecase *uc_info;
1001 struct audio_device *adev = out->dev;
1002
Eric Laurent994a6932013-07-17 11:51:42 -07001003 ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001004 __func__, out->usecase, use_case_table[out->usecase], out->devices);
Eric Laurentb23d5282013-05-14 15:27:20 -07001005 out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001006 if (out->pcm_device_id < 0) {
1007 ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
1008 __func__, out->pcm_device_id, out->usecase);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001009 ret = -EINVAL;
1010 goto error_config;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001011 }
1012
1013 uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
1014 uc_info->id = out->usecase;
1015 uc_info->type = PCM_PLAYBACK;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001016 uc_info->stream.out = out;
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001017 uc_info->devices = out->devices;
1018 uc_info->in_snd_device = SND_DEVICE_NONE;
1019 uc_info->out_snd_device = SND_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001020
Eric Laurent07eeafd2013-10-06 12:52:49 -07001021 /* This must be called before adding this usecase to the list */
1022 if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
1023 check_and_set_hdmi_channels(adev, out->config.channels);
1024
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08001025 list_add_tail(&adev->usecase_list, &uc_info->list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001026
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001027 select_devices(adev, out->usecase);
1028
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001029 ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)",
1030 __func__, 0, out->pcm_device_id);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001031 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1032 out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id,
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001033 PCM_OUT | PCM_MONOTONIC, &out->config);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001034 if (out->pcm && !pcm_is_ready(out->pcm)) {
1035 ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
1036 pcm_close(out->pcm);
1037 out->pcm = NULL;
1038 ret = -EIO;
1039 goto error_open;
1040 }
1041 } else {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001042 out->pcm = NULL;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001043 out->compr = compress_open(SOUND_CARD, out->pcm_device_id,
1044 COMPRESS_IN, &out->compr_config);
1045 if (out->compr && !is_compress_ready(out->compr)) {
1046 ALOGE("%s: %s", __func__, compress_get_error(out->compr));
1047 compress_close(out->compr);
1048 out->compr = NULL;
1049 ret = -EIO;
1050 goto error_open;
1051 }
1052 if (out->offload_callback)
1053 compress_nonblock(out->compr, out->non_blocking);
Eric Laurentc4aef752013-09-12 17:45:53 -07001054
1055 if (adev->visualizer_start_output != NULL)
1056 adev->visualizer_start_output(out->handle);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001057 }
Eric Laurent994a6932013-07-17 11:51:42 -07001058 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001059 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001060error_open:
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001061 stop_output_stream(out);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001062error_config:
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08001063 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001064}
1065
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001066static int check_input_parameters(uint32_t sample_rate,
1067 audio_format_t format,
1068 int channel_count)
1069{
1070 if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL;
1071
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001072 if ((channel_count < 1) || (channel_count > 6)) return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001073
1074 switch (sample_rate) {
1075 case 8000:
1076 case 11025:
1077 case 12000:
1078 case 16000:
1079 case 22050:
1080 case 24000:
1081 case 32000:
1082 case 44100:
1083 case 48000:
1084 break;
1085 default:
1086 return -EINVAL;
1087 }
1088
1089 return 0;
1090}
1091
1092static size_t get_input_buffer_size(uint32_t sample_rate,
1093 audio_format_t format,
1094 int channel_count)
1095{
1096 size_t size = 0;
1097
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001098 if (check_input_parameters(sample_rate, format, channel_count) != 0)
1099 return 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001100
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001101 size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000;
1102 /* ToDo: should use frame_size computed based on the format and
1103 channel_count here. */
1104 size *= sizeof(short) * channel_count;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001105
Ravi Kumar Alamanda33d33062013-06-11 14:40:01 -07001106 /* make sure the size is multiple of 64 */
1107 size += 0x3f;
1108 size &= ~0x3f;
1109
1110 return size;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001111}
1112
1113static uint32_t out_get_sample_rate(const struct audio_stream *stream)
1114{
1115 struct stream_out *out = (struct stream_out *)stream;
1116
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001117 return out->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001118}
1119
1120static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1121{
1122 return -ENOSYS;
1123}
1124
1125static size_t out_get_buffer_size(const struct audio_stream *stream)
1126{
1127 struct stream_out *out = (struct stream_out *)stream;
1128
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001129 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1130 return out->compr_config.fragment_size;
1131 }
1132
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001133 return out->config.period_size * audio_stream_frame_size(stream);
1134}
1135
1136static uint32_t out_get_channels(const struct audio_stream *stream)
1137{
1138 struct stream_out *out = (struct stream_out *)stream;
1139
1140 return out->channel_mask;
1141}
1142
1143static audio_format_t out_get_format(const struct audio_stream *stream)
1144{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001145 struct stream_out *out = (struct stream_out *)stream;
1146
1147 return out->format;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001148}
1149
1150static int out_set_format(struct audio_stream *stream, audio_format_t format)
1151{
1152 return -ENOSYS;
1153}
1154
1155static int out_standby(struct audio_stream *stream)
1156{
1157 struct stream_out *out = (struct stream_out *)stream;
1158 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001159
Eric Laurent994a6932013-07-17 11:51:42 -07001160 ALOGV("%s: enter: usecase(%d: %s)", __func__,
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001161 out->usecase, use_case_table[out->usecase]);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001162
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001163 pthread_mutex_lock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001164 if (!out->standby) {
1165 out->standby = true;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001166 if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1167 if (out->pcm) {
1168 pcm_close(out->pcm);
1169 out->pcm = NULL;
1170 }
1171 } else {
1172 stop_compressed_output_l(out);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001173 out->gapless_mdata.encoder_delay = 0;
1174 out->gapless_mdata.encoder_padding = 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001175 if (out->compr != NULL) {
1176 compress_close(out->compr);
1177 out->compr = NULL;
1178 }
Eric Laurent150dbfe2013-02-27 14:31:02 -08001179 }
1180 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001181 stop_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001182 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001183 }
1184 pthread_mutex_unlock(&out->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001185 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001186 return 0;
1187}
1188
1189static int out_dump(const struct audio_stream *stream, int fd)
1190{
1191 return 0;
1192}
1193
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001194static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
1195{
1196 int ret = 0;
1197 char value[32];
1198 struct compr_gapless_mdata tmp_mdata;
1199
1200 if (!out || !parms) {
1201 return -EINVAL;
1202 }
1203
1204 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
1205 if (ret >= 0) {
1206 tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
1207 } else {
1208 return -EINVAL;
1209 }
1210
1211 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
1212 if (ret >= 0) {
1213 tmp_mdata.encoder_padding = atoi(value);
1214 } else {
1215 return -EINVAL;
1216 }
1217
1218 out->gapless_mdata = tmp_mdata;
1219 out->send_new_metadata = 1;
1220 ALOGV("%s new encoder delay %u and padding %u", __func__,
1221 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
1222
1223 return 0;
1224}
1225
1226
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001227static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
1228{
1229 struct stream_out *out = (struct stream_out *)stream;
1230 struct audio_device *adev = out->dev;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001231 struct audio_usecase *usecase;
1232 struct listnode *node;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001233 struct str_parms *parms;
1234 char value[32];
1235 int ret, val = 0;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001236 bool select_new_device = false;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001237
sangwoobc677242013-08-08 16:53:43 +09001238 ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001239 __func__, out->usecase, use_case_table[out->usecase], kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001240 parms = str_parms_create_str(kvpairs);
1241 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1242 if (ret >= 0) {
1243 val = atoi(value);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001244 pthread_mutex_lock(&out->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001245 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001246
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001247 /*
1248 * When HDMI cable is unplugged the music playback is paused and
1249 * the policy manager sends routing=0. But the audioflinger
1250 * continues to write data until standby time (3sec).
1251 * As the HDMI core is turned off, the write gets blocked.
1252 * Avoid this by routing audio to speaker until standby.
1253 */
1254 if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
1255 val == AUDIO_DEVICE_NONE) {
1256 val = AUDIO_DEVICE_OUT_SPEAKER;
1257 }
1258
1259 /*
1260 * select_devices() call below switches all the usecases on the same
1261 * backend to the new device. Refer to check_usecases_codec_backend() in
1262 * the select_devices(). But how do we undo this?
1263 *
1264 * For example, music playback is active on headset (deep-buffer usecase)
1265 * and if we go to ringtones and select a ringtone, low-latency usecase
1266 * will be started on headset+speaker. As we can't enable headset+speaker
1267 * and headset devices at the same time, select_devices() switches the music
1268 * playback to headset+speaker while starting low-lateny usecase for ringtone.
1269 * So when the ringtone playback is completed, how do we undo the same?
1270 *
1271 * We are relying on the out_set_parameters() call on deep-buffer output,
1272 * once the ringtone playback is ended.
1273 * NOTE: We should not check if the current devices are same as new devices.
1274 * Because select_devices() must be called to switch back the music
1275 * playback to headset.
1276 */
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001277 if (val != 0) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001278 out->devices = val;
1279
1280 if (!out->standby)
1281 select_devices(adev, out->usecase);
1282
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001283 if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1284 !voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001285 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001286 voice_start_call(adev);
1287 } else if ((adev->mode == AUDIO_MODE_IN_CALL) &&
1288 voice_is_in_call(adev) &&
1289 (out == adev->primary_output)) {
1290 select_devices(adev, get_voice_usecase_id_from_list(adev));
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001291 }
1292 }
1293
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001294 if ((adev->mode == AUDIO_MODE_NORMAL) &&
1295 voice_is_in_call(adev) &&
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001296 (out == adev->primary_output)) {
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001297 voice_stop_call(adev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001298 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001299
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001300 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001301 pthread_mutex_unlock(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001302 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001303
1304 if (out == adev->primary_output) {
1305 pthread_mutex_lock(&adev->lock);
1306 audio_extn_set_parameters(adev, parms);
1307 pthread_mutex_unlock(&adev->lock);
1308 }
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001309 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1310 parse_compress_metadata(out, parms);
1311 }
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07001312
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001313 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001314 ALOGV("%s: exit: code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001315 return ret;
1316}
1317
1318static char* out_get_parameters(const struct audio_stream *stream, const char *keys)
1319{
1320 struct stream_out *out = (struct stream_out *)stream;
1321 struct str_parms *query = str_parms_create_str(keys);
1322 char *str;
1323 char value[256];
1324 struct str_parms *reply = str_parms_create();
1325 size_t i, j;
1326 int ret;
1327 bool first = true;
Eric Laurent994a6932013-07-17 11:51:42 -07001328 ALOGV("%s: enter: keys - %s", __func__, keys);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001329 ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value));
1330 if (ret >= 0) {
1331 value[0] = '\0';
1332 i = 0;
1333 while (out->supported_channel_masks[i] != 0) {
1334 for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) {
1335 if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) {
1336 if (!first) {
1337 strcat(value, "|");
1338 }
1339 strcat(value, out_channels_name_to_enum_table[j].name);
1340 first = false;
1341 break;
1342 }
1343 }
1344 i++;
1345 }
1346 str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value);
1347 str = str_parms_to_str(reply);
1348 } else {
1349 str = strdup(keys);
1350 }
1351 str_parms_destroy(query);
1352 str_parms_destroy(reply);
Eric Laurent994a6932013-07-17 11:51:42 -07001353 ALOGV("%s: exit: returns - %s", __func__, str);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001354 return str;
1355}
1356
1357static uint32_t out_get_latency(const struct audio_stream_out *stream)
1358{
1359 struct stream_out *out = (struct stream_out *)stream;
1360
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001361 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
1362 return COMPRESS_OFFLOAD_PLAYBACK_LATENCY;
1363
1364 return (out->config.period_count * out->config.period_size * 1000) /
1365 (out->config.rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001366}
1367
1368static int out_set_volume(struct audio_stream_out *stream, float left,
1369 float right)
1370{
Eric Laurenta9024de2013-04-04 09:19:12 -07001371 struct stream_out *out = (struct stream_out *)stream;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001372 int volume[2];
1373
Eric Laurenta9024de2013-04-04 09:19:12 -07001374 if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) {
1375 /* only take left channel into account: the API is for stereo anyway */
1376 out->muted = (left == 0.0f);
1377 return 0;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001378 } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1379 const char *mixer_ctl_name = "Compress Playback Volume";
1380 struct audio_device *adev = out->dev;
1381 struct mixer_ctl *ctl;
1382
1383 ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
1384 if (!ctl) {
1385 ALOGE("%s: Could not get ctl for mixer cmd - %s",
1386 __func__, mixer_ctl_name);
1387 return -EINVAL;
1388 }
1389 volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
1390 volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
1391 mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0]));
1392 return 0;
Eric Laurenta9024de2013-04-04 09:19:12 -07001393 }
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001394
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001395 return -ENOSYS;
1396}
1397
1398static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
1399 size_t bytes)
1400{
1401 struct stream_out *out = (struct stream_out *)stream;
1402 struct audio_device *adev = out->dev;
Eric Laurent6e895242013-09-05 16:10:57 -07001403 ssize_t ret = 0;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001404
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001405 pthread_mutex_lock(&out->lock);
1406 if (out->standby) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001407 out->standby = false;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001408 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001409 ret = start_output_stream(out);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001410 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001411 /* ToDo: If use case is compress offload should return 0 */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001412 if (ret != 0) {
Ravi Kumar Alamanda59d296d2013-05-02 11:25:27 -07001413 out->standby = true;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001414 goto exit;
1415 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001416 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001417
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001418 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001419 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
1420 if (out->send_new_metadata) {
1421 ALOGVV("send new gapless metadata");
1422 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
1423 out->send_new_metadata = 0;
1424 }
1425
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001426 ret = compress_write(out->compr, buffer, bytes);
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001427 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
Eric Laurent6e895242013-09-05 16:10:57 -07001428 if (ret >= 0 && ret < (ssize_t)bytes) {
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001429 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
1430 }
1431 if (!out->playback_started) {
1432 compress_start(out->compr);
1433 out->playback_started = 1;
1434 out->offload_state = OFFLOAD_STATE_PLAYING;
1435 }
1436 pthread_mutex_unlock(&out->lock);
1437 return ret;
1438 } else {
1439 if (out->pcm) {
1440 if (out->muted)
1441 memset((void *)buffer, 0, bytes);
1442 ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes);
1443 ret = pcm_write(out->pcm, (void *)buffer, bytes);
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001444 if (ret == 0)
1445 out->written += bytes / (out->config.channels * sizeof(short));
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001446 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001447 }
1448
1449exit:
1450 pthread_mutex_unlock(&out->lock);
1451
1452 if (ret != 0) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001453 if (out->pcm)
1454 ALOGE("%s: error %d - %s", __func__, ret, pcm_get_error(out->pcm));
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001455 out_standby(&out->stream.common);
1456 usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
1457 out_get_sample_rate(&out->stream.common));
1458 }
1459 return bytes;
1460}
1461
1462static int out_get_render_position(const struct audio_stream_out *stream,
1463 uint32_t *dsp_frames)
1464{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001465 struct stream_out *out = (struct stream_out *)stream;
1466 *dsp_frames = 0;
1467 if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
1468 pthread_mutex_lock(&out->lock);
1469 if (out->compr != NULL) {
1470 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
1471 &out->sample_rate);
1472 ALOGVV("%s rendered frames %d sample_rate %d",
1473 __func__, *dsp_frames, out->sample_rate);
1474 }
1475 pthread_mutex_unlock(&out->lock);
1476 return 0;
1477 } else
1478 return -EINVAL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001479}
1480
1481static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1482{
1483 return 0;
1484}
1485
1486static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
1487{
1488 return 0;
1489}
1490
1491static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
1492 int64_t *timestamp)
1493{
1494 return -EINVAL;
1495}
1496
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001497static int out_get_presentation_position(const struct audio_stream_out *stream,
1498 uint64_t *frames, struct timespec *timestamp)
1499{
1500 struct stream_out *out = (struct stream_out *)stream;
1501 int ret = -1;
Eric Laurent949a0892013-09-20 09:20:13 -07001502 unsigned long dsp_frames;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001503
1504 pthread_mutex_lock(&out->lock);
1505
Eric Laurent949a0892013-09-20 09:20:13 -07001506 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1507 if (out->compr != NULL) {
1508 compress_get_tstamp(out->compr, &dsp_frames,
1509 &out->sample_rate);
1510 ALOGVV("%s rendered frames %ld sample_rate %d",
1511 __func__, dsp_frames, out->sample_rate);
1512 *frames = dsp_frames;
1513 ret = 0;
1514 /* this is the best we can do */
1515 clock_gettime(CLOCK_MONOTONIC, timestamp);
1516 }
1517 } else {
1518 if (out->pcm) {
1519 size_t avail;
1520 if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
1521 size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
Eric Laurent949a0892013-09-20 09:20:13 -07001522 int64_t signed_frames = out->written - kernel_buffer_size + avail;
Haynes Mathew George7ff216f2013-09-11 19:51:41 -07001523 // This adjustment accounts for buffering after app processor.
1524 // It is based on estimated DSP latency per use case, rather than exact.
1525 signed_frames -=
1526 (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);
1527
Eric Laurent949a0892013-09-20 09:20:13 -07001528 // It would be unusual for this value to be negative, but check just in case ...
1529 if (signed_frames >= 0) {
1530 *frames = signed_frames;
1531 ret = 0;
1532 }
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001533 }
1534 }
1535 }
1536
1537 pthread_mutex_unlock(&out->lock);
1538
1539 return ret;
1540}
1541
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001542static int out_set_callback(struct audio_stream_out *stream,
1543 stream_callback_t callback, void *cookie)
1544{
1545 struct stream_out *out = (struct stream_out *)stream;
1546
1547 ALOGV("%s", __func__);
1548 pthread_mutex_lock(&out->lock);
1549 out->offload_callback = callback;
1550 out->offload_cookie = cookie;
1551 pthread_mutex_unlock(&out->lock);
1552 return 0;
1553}
1554
1555static int out_pause(struct audio_stream_out* stream)
1556{
1557 struct stream_out *out = (struct stream_out *)stream;
1558 int status = -ENOSYS;
1559 ALOGV("%s", __func__);
1560 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1561 pthread_mutex_lock(&out->lock);
1562 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
1563 status = compress_pause(out->compr);
1564 out->offload_state = OFFLOAD_STATE_PAUSED;
1565 }
1566 pthread_mutex_unlock(&out->lock);
1567 }
1568 return status;
1569}
1570
1571static int out_resume(struct audio_stream_out* stream)
1572{
1573 struct stream_out *out = (struct stream_out *)stream;
1574 int status = -ENOSYS;
1575 ALOGV("%s", __func__);
1576 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1577 status = 0;
1578 pthread_mutex_lock(&out->lock);
1579 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
1580 status = compress_resume(out->compr);
1581 out->offload_state = OFFLOAD_STATE_PLAYING;
1582 }
1583 pthread_mutex_unlock(&out->lock);
1584 }
1585 return status;
1586}
1587
1588static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
1589{
1590 struct stream_out *out = (struct stream_out *)stream;
1591 int status = -ENOSYS;
1592 ALOGV("%s", __func__);
1593 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1594 pthread_mutex_lock(&out->lock);
1595 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
1596 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
1597 else
1598 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
1599 pthread_mutex_unlock(&out->lock);
1600 }
1601 return status;
1602}
1603
1604static int out_flush(struct audio_stream_out* stream)
1605{
1606 struct stream_out *out = (struct stream_out *)stream;
1607 ALOGV("%s", __func__);
1608 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
1609 pthread_mutex_lock(&out->lock);
1610 stop_compressed_output_l(out);
1611 pthread_mutex_unlock(&out->lock);
1612 return 0;
1613 }
1614 return -ENOSYS;
1615}
1616
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001617/** audio_stream_in implementation **/
1618static uint32_t in_get_sample_rate(const struct audio_stream *stream)
1619{
1620 struct stream_in *in = (struct stream_in *)stream;
1621
1622 return in->config.rate;
1623}
1624
1625static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
1626{
1627 return -ENOSYS;
1628}
1629
1630static size_t in_get_buffer_size(const struct audio_stream *stream)
1631{
1632 struct stream_in *in = (struct stream_in *)stream;
1633
1634 return in->config.period_size * audio_stream_frame_size(stream);
1635}
1636
1637static uint32_t in_get_channels(const struct audio_stream *stream)
1638{
1639 struct stream_in *in = (struct stream_in *)stream;
1640
1641 return in->channel_mask;
1642}
1643
1644static audio_format_t in_get_format(const struct audio_stream *stream)
1645{
1646 return AUDIO_FORMAT_PCM_16_BIT;
1647}
1648
1649static int in_set_format(struct audio_stream *stream, audio_format_t format)
1650{
1651 return -ENOSYS;
1652}
1653
1654static int in_standby(struct audio_stream *stream)
1655{
1656 struct stream_in *in = (struct stream_in *)stream;
1657 struct audio_device *adev = in->dev;
1658 int status = 0;
Eric Laurent994a6932013-07-17 11:51:42 -07001659 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001660 pthread_mutex_lock(&in->lock);
1661 if (!in->standby) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001662 in->standby = true;
Eric Laurent150dbfe2013-02-27 14:31:02 -08001663 if (in->pcm) {
1664 pcm_close(in->pcm);
1665 in->pcm = NULL;
1666 }
1667 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001668 status = stop_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001669 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001670 }
1671 pthread_mutex_unlock(&in->lock);
Eric Laurent994a6932013-07-17 11:51:42 -07001672 ALOGV("%s: exit: status(%d)", __func__, status);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001673 return status;
1674}
1675
1676static int in_dump(const struct audio_stream *stream, int fd)
1677{
1678 return 0;
1679}
1680
1681static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
1682{
1683 struct stream_in *in = (struct stream_in *)stream;
1684 struct audio_device *adev = in->dev;
1685 struct str_parms *parms;
1686 char *str;
1687 char value[32];
1688 int ret, val = 0;
1689
Eric Laurent994a6932013-07-17 11:51:42 -07001690 ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001691 parms = str_parms_create_str(kvpairs);
1692
1693 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
1694
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001695 pthread_mutex_lock(&in->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001696 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001697 if (ret >= 0) {
1698 val = atoi(value);
1699 /* no audio source uses val == 0 */
1700 if ((in->source != val) && (val != 0)) {
1701 in->source = val;
1702 }
1703 }
1704
1705 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
1706 if (ret >= 0) {
1707 val = atoi(value);
1708 if ((in->device != val) && (val != 0)) {
1709 in->device = val;
1710 /* If recording is in progress, change the tx device to new device */
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001711 if (!in->standby)
1712 ret = select_devices(adev, in->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001713 }
1714 }
1715
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001716 pthread_mutex_unlock(&adev->lock);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001717 pthread_mutex_unlock(&in->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001718
1719 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07001720 ALOGV("%s: exit: status(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001721 return ret;
1722}
1723
1724static char* in_get_parameters(const struct audio_stream *stream,
1725 const char *keys)
1726{
1727 return strdup("");
1728}
1729
1730static int in_set_gain(struct audio_stream_in *stream, float gain)
1731{
1732 return 0;
1733}
1734
1735static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
1736 size_t bytes)
1737{
1738 struct stream_in *in = (struct stream_in *)stream;
1739 struct audio_device *adev = in->dev;
1740 int i, ret = -1;
1741
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001742 pthread_mutex_lock(&in->lock);
1743 if (in->standby) {
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07001744 pthread_mutex_lock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001745 ret = start_input_stream(in);
Eric Laurent150dbfe2013-02-27 14:31:02 -08001746 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001747 if (ret != 0) {
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001748 goto exit;
1749 }
1750 in->standby = 0;
1751 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001752
1753 if (in->pcm) {
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001754 if (audio_extn_ssr_get_enabled() && popcount(in->channel_mask) == 6)
1755 ret = audio_extn_ssr_read(stream, buffer, bytes);
1756 else
1757 ret = pcm_read(in->pcm, buffer, bytes);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001758 }
1759
1760 /*
1761 * Instead of writing zeroes here, we could trust the hardware
1762 * to always provide zeroes when muted.
1763 */
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07001764 if (ret == 0 && voice_get_mic_mute(adev))
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001765 memset(buffer, 0, bytes);
1766
1767exit:
1768 pthread_mutex_unlock(&in->lock);
1769
1770 if (ret != 0) {
1771 in_standby(&in->stream.common);
1772 ALOGV("%s: read failed - sleeping for buffer duration", __func__);
1773 usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) /
1774 in_get_sample_rate(&in->stream.common));
1775 }
1776 return bytes;
1777}
1778
1779static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
1780{
1781 return 0;
1782}
1783
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001784static int add_remove_audio_effect(const struct audio_stream *stream,
1785 effect_handle_t effect,
1786 bool enable)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001787{
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001788 struct stream_in *in = (struct stream_in *)stream;
1789 int status = 0;
1790 effect_descriptor_t desc;
1791
1792 status = (*effect)->get_descriptor(effect, &desc);
1793 if (status != 0)
1794 return status;
1795
1796 pthread_mutex_lock(&in->lock);
1797 pthread_mutex_lock(&in->dev->lock);
1798 if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
1799 in->enable_aec != enable &&
1800 (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) {
1801 in->enable_aec = enable;
1802 if (!in->standby)
1803 select_devices(in->dev, in->usecase);
1804 }
1805 pthread_mutex_unlock(&in->dev->lock);
1806 pthread_mutex_unlock(&in->lock);
1807
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001808 return 0;
1809}
1810
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001811static int in_add_audio_effect(const struct audio_stream *stream,
1812 effect_handle_t effect)
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001813{
Eric Laurent994a6932013-07-17 11:51:42 -07001814 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001815 return add_remove_audio_effect(stream, effect, true);
1816}
1817
1818static int in_remove_audio_effect(const struct audio_stream *stream,
1819 effect_handle_t effect)
1820{
Eric Laurent994a6932013-07-17 11:51:42 -07001821 ALOGV("%s: effect %p", __func__, effect);
Ravi Kumar Alamandaf70ffb42013-04-16 15:55:53 -07001822 return add_remove_audio_effect(stream, effect, false);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001823}
1824
1825static int adev_open_output_stream(struct audio_hw_device *dev,
1826 audio_io_handle_t handle,
1827 audio_devices_t devices,
1828 audio_output_flags_t flags,
1829 struct audio_config *config,
1830 struct audio_stream_out **stream_out)
1831{
1832 struct audio_device *adev = (struct audio_device *)dev;
1833 struct stream_out *out;
1834 int i, ret;
1835
Eric Laurent994a6932013-07-17 11:51:42 -07001836 ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)",
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001837 __func__, config->sample_rate, config->channel_mask, devices, flags);
1838 *stream_out = NULL;
1839 out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
1840
1841 if (devices == AUDIO_DEVICE_NONE)
1842 devices = AUDIO_DEVICE_OUT_SPEAKER;
1843
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001844 out->flags = flags;
1845 out->devices = devices;
Haynes Mathew George47cd4cb2013-07-19 11:58:50 -07001846 out->dev = adev;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001847 out->format = config->format;
1848 out->sample_rate = config->sample_rate;
1849 out->channel_mask = AUDIO_CHANNEL_OUT_STEREO;
1850 out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO;
Eric Laurentc4aef752013-09-12 17:45:53 -07001851 out->handle = handle;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001852
1853 /* Init use case and pcm_config */
1854 if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT &&
1855 out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001856 pthread_mutex_lock(&adev->lock);
1857 ret = read_hdmi_channel_masks(out);
1858 pthread_mutex_unlock(&adev->lock);
Eric Laurent07eeafd2013-10-06 12:52:49 -07001859 if (ret != 0)
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001860 goto error_open;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001861
1862 if (config->sample_rate == 0)
1863 config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE;
1864 if (config->channel_mask == 0)
1865 config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1;
1866
1867 out->channel_mask = config->channel_mask;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001868 out->sample_rate = config->sample_rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001869 out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH;
1870 out->config = pcm_config_hdmi_multi;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001871 out->config.rate = config->sample_rate;
1872 out->config.channels = popcount(out->channel_mask);
1873 out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001874 } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
1875 out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER;
1876 out->config = pcm_config_deep_buffer;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001877 out->sample_rate = out->config.rate;
1878 } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
1879 if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version ||
1880 config->offload_info.size != AUDIO_INFO_INITIALIZER.size) {
1881 ALOGE("%s: Unsupported Offload information", __func__);
1882 ret = -EINVAL;
1883 goto error_open;
1884 }
1885 if (!is_supported_format(config->offload_info.format)) {
1886 ALOGE("%s: Unsupported audio format", __func__);
1887 ret = -EINVAL;
1888 goto error_open;
1889 }
1890
1891 out->compr_config.codec = (struct snd_codec *)
1892 calloc(1, sizeof(struct snd_codec));
1893
1894 out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD;
1895 if (config->offload_info.channel_mask)
1896 out->channel_mask = config->offload_info.channel_mask;
1897 else if (config->channel_mask)
1898 out->channel_mask = config->channel_mask;
1899 out->format = config->offload_info.format;
1900 out->sample_rate = config->offload_info.sample_rate;
1901
1902 out->stream.set_callback = out_set_callback;
1903 out->stream.pause = out_pause;
1904 out->stream.resume = out_resume;
1905 out->stream.drain = out_drain;
1906 out->stream.flush = out_flush;
1907
1908 out->compr_config.codec->id =
1909 get_snd_codec_id(config->offload_info.format);
1910 out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE;
1911 out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS;
1912 out->compr_config.codec->sample_rate =
1913 compress_get_alsa_rate(config->offload_info.sample_rate);
1914 out->compr_config.codec->bit_rate =
1915 config->offload_info.bit_rate;
1916 out->compr_config.codec->ch_in =
1917 popcount(config->channel_mask);
1918 out->compr_config.codec->ch_out = out->compr_config.codec->ch_in;
1919
1920 if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING)
1921 out->non_blocking = 1;
Haynes Mathew George352f27b2013-07-26 00:00:15 -07001922
1923 out->send_new_metadata = 1;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001924 create_offload_callback_thread(out);
1925 ALOGV("%s: offloaded output offload_info version %04x bit rate %d",
1926 __func__, config->offload_info.version,
1927 config->offload_info.bit_rate);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001928 } else {
1929 out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY;
1930 out->config = pcm_config_low_latency;
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001931 out->sample_rate = out->config.rate;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001932 }
1933
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001934 if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) {
1935 if(adev->primary_output == NULL)
1936 adev->primary_output = out;
1937 else {
1938 ALOGE("%s: Primary output is already opened", __func__);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001939 ret = -EEXIST;
1940 goto error_open;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08001941 }
1942 }
1943
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001944 /* Check if this usecase is already existing */
1945 pthread_mutex_lock(&adev->lock);
1946 if (get_usecase_from_list(adev, out->usecase) != NULL) {
1947 ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001948 pthread_mutex_unlock(&adev->lock);
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001949 ret = -EEXIST;
1950 goto error_open;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001951 }
1952 pthread_mutex_unlock(&adev->lock);
1953
1954 out->stream.common.get_sample_rate = out_get_sample_rate;
1955 out->stream.common.set_sample_rate = out_set_sample_rate;
1956 out->stream.common.get_buffer_size = out_get_buffer_size;
1957 out->stream.common.get_channels = out_get_channels;
1958 out->stream.common.get_format = out_get_format;
1959 out->stream.common.set_format = out_set_format;
1960 out->stream.common.standby = out_standby;
1961 out->stream.common.dump = out_dump;
1962 out->stream.common.set_parameters = out_set_parameters;
1963 out->stream.common.get_parameters = out_get_parameters;
1964 out->stream.common.add_audio_effect = out_add_audio_effect;
1965 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1966 out->stream.get_latency = out_get_latency;
1967 out->stream.set_volume = out_set_volume;
1968 out->stream.write = out_write;
1969 out->stream.get_render_position = out_get_render_position;
1970 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001971 out->stream.get_presentation_position = out_get_presentation_position;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001972
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001973 out->standby = 1;
Eric Laurenta9024de2013-04-04 09:19:12 -07001974 /* out->muted = false; by calloc() */
Glenn Kasten2ccd7ba2013-09-10 09:04:31 -07001975 /* out->written = 0; by calloc() */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001976
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001977 pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
1978 pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001979
1980 config->format = out->stream.common.get_format(&out->stream.common);
1981 config->channel_mask = out->stream.common.get_channels(&out->stream.common);
1982 config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common);
1983
1984 *stream_out = &out->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07001985 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001986 return 0;
Ravi Kumar Alamandab1995062013-03-21 23:18:20 -07001987
1988error_open:
1989 free(out);
1990 *stream_out = NULL;
1991 ALOGD("%s: exit: ret %d", __func__, ret);
1992 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08001993}
1994
1995static void adev_close_output_stream(struct audio_hw_device *dev,
1996 struct audio_stream_out *stream)
1997{
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07001998 struct stream_out *out = (struct stream_out *)stream;
1999 struct audio_device *adev = out->dev;
2000
Eric Laurent994a6932013-07-17 11:51:42 -07002001 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002002 out_standby(&stream->common);
Ravi Kumar Alamanda4e02e552013-07-17 15:22:04 -07002003 if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
2004 destroy_offload_callback_thread(out);
2005
2006 if (out->compr_config.codec != NULL)
2007 free(out->compr_config.codec);
2008 }
2009 pthread_cond_destroy(&out->cond);
2010 pthread_mutex_destroy(&out->lock);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002011 free(stream);
Eric Laurent994a6932013-07-17 11:51:42 -07002012 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002013}
2014
2015static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
2016{
2017 struct audio_device *adev = (struct audio_device *)dev;
2018 struct str_parms *parms;
2019 char *str;
2020 char value[32];
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002021 int val;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002022 int ret;
2023
Eric Laurent994a6932013-07-17 11:51:42 -07002024 ALOGV("%s: enter: %s", __func__, kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002025 parms = str_parms_create_str(kvpairs);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002026
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002027 voice_set_parameters(adev, parms);
2028 platform_set_parameters(adev->platform, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002029
2030 ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value));
2031 if (ret >= 0) {
2032 /* When set to false, HAL should disable EC and NS
2033 * But it is currently not supported.
2034 */
2035 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2036 adev->bluetooth_nrec = true;
2037 else
2038 adev->bluetooth_nrec = false;
2039 }
2040
2041 ret = str_parms_get_str(parms, "screen_state", value, sizeof(value));
2042 if (ret >= 0) {
2043 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0)
2044 adev->screen_off = false;
2045 else
2046 adev->screen_off = true;
2047 }
2048
Jean-Michel Trivic56336b2013-05-24 16:55:17 -07002049 ret = str_parms_get_int(parms, "rotation", &val);
2050 if (ret >= 0) {
2051 bool reverse_speakers = false;
2052 switch(val) {
2053 // FIXME: note that the code below assumes that the speakers are in the correct placement
2054 // relative to the user when the device is rotated 90deg from its default rotation. This
2055 // assumption is device-specific, not platform-specific like this code.
2056 case 270:
2057 reverse_speakers = true;
2058 break;
2059 case 0:
2060 case 90:
2061 case 180:
2062 break;
2063 default:
2064 ALOGE("%s: unexpected rotation of %d", __func__, val);
2065 }
2066 pthread_mutex_lock(&adev->lock);
2067 if (adev->speaker_lr_swap != reverse_speakers) {
2068 adev->speaker_lr_swap = reverse_speakers;
2069 // only update the selected device if there is active pcm playback
2070 struct audio_usecase *usecase;
2071 struct listnode *node;
2072 list_for_each(node, &adev->usecase_list) {
2073 usecase = node_to_item(node, struct audio_usecase, list);
2074 if (usecase->type == PCM_PLAYBACK) {
2075 select_devices(adev, usecase->id);
2076 break;
2077 }
2078 }
2079 }
2080 pthread_mutex_unlock(&adev->lock);
2081 }
2082
Apoorv Raghuvanshi9eaf94e2013-10-04 16:13:44 -07002083 audio_extn_set_parameters(adev, parms);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002084 str_parms_destroy(parms);
Eric Laurent994a6932013-07-17 11:51:42 -07002085 ALOGV("%s: exit with code(%d)", __func__, ret);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002086 return ret;
2087}
2088
2089static char* adev_get_parameters(const struct audio_hw_device *dev,
2090 const char *keys)
2091{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002092 struct audio_device *adev = (struct audio_device *)dev;
2093 struct str_parms *reply = str_parms_create();
2094 struct str_parms *query = str_parms_create_str(keys);
2095 char *str;
2096
2097 audio_extn_get_parameters(adev, query, reply);
2098 platform_get_parameters(adev->platform, query, reply);
2099 str = str_parms_to_str(reply);
2100 str_parms_destroy(query);
2101 str_parms_destroy(reply);
2102
2103 ALOGV("%s: exit: returns - %s", __func__, str);
2104 return str;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002105}
2106
2107static int adev_init_check(const struct audio_hw_device *dev)
2108{
2109 return 0;
2110}
2111
2112static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
2113{
Haynes Mathew George5191a852013-09-11 14:19:36 -07002114 int ret;
2115 struct audio_device *adev = (struct audio_device *)dev;
2116 pthread_mutex_lock(&adev->lock);
2117 /* cache volume */
Shruthi Krishnaace10852013-10-25 14:32:12 -07002118 ret = voice_set_volume(adev, volume);
Haynes Mathew George5191a852013-09-11 14:19:36 -07002119 pthread_mutex_unlock(&adev->lock);
2120 return ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002121}
2122
2123static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
2124{
2125 return -ENOSYS;
2126}
2127
2128static int adev_get_master_volume(struct audio_hw_device *dev,
2129 float *volume)
2130{
2131 return -ENOSYS;
2132}
2133
2134static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
2135{
2136 return -ENOSYS;
2137}
2138
2139static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
2140{
2141 return -ENOSYS;
2142}
2143
2144static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
2145{
2146 struct audio_device *adev = (struct audio_device *)dev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002147 pthread_mutex_lock(&adev->lock);
2148 if (adev->mode != mode) {
2149 adev->mode = mode;
2150 }
2151 pthread_mutex_unlock(&adev->lock);
2152 return 0;
2153}
2154
2155static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
2156{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002157 return voice_set_mic_mute((struct audio_device *)dev, state);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002158}
2159
2160static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
2161{
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002162 *state = voice_get_mic_mute((struct audio_device *)dev);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002163 return 0;
2164}
2165
2166static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
2167 const struct audio_config *config)
2168{
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002169 int channel_count = popcount(config->channel_mask);
2170
2171 return get_input_buffer_size(config->sample_rate, config->format, channel_count);
2172}
2173
2174static int adev_open_input_stream(struct audio_hw_device *dev,
2175 audio_io_handle_t handle,
2176 audio_devices_t devices,
2177 struct audio_config *config,
2178 struct audio_stream_in **stream_in)
2179{
2180 struct audio_device *adev = (struct audio_device *)dev;
2181 struct stream_in *in;
2182 int ret, buffer_size, frame_size;
2183 int channel_count = popcount(config->channel_mask);
2184
Eric Laurent994a6932013-07-17 11:51:42 -07002185 ALOGV("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002186 *stream_in = NULL;
2187 if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0)
2188 return -EINVAL;
2189
2190 in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
2191
2192 in->stream.common.get_sample_rate = in_get_sample_rate;
2193 in->stream.common.set_sample_rate = in_set_sample_rate;
2194 in->stream.common.get_buffer_size = in_get_buffer_size;
2195 in->stream.common.get_channels = in_get_channels;
2196 in->stream.common.get_format = in_get_format;
2197 in->stream.common.set_format = in_set_format;
2198 in->stream.common.standby = in_standby;
2199 in->stream.common.dump = in_dump;
2200 in->stream.common.set_parameters = in_set_parameters;
2201 in->stream.common.get_parameters = in_get_parameters;
2202 in->stream.common.add_audio_effect = in_add_audio_effect;
2203 in->stream.common.remove_audio_effect = in_remove_audio_effect;
2204 in->stream.set_gain = in_set_gain;
2205 in->stream.read = in_read;
2206 in->stream.get_input_frames_lost = in_get_input_frames_lost;
2207
2208 in->device = devices;
2209 in->source = AUDIO_SOURCE_DEFAULT;
2210 in->dev = adev;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002211 in->standby = 1;
2212 in->channel_mask = config->channel_mask;
2213
2214 /* Update config params with the requested sample rate and channels */
2215 in->usecase = USECASE_AUDIO_RECORD;
2216 in->config = pcm_config_audio_capture;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002217 in->config.rate = config->sample_rate;
2218
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002219 if (audio_extn_ssr_get_enabled()&& channel_count == 6) {
2220 if(audio_extn_ssr_init(adev, in))
2221 ALOGE("%s: audio_extn_ssr_init failed", __func__);
2222 } else {
2223 in->config.channels = channel_count;
2224 frame_size = audio_stream_frame_size((struct audio_stream *)in);
2225 buffer_size = get_input_buffer_size(config->sample_rate,
2226 config->format,
2227 channel_count);
2228 in->config.period_size = buffer_size / frame_size;
2229 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002230
2231 *stream_in = &in->stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002232 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002233 return 0;
2234
2235err_open:
2236 free(in);
2237 *stream_in = NULL;
2238 return ret;
2239}
2240
2241static void adev_close_input_stream(struct audio_hw_device *dev,
2242 struct audio_stream_in *stream)
2243{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002244 struct stream_in *in = (struct stream_in *)stream;
Eric Laurent994a6932013-07-17 11:51:42 -07002245 ALOGV("%s", __func__);
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002246
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002247 in_standby(&stream->common);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07002248 if (audio_extn_ssr_get_enabled() && (popcount(in->channel_mask) == 6)) {
2249 audio_extn_ssr_deinit();
2250 }
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002251 free(stream);
2252
2253 return;
2254}
2255
2256static int adev_dump(const audio_hw_device_t *device, int fd)
2257{
2258 return 0;
2259}
2260
2261static int adev_close(hw_device_t *device)
2262{
2263 struct audio_device *adev = (struct audio_device *)device;
2264 audio_route_free(adev->audio_route);
Eric Laurentb23d5282013-05-14 15:27:20 -07002265 free(adev->snd_dev_ref_cnt);
2266 platform_deinit(adev->platform);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002267 free(device);
2268 return 0;
2269}
2270
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002271static int adev_open(const hw_module_t *module, const char *name,
2272 hw_device_t **device)
2273{
Ravi Kumar Alamanda71c84b72013-03-10 23:50:28 -07002274 int i, ret;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002275
Ravi Kumar Alamanda75d924d2013-02-20 21:30:08 -08002276 ALOGD("%s: enter", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002277 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL;
2278
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002279 pthread_mutex_lock(&adev_init_lock);
2280 if (is_adev_initialised == true){
2281 *device = &adev->device.common;
2282 ALOGD("%s: returning existing instance of adev", __func__);
2283 ALOGD("%s: exit", __func__);
2284 pthread_mutex_unlock(&adev_init_lock);
2285 return 0;
2286 }
2287
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002288 adev = calloc(1, sizeof(struct audio_device));
2289
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002290 adev->device.common.tag = HARDWARE_DEVICE_TAG;
2291 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
2292 adev->device.common.module = (struct hw_module_t *)module;
2293 adev->device.common.close = adev_close;
2294
2295 adev->device.init_check = adev_init_check;
2296 adev->device.set_voice_volume = adev_set_voice_volume;
2297 adev->device.set_master_volume = adev_set_master_volume;
2298 adev->device.get_master_volume = adev_get_master_volume;
2299 adev->device.set_master_mute = adev_set_master_mute;
2300 adev->device.get_master_mute = adev_get_master_mute;
2301 adev->device.set_mode = adev_set_mode;
2302 adev->device.set_mic_mute = adev_set_mic_mute;
2303 adev->device.get_mic_mute = adev_get_mic_mute;
2304 adev->device.set_parameters = adev_set_parameters;
2305 adev->device.get_parameters = adev_get_parameters;
2306 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
2307 adev->device.open_output_stream = adev_open_output_stream;
2308 adev->device.close_output_stream = adev_close_output_stream;
2309 adev->device.open_input_stream = adev_open_input_stream;
2310 adev->device.close_input_stream = adev_close_input_stream;
2311 adev->device.dump = adev_dump;
2312
2313 /* Set the default route before the PCM stream is opened */
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002314 adev->mode = AUDIO_MODE_NORMAL;
Eric Laurentc8400632013-02-14 19:04:54 -08002315 adev->active_input = NULL;
Ravi Kumar Alamanda096c87f2013-02-28 20:54:57 -08002316 adev->primary_output = NULL;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002317 adev->out_device = AUDIO_DEVICE_NONE;
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002318 adev->bluetooth_nrec = true;
Ravi Kumar Alamandaf9967042013-02-14 19:35:14 -08002319 adev->acdb_settings = TTY_MODE_OFF;
Eric Laurent07eeafd2013-10-06 12:52:49 -07002320 /* adev->cur_hdmi_channels = 0; by calloc() */
Eric Laurentb23d5282013-05-14 15:27:20 -07002321 adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int));
Shiv Maliyappanahalli34b585f2013-10-01 15:49:05 -07002322 voice_init(adev);
Ravi Kumar Alamanda3b1816c2013-02-27 23:01:21 -08002323 list_init(&adev->usecase_list);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002324
2325 /* Loads platform specific libraries dynamically */
Eric Laurentb23d5282013-05-14 15:27:20 -07002326 adev->platform = platform_init(adev);
2327 if (!adev->platform) {
2328 free(adev->snd_dev_ref_cnt);
2329 free(adev);
2330 ALOGE("%s: Failed to init platform data, aborting.", __func__);
2331 *device = NULL;
2332 return -EINVAL;
2333 }
Eric Laurentc4aef752013-09-12 17:45:53 -07002334
2335 if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
2336 adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
2337 if (adev->visualizer_lib == NULL) {
2338 ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH);
2339 } else {
2340 ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH);
2341 adev->visualizer_start_output =
2342 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2343 "visualizer_hal_start_output");
2344 adev->visualizer_stop_output =
2345 (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib,
2346 "visualizer_hal_stop_output");
2347 }
2348 }
2349
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002350 *device = &adev->device.common;
2351
Apoorv Raghuvanshi6e262842013-10-06 14:39:35 -07002352 /* update init flag*/
2353 is_adev_initialised = true;
2354 pthread_mutex_unlock(&adev_init_lock);
2355
Eric Laurent994a6932013-07-17 11:51:42 -07002356 ALOGV("%s: exit", __func__);
Ravi Kumar Alamanda2dfba2b2013-01-17 16:50:22 -08002357 return 0;
2358}
2359
2360static struct hw_module_methods_t hal_module_methods = {
2361 .open = adev_open,
2362};
2363
2364struct audio_module HAL_MODULE_INFO_SYM = {
2365 .common = {
2366 .tag = HARDWARE_MODULE_TAG,
2367 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
2368 .hal_api_version = HARDWARE_HAL_API_VERSION,
2369 .id = AUDIO_HARDWARE_MODULE_ID,
2370 .name = "QCOM Audio HAL",
2371 .author = "Code Aurora Forum",
2372 .methods = &hal_module_methods,
2373 },
2374};